diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 66d9497cb4..cbf083076a 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -59,6 +59,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/object-cache/audit-full-page-cache @manuelRod @westonruter /tests/modules/object-cache/audit-full-page-cache @manuelRod @westonruter diff --git a/modules/images/fetchpriority/load.php b/modules/images/fetchpriority/load.php new file mode 100644 index 0000000000..6898cfede4 --- /dev/null +++ b/modules/images/fetchpriority/load.php @@ -0,0 +1,54 @@ +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', + ) + ); + 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(); + } + + 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' ) ); + } + + 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' ); + + $img = ' +
' . $img . '
+ + +

This is an example page.

+ + +
' . $img_2 . '
+ +'; + // 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' ); + $this->assertStringContainsString( 'fetchpriority="high"', $content ); + $this->assertStringContainsString( 'loading="lazy"', $content ); + $this->assertTrue( strpos( $content, 'fetchpriority="high"' ) < strpos( $content, 'loading="lazy"' ) ); + + $this->assertEquals( 1, substr_count( $content, 'fetchpriority' ) ); + + // 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->assertStringNotContainsString( 'fetchpriority="high"', $content ); + + } +}