From 2be5c02da85ec05f5d444f0c7cf586579d4a47e3 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 3 Nov 2022 14:47:21 -0700 Subject: [PATCH 01/10] Fix bug so that first content image is not lazy-loaded in block themes either. --- src/wp-includes/block-template.php | 2 +- .../blocks/post-featured-image.php | 6 ++ src/wp-includes/media.php | 3 + tests/phpunit/tests/media.php | 93 +++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 0aa571117e899..48e1d33be892f 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -241,7 +241,7 @@ function get_the_block_template_html() { $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = shortcode_unautop( $content ); - $content = wp_filter_content_tags( $content ); + $content = wp_filter_content_tags( $content, 'the_block_template' ); $content = do_shortcode( $content ); $content = str_replace( ']]>', ']]>', $content ); diff --git a/src/wp-includes/blocks/post-featured-image.php b/src/wp-includes/blocks/post-featured-image.php index 495c8ec534a41..9f0b205696868 100644 --- a/src/wp-includes/blocks/post-featured-image.php +++ b/src/wp-includes/blocks/post-featured-image.php @@ -19,6 +19,12 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) } $post_ID = $block->context['postId']; + // Check is needed for backward compatibility with third-party plugins + // that might rely on the `in_the_loop` check; calling `the_post` sets it to true. + if ( ! in_the_loop() && have_posts() ) { + the_post(); + } + $is_link = isset( $attributes['isLink'] ) && $attributes['isLink']; $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail'; $post_title = trim( strip_tags( get_the_title( $post_ID ) ) ); diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index bfd71cdcb3b81..2ee7676bd690d 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5428,6 +5428,9 @@ function wp_get_webp_info( $filename ) { function wp_get_loading_attr_default( $context ) { // Only elements with 'the_content' or 'the_post_thumbnail' context have special handling. if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { + if ( 'the_block_template' === $context ) { + return false; + } return 'lazy'; } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 0d5ac7586369b..29b2a925f3bd7 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3451,6 +3451,7 @@ public function data_attachment_permalinks_based_on_parent_status() { /** * @ticket 53675 + * @ticket 56930 * @dataProvider data_wp_get_loading_attr_default * * @param string $context @@ -3490,6 +3491,9 @@ function test_wp_get_loading_attr_default( $context ) { // Yes, for all subsequent elements. $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); + + // The only exception is 'the_block_template' context, which shouldn't lazy-load images by default. + $this->assertFalse( wp_get_loading_attr_default( 'the_block_template' ) ); } } @@ -3603,6 +3607,95 @@ function() { $this->assertSame( 3, $omit_threshold ); } + /** + * @ticket 56930 + */ + 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 = ''; + + $html = get_the_block_template_html(); + $this->assertSame( '
' . $expected_content . '
', $html ); + } + + /** + * @ticket 56930 + */ + public function test_wp_filter_content_tags_does_not_lazy_load_first_featured_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' ); + 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 = '
' . get_the_post_thumbnail( self::$post_ids['publish'], 'post-thumbnail', array( 'loading' => false ) ) . '
'; + + // 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 = ' '; + + $html = get_the_block_template_html(); + $this->assertSame( '
' . $expected_featured_image . '
' . $expected_content . '
', $html ); + } + private function reset_content_media_count() { // Get current value without increasing. $content_media_count = wp_increase_content_media_count( 0 ); From 6f61ab5a81a7301bef4e96fab31ba95bfc9f0dde Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 16 Jan 2023 22:11:12 +0100 Subject: [PATCH 02/10] Enhance fix to also provide basic support for block templates. --- src/wp-includes/block-template.php | 2 +- src/wp-includes/blocks/template-part.php | 2 +- src/wp-includes/media.php | 40 +++++++++++------- tests/phpunit/tests/media.php | 53 ++++++++++++++++++++++-- 4 files changed, 77 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 1ffe4783f223b..7464dfe040151 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -241,7 +241,7 @@ function get_the_block_template_html() { $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = shortcode_unautop( $content ); - $content = wp_filter_content_tags( $content, 'the_block_template' ); + $content = wp_filter_content_tags( $content, 'the_template' ); $content = do_shortcode( $content ); $content = str_replace( ']]>', ']]>', $content ); diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index bef49341d7bb5..972f399f36a87 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -134,7 +134,7 @@ function render_block_core_template_part( $attributes ) { $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = shortcode_unautop( $content ); - $content = wp_filter_content_tags( $content ); + $content = wp_filter_content_tags( $content, "the_template_part_{$area}" ); $content = do_shortcode( $content ); // Handle embeds for block template parts. diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index bc8d3bbdae266..0993070d57e82 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5432,28 +5432,38 @@ function wp_get_webp_info( $filename ) { * that the `loading` attribute should be skipped. */ function wp_get_loading_attr_default( $context ) { - // Only elements with 'the_content' or 'the_post_thumbnail' context have special handling. - if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { - if ( 'the_block_template' === $context ) { - return false; - } - return 'lazy'; + // Skip lazy-loading for the overall block template, as it is handled more granularly. + if ( 'the_template' === $context ) { + return false; } - // Only elements within the main query loop have special handling. - if ( is_admin() || ! in_the_loop() || ! is_main_query() ) { - return 'lazy'; + // Do not lazy-load images in the header block template part, as they are likely above the fold. + $header_area = WP_TEMPLATE_PART_AREA_HEADER; + if ( "the_template_part_{$header_area}" === $context ) { + return false; } - // Increase the counter since this is a main query content element. - $content_media_count = wp_increase_content_media_count(); + // The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded, + // as they are likely above the fold. + if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) { + // Only elements within the main query loop have special handling. + if ( is_admin() || ! in_the_loop() || ! is_main_query() ) { + return 'lazy'; + } - // If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted. - if ( $content_media_count <= wp_omit_loading_attr_threshold() ) { - return false; + // Increase the counter since this is a main query content element. + $content_media_count = wp_increase_content_media_count(); + + // If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted. + if ( $content_media_count <= wp_omit_loading_attr_threshold() ) { + return false; + } + + // For elements after the threshold, lazy-load them as usual. + return 'lazy'; } - // For elements after the threshold, lazy-load them as usual. + // Lazy-load by default for any unknown context. return 'lazy'; } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 891e163686c9a..1d463a3ad062a 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3513,10 +3513,11 @@ public function test_wp_get_loading_attr_default( $context ) { // Yes, for all subsequent elements. $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); - - // The only exception is 'the_block_template' context, which shouldn't lazy-load images by default. - $this->assertFalse( wp_get_loading_attr_default( 'the_block_template' ) ); } + + // Exceptions: In the following contexts, images shouldn't be lazy-loaded by default. + $this->assertFalse( wp_get_loading_attr_default( 'the_template' ) ); + $this->assertFalse( wp_get_loading_attr_default( 'the_template_part_' . WP_TEMPLATE_PART_AREA_HEADER ) ); } public function data_wp_get_loading_attr_default() { @@ -3718,6 +3719,52 @@ function( $attr ) { $this->assertSame( '
' . $expected_featured_image . '
' . $expected_content . '
', $html ); } + /** + * @ticket 56930 + */ + public function test_wp_filter_content_tags_does_not_lazy_load_images_in_header() { + 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 = ''; + + // Header image should not be lazy-loaded, footer image should be lazy-loaded. + $expected_template_content = '
' . $header_img . '
'; + $expected_template_content .= '
' . wp_img_tag_add_loading_attr( $footer_img, 'force-lazy' ) . '
'; + + $html = get_the_block_template_html(); + $this->assertSame( '
' . $expected_template_content . '
', $html ); + } + private function reset_content_media_count() { // Get current value without increasing. $content_media_count = wp_increase_content_media_count( 0 ); From af3dc5e3014fa416804ab6683ab494024fd78307 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 16 Jan 2023 22:15:30 +0100 Subject: [PATCH 03/10] Fix WPCS violation. --- tests/phpunit/tests/media.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 1d463a3ad062a..9b5e317635cf8 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3736,20 +3736,20 @@ public function test_wp_filter_content_tags_does_not_lazy_load_images_in_header( // 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, + '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, + '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' ); From 9597731a058fd7d962dcd05c21b1f181d93d9285 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 27 Jan 2023 14:52:28 -0800 Subject: [PATCH 04/10] Remove unnecessary the_ prefix, in accordance with https://github.com/WordPress/gutenberg/pull/47203. --- src/wp-includes/block-template.php | 2 +- src/wp-includes/blocks/template-part.php | 2 +- src/wp-includes/media.php | 4 ++-- tests/phpunit/tests/media.php | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 7464dfe040151..4d5742a4b91a1 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -241,7 +241,7 @@ function get_the_block_template_html() { $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = shortcode_unautop( $content ); - $content = wp_filter_content_tags( $content, 'the_template' ); + $content = wp_filter_content_tags( $content, 'template' ); $content = do_shortcode( $content ); $content = str_replace( ']]>', ']]>', $content ); diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 972f399f36a87..51d8ab041ab29 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -134,7 +134,7 @@ function render_block_core_template_part( $attributes ) { $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = shortcode_unautop( $content ); - $content = wp_filter_content_tags( $content, "the_template_part_{$area}" ); + $content = wp_filter_content_tags( $content, "template_part_{$area}" ); $content = do_shortcode( $content ); // Handle embeds for block template parts. diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 0993070d57e82..0fdd323d52eaa 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5433,13 +5433,13 @@ function wp_get_webp_info( $filename ) { */ function wp_get_loading_attr_default( $context ) { // Skip lazy-loading for the overall block template, as it is handled more granularly. - if ( 'the_template' === $context ) { + if ( 'template' === $context ) { return false; } // Do not lazy-load images in the header block template part, as they are likely above the fold. $header_area = WP_TEMPLATE_PART_AREA_HEADER; - if ( "the_template_part_{$header_area}" === $context ) { + if ( "template_part_{$header_area}" === $context ) { return false; } diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 9b5e317635cf8..e3a75da5dc95e 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3516,8 +3516,8 @@ public function test_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( 'the_template' ) ); - $this->assertFalse( wp_get_loading_attr_default( 'the_template_part_' . WP_TEMPLATE_PART_AREA_HEADER ) ); + $this->assertFalse( wp_get_loading_attr_default( 'template' ) ); + $this->assertFalse( wp_get_loading_attr_default( 'template_part_' . WP_TEMPLATE_PART_AREA_HEADER ) ); } public function data_wp_get_loading_attr_default() { From 9ab8c44f4f192dcbd831ffc4b27a4b79265c7c42 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 27 Jan 2023 14:56:42 -0800 Subject: [PATCH 05/10] Make code changes for parity with https://github.com/WordPress/gutenberg/pull/47203. --- src/wp-includes/blocks/template-part.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 51d8ab041ab29..b762be7280da3 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -127,6 +127,21 @@ function render_block_core_template_part( $attributes ) { ''; } + // Look up area definition. + $area_definition = null; + $defined_areas = get_allowed_block_template_part_areas(); + foreach ( $defined_areas as $defined_area ) { + if ( $defined_area['area'] === $area ) { + $area_definition = $defined_area; + break; + } + } + + // If $area is not allowed, set it back to the uncategorized default. + if ( ! $area_definition ) { + $area = WP_TEMPLATE_PART_AREA_UNCATEGORIZED; + } + // Run through the actions that are typically taken on the_content. $seen_ids[ $template_part_id ] = true; $content = do_blocks( $content ); @@ -142,12 +157,9 @@ function render_block_core_template_part( $attributes ) { $content = $wp_embed->autoembed( $content ); if ( empty( $attributes['tagName'] ) ) { - $defined_areas = get_allowed_block_template_part_areas(); - $area_tag = 'div'; - foreach ( $defined_areas as $defined_area ) { - if ( $defined_area['area'] === $area && isset( $defined_area['area_tag'] ) ) { - $area_tag = $defined_area['area_tag']; - } + $area_tag = 'div'; + if ( $area_definition && isset( $area_definition['area_tag'] ) ) { + $area_tag = $area_definition['area_tag']; } $html_tag = $area_tag; } else { From 061565acff172dbc7cf26e354c2827bf706a1d49 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 3 Feb 2023 11:19:33 -0800 Subject: [PATCH 06/10] Add covers test annotations. --- tests/phpunit/tests/media.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 73522b1243e16..88ab05aca6b50 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3549,6 +3549,7 @@ public function data_attachment_permalinks_based_on_parent_status() { /** * @ticket 53675 * @ticket 56930 + * @covers ::wp_get_loading_attr_default * @dataProvider data_wp_get_loading_attr_default * * @param string $context @@ -3707,6 +3708,8 @@ function() { /** * @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; @@ -3746,6 +3749,8 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl /** * @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() { global $_wp_current_template_content, $wp_query, $wp_the_query, $post; @@ -3796,6 +3801,8 @@ function( $attr ) { /** * @ticket 56930 + * @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() { global $_wp_current_template_content; From 36fb70366ef21e734bc249d1512aef469a9f0afe Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 10 Feb 2023 17:17:44 -0800 Subject: [PATCH 07/10] Update test docs spacing and use multiple messages for assertions in same test. --- tests/phpunit/tests/media.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index a97ce5054cfa5..7b35eea854eea 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3549,7 +3549,9 @@ public function data_attachment_permalinks_based_on_parent_status() { /** * @ticket 53675 * @ticket 56930 + * * @covers ::wp_get_loading_attr_default + * * @dataProvider data_wp_get_loading_attr_default * * @param string $context @@ -3592,8 +3594,8 @@ public function test_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' ) ); - $this->assertFalse( wp_get_loading_attr_default( 'template_part_' . WP_TEMPLATE_PART_AREA_HEADER ) ); + $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() { @@ -3708,6 +3710,7 @@ function() { /** * @ticket 56930 + * * @covers ::wp_filter_content_tags * @covers ::wp_get_loading_attr_default */ @@ -3749,6 +3752,7 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl /** * @ticket 56930 + * * @covers ::wp_filter_content_tags * @covers ::wp_get_loading_attr_default */ @@ -3801,6 +3805,7 @@ function( $attr ) { /** * @ticket 56930 + * * @covers ::wp_filter_content_tags * @covers ::wp_get_loading_attr_default */ From f4ccd965ff76fd880deb16a45d610d6bc0d6ca11 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 10 Feb 2023 17:19:00 -0800 Subject: [PATCH 08/10] Update multiline comment format. --- src/wp-includes/media.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 7748c723a8104..95836d98a24b2 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5455,8 +5455,10 @@ function wp_get_loading_attr_default( $context ) { return false; } - // The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded, - // as they are likely above the fold. + /* + * The first elements in 'the_content' or 'the_post_thumbnail' should not be lazy-loaded, + * as they are likely above the fold. + */ if ( 'the_content' === $context || 'the_post_thumbnail' === $context ) { // Only elements within the main query loop have special handling. if ( is_admin() || ! in_the_loop() || ! is_main_query() ) { From eaa4d612526a901a7cec1a60e00068cf49c94258 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 10 Feb 2023 17:26:31 -0800 Subject: [PATCH 09/10] Add descriptions to test methods. --- tests/phpunit/tests/media.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 7b35eea854eea..d1cf06f0d24a6 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3547,6 +3547,8 @@ 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 * @@ -3709,6 +3711,9 @@ function() { } /** + * 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 @@ -3751,6 +3756,9 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl } /** + * 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 @@ -3804,6 +3812,9 @@ function( $attr ) { } /** + * Tests that wp_filter_content_tags() does not add loading="lazy" to images + * in a "Header" template part. + * * @ticket 56930 * * @covers ::wp_filter_content_tags From bdb0121c84346e7bb10e22fe7dc089e6df5689c7 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 13 Feb 2023 10:10:49 -0800 Subject: [PATCH 10/10] Fix broken tests after recent trunk update. --- tests/phpunit/tests/media.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index d1cf06f0d24a6..8e76c43807fcd 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -3752,7 +3752,7 @@ public function test_wp_filter_content_tags_does_not_lazy_load_first_image_in_bl $_wp_current_template_content = ''; $html = get_the_block_template_html(); - $this->assertSame( '
' . $expected_content . '
', $html ); + $this->assertSame( '
' . $expected_content . '
', $html ); } /** @@ -3808,7 +3808,7 @@ function( $attr ) { $_wp_current_template_content = ' '; $html = get_the_block_template_html(); - $this->assertSame( '
' . $expected_featured_image . '
' . $expected_content . '
', $html ); + $this->assertSame( '
' . $expected_featured_image . '
' . $expected_content . '
', $html ); } /**