<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55318] trunk: Media: Enhance logic to determine LCP image in block themes and avoid lazy-loading it.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55318">55318</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55318","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-02-13 18:32:40 +0000 (Mon, 13 Feb 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Enhance logic to determine LCP image in block themes and avoid lazy-loading it.
<a href="https://core.trac.wordpress.org/changeset/52065">[52065]</a> originally introduced the logic to guess the LCP image based on certain heuristics and to not lazy-load that image. However, with the introduction of block themes, that logic was not functioning correctly, resulting in all featured images to be lazy-loaded, regardless of whether it was the LCP image or not.
Together with an update to the `core/post-featured-image` block included in <a href="https://core.trac.wordpress.org/changeset/55079">[55079]</a>, this changeset fixes the logic to correctly handle featured images in block themes as well.
Additionally, in combination with an update to the `core/template-part` block from <a href="https://core.trac.wordpress.org/changeset/55246">[55246]</a>, this changeset includes an enhancement which uses the benefits of block template parts to avoid lazy-loading images in the `header` block template part, making the lazy-loading heuristics even more accurate for sites using a block theme.
Props flixos90, adamsilverstein, mamaduka, antonvlasenko, shahidul95, reduanmasud, costdev, mukesh27, ironprogrammer, manfcarlo, robinwpdeveloper, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/56930">#56930</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocktemplatephp">trunk/src/wp-includes/block-template.php</a></li>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-template.php 2023-02-13 15:18:19 UTC (rev 55317)
+++ trunk/src/wp-includes/block-template.php 2023-02-13 18:32:40 UTC (rev 55318)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,7 +241,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $content = wptexturize( $content );
</span><span class="cx" style="display: block; padding: 0 10px"> $content = convert_smilies( $content );
</span><span class="cx" style="display: block; padding: 0 10px"> $content = shortcode_unautop( $content );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content = wp_filter_content_tags( $content );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content = wp_filter_content_tags( $content, 'template' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $content = do_shortcode( $content );
</span><span class="cx" style="display: block; padding: 0 10px"> $content = str_replace( ']]>', ']]>', $content );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php 2023-02-13 15:18:19 UTC (rev 55317)
+++ trunk/src/wp-includes/media.php 2023-02-13 18:32:40 UTC (rev 55318)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5444,25 +5444,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * that the `loading` attribute should be skipped.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_get_loading_attr_default( $context ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Only elements with 'the_content' or 'the_post_thumbnail' context have special handling.
- if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) {
- return 'lazy';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Skip lazy-loading for the overall block template, as it is handled more granularly.
+ if ( 'template' === $context ) {
+ return false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Only elements within the main query loop have special handling.
- if ( is_admin() || ! in_the_loop() || ! is_main_query() ) {
- return 'lazy';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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 ( "template_part_{$header_area}" === $context ) {
+ return false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Increase the counter since this is a main query content element.
- $content_media_count = wp_increase_content_media_count();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * 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';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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';
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // For elements after the threshold, lazy-load them as usual.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Lazy-load by default for any unknown context.
</ins><span class="cx" style="display: block; padding: 0 10px"> return 'lazy';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php 2023-02-13 15:18:19 UTC (rev 55317)
+++ trunk/tests/phpunit/tests/media.php 2023-02-13 18:32:40 UTC (rev 55318)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3547,7 +3547,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that wp_get_loading_attr_default() returns the expected loading attribute value.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 53675
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 56930
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_wp_get_loading_attr_default
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $context
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3588,6 +3594,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Yes, for all subsequent elements.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // 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.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function data_wp_get_loading_attr_default() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3700,6 +3710,159 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 3, $omit_threshold );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * 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() {
+ 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
+ *
+ * @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;
+
+ // 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 );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> private function reset_content_media_count() {
</span><span class="cx" style="display: block; padding: 0 10px"> // Get current value without increasing.
</span><span class="cx" style="display: block; padding: 0 10px"> $content_media_count = wp_increase_content_media_count( 0 );
</span></span></pre>
</div>
</div>
</body>
</html>