diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index fac23852ba..043657d33e 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -826,8 +826,8 @@ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa if ( is_array( $image_meta ) ) { $size_array = array( absint( $width ), absint( $height ) ); - $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); - $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src ); + $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); + $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { $attr['srcset'] = $srcset; @@ -933,8 +933,8 @@ function _wp_get_image_size_from_meta( $size_name, $image_meta ) { * @since 4.4.0 * * @param int $attachment_id Image attachment ID. - * @param array|string $size Image size. Accepts any valid image size, or an array of width and height - * values in pixels (in that order). Default 'medium'. + * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of + * width and height values in pixels (in that order). Default 'medium'. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * @return string|bool A 'srcset' value string or false. */ @@ -953,7 +953,7 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag absint( $image[2] ) ); - return wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id ); + return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); } /** @@ -961,13 +961,13 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag * * @since 4.4.0 * - * @param string $image_src The 'src' of the image. * @param array $size_array Array of width and height values in pixels (in that order). + * @param string $image_src The 'src' of the image. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Optional. The image attachment ID to pass to the filter. * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. */ -function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) { +function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { if ( empty( $image_meta['sizes'] ) ) { return false; } @@ -1105,13 +1105,42 @@ function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac return rtrim( $srcset, ', ' ); } +/** + * Retrieves the value for an image attachment's 'sizes' attribute. + * + * @since 4.4.0 + * + * @param int $attachment_id Image attachment ID. + * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width + * and height values in pixels (in that order). Default 'medium'. + * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. + * @return string|bool A 'srcset' value string or false. + */ +function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { + if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { + return false; + } + + if ( ! is_array( $image_meta ) ) { + $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); + } + + $image_src = $image[0]; + $size_array = array( + absint( $image[1] ), + absint( $image[2] ) + ); + + return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); +} + /** * Create 'sizes' attribute value for an image. * * @since 4.4.0 * - * @param array|string $size Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.), - * or an array of width and height values in pixels (in that order). + * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array + * of width and height values in pixels (in that order). Default 'medium'. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed * when using the image size name as argument for `$size`. @@ -1119,7 +1148,7 @@ function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac * * @return string|bool A valid source size value for use in a 'sizes' attribute or false. */ -function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) { +function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) { $width = 0; if ( is_array( $size ) ) { @@ -1145,18 +1174,18 @@ function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); /** - * Filter the output of 'wp_get_attachment_image_sizes()'. + * Filter the output of 'wp_calculate_image_sizes()'. * * @since 4.4.0 * * @param string $sizes A source size value for use in a 'sizes' attribute. - * @param array|string $size Image size. Image size name, or an array of width and height - * values in pixels (in that order). + * @param array|string $size Requested size. Image size or array of width and height values + * in pixels (in that order). Default 'medium'. + * @param string $image_src The URL to the image file. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Image attachment ID of the original image. - * @param string $image_src Optional. The URL to the image file. */ - return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); + return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); } /** @@ -1225,17 +1254,17 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { return $image; } - $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; - list( $src ) = explode( '?', $src ); + $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; + list( $image_src ) = explode( '?', $image_src ); // Return early if we couldn't get the image source. - if ( ! $src ) { + if ( ! $image_src ) { return $image; } // Bail early if an image has been inserted and later edited. if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && - strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { + strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { return $image; } @@ -1248,7 +1277,7 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { * If attempts to parse the size value failed, attempt to use the image meta data to match * the image file name from 'src' against the available sizes for an attachment. */ - $image_filename = wp_basename( $src ); + $image_filename = wp_basename( $image_src ); if ( $image_filename === wp_basename( $image_meta['file'] ) ) { $width = (int) $image_meta['width']; @@ -1269,10 +1298,10 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { } $size_array = array( $width, $height ); - $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); + $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); if ( $srcset ) { - $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src ); + $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); } if ( $srcset && $sizes ) { diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index e4feb8b77f..67158d7c4d 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -754,7 +754,7 @@ EOF; foreach ( $sizes as $size ) { $image_url = wp_get_attachment_image_url( self::$large_id, $size ); $size_array = $this->_get_image_size_array_from_name( $size ); - $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); + $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } } @@ -786,7 +786,7 @@ EOF; foreach ( $sizes as $size ) { $size_array = $this->_get_image_size_array_from_name( $size ); $image_url = wp_get_attachment_image_url( $id, $size ); - $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); + $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); } // Remove the attachment @@ -821,7 +821,7 @@ EOF; $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); // Calculate a srcset array. - $sizes = explode( ', ', wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); + $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); // Test to confirm all sources in the array include the same edit hash. foreach ( $sizes as $size ) { @@ -833,7 +833,7 @@ EOF; * @ticket 33641 */ function test_wp_calculate_image_srcset_false() { - $sizes = wp_calculate_image_srcset( 'file.png', array( 400, 300 ), array() ); + $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() ); // For canola.jpg we should return $this->assertFalse( $sizes ); @@ -849,7 +849,7 @@ EOF; $size_array = array(0, 0); - $srcset = wp_calculate_image_srcset( $image_url, $size_array, $image_meta ); + $srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); // The srcset should be false. $this->assertFalse( $srcset ); @@ -914,14 +914,32 @@ EOF; function test_wp_get_attachment_image_sizes() { // Test sizes against the default WP sizes. $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); + + foreach( $intermediates as $int_size ) { + $image = wp_get_attachment_image_src( self::$large_id, $int_size ); + + $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px'; + $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size ); + + $this->assertSame( $expected, $sizes ); + } + } + + /** + * @ticket 33641 + */ + function test_wp_calculate_image_sizes() { + // Test sizes against the default WP sizes. + $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); $image_meta = wp_get_attachment_metadata( self::$large_id ); foreach( $intermediates as $int_size ) { $size_array = $this->_get_image_size_array_from_name( $int_size ); + $image_src = $image_meta['sizes'][$int_size]['file']; list( $width, $height ) = $size_array; $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; - $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta ); + $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta ); $this->assertSame( $expected, $sizes ); } @@ -935,7 +953,7 @@ EOF; $size_array = $this->_get_image_size_array_from_name( 'medium' ); $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); - $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id ) ); + $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); // Function used to build HTML for the editor. $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); @@ -1019,8 +1037,8 @@ EOF; $size_array = array(900, 450); // Full size GIFs should not return a srcset. - $this->assertFalse( wp_calculate_image_srcset( $full_src, $size_array, $image_meta ) ); + $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) ); // Intermediate sized GIFs should not include the full size in the srcset. - $this->assertFalse( strpos( wp_calculate_image_srcset( $large_src, $size_array, $image_meta ), $full_src ) ); + $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) ); } }