From 07730dff875ead51b1725ae230f85df34b5533be Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 12 Sep 2022 15:53:23 -0400 Subject: [PATCH 01/31] first commit for fetchprionty --- modules/images/fetchpriority/load.php | 34 +++++++++++++++++++ .../fetchpriority/Fetchpriority_Tests.php | 14 ++++++++ 2 files changed, 48 insertions(+) create mode 100644 modules/images/fetchpriority/load.php create mode 100644 tests/modules/images/fetchpriority/Fetchpriority_Tests.php diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php new file mode 100644 index 0000000000..e7be702367 --- /dev/null +++ b/modules/images/fetchpriority/load.php @@ -0,0 +1,34 @@ +factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + $img = get_image_tag( $attachment_id, '', '', '', 'large' ); + $content = "$img\n$img"; + + $this->assertStringContainsString( 'fetchpriority ', wp_filter_content_tags( $content ) ); + } +} From 412ae47e6baf79bc12517477ce578dbdac5ed492 Mon Sep 17 00:00:00 2001 From: pbearne Date: Mon, 12 Sep 2022 18:17:09 -0400 Subject: [PATCH 02/31] added basic tests --- .../{Fetchpriority_Tests.php => Fetchpriority-Tests.php} | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) rename tests/modules/images/fetchpriority/{Fetchpriority_Tests.php => Fetchpriority-Tests.php} (61%) diff --git a/tests/modules/images/fetchpriority/Fetchpriority_Tests.php b/tests/modules/images/fetchpriority/Fetchpriority-Tests.php similarity index 61% rename from tests/modules/images/fetchpriority/Fetchpriority_Tests.php rename to tests/modules/images/fetchpriority/Fetchpriority-Tests.php index ade5e6cc08..5cb3c690fb 100644 --- a/tests/modules/images/fetchpriority/Fetchpriority_Tests.php +++ b/tests/modules/images/fetchpriority/Fetchpriority-Tests.php @@ -1,14 +1,12 @@ factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); $img = get_image_tag( $attachment_id, '', '', '', 'large' ); - $content = "$img\n$img"; - $this->assertStringContainsString( 'fetchpriority ', wp_filter_content_tags( $content ) ); + $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'the_content' ) ); + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'not_content' ) ); } } From 5793388fef55bf8c4546706d5ae32defb81ecbc2 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 11:58:55 -0400 Subject: [PATCH 03/31] Update modules/images/fetchpriority/load.php Co-authored-by: Adam Silverstein --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index e7be702367..7dad0e99eb 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -1,7 +1,7 @@ Date: Tue, 13 Sep 2022 11:59:01 -0400 Subject: [PATCH 04/31] Update modules/images/fetchpriority/load.php Co-authored-by: Adam Silverstein --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 7dad0e99eb..736ff50379 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -9,7 +9,7 @@ */ /** - * Filter image tags in content to add fetchpriority to the image tag if not lazy loaded set. + * Filter image tags in content to add fetchpriority to the image tag if lazy loaded is not set. * * @since 1.5.0 * From ca092d6f8f616982d826a04b001ef291c3e10122 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 12:00:15 -0400 Subject: [PATCH 05/31] Update modules/images/fetchpriority/load.php Co-authored-by: Mukesh Panchal --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 736ff50379..43851878d5 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -4,8 +4,8 @@ * Description: Adds `fetchpriority=high` parameter to the non lazy loaded images. * Experimental: Yes * + * @since n.e.x.t * @package performance-lab - * @since 1.5.0 */ /** From 8939080c21bedd0bd474c3bfe40714cc222d7e72 Mon Sep 17 00:00:00 2001 From: pbearne Date: Tue, 13 Sep 2022 14:00:09 -0400 Subject: [PATCH 06/31] code review feedback --- modules/images/fetchpriority/load.php | 6 +++--- tests/modules/images/fetchpriority/Fetchpriority-Tests.php | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 43851878d5..a595e8607c 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -11,7 +11,7 @@ /** * Filter image tags in content to add fetchpriority to the image tag if lazy loaded is not set. * - * @since 1.5.0 + * @since n.e.x.t * * @param string $filtered_image The filtered image. * @param string $context The context of the image. @@ -20,15 +20,15 @@ */ function fetchpriority_img_tag_add( $filtered_image, $context ) { - // Only apply this in `the_content` for now, since otherwise it can result in duplicate runs due to a problem with full site editing logic. if ( 'the_content' !== $context ) { return $filtered_image; } if ( strpos( $filtered_image, 'loading="lazy"' ) === false ) { $filtered_image = str_replace( 'assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'the_content' ) ); $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'not_content' ) ); + + $img = str_replace( 'assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'the_content' ) ); } } From 280f11d81f135dc3d0a70436293588628cb5cd78 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Sep 2022 11:40:33 -0700 Subject: [PATCH 07/31] Add CODEOWNERS update for fetchpriority module. --- .github/CODEOWNERS | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 15ea092d62..846f975278 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -60,6 +60,11 @@ /tests/modules/images/dominant-color @pbearne @spacedmonkey /tests/testdata/modules/images/dominant-color @pbearne @spacedmonkey +# Module: Fetchpriority +/modules/images/fetchpriority @pbearne @adamsilverstein +/tests/modules/images/fetchpriority @pbearne @adamsilverstein +/tests/testdata/modules/images/fetchpriority @pbearne @adamsilverstein + # Module: Full Page Cache Health Check /modules/site-health/audit-full-page-cache @manuelRod @westonruter /tests/modules/site-health/audit-full-page-cache @manuelRod @westonruter From 5480988b0e66b5d07cd93944f32fe95c471c8117 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Sep 2022 11:41:42 -0700 Subject: [PATCH 08/31] Use lowercase file name for test class file. --- .../{Fetchpriority-Tests.php => fetchpriority-test.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/modules/images/fetchpriority/{Fetchpriority-Tests.php => fetchpriority-test.php} (93%) diff --git a/tests/modules/images/fetchpriority/Fetchpriority-Tests.php b/tests/modules/images/fetchpriority/fetchpriority-test.php similarity index 93% rename from tests/modules/images/fetchpriority/Fetchpriority-Tests.php rename to tests/modules/images/fetchpriority/fetchpriority-test.php index d21cb03cf5..0ada049d9d 100644 --- a/tests/modules/images/fetchpriority/Fetchpriority-Tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-test.php @@ -1,6 +1,6 @@ factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); From ddc78b8f09c7da6e1de59f2635c30eb9538408bd Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 13 Sep 2022 11:44:02 -0700 Subject: [PATCH 09/31] Fix CODEOWNERS indentation. --- .github/CODEOWNERS | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 846f975278..0dba96791f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -61,9 +61,9 @@ /tests/testdata/modules/images/dominant-color @pbearne @spacedmonkey # Module: Fetchpriority -/modules/images/fetchpriority @pbearne @adamsilverstein -/tests/modules/images/fetchpriority @pbearne @adamsilverstein -/tests/testdata/modules/images/fetchpriority @pbearne @adamsilverstein +/modules/images/fetchpriority @pbearne @adamsilverstein +/tests/modules/images/fetchpriority @pbearne @adamsilverstein +/tests/testdata/modules/images/fetchpriority @pbearne @adamsilverstein # Module: Full Page Cache Health Check /modules/site-health/audit-full-page-cache @manuelRod @westonruter From 1cdf6a4efa23d445340eb38e97339a1f6d33aaad Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 15:29:22 -0400 Subject: [PATCH 10/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index a595e8607c..151c2de655 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -24,7 +24,7 @@ function fetchpriority_img_tag_add( $filtered_image, $context ) { return $filtered_image; } - if ( strpos( $filtered_image, 'loading="lazy"' ) === false ) { + if ( strpos( $filtered_image, ' loading="lazy"' ) === false && strpos( $filtered_image, ' fetchpriority=' ) === false ) { $filtered_image = str_replace( ' Date: Tue, 13 Sep 2022 15:29:31 -0400 Subject: [PATCH 11/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 151c2de655..6d949e5dfe 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -13,10 +13,9 @@ * * @since n.e.x.t * - * @param string $filtered_image The filtered image. + * @param string $filtered_image The image tag to filter. * @param string $context The context of the image. - * - * @return string image tag + * @return string The filtered image tag. */ function fetchpriority_img_tag_add( $filtered_image, $context ) { From 15665fb56be56b52ce8edca33e04fc0b88ff9edd Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 15:29:46 -0400 Subject: [PATCH 12/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 6d949e5dfe..ea21ed5785 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -25,7 +25,8 @@ function fetchpriority_img_tag_add( $filtered_image, $context ) { if ( strpos( $filtered_image, ' loading="lazy"' ) === false && strpos( $filtered_image, ' fetchpriority=' ) === false ) { $filtered_image = str_replace( ' Date: Tue, 13 Sep 2022 15:29:56 -0400 Subject: [PATCH 13/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index ea21ed5785..034a527a28 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -31,4 +31,4 @@ function fetchpriority_img_tag_add( $filtered_image, $context ) { return $filtered_image; } -add_filter( 'wp_content_img_tag', 'fetchpriority_img_tag_add', 10, 2 ); +add_filter( 'wp_content_img_tag', 'fetchpriority_img_tag_add_attr', 10, 2 ); From 6702525eb79c3a8d24b5bebbcd7b231951221c67 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 15:30:50 -0400 Subject: [PATCH 14/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 034a527a28..d2f7618f91 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -17,7 +17,7 @@ * @param string $context The context of the image. * @return string The filtered image tag. */ -function fetchpriority_img_tag_add( $filtered_image, $context ) { +function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { if ( 'the_content' !== $context ) { return $filtered_image; From 2fbc82566c0eee2d47958babfb6ef882c01e7159 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 15:31:18 -0400 Subject: [PATCH 15/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index d2f7618f91..38b2fb4a03 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -9,7 +9,7 @@ */ /** - * Filter image tags in content to add fetchpriority to the image tag if lazy loaded is not set. + * Filters an image tag in content to add the fetchpriority attribute if it is not lazy-loaded. * * @since n.e.x.t * From 7caf166ae5a0473695ba31b427be76da25b64df1 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Sep 2022 15:32:22 -0400 Subject: [PATCH 16/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 38b2fb4a03..65bec04c58 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -19,7 +19,7 @@ */ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { - if ( 'the_content' !== $context ) { + if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { return $filtered_image; } From e6826eb1f164ede4582d793d709d2e925540e8d4 Mon Sep 17 00:00:00 2001 From: pbearne Date: Tue, 13 Sep 2022 15:43:39 -0400 Subject: [PATCH 17/31] code review feedback --- modules/images/fetchpriority/load.php | 13 +++++++++++++ ...tchpriority-test.php => Fetchpriority-Tests.php} | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) rename tests/modules/images/fetchpriority/{fetchpriority-test.php => Fetchpriority-Tests.php} (67%) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 65bec04c58..6d0a46bdbf 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -32,3 +32,16 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { return $filtered_image; } add_filter( 'wp_content_img_tag', 'fetchpriority_img_tag_add_attr', 10, 2 ); + +/** + * Filters the post thumbnail HTML to conditionally add the fetchpriority attribute. + * + * @since n.e.x.t + * + * @param string $html The post thumbnail HTML to filter. + * @return string The filtered post thumbnail HTML. + */ +function fetchpriority_filter_post_thumbnail_html( $html ) { + return fetchpriority_img_tag_add_attr( $html, 'the_post_thumbnail' ); +} +add_filter( 'post_thumbnail_html', 'fetchpriority_filter_post_thumbnail_html' ); diff --git a/tests/modules/images/fetchpriority/fetchpriority-test.php b/tests/modules/images/fetchpriority/Fetchpriority-Tests.php similarity index 67% rename from tests/modules/images/fetchpriority/fetchpriority-test.php rename to tests/modules/images/fetchpriority/Fetchpriority-Tests.php index 0ada049d9d..1214602f0a 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-test.php +++ b/tests/modules/images/fetchpriority/Fetchpriority-Tests.php @@ -2,14 +2,14 @@ class Fetchpriority_Test extends WP_UnitTestCase { - public function test_wp_filter_content_tags_filter_adds_fetchpriority_fist_image_only() { + public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); $img = get_image_tag( $attachment_id, '', '', '', 'large' ); - $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'the_content' ) ); - $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'not_content' ) ); + $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); $img = str_replace( 'assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add( $img, 'the_content' ) ); + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); } } From 6dd14bb9974bab70d92cdff828f4e50f492c7507 Mon Sep 17 00:00:00 2001 From: pbearne Date: Wed, 14 Sep 2022 09:37:22 -0400 Subject: [PATCH 18/31] added test for wp_filter_content_tags() function tests not working --- modules/images/fetchpriority/load.php | 10 +++-- .../fetchpriority/Fetchpriority-Tests.php | 15 -------- .../fetchpriority/fetchpriority-tests.php | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 19 deletions(-) delete mode 100644 tests/modules/images/fetchpriority/Fetchpriority-Tests.php create mode 100644 tests/modules/images/fetchpriority/fetchpriority-tests.php diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 6d0a46bdbf..56489e4a25 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -23,10 +23,9 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { return $filtered_image; } - if ( strpos( $filtered_image, ' loading="lazy"' ) === false && strpos( $filtered_image, ' fetchpriority=' ) === false ) { + if ( ! empty( $filtered_image ) && strpos( $filtered_image, 'loading="lazy"' ) === false && strpos( $filtered_image, 'fetchpriority=' ) === false ) { $filtered_image = str_replace( 'factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); - $img = get_image_tag( $attachment_id, '', '', '', 'large' ); - - $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); - $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); - - $img = str_replace( 'assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); - } -} diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php new file mode 100644 index 0000000000..6773657c66 --- /dev/null +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -0,0 +1,37 @@ +post->create_and_get(); + + $file = TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'; + self::$attachment_id = $factory->attachment->create_upload_object( + $file, + self::$post->ID, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + } + public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); + + $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); + + $img = str_replace( 'assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); + } +} From 958987ea869df24676a60f1cc3d8078e09a72c20 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 22 Sep 2022 12:42:22 -0400 Subject: [PATCH 19/31] Update tests/modules/images/fetchpriority/fetchpriority-tests.php Co-authored-by: Mukesh Panchal --- tests/modules/images/fetchpriority/fetchpriority-tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index 6773657c66..ad630e9311 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -3,7 +3,7 @@ /** * Tests the adding of fetchpriority to img tags in the content. * - * @Covers ::wp_filter_content_tags + * @covers ::fetchpriority_img_tag_add_attr */ class Fetchpriority_Test extends WP_UnitTestCase { From 28122a215407a3e20e1a4429d6afcbb7e8df0e5b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 22 Sep 2022 12:42:34 -0400 Subject: [PATCH 20/31] Update tests/modules/images/fetchpriority/fetchpriority-tests.php Co-authored-by: Mukesh Panchal --- tests/modules/images/fetchpriority/fetchpriority-tests.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index ad630e9311..9baf1c097e 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -26,7 +26,11 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { ); } public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { - $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); + $attachment_id = $this->factory->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' + ); + + $img = get_image_tag( $attachment_id, '', '', '', 'large' ); $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); From 55b3b921ebb2f27e1dd817f8f5e651bdf4b6b82a Mon Sep 17 00:00:00 2001 From: pbearne Date: Wed, 28 Sep 2022 19:32:36 -0400 Subject: [PATCH 21/31] added test for wp_filter_content_tags() function tests not working --- modules/images/fetchpriority/load.php | 1 + .../fetchpriority/fetchpriority-tests.php | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 56489e4a25..6ab63949de 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -20,6 +20,7 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { + return $filtered_image; } diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index 6773657c66..721928d33a 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -34,4 +34,28 @@ public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading $img = str_replace( 'assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); } + + public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); + + $img = ' +
' . $img . '
+ + +

This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:

+ + +
' . $img . '
+ +'; + $content = wp_filter_content_tags( $img, 'the_content' ); + $this->assertStringContainsString( 'fetchpriority="high"', $content ); + + $spilt = explode( 'fetchpriority', $content ); + $this->assertEquals( 2, count( $spilt ) ); + } + public function test_get_the_post_thumbnail() { + set_post_thumbnail( self::$post, self::$attachment_id ); + $this->assertStringContainsString( 'fetchpriority="high"', get_the_post_thumbnail( self::$post ) ); + } } From bf515a285a368f496a9eb1ab5d2783fb5dcc3717 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 28 Oct 2022 09:44:56 -0600 Subject: [PATCH 22/31] Improve tests with multiple attachments, in_the_loop true --- modules/images/fetchpriority/load.php | 1 - .../fetchpriority/fetchpriority-tests.php | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 6ab63949de..56489e4a25 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -20,7 +20,6 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { - return $filtered_image; } diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index e6b80862ee..a9bc1cb523 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -1,36 +1,44 @@ post->create_and_get(); - - $file = TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'; - self::$attachment_id = $factory->attachment->create_upload_object( + self::$post = $factory->post->create_and_get(); + $file = DIR_TESTDATA . '/images/canola.jpg'; + self::$attachment_id = $factory->attachment->create_upload_object( $file, self::$post->ID, array( 'post_mime_type' => 'image/jpeg', ) ); - } - public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { - $attachment_id = $this->factory->attachment->create_upload_object( - TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' + self::$attachment_id_2 = $factory->attachment->create_upload_object( + $file, + self::$post->ID, + array( + 'post_mime_type' => 'image/jpeg', + ) ); + } + + public static function tear_down_after_class() { + wp_delete_attachment( self::$attachment_id, true ); + wp_delete_attachment( self::$attachment_id_2, true ); + parent::tear_down_after_class(); + } - $img = get_image_tag( $attachment_id, '', '', '', 'large' ); + public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading_lazy() { + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); $this->assertStringContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'not_content' ) ); @@ -40,26 +48,30 @@ public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading } public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { - $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); + global $wp_query; + + $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); + $img_2 = get_image_tag( self::$attachment_id_2, '', '', '', 'large' ); - $img = ' + $img = '
' . $img . '
-

This is an example page. It\'s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:

+

This is an example page.

- -
' . $img . '
+ +
' . $img_2 . '
'; - $content = wp_filter_content_tags( $img, 'the_content' ); + // Ensure image filtering occurs 'in_the_loop'. + $wp_query->in_the_loop = true; + $content = wp_filter_content_tags( $img, 'the_content' ); + $wp_query->in_the_loop = false; $this->assertStringContainsString( 'fetchpriority="high"', $content ); + $this->assertStringContainsString( 'loading="lazy"', $content ); + $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); $spilt = explode( 'fetchpriority', $content ); $this->assertEquals( 2, count( $spilt ) ); } - public function test_get_the_post_thumbnail() { - set_post_thumbnail( self::$post, self::$attachment_id ); - $this->assertStringContainsString( 'fetchpriority="high"', get_the_post_thumbnail( self::$post ) ); - } } From 4a4c2377419927eeca31c8fe041520954d157301 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 28 Oct 2022 09:59:18 -0600 Subject: [PATCH 23/31] Mock is_main_query --- tests/modules/images/fetchpriority/fetchpriority-tests.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index a9bc1cb523..c8868d86d8 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -49,7 +49,7 @@ public function test_fetchpriority_img_tag_add_attr_based_on_context_and_loading public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { global $wp_query; - + global $wp_the_query; $img = get_image_tag( self::$attachment_id, '', '', '', 'large' ); $img_2 = get_image_tag( self::$attachment_id_2, '', '', '', 'large' ); @@ -63,10 +63,10 @@ public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() {
' . $img_2 . '
'; - // Ensure image filtering occurs 'in_the_loop'. + // Ensure image filtering occurs 'in_the_loop', is_main_query. + $wp_the_query = $wp_query; $wp_query->in_the_loop = true; $content = wp_filter_content_tags( $img, 'the_content' ); - $wp_query->in_the_loop = false; $this->assertStringContainsString( 'fetchpriority="high"', $content ); $this->assertStringContainsString( 'loading="lazy"', $content ); $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); From 8f42c02548316a7032feeb299715049f894eea20 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 14 Nov 2022 14:30:05 -0700 Subject: [PATCH 24/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 56489e4a25..cd406ea9a7 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -26,6 +26,7 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { if ( ! empty( $filtered_image ) && strpos( $filtered_image, 'loading="lazy"' ) === false && strpos( $filtered_image, 'fetchpriority=' ) === false ) { $filtered_image = str_replace( ' Date: Mon, 14 Nov 2022 14:30:43 -0700 Subject: [PATCH 25/31] Update modules/images/fetchpriority/load.php Co-authored-by: Felix Arntz --- modules/images/fetchpriority/load.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index cd406ea9a7..058b3bdada 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -43,7 +43,6 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { */ function fetchpriority_filter_post_thumbnail_html( $html ) { $html = fetchpriority_img_tag_add_attr( $html, 'the_post_thumbnail' ); - remove_filter( 'post_thumbnail_html', 'fetchpriority_filter_post_thumbnail_html' ); return $html; } From 0e3061c4f0b0a0acff267db36f3e847b260a37b0 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 14 Nov 2022 14:37:10 -0700 Subject: [PATCH 26/31] =?UTF-8?q?test=5Ffetchpriority=E2=80=A6=20expect=20?= =?UTF-8?q?exactly=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/modules/images/fetchpriority/fetchpriority-tests.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index c8868d86d8..5ac2589115 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -71,7 +71,6 @@ public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { $this->assertStringContainsString( 'loading="lazy"', $content ); $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); - $spilt = explode( 'fetchpriority', $content ); - $this->assertEquals( 2, count( $spilt ) ); + $this->assertEquals( 1, substr_count( $content, 'fetchpriority' ) ); } } From 0494e6d255833b40d009f7c7f3bead8ea781d33b Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 14 Nov 2022 14:45:38 -0700 Subject: [PATCH 27/31] Fetchpriority relies on lazy loading logic --- modules/images/fetchpriority/load.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 058b3bdada..50ae5a6ae5 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -23,6 +23,11 @@ function fetchpriority_img_tag_add_attr( $filtered_image, $context ) { return $filtered_image; } + // Fetchpriority relies on lazy loading logic. + if ( ! wp_lazy_loading_enabled( 'img', $context ) ) { + return $filtered_image; + } + if ( ! empty( $filtered_image ) && strpos( $filtered_image, 'loading="lazy"' ) === false && strpos( $filtered_image, 'fetchpriority=' ) === false ) { $filtered_image = str_replace( ' Date: Mon, 14 Nov 2022 14:54:13 -0700 Subject: [PATCH 28/31] Add test_fetchpriority_img_tag_with_lazyload_disabled --- .../fetchpriority/fetchpriority-tests.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index 5ac2589115..89f26cd067 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -73,4 +73,23 @@ public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { $this->assertEquals( 1, substr_count( $content, 'fetchpriority' ) ); } + + public function test_fetchpriority_img_tag_with_lazyload_disabled() { + // Disable lazy loading. + add_filter( 'wp_lazy_loading_enabled', '__return_false' ); + + $post = $factory->post->create_and_get(); + $attachment_id = $factory->attachment->create_upload_object( + DIR_TESTDATA . '/images/canola.jpg', + $post->ID, + array( + 'post_mime_type' => 'image/jpeg', + ) + ); + $img = get_image_tag( $attachment_id, '', '', '', 'large' ); + $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); + + // Cleanup. + wp_delete_attachment( self::$attachment_id, true ); + } } From a7392d22ac6fa250f36ce95d8f9469f498d6749b Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 14 Nov 2022 15:06:29 -0700 Subject: [PATCH 29/31] Combine tests --- .../fetchpriority/fetchpriority-tests.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index 89f26cd067..a70e56410a 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -72,24 +72,11 @@ public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); $this->assertEquals( 1, substr_count( $content, 'fetchpriority' ) ); - } - public function test_fetchpriority_img_tag_with_lazyload_disabled() { - // Disable lazy loading. + // Disable lazy loading and verify fetchpriority isn't added. add_filter( 'wp_lazy_loading_enabled', '__return_false' ); + $content = wp_filter_content_tags( $img, 'the_content' ); + $this->assertNotStringContainsString( 'fetchpriority="high"', $content ); - $post = $factory->post->create_and_get(); - $attachment_id = $factory->attachment->create_upload_object( - DIR_TESTDATA . '/images/canola.jpg', - $post->ID, - array( - 'post_mime_type' => 'image/jpeg', - ) - ); - $img = get_image_tag( $attachment_id, '', '', '', 'large' ); - $this->assertStringNotContainsString( 'fetchpriority="high"', fetchpriority_img_tag_add_attr( $img, 'the_content' ) ); - - // Cleanup. - wp_delete_attachment( self::$attachment_id, true ); } } From 0a4fe5864cfbfb353a6881e292a13f3270a7da14 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 14 Nov 2022 15:33:08 -0700 Subject: [PATCH 30/31] Correct assertion typo --- tests/modules/images/fetchpriority/fetchpriority-tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/images/fetchpriority/fetchpriority-tests.php b/tests/modules/images/fetchpriority/fetchpriority-tests.php index a70e56410a..f60357b981 100644 --- a/tests/modules/images/fetchpriority/fetchpriority-tests.php +++ b/tests/modules/images/fetchpriority/fetchpriority-tests.php @@ -76,7 +76,7 @@ public function test_fetchpriority_img_tag_add_in_wp_filter_content_tags() { // Disable lazy loading and verify fetchpriority isn't added. add_filter( 'wp_lazy_loading_enabled', '__return_false' ); $content = wp_filter_content_tags( $img, 'the_content' ); - $this->assertNotStringContainsString( 'fetchpriority="high"', $content ); + $this->assertStringNotContainsString( 'fetchpriority="high"', $content ); } } From 66d2a0da75dc0930f7d7e9625b442ec5fd7236b6 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 22 Nov 2022 09:52:07 -0800 Subject: [PATCH 31/31] SImplify fetchpriority module description. --- modules/images/fetchpriority/load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php index 50ae5a6ae5..6898cfede4 100644 --- a/modules/images/fetchpriority/load.php +++ b/modules/images/fetchpriority/load.php @@ -1,7 +1,7 @@