param array $usable_dimensions The parameters to check against. * * @return bool True if the image has usable measurements, false if not. */ private static function has_usable_dimensions( $dimensions, $usable_dimensions ) { foreach ( [ 'width', 'height' ] as $param ) { $minimum = $usable_dimensions[ 'min_' . $param ]; $maximum = $usable_dimensions[ 'max_' . $param ]; $current = $dimensions[ $param ]; if ( ( $current < $minimum ) || ( $current > $maximum ) ) { return false; } } return true; } /** * Gets the post's first usable content image. Null if none is available. * * @param int|null $post_id The post id. * * @return string|null The image URL. */ public static function get_first_usable_content_image_for_post( $post_id = null ) { $post = get_post( $post_id ); // We know get_post() returns the post or null. if ( ! $post ) { return null; } $image_finder = new WPSEO_Content_Images(); $images = $image_finder->get_images( $post->ID, $post ); return self::get_first_image( $images ); } /** * Gets the term's first usable content image. Null if none is available. * * @param int $term_id The term id. * * @return string|null The image URL. */ public static function get_first_content_image_for_term( $term_id ) { $term_description = term_description( $term_id ); // We know term_description() returns a string which may be empty. if ( $term_description === '' ) { return null; } $image_finder = new WPSEO_Content_Images(); $images = $image_finder->get_images_from_content( $term_description ); return self::get_first_image( $images ); } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public static function get_attachment_id_from_settings( $setting ) { $image_id = WPSEO_Options::get( $setting . '_id', false ); if ( $image_id ) { return $image_id; } $image = WPSEO_Options::get( $setting, false ); if ( $image ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_id = self::get_attachment_by_url( $image ); } // Only store a new ID if it is not 0, to prevent an update loop. if ( $image_id ) { WPSEO_Options::set( $setting . '_id', $image_id ); } return $image_id; } /** * Retrieves the first possible image url from an array of images. * * @param array $images The array to extract image url from. * * @return string|null The extracted image url when found, null when not found. */ protected static function get_first_image( $images ) { if ( ! is_array( $images ) ) { return null; } $images = array_filter( $images ); if ( empty( $images ) ) { return null; } return reset( $images ); } }