From 909b731e6a8d124efa5394a0740693b2f4b585d6 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 28 Aug 2024 09:48:39 +0530 Subject: [PATCH 01/11] Add auto sizes for lazy-loaded images --- src/wp-includes/media.php | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 5c93aee2256b4..91d05d03edc15 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1137,6 +1137,15 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f } } + // Adds auto to the sizes attribute if applicable. + if ( + ( isset( $attr['loading'] ) && 'lazy' === $attr['loading'] ) && + isset( $attr['sizes'] ) && + ! wp_sizes_attribute_includes_valid_auto( $attr['sizes'] ) + ) { + $attr['sizes'] = 'auto, ' . $attr['sizes']; + } + /** * Filters the list of attachment image attributes. * @@ -1917,6 +1926,9 @@ function wp_filter_content_tags( $content, $context = null ) { // Add loading optimization attributes if applicable. $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); + // Adds auto to the sizes attribute if applicable. + $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); + /** * Filters an img tag within the content for a given context. * @@ -1963,6 +1975,63 @@ function wp_filter_content_tags( $content, $context = null ) { return $content; } +/** + * Adds auto to the sizes attribute to the image, if image lazy loaded. + * + * @since 6.7.0 + * + * @param string|mixed $html The HTML image tag markup being filtered. + * @return string The filtered HTML image tag markup. + */ +function wp_img_tag_add_auto_sizes( $image ) { + if ( ! is_string( $image ) ) { + $image = ''; + } + + $processor = new WP_HTML_Tag_Processor( $image ); + + // Bail if there is no IMG tag. + if ( ! $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) { + return $image; + } + + // Bail early if the image is not lazy-loaded. + $value = $processor->get_attribute( 'loading' ); + if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) { + return $image; + } + + $sizes = $processor->get_attribute( 'sizes' ); + + // Bail early if the image is not responsive. + if ( ! is_string( $sizes ) ) { + return $image; + } + + // Don't add 'auto' to the sizes attribute if it already exists. + if ( wp_sizes_attribute_includes_valid_auto( $sizes ) ) { + return $image; + } + + $processor->set_attribute( 'sizes', "auto, $sizes" ); + return $processor->get_updated_html(); +} + +/** + * Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list. + * + * Per the HTML spec, if present it must be the first entry. + * + * @since 6.7.0 + * + * @param string $sizes_attr The 'sizes' attribute value. + * @return bool True if the 'auto' keyword is present, false otherwise. + */ +function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ) { + $token = strtok( strtolower( $sizes_attr ), ',' ); + return false !== $token && 'auto' === trim( $token, " \t\f\r\n" ); +} + /** * Adds optimization attributes to an `img` HTML tag. * From ea207f39c79d3697e709a4ee8f833418288f538e Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 28 Aug 2024 09:59:25 +0530 Subject: [PATCH 02/11] Add unit tests --- tests/phpunit/tests/media.php | 279 +++++++++++++++++++++++++++++++++- 1 file changed, 278 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index b58fc7353b5fa..8cf6a8d9544af 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -2467,6 +2467,9 @@ public function test_wp_calculate_image_srcset_animated_gifs() { * @requires function imagejpeg */ public function test_wp_filter_content_tags_schemes() { + // Disable lazy loading attribute. + add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); + $image_meta = wp_get_attachment_metadata( self::$large_id ); $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' ); @@ -2680,7 +2683,7 @@ public function test_wp_get_attachment_image_should_use_wp_get_attachment_metada 'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' . 'class="attachment-testsize size-testsize" alt="" decoding="async" loading="lazy" ' . 'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' . - 'sizes="(max-width: 999px) 100vw, 999px" />'; + 'sizes="auto, (max-width: 999px) 100vw, 999px" />'; $actual = wp_get_attachment_image( self::$large_id, 'testsize' ); @@ -5117,6 +5120,9 @@ static function ( $loading_attrs ) { } ); + // Remove sizes attribute as it is irrelevant for this test. + add_filter( 'wp_calculate_image_sizes', '__return_false' ); + // Add shortcode that prints a large image, and a block type that wraps it. add_shortcode( 'full_image', @@ -5992,6 +5998,277 @@ static function ( $loading_attrs ) { ); } + /** + * Test generated markup for an image with lazy loading gets auto-sizes. + * + * @ticket 61847 + */ + public function test_image_with_lazy_loading_has_auto_sizes() { + $this->assertStringContainsString( + 'sizes="auto, ', + wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => 'lazy' ) ), + 'Failed asserting that the sizes attribute for a lazy-loaded image includes "auto".' + ); + } + + /** + * Test generated markup for an image without lazy loading does not get auto-sizes. + * + * @ticket 61847 + */ + public function test_image_without_lazy_loading_does_not_have_auto_sizes() { + $this->assertStringContainsString( + 'sizes="(max-width: 1024px) 100vw, 1024px"', + wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => '' ) ), + 'Failed asserting that the sizes attribute for an image without lazy loading is set to the expected value.' + ); + } + + /** + * Test content filtered markup with lazy loading gets auto-sizes. + * + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + */ + public function test_content_image_with_lazy_loading_has_auto_sizes() { + // Force lazy loading attribute. + add_filter( 'wp_img_tag_add_loading_attr', '__return_true' ); + + $this->assertStringContainsString( + 'sizes="auto, (max-width: 1024px) 100vw, 1024px"', + wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), + 'Failed asserting that the sizes attribute for a content image with lazy loading includes "auto" with the expected sizes.' + ); + } + + /** + * Test content filtered markup without lazy loading does not get auto-sizes. + * + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + */ + public function test_content_image_without_lazy_loading_does_not_have_auto_sizes() { + // Disable lazy loading attribute. + add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); + + $this->assertStringContainsString( + 'sizes="(max-width: 1024px) 100vw, 1024px"', + wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), + 'Failed asserting that the sizes attribute for a content image without lazy loading is set to the expected value.' + ); + } + + /** + * Test generated markup for an image with 'auto' keyword already present in sizes does not receive it again. + * + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + * @covers ::wp_sizes_attribute_includes_valid_auto + * + * @dataProvider data_image_with_existing_auto_sizes + * + * @param string $initial_sizes The initial sizes attribute to test. + * @param bool $expected_processed Whether the auto sizes should be processed or not. + */ + public function test_image_with_existing_auto_sizes_is_not_processed_again( string $initial_sizes, bool $expected_processed ) { + $image_tag = wp_get_attachment_image( + self::$large_id, + 'large', + false, + array( + // Force pre-existing 'sizes' attribute and lazy-loading. + 'sizes' => $initial_sizes, + 'loading' => 'lazy', + ) + ); + if ( $expected_processed ) { + $this->assertStringContainsString( + 'sizes="auto, ' . $initial_sizes . '"', + $image_tag, + 'Failed asserting that "auto" keyword is not added to sizes attribute when it already exists.' + ); + } else { + $this->assertStringContainsString( + 'sizes="' . $initial_sizes . '"', + $image_tag, + 'Failed asserting that "auto" keyword is not added to sizes attribute when it already exists.' + ); + } + } + + /** + * Test content filtered markup with 'auto' keyword already present in sizes does not receive it again. + * + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + * @covers ::wp_sizes_attribute_includes_valid_auto + * + * @dataProvider data_image_with_existing_auto_sizes + * + * @param string $initial_sizes The initial sizes attribute to test. + * @param bool $expected_processed Whether the auto sizes should be processed or not. + */ + public function test_content_image_with_existing_auto_sizes_is_not_processed_again( string $initial_sizes, bool $expected_processed ) { + // Force lazy loading attribute. + add_filter( 'wp_img_tag_add_loading_attr', '__return_true' ); + + add_filter( + 'get_image_tag', + static function ( $html ) use ( $initial_sizes ) { + return str_replace( + '" />', + '" sizes="' . $initial_sizes . '" />', + $html + ); + } + ); + + $image_content = wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ); + if ( $expected_processed ) { + $this->assertStringContainsString( + 'sizes="auto, ' . $initial_sizes . '"', + $image_content, + 'Failed asserting that "auto" keyword is not added to sizes attribute in filtered content when it already exists.' + ); + } else { + $this->assertStringContainsString( + 'sizes="' . $initial_sizes . '"', + $image_content, + 'Failed asserting that "auto" keyword is not added to sizes attribute in filtered content when it already exists.' + ); + } + } + + /** + * Returns data for the above test methods to assert correct behavior with a pre-existing sizes attribute. + * + * @return array Arguments for the test scenarios. + */ + public function data_image_with_existing_auto_sizes() { + return array( + 'not present' => array( + '(max-width: 1024px) 100vw, 1024px', + true, + ), + 'in beginning, without space' => array( + 'auto,(max-width: 1024px) 100vw, 1024px', + false, + ), + 'in beginning, with space' => array( + 'auto, (max-width: 1024px) 100vw, 1024px', + false, + ), + 'sole keyword' => array( + 'auto', + false, + ), + 'with space before' => array( + ' auto, (max-width: 1024px) 100vw, 1024px', + false, + ), + 'with uppercase' => array( + 'AUTO, (max-width: 1024px) 100vw, 1024px', + false, + ), + + /* + * The following scenarios technically include the 'auto' keyword, + * but it is in the wrong place, as per the HTML spec it must be + * the first entry in the list. + * Therefore in these invalid cases the 'auto' keyword should still + * be added to the beginning of the list. + */ + 'within, without space' => array( + '(max-width: 1024px) 100vw, auto,1024px', + true, + ), + 'within, with space' => array( + '(max-width: 1024px) 100vw, auto, 1024px', + true, + ), + 'at the end, without space' => array( + '(max-width: 1024px) 100vw,auto', + true, + ), + 'at the end, with space' => array( + '(max-width: 1024px) 100vw, auto', + true, + ), + ); + } + + /** + * Data provider for test_auto_sizes_update_content_img_tag(). + * + * @return array + */ + public function data_provider_to_test_wp_img_tag_add_auto_sizes() { + return array( + 'expected_with_single_quoted_attributes' => array( + 'input' => "", + 'expected' => "", + ), + 'expected_with_data_sizes_attribute' => array( + 'input' => '', + 'expected' => '', + ), + 'expected_with_data_sizes_attribute_already_present' => array( + 'input' => '', + 'expected' => '', + ), + 'not_expected_with_loading_lazy_in_attr_value' => array( + 'input' => '\'This', + 'expected' => '\'This', + ), + 'not_expected_with_data_loading_attribute_present' => array( + 'input' => '', + 'expected' => '', + ), + 'expected_when_attributes_have_spaces_after_them' => array( + 'input' => '', + 'expected' => '', + ), + 'expected_when_attributes_are_upper_case' => array( + 'input' => '', + 'expected' => '', + ), + 'expected_when_loading_lazy_lacks_quotes' => array( + 'input' => '', + 'expected' => '', + ), + 'expected_when_loading_lazy_has_whitespace' => array( + 'input' => '', + 'expected' => '', + ), + 'not_expected_when_sizes_auto_lacks_quotes' => array( + 'input' => '', + 'expected' => '', + ), + ); + } + + /** + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + * + * @dataProvider data_provider_to_test_wp_img_tag_add_auto_sizes + * + * @param string $input The input HTML string. + * @param string $expected The expected output HTML string. + */ + public function test_auto_sizes_update_content_img_tag( string $input, string $expected ) { + $this->assertSame( + $expected, + wp_img_tag_add_auto_sizes( $input ), + 'Failed asserting that "auto" keyword is correctly added or not added to sizes attribute in the image tag.' + ); + } + /** * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter. * From f0d949c431db8a8f94019cac2e97616e8cb44b5a Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 29 Aug 2024 09:47:12 +0530 Subject: [PATCH 03/11] Apply suggestions from code review Co-authored-by: Felix Arntz --- src/wp-includes/media.php | 6 +----- tests/phpunit/tests/media.php | 18 +++++++++--------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 91d05d03edc15..22d68e12a3ec4 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1980,14 +1980,10 @@ function wp_filter_content_tags( $content, $context = null ) { * * @since 6.7.0 * - * @param string|mixed $html The HTML image tag markup being filtered. + * @param string $html The HTML image tag markup being filtered. * @return string The filtered HTML image tag markup. */ function wp_img_tag_add_auto_sizes( $image ) { - if ( ! is_string( $image ) ) { - $image = ''; - } - $processor = new WP_HTML_Tag_Processor( $image ); // Bail if there is no IMG tag. diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 8cf6a8d9544af..21a7ffb9820ad 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -2467,7 +2467,7 @@ public function test_wp_calculate_image_srcset_animated_gifs() { * @requires function imagejpeg */ public function test_wp_filter_content_tags_schemes() { - // Disable lazy loading attribute. + // Disable lazy loading attribute to not add the 'auto' keyword to the `sizes` attribute. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); $image_meta = wp_get_attachment_metadata( self::$large_id ); @@ -5120,7 +5120,7 @@ static function ( $loading_attrs ) { } ); - // Remove sizes attribute as it is irrelevant for this test. + // Do not calculate sizes attribute as it is irrelevant for this test. add_filter( 'wp_calculate_image_sizes', '__return_false' ); // Add shortcode that prints a large image, and a block type that wraps it. @@ -6017,10 +6017,10 @@ public function test_image_with_lazy_loading_has_auto_sizes() { * @ticket 61847 */ public function test_image_without_lazy_loading_does_not_have_auto_sizes() { - $this->assertStringContainsString( - 'sizes="(max-width: 1024px) 100vw, 1024px"', - wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => '' ) ), - 'Failed asserting that the sizes attribute for an image without lazy loading is set to the expected value.' + $this->assertStringNotContainsString( + 'sizes="auto, ', + wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => false ) ), + 'Failed asserting that the sizes attribute for an image without lazy loading does not include "auto".' ); } @@ -6056,7 +6056,7 @@ public function test_content_image_without_lazy_loading_does_not_have_auto_sizes $this->assertStringContainsString( 'sizes="(max-width: 1024px) 100vw, 1024px"', wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), - 'Failed asserting that the sizes attribute for a content image without lazy loading is set to the expected value.' + 'Failed asserting that the sizes attribute for a content image without lazy loading does not include "auto" with the expected sizes.' ); } @@ -6202,7 +6202,7 @@ public function data_image_with_existing_auto_sizes() { } /** - * Data provider for test_auto_sizes_update_content_img_tag(). + * Data provider for test_wp_img_tag_add_auto_sizes(). * * @return array */ @@ -6261,7 +6261,7 @@ public function data_provider_to_test_wp_img_tag_add_auto_sizes() { * @param string $input The input HTML string. * @param string $expected The expected output HTML string. */ - public function test_auto_sizes_update_content_img_tag( string $input, string $expected ) { + public function test_wp_img_tag_add_auto_sizes( string $input, string $expected ) { $this->assertSame( $expected, wp_img_tag_add_auto_sizes( $input ), From 1efb4e6501e3329e34e47f37c1287f6d42210416 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 30 Aug 2024 09:08:42 +0530 Subject: [PATCH 04/11] Apply suggestions from code review Co-authored-by: Felix Arntz --- src/wp-includes/media.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 22d68e12a3ec4..db90b3af84f98 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1137,9 +1137,10 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f } } - // Adds auto to the sizes attribute if applicable. + // Adds 'auto' to the sizes attribute if applicable. if ( - ( isset( $attr['loading'] ) && 'lazy' === $attr['loading'] ) && + isset( $attr['loading'] ) && + 'lazy' === $attr['loading'] && isset( $attr['sizes'] ) && ! wp_sizes_attribute_includes_valid_auto( $attr['sizes'] ) ) { @@ -1926,7 +1927,7 @@ function wp_filter_content_tags( $content, $context = null ) { // Add loading optimization attributes if applicable. $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); - // Adds auto to the sizes attribute if applicable. + // Adds 'auto' to the sizes attribute if applicable. $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); /** @@ -1976,7 +1977,7 @@ function wp_filter_content_tags( $content, $context = null ) { } /** - * Adds auto to the sizes attribute to the image, if image lazy loaded. + * Adds 'auto' to the sizes attribute to the image, if the image is lazy loaded and does not already include it. * * @since 6.7.0 * From deec3ba053e53480807cdcf0fe7b6bfbb55f0797 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 30 Aug 2024 10:24:44 +0530 Subject: [PATCH 05/11] Introduce wp_img_auto_sizes_enabled filter --- src/wp-includes/media.php | 17 +++++++++++++- tests/phpunit/tests/media.php | 42 +++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index db90b3af84f98..90cd113e7b73c 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1137,8 +1137,12 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f } } + /** This filter is documented in wp-includes/media.php */ + $is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled', true ); + // Adds 'auto' to the sizes attribute if applicable. if ( + $is_auto_sizes_enabled && isset( $attr['loading'] ) && 'lazy' === $attr['loading'] && isset( $attr['sizes'] ) && @@ -1907,6 +1911,15 @@ function wp_filter_content_tags( $content, $context = null ) { _prime_post_caches( $attachment_ids, false, true ); } + /** + * Filters whether the auto sizes enabled for lazy load images. + * + * @since 6.7.0 + * + * @param bool $is_auto_sizes_enabled Whether the auto sizes enabled for lazy load images. Default true. + */ + $is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled', true ); + // Iterate through the matches in order of occurrence as it is relevant for whether or not to lazy-load. foreach ( $matches as $match ) { // Filter an image match. @@ -1928,7 +1941,9 @@ function wp_filter_content_tags( $content, $context = null ) { $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); // Adds 'auto' to the sizes attribute if applicable. - $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); + if ( $is_auto_sizes_enabled ) { + $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); + } /** * Filters an img tag within the content for a given context. diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 21a7ffb9820ad..8a7c726d74236 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -6011,6 +6011,23 @@ public function test_image_with_lazy_loading_has_auto_sizes() { ); } + /** + * Test that when the "wp_img_auto_sizes_enabled" filter is set to false, + * the generated markup for an image does not include the "auto" keyword in the sizes attribute. + * + * @ticket 61847 + */ + public function test_image_with_wp_img_auto_sizes_enabled_filter_does_not_have_auto_sizes() { + // Apply the filter to disable the auto sizes feature. + add_filter( 'wp_img_auto_sizes_enabled', '__return_false' ); + + $this->assertStringNotContainsString( + 'sizes="auto, ', + wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => 'lazy' ) ), + 'Failed asserting that the sizes attribute for an image does not include "auto" when the auto sizes feature is disabled.' + ); + } + /** * Test generated markup for an image without lazy loading does not get auto-sizes. * @@ -6042,6 +6059,27 @@ public function test_content_image_with_lazy_loading_has_auto_sizes() { ); } + /** + * Test content filtered markup with lazy loading gets auto-sizes. + * + * @ticket 61847 + * + * @covers ::wp_img_tag_add_auto_sizes + */ + public function test_content_with_wp_img_auto_sizes_enabled_filter_does_not_have_auto_sizes() { + // Force lazy loading attribute. + add_filter( 'wp_img_tag_add_loading_attr', '__return_true' ); + + // Apply the filter to disable the auto sizes feature. + add_filter( 'wp_img_auto_sizes_enabled', '__return_false' ); + + $this->assertStringNotContainsString( + 'sizes="auto, ', + wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), + 'Failed asserting that the sizes attribute for a content image with lazy loading includes "auto" with the expected sizes.' + ); + } + /** * Test content filtered markup without lazy loading does not get auto-sizes. * @@ -6053,8 +6091,8 @@ public function test_content_image_without_lazy_loading_does_not_have_auto_sizes // Disable lazy loading attribute. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); - $this->assertStringContainsString( - 'sizes="(max-width: 1024px) 100vw, 1024px"', + $this->assertStringNotContainsString( + 'sizes="auto, ', wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), 'Failed asserting that the sizes attribute for a content image without lazy loading does not include "auto" with the expected sizes.' ); From e4a141a971aa2f51824b20d9e06dc2345a2c497f Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 4 Sep 2024 09:36:40 +0530 Subject: [PATCH 06/11] Remove wp_img_auto_sizes_enabled filter --- src/wp-includes/media.php | 17 +--------------- tests/phpunit/tests/media.php | 38 ----------------------------------- 2 files changed, 1 insertion(+), 54 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 1ec6f88a4d95f..a838947df503b 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1137,12 +1137,8 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f } } - /** This filter is documented in wp-includes/media.php */ - $is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled', true ); - // Adds 'auto' to the sizes attribute if applicable. if ( - $is_auto_sizes_enabled && isset( $attr['loading'] ) && 'lazy' === $attr['loading'] && isset( $attr['sizes'] ) && @@ -1911,15 +1907,6 @@ function wp_filter_content_tags( $content, $context = null ) { _prime_post_caches( $attachment_ids, false, true ); } - /** - * Filters whether the auto sizes enabled for lazy load images. - * - * @since 6.7.0 - * - * @param bool $is_auto_sizes_enabled Whether the auto sizes enabled for lazy load images. Default true. - */ - $is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled', true ); - // Iterate through the matches in order of occurrence as it is relevant for whether or not to lazy-load. foreach ( $matches as $match ) { // Filter an image match. @@ -1941,9 +1928,7 @@ function wp_filter_content_tags( $content, $context = null ) { $filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); // Adds 'auto' to the sizes attribute if applicable. - if ( $is_auto_sizes_enabled ) { - $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); - } + $filtered_image = wp_img_tag_add_auto_sizes( $filtered_image ); /** * Filters an img tag within the content for a given context. diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 57797705c3f73..d2edbc53747b1 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -6047,23 +6047,6 @@ public function test_image_with_lazy_loading_has_auto_sizes() { ); } - /** - * Test that when the "wp_img_auto_sizes_enabled" filter is set to false, - * the generated markup for an image does not include the "auto" keyword in the sizes attribute. - * - * @ticket 61847 - */ - public function test_image_with_wp_img_auto_sizes_enabled_filter_does_not_have_auto_sizes() { - // Apply the filter to disable the auto sizes feature. - add_filter( 'wp_img_auto_sizes_enabled', '__return_false' ); - - $this->assertStringNotContainsString( - 'sizes="auto, ', - wp_get_attachment_image( self::$large_id, 'large', false, array( 'loading' => 'lazy' ) ), - 'Failed asserting that the sizes attribute for an image does not include "auto" when the auto sizes feature is disabled.' - ); - } - /** * Test generated markup for an image without lazy loading does not get auto-sizes. * @@ -6095,27 +6078,6 @@ public function test_content_image_with_lazy_loading_has_auto_sizes() { ); } - /** - * Test content filtered markup with lazy loading gets auto-sizes. - * - * @ticket 61847 - * - * @covers ::wp_img_tag_add_auto_sizes - */ - public function test_content_with_wp_img_auto_sizes_enabled_filter_does_not_have_auto_sizes() { - // Force lazy loading attribute. - add_filter( 'wp_img_tag_add_loading_attr', '__return_true' ); - - // Apply the filter to disable the auto sizes feature. - add_filter( 'wp_img_auto_sizes_enabled', '__return_false' ); - - $this->assertStringNotContainsString( - 'sizes="auto, ', - wp_filter_content_tags( get_image_tag( self::$large_id, '', '', '', 'large' ) ), - 'Failed asserting that the sizes attribute for a content image with lazy loading includes "auto" with the expected sizes.' - ); - } - /** * Test content filtered markup without lazy loading does not get auto-sizes. * From 67b7882482b70f2cb98956b2d849e52b6b697062 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 4 Sep 2024 18:37:32 +0530 Subject: [PATCH 07/11] Minor doc update --- src/wp-includes/media.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index a838947df503b..0f78298d1c753 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1981,8 +1981,8 @@ function wp_filter_content_tags( $content, $context = null ) { * * @since 6.7.0 * - * @param string $html The HTML image tag markup being filtered. - * @return string The filtered HTML image tag markup. + * @param string image The image tag markup being filtered. + * @return string The filtered image tag markup. */ function wp_img_tag_add_auto_sizes( $image ) { $processor = new WP_HTML_Tag_Processor( $image ); From fedaf73ff857a64b74c4df7960028be2161ab825 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 4 Sep 2024 18:38:45 +0530 Subject: [PATCH 08/11] Minor fix --- src/wp-includes/media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 0f78298d1c753..09f375f0fc379 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1981,7 +1981,7 @@ function wp_filter_content_tags( $content, $context = null ) { * * @since 6.7.0 * - * @param string image The image tag markup being filtered. + * @param string $image The image tag markup being filtered. * @return string The filtered image tag markup. */ function wp_img_tag_add_auto_sizes( $image ) { From 639c08613301b37c0f76975e5611a583bc65954e Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 4 Sep 2024 19:46:23 +0530 Subject: [PATCH 09/11] Eliminate use of strtok() --- src/wp-includes/media.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 09f375f0fc379..a96598b9ac13e 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2025,8 +2025,8 @@ function wp_img_tag_add_auto_sizes( $image ) { * @return bool True if the 'auto' keyword is present, false otherwise. */ function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ) { - $token = strtok( strtolower( $sizes_attr ), ',' ); - return false !== $token && 'auto' === trim( $token, " \t\f\r\n" ); + list( $first_size ) = explode( ',', $sizes_attr, 2 ); + return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) ); } /** From ce31c1a302fcaa69202c5b246eba6a0fb9442557 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 5 Sep 2024 09:14:39 +0530 Subject: [PATCH 10/11] Apply suggestions from code review Co-authored-by: Weston Ruter --- src/wp-includes/media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index a96598b9ac13e..0063da6fef5c1 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1984,7 +1984,7 @@ function wp_filter_content_tags( $content, $context = null ) { * @param string $image The image tag markup being filtered. * @return string The filtered image tag markup. */ -function wp_img_tag_add_auto_sizes( $image ) { +function wp_img_tag_add_auto_sizes( string $image ): string { $processor = new WP_HTML_Tag_Processor( $image ); // Bail if there is no IMG tag. From d7f497c3aa6ce497853a22ef23f12d04d0fb48c1 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Fri, 6 Sep 2024 09:18:40 +0530 Subject: [PATCH 11/11] Apply suggestions from code review Co-authored-by: Weston Ruter --- src/wp-includes/media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 0063da6fef5c1..82ea8f27edc09 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2024,7 +2024,7 @@ function wp_img_tag_add_auto_sizes( string $image ): string { * @param string $sizes_attr The 'sizes' attribute value. * @return bool True if the 'auto' keyword is present, false otherwise. */ -function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ) { +function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool { list( $first_size ) = explode( ',', $sizes_attr, 2 ); return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) ); }