ATOR );
}
return $path;
}
/**
* Get the relative path of the image.
*
* @param string $img Image URL.
*
* @return string The expanded image URL.
*/
public static function get_relative_path( $img ) {
if ( $img[0] !== '/' ) {
return $img;
}
// If it's a relative URL, it's relative to the domain, not necessarily to the WordPress install, we
// want to preserve domain name and URL scheme (http / https) though.
$parsed_url = wp_parse_url( home_url() );
$img = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $img;
return $img;
}
/**
* Get the image file size.
*
* @param array $image An image array object.
*
* @return int The file size in bytes.
*/
public static function get_file_size( $image ) {
if ( isset( $image['filesize'] ) ) {
return $image['filesize'];
}
if ( ! isset( $image['path'] ) ) {
return 0;
}
// If the file size for the file is over our limit, we're going to go for a smaller version.
if ( function_exists( 'wp_filesize' ) ) {
return wp_filesize( self::get_absolute_path( $image['path'] ) );
}
return file_exists( $image['path'] ) ? (int) filesize( $image['path'] ) : 0;
}
/**
* Returns the different image variations for consideration.
*
* @param int $attachment_id The attachment to return the variations for.
*
* @return array The different variations possible for this attachment ID.
*/
public static function get_variations( $attachment_id ) {
$variations = [];
foreach ( self::get_sizes() as $size ) {
$variation = self::get_image( $attachment_id, $size );
// The get_image function returns false if the size doesn't exist for this attachment.
if ( $variation ) {
$variations[] = $variation;
}
}
return $variations;
}
/**
* Check original size of image. If original image is too small, return false, else return true.
*
* Filters a list of variations by a certain set of usable dimensions.
*
* @param array $usable_dimensions {
* The parameters to check against.
*
* @type int $min_width Minimum width of image.
* @type int $max_width Maximum width of image.
* @type int $min_height Minimum height of image.
* @type int $max_height Maximum height of image.
* }
* @param array $variations The variations that should be considered.
*
* @return array Whether a variation is fit for display or not.
*/
public static function filter_usable_dimensions( $usable_dimensions, $variations ) {
$filtered = [];
foreach ( $variations as $variation ) {
$dimensions = $variation;
if ( self::has_usable_dimensions( $dimensions, $usable_dimensions ) ) {
$filtered[] = $variation;
}
}
return $filtered;
}
/**
* Filters a list of variations by (disk) file size.
*
* @param array $variations The variations to consider.
*
* @return array The validations that pass the required file size limits.
*/
public static function filter_usable_file_size( $variations ) {
foreach ( $variations as $variation ) {
// We return early to prevent measuring the file size of all the variations.
if ( self::has_usable_file_size( $variation ) ) {
return [ $variation ];
}
}
return [];
}
/**
* Retrieve the internal WP image file sizes.
*
* @return array An array of image sizes.
*/
public static function get_sizes() {
/**
* Filter: 'wpseo_image_sizes' - Determines which image sizes we'll loop through to get an appropriate image.
*
* @param array