From 9b9d310c748d5ebced94ceb1790adb905c77a3eb Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Mon, 28 Dec 2015 02:28:53 +0000 Subject: [PATCH] Responsive images: when creating `srcset` do not exclude the image size which is in the `src` attribute even when it is larger than `max_srcset_image_width`. Props joemcgill. Fixes #35108 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@36110 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 9 +++++-- tests/phpunit/tests/media.php | 48 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 9d4025d263..d200d4ee91 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1046,8 +1046,13 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac continue; } - // Filter out images that are wider than '$max_srcset_image_width'. - if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width ) { + /* + * Filter out images that are wider than '$max_srcset_image_width' unless + * that file is in the 'src' attribute. + */ + if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && + false === strpos( $image_src, $image['file'] ) ) { + continue; } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index ede300f208..c6784a5824 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -893,10 +893,11 @@ EOF; /** * @ticket 34955 + * @ticket 33641 */ function test_wp_calculate_image_srcset_ratio_variance() { // Mock data for this test. - $size_array = array( 218, 300); + $size_array = array( 218, 300 ); $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png'; $image_meta = array( 'width' => 768, @@ -935,6 +936,51 @@ EOF; $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); } + /** + * @ticket 35108 + * @ticket 33641 + */ + function test_wp_calculate_image_srcset_include_src() { + // Mock data for this test. + $size_array = array( 2000, 1000 ); + $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png'; + $image_meta = array( + 'width' => 2000, + 'height' => 1000, + 'file' => '2015/12/test.png', + 'sizes' => array( + 'thumbnail' => array( + 'file' => 'test-150x150.png', + 'width' => 150, + 'height' => 150, + 'mime-type' => 'image/png', + ), + 'medium' => array( + 'file' => 'test-300x150.png', + 'width' => 300, + 'height' => 150, + 'mime-type' => 'image/png', + ), + 'medium_large' => array( + 'file' => 'test-768x384.png', + 'width' => 768, + 'height' => 384, + 'mime-type' => 'image/png', + ), + 'large' => array( + 'file' => 'test-1024x512.png', + 'width' => 1024, + 'height' => 512, + 'mime-type' => 'image/png', + ), + ), + ); + + $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w'; + + $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); + } + /** * @ticket 33641 */