-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Ensure that first content image is not lazy-loaded in block themes either #3560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2be5c02
0f2b239
6f61ab5
af3dc5e
6fe3831
6230509
9597731
9ab8c44
ed2b3ef
061565a
bf56e30
b5ba312
36fb703
f4ccd96
eaa4d61
30b9443
bdb0121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3547,7 +3547,13 @@ public function data_attachment_permalinks_based_on_parent_status() { | |
| } | ||
|
|
||
| /** | ||
| * Tests that wp_get_loading_attr_default() returns the expected loading attribute value. | ||
| * | ||
| * @ticket 53675 | ||
| * @ticket 56930 | ||
| * | ||
| * @covers ::wp_get_loading_attr_default | ||
| * | ||
| * @dataProvider data_wp_get_loading_attr_default | ||
| * | ||
| * @param string $context | ||
|
|
@@ -3588,6 +3594,10 @@ public function test_wp_get_loading_attr_default( $context ) { | |
| // Yes, for all subsequent elements. | ||
| $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); | ||
| } | ||
|
|
||
| // Exceptions: In the following contexts, images shouldn't be lazy-loaded by default. | ||
| $this->assertFalse( wp_get_loading_attr_default( 'template' ), 'Images run through the overall block template filter should not be lazy-loaded.' ); | ||
| $this->assertFalse( wp_get_loading_attr_default( 'template_part_' . WP_TEMPLATE_PART_AREA_HEADER ), 'Images in the footer block template part should not be lazy-loaded.' ); | ||
| } | ||
|
|
||
| public function data_wp_get_loading_attr_default() { | ||
|
|
@@ -3700,6 +3710,159 @@ function() { | |
| $this->assertSame( 3, $omit_threshold ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_filter_content_tags() does not add loading="lazy" to the first | ||
| * image in the loop when using a block theme. | ||
| * | ||
| * @ticket 56930 | ||
| * | ||
| * @covers ::wp_filter_content_tags | ||
| * @covers ::wp_get_loading_attr_default | ||
| */ | ||
| public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_block_theme() { | ||
| global $_wp_current_template_content, $wp_query, $wp_the_query, $post; | ||
|
|
||
| // 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' ); | ||
|
|
||
| $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); | ||
| $img2 = get_image_tag( self::$large_id, '', '', '', 'medium' ); | ||
| $lazy_img2 = wp_img_tag_add_loading_attr( $img2, 'the_content' ); | ||
|
|
||
| // Only the second image should be lazy-loaded. | ||
| $post_content = $img1 . $img2; | ||
| $expected_content = wpautop( $img1 . $lazy_img2 ); | ||
|
|
||
| // Update the post to test with so that it has the above post content. | ||
| wp_update_post( | ||
| array( | ||
| 'ID' => self::$post_ids['publish'], | ||
| 'post_content' => $post_content, | ||
| 'post_content_filtered' => $post_content, | ||
| ) | ||
| ); | ||
|
|
||
| $wp_query = new WP_Query( array( 'p' => self::$post_ids['publish'] ) ); | ||
| $wp_the_query = $wp_query; | ||
| $post = get_post( self::$post_ids['publish'] ); | ||
| $this->reset_content_media_count(); | ||
| $this->reset_omit_loading_attr_filter(); | ||
|
|
||
| $_wp_current_template_content = '<!-- wp:post-content /-->'; | ||
|
|
||
| $html = get_the_block_template_html(); | ||
| $this->assertSame( '<div class="wp-site-blocks"><div class="entry-content wp-block-post-content is-layout-flow">' . $expected_content . '</div></div>', $html ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_filter_content_tags() does not add loading="lazy" | ||
| * to the featured image when using a block theme. | ||
| * | ||
| * @ticket 56930 | ||
| * | ||
| * @covers ::wp_filter_content_tags | ||
| * @covers ::wp_get_loading_attr_default | ||
| */ | ||
| public function test_wp_filter_content_tags_does_not_lazy_load_first_featured_image_in_block_theme() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Query: Should this have "first_featured_image", or just "featured_image"?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "first_featured_image" is correct, as other featured images further down in the page should still be lazy-loaded.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to testing:
Should we also cover this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, this is already being tested here I think. The final assertion tests that the featured image is not lazy-loaded, while the first image afterwards (an in-content image) is lazy-loaded. |
||
| global $_wp_current_template_content, $wp_query, $wp_the_query, $post; | ||
|
|
||
| // 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_get_attachment_image_attributes', | ||
| function( $attr ) { | ||
| unset( $attr['srcset'], $attr['sizes'], $attr['decoding'] ); | ||
| return $attr; | ||
| } | ||
| ); | ||
|
|
||
| $content_img = get_image_tag( self::$large_id, '', '', '', 'large' ); | ||
| $lazy_content_img = wp_img_tag_add_loading_attr( $content_img, 'the_content' ); | ||
|
|
||
| // The featured image should not be lazy-loaded as it is the first image. | ||
| $featured_image_id = self::$large_id; | ||
| update_post_meta( self::$post_ids['publish'], '_thumbnail_id', $featured_image_id ); | ||
| $expected_featured_image = '<figure class="wp-block-post-featured-image">' . get_the_post_thumbnail( self::$post_ids['publish'], 'post-thumbnail', array( 'loading' => false ) ) . '</figure>'; | ||
|
|
||
| // The post content image should be lazy-loaded since the featured image appears above. | ||
| $post_content = $content_img; | ||
| $expected_content = wpautop( $lazy_content_img ); | ||
|
|
||
| // Update the post to test with so that it has the above post content. | ||
| wp_update_post( | ||
| array( | ||
| 'ID' => self::$post_ids['publish'], | ||
| 'post_content' => $post_content, | ||
| 'post_content_filtered' => $post_content, | ||
| ) | ||
| ); | ||
|
|
||
| $wp_query = new WP_Query( array( 'p' => self::$post_ids['publish'] ) ); | ||
| $wp_the_query = $wp_query; | ||
| $post = get_post( self::$post_ids['publish'] ); | ||
| $this->reset_content_media_count(); | ||
| $this->reset_omit_loading_attr_filter(); | ||
|
|
||
| $_wp_current_template_content = '<!-- wp:post-featured-image /--> <!-- wp:post-content /-->'; | ||
|
|
||
| $html = get_the_block_template_html(); | ||
| $this->assertSame( '<div class="wp-site-blocks">' . $expected_featured_image . ' <div class="entry-content wp-block-post-content is-layout-flow">' . $expected_content . '</div></div>', $html ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that wp_filter_content_tags() does not add loading="lazy" to images | ||
| * in a "Header" template part. | ||
| * | ||
| * @ticket 56930 | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @covers ::wp_filter_content_tags | ||
| * @covers ::wp_get_loading_attr_default | ||
| */ | ||
| public function test_wp_filter_content_tags_does_not_lazy_load_images_in_header() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also add another test to ensure that images in a footer template part are always lazy loaded? I'm thinking in case something is later changed that accidentally excludes footer images from lazy loading.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @costdev This is already being tested by this method, the final assertion checks the entire template which includes both a header and footer template part, each with an image. Only the one in the header template part is expected to not be lazy-loaded, while the one in the footer should be. |
||
| global $_wp_current_template_content; | ||
|
|
||
| // 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' ); | ||
|
|
||
| // Use a single image for each header and footer template parts. | ||
| $header_img = get_image_tag( self::$large_id, '', '', '', 'large' ); | ||
| $footer_img = get_image_tag( self::$large_id, '', '', '', 'medium' ); | ||
|
|
||
| // Create header and footer template parts. | ||
| $header_post_id = self::factory()->post->create( | ||
| array( | ||
| 'post_type' => 'wp_template_part', | ||
| 'post_status' => 'publish', | ||
| 'post_name' => 'header', | ||
| 'post_content' => $header_img, | ||
| ) | ||
| ); | ||
| wp_set_post_terms( $header_post_id, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); | ||
| wp_set_post_terms( $header_post_id, get_stylesheet(), 'wp_theme' ); | ||
| $footer_post_id = self::factory()->post->create( | ||
| array( | ||
| 'post_type' => 'wp_template_part', | ||
| 'post_status' => 'publish', | ||
| 'post_name' => 'footer', | ||
| 'post_content' => $footer_img, | ||
| ) | ||
| ); | ||
| wp_set_post_terms( $footer_post_id, WP_TEMPLATE_PART_AREA_FOOTER, 'wp_template_part_area' ); | ||
| wp_set_post_terms( $footer_post_id, get_stylesheet(), 'wp_theme' ); | ||
|
|
||
| $_wp_current_template_content = '<!-- wp:template-part {"slug":"header","theme":"' . get_stylesheet() . '","tagName":"header"} /--><!-- wp:template-part {"slug":"footer","theme":"' . get_stylesheet() . '","tagName":"footer"} /-->'; | ||
|
|
||
| // Header image should not be lazy-loaded, footer image should be lazy-loaded. | ||
| $expected_template_content = '<header class="wp-block-template-part">' . $header_img . '</header>'; | ||
| $expected_template_content .= '<footer class="wp-block-template-part">' . wp_img_tag_add_loading_attr( $footer_img, 'force-lazy' ) . '</footer>'; | ||
|
|
||
| $html = get_the_block_template_html(); | ||
| $this->assertSame( '<div class="wp-site-blocks">' . $expected_template_content . '</div>', $html ); | ||
| } | ||
|
|
||
| private function reset_content_media_count() { | ||
| // Get current value without increasing. | ||
| $content_media_count = wp_increase_content_media_count( 0 ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.