From 012df11450bcfa46eb7fb4182f0be1fc5296d56e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 4 May 2023 13:37:10 +0100 Subject: [PATCH 1/8] change default to 3 for wp_omit_loading_attr_threshold --- src/wp-includes/media.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 95836d98a24b2..372e8f13a120b 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5434,7 +5434,7 @@ function wp_get_webp_info( $filename ) { * * Under the hood, the function uses {@see wp_increase_content_media_count()} every time it is called for an element * within the main content. If the element is the very first content element, the `loading` attribute will be omitted. - * This default threshold of 1 content element to omit the `loading` attribute for can be customized using the + * This default threshold of 3 content element to omit the `loading` attribute for can be customized using the * {@see 'wp_omit_loading_attr_threshold'} filter. * * @since 5.9.0 @@ -5484,7 +5484,7 @@ function wp_get_loading_attr_default( $context ) { /** * Gets the threshold for how many of the first content media elements to not lazy-load. * - * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 1. + * This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3. * The filter is only run once per page load, unless the `$force` parameter is used. * * @since 5.9.0 @@ -5506,9 +5506,9 @@ function wp_omit_loading_attr_threshold( $force = false ) { * * @since 5.9.0 * - * @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 1. + * @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 3. */ - $omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 1 ); + $omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 3 ); } return $omit_threshold; From 57f92c4504306d02b372dcece7071f2413fff6bb Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 4 May 2023 13:45:43 +0100 Subject: [PATCH 2/8] logic update asper the new default. --- tests/phpunit/tests/media.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 1817af955bb0d..7d4b2e19b5d95 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3585,8 +3585,10 @@ public function test_wp_get_loading_attr_default( $context ) { // and in the main query, and do not increase the content media count. $this->assertSame( 'lazy', wp_get_loading_attr_default( 'wp_get_attachment_image' ) ); - // Return `false` if in the loop and in the main query and it is the first element. - $this->assertFalse( wp_get_loading_attr_default( $context ) ); + // Return `false` if in the loop and in the main query for first three element. + for ( $i = 0; $i < 3; $i++ ) { + $this->assertFalse( wp_get_loading_attr_default( $context ) ); + } // Return 'lazy' if in the loop and in the main query for any subsequent elements. $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); From 8e8eda48d3f6adc1bdc710f447fe688c5ce5d92c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 4 May 2023 14:27:41 +0100 Subject: [PATCH 3/8] change default --- tests/phpunit/tests/media.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 7d4b2e19b5d95..8ff9a3fd5df3b 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3624,15 +3624,15 @@ public function test_wp_omit_loading_attr_threshold_filter() { add_filter( 'wp_omit_loading_attr_threshold', function() { - return 3; + return 5; } ); while ( have_posts() ) { the_post(); - // Due to the filter, now the first three elements should not be lazy-loaded, i.e. return `false`. - for ( $i = 0; $i < 3; $i++ ) { + // Due to the filter, now the first five elements should not be lazy-loaded, i.e. return `false`. + for ( $i = 0; $i < 5; $i++ ) { $this->assertFalse( wp_get_loading_attr_default( 'the_content' ) ); } @@ -3692,24 +3692,24 @@ function() { public function test_wp_omit_loading_attr_threshold() { $this->reset_omit_loading_attr_filter(); - // Apply filter, ensure default value of 1. + // Apply filter, ensure default value of 3. $omit_threshold = wp_omit_loading_attr_threshold(); - $this->assertSame( 1, $omit_threshold ); + $this->assertSame( 3, $omit_threshold ); - // Add a filter that changes the value to 3. However, the filter is not applied a subsequent time in a single - // page load by default, so the value is still 1. + // Add a filter that changes the value to 1. However, the filter is not applied a subsequent time in a single + // page load by default, so the value is still 3. add_filter( 'wp_omit_loading_attr_threshold', function() { - return 3; + return 1; } ); $omit_threshold = wp_omit_loading_attr_threshold(); - $this->assertSame( 1, $omit_threshold ); + $this->assertSame( 3, $omit_threshold ); // Only by enforcing a fresh check, the filter gets re-applied. $omit_threshold = wp_omit_loading_attr_threshold( true ); - $this->assertSame( 3, $omit_threshold ); + $this->assertSame( 1, $omit_threshold ); } /** From 69855037ddd86af4679c9bbabbf367f903ce2ddb Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Fri, 12 May 2023 19:35:40 +0530 Subject: [PATCH 4/8] add since for the new change Co-authored-by: Felix Arntz --- src/wp-includes/media.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 372e8f13a120b..813710f990ca1 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5505,6 +5505,7 @@ function wp_omit_loading_attr_threshold( $force = false ) { * for only the very first content media element. * * @since 5.9.0 + * @since 6.3.0 The default threshold was changed from 1 to 3. * * @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 3. */ From 15df68b8f50278ba14efdeeeefe9fc0bdbb70d20 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Fri, 12 May 2023 19:36:14 +0530 Subject: [PATCH 5/8] grammar fix Co-authored-by: Felix Arntz --- 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 813710f990ca1..0fde2418412c6 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5434,7 +5434,7 @@ function wp_get_webp_info( $filename ) { * * Under the hood, the function uses {@see wp_increase_content_media_count()} every time it is called for an element * within the main content. If the element is the very first content element, the `loading` attribute will be omitted. - * This default threshold of 3 content element to omit the `loading` attribute for can be customized using the + * This default threshold of 3 content elements to omit the `loading` attribute for can be customized using the * {@see 'wp_omit_loading_attr_threshold'} filter. * * @since 5.9.0 From 9561247de774ff29350ff4099feaff5b6fc9b06e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 12 May 2023 20:09:52 +0530 Subject: [PATCH 6/8] add filter to change threshold --- tests/phpunit/tests/media.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 8ff9a3fd5df3b..11ba4cf60fd3d 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3620,7 +3620,7 @@ public function test_wp_omit_loading_attr_threshold_filter() { $this->reset_content_media_count(); $this->reset_omit_loading_attr_filter(); - // Use the filter to alter the threshold for not lazy-loading to the first three elements. + // Use the filter to alter the threshold for not lazy-loading to the first five elements. add_filter( 'wp_omit_loading_attr_threshold', function() { @@ -3727,6 +3727,12 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl // Do not add srcset, sizes, or decoding attributes as they are irrelevant for this test. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); + add_filter( + 'wp_omit_loading_attr_threshold', + function() { + return 1; + } + ); $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); $img2 = get_image_tag( self::$large_id, '', '', '', 'medium' ); @@ -3779,6 +3785,12 @@ function( $attr ) { return $attr; } ); + add_filter( + 'wp_omit_loading_attr_threshold', + function() { + return 1; + } + ); $content_img = get_image_tag( self::$large_id, '', '', '', 'large' ); $lazy_content_img = wp_img_tag_add_loading_attr( $content_img, 'the_content' ); From b94ed4deea78f70cd2a83763c0fb00e4476faa12 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 May 2023 12:25:20 +0530 Subject: [PATCH 7/8] change closure to a common method. --- tests/phpunit/tests/media.php | 49 ++++++++++++++--------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 11ba4cf60fd3d..c4b0a881876ed 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3621,12 +3621,7 @@ public function test_wp_omit_loading_attr_threshold_filter() { $this->reset_omit_loading_attr_filter(); // Use the filter to alter the threshold for not lazy-loading to the first five elements. - add_filter( - 'wp_omit_loading_attr_threshold', - function() { - return 5; - } - ); + $this->force_omit_loading_attr_threshold( 5 ); while ( have_posts() ) { the_post(); @@ -3657,12 +3652,7 @@ public function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { $lazy_iframe2 = wp_iframe_tag_add_loading_attr( $iframe2, 'the_content' ); // Use a threshold of 2. - add_filter( - 'wp_omit_loading_attr_threshold', - function() { - return 2; - } - ); + $this->force_omit_loading_attr_threshold( 2 ); // Following the threshold of 2, the first two content media elements should not be lazy-loaded. $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2; @@ -3698,12 +3688,8 @@ public function test_wp_omit_loading_attr_threshold() { // Add a filter that changes the value to 1. However, the filter is not applied a subsequent time in a single // page load by default, so the value is still 3. - add_filter( - 'wp_omit_loading_attr_threshold', - function() { - return 1; - } - ); + $this->force_omit_loading_attr_threshold( 1 ); + $omit_threshold = wp_omit_loading_attr_threshold(); $this->assertSame( 3, $omit_threshold ); @@ -3727,12 +3713,7 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl // Do not add srcset, sizes, or decoding attributes as they are irrelevant for this test. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' ); - add_filter( - 'wp_omit_loading_attr_threshold', - function() { - return 1; - } - ); + $this->force_omit_loading_attr_threshold( 1 ); $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); $img2 = get_image_tag( self::$large_id, '', '', '', 'medium' ); @@ -3785,12 +3766,7 @@ function( $attr ) { return $attr; } ); - add_filter( - 'wp_omit_loading_attr_threshold', - function() { - return 1; - } - ); + $this->force_omit_loading_attr_threshold( 1 ); $content_img = get_image_tag( self::$large_id, '', '', '', 'large' ); $lazy_content_img = wp_img_tag_add_loading_attr( $content_img, 'the_content' ); @@ -4026,6 +4002,19 @@ public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) { } } + /** + * Change the omit loading attribute threshold value. + * + * @param int $threshold Threshold value to change. + */ + public function force_omit_loading_attr_threshold( $threshold ) { + add_filter( + 'wp_omit_loading_attr_threshold', + static function() use ( $threshold ) { + return $threshold; + } + ); + } } /** From 112643f7b9918fe9da6fabc585c96aa558ab354f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 May 2023 13:47:06 +0530 Subject: [PATCH 8/8] add message to assertion and also avoid loop --- tests/phpunit/tests/media.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index c4b0a881876ed..1cb677dacf466 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3585,10 +3585,10 @@ public function test_wp_get_loading_attr_default( $context ) { // and in the main query, and do not increase the content media count. $this->assertSame( 'lazy', wp_get_loading_attr_default( 'wp_get_attachment_image' ) ); - // Return `false` if in the loop and in the main query for first three element. - for ( $i = 0; $i < 3; $i++ ) { - $this->assertFalse( wp_get_loading_attr_default( $context ) ); - } + // Return `false` in the main query for first three element. + $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected first image to not be lazy-loaded.' ); + $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected second image to not be lazy-loaded.' ); + $this->assertFalse( wp_get_loading_attr_default( $context ), 'Expected third image to not be lazy-loaded.' ); // Return 'lazy' if in the loop and in the main query for any subsequent elements. $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );