<!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>[55847] trunk: Media: Conditionally skip lazy-loading on images before the loop to improve LCP performance.</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/55847">55847</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/55847","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-05-22 19:11:36 +0000 (Mon, 22 May 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: Conditionally skip lazy-loading on images before the loop to improve LCP performance.
When the logic to exclude images that likely appear above the fold from being lazy-loaded was introduced in WordPress 5.9, initially only images that appear within the main query loop were being considered. However, there is a good chance that images above the fold are rendered before the loop starts, for example in the header template part.
It is particularly common for a theme to display the featured image for a single post in the header. Based on HTTP Archive data from February 2023, the majority of LCP images that are still being lazy-loaded on WordPress sites use the `wp-post-image` class, i.e. are featured images.
This changeset enhances the logic in `wp_get_loading_attr_default()` to not lazy-load images that appear within or after the header template part and before the query loop, using a new `WP_Query::$before_loop` property.
For block themes, this was for the most part already addressed in <a href="https://core.trac.wordpress.org/changeset/55318">[55318]</a>, however this enhancement implements the solution in a more generally applicable way that brings the improvement to classic themes as well.
Props thekt12, flixos90, spacedmonkey, costdev, zunaid321, mukesh27.
Fixes <a href="https://core.trac.wordpress.org/ticket/58211">#58211</a>.
See <a href="https://core.trac.wordpress.org/ticket/53675">#53675</a>, <a href="https://core.trac.wordpress.org/ticket/56930">#56930</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.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>
<li><a href="#trunktestsphpunittestsqueryphp">trunk/tests/phpunit/tests/query.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></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/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php 2023-05-22 14:14:10 UTC (rev 55846)
+++ trunk/src/wp-includes/class-wp-query.php 2023-05-22 19:11:36 UTC (rev 55847)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -109,6 +109,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $current_post = -1;
</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">+ * Whether the caller is before the loop.
+ *
+ * @since 6.3.0
+ * @var bool
+ */
+ public $before_loop = true;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Whether the loop has started and the caller is in the loop.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -517,6 +525,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->post_count = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->current_post = -1;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->in_the_loop = false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->before_loop = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $this->request );
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->post );
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $this->comments );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3631,6 +3640,7 @@
</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"> $this->in_the_loop = true;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->before_loop = false;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( -1 == $this->current_post ) { // Loop has just started.
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3671,6 +3681,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Do some cleaning up after the loop.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rewind_posts();
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( 0 === $this->post_count ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->before_loop = false;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires if no results are found in a post query.
</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-05-22 14:14:10 UTC (rev 55846)
+++ trunk/src/wp-includes/media.php 2023-05-22 19:11:36 UTC (rev 55847)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5490,11 +5490,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @global WP_Query $wp_query WordPress Query object.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param string $context Context for the element for which the `loading` attribute value is requested.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|bool The default `loading` attribute value. Either 'lazy', 'eager', or a boolean `false`, to indicate
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_query;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Skip lazy-loading for the overall block template, as it is handled more granularly.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'template' === $context ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5501,19 +5505,37 @@
</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"> // Do not lazy-load images in the header block template part, as they are likely above the fold.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // For classic themes, this is handled in the condition below using the 'get_header' action.
</ins><span class="cx" style="display: block; padding: 0 10px"> $header_area = WP_TEMPLATE_PART_AREA_HEADER;
</span><span class="cx" style="display: block; padding: 0 10px"> if ( "template_part_{$header_area}" === $context ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><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">- /*
- * Skip programmatically created images within post content as they need to be handled together with the other
- * images within the post content.
- * Without this clause, they would already be counted below which skews the number and can result in the first
- * post content image being lazy-loaded only because there are images elsewhere in the post content.
- */
- if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) && doing_filter( 'the_content' ) ) {
- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Special handling for programmatically created image tags.
+ if ( ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) ) {
+ /*
+ * Skip programmatically created images within post content as they need to be handled together with the other
+ * images within the post content.
+ * Without this clause, they would already be counted below which skews the number and can result in the first
+ * post content image being lazy-loaded only because there are images elsewhere in the post content.
+ */
+ if ( doing_filter( 'the_content' ) ) {
+ return false;
+ }
+
+ // Conditionally skip lazy-loading on images before the loop.
+ if (
+ // Only apply for main query but before the loop.
+ $wp_query->before_loop && $wp_query->is_main_query()
+ /*
+ * Any image before the loop, but after the header has started should not be lazy-loaded,
+ * except when the footer has already started which can happen when the current template
+ * does not include any loop.
+ */
+ && did_action( 'get_header' ) && ! did_action( 'get_footer' )
+ ) {
+ return false;
+ }
</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"> /*
</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-05-22 14:14:10 UTC (rev 55846)
+++ trunk/tests/phpunit/tests/media.php 2023-05-22 19:11:36 UTC (rev 55847)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3559,8 +3559,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $context
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_get_loading_attr_default( $context ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_query, $wp_the_query;
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Return 'lazy' by default.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'lazy', wp_get_loading_attr_default( 'test' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'lazy', wp_get_loading_attr_default( 'wp_get_attachment_image' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3568,7 +3566,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Return 'lazy' if not in the loop or the main query.
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query = $this->get_new_wp_query_for_published_post();
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->reset_content_media_count();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->reset_omit_loading_attr_filter();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3579,7 +3577,7 @@
</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><span class="cx" style="display: block; padding: 0 10px"> // Set as main query.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_the_query = $wp_query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->set_main_query( $query );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // For contexts other than for the main content, still return 'lazy' even in the loop
</span><span class="cx" style="display: block; padding: 0 10px"> // and in the main query, and do not increase the content media count.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3613,10 +3611,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 53675
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_omit_loading_attr_threshold_filter() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_query, $wp_the_query;
-
- $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
- $wp_the_query = $wp_query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $query );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->reset_content_media_count();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->reset_omit_loading_attr_filter();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3640,8 +3636,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 53675
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_filter_content_tags_with_wp_get_loading_attr_default() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_query, $wp_the_query;
-
</del><span class="cx" style="display: block; padding: 0 10px"> $img1 = get_image_tag( self::$large_id, '', '', '', 'large' );
</span><span class="cx" style="display: block; padding: 0 10px"> $iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
</span><span class="cx" style="display: block; padding: 0 10px"> $img2 = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3659,8 +3653,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $content_expected = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;
</span><span class="cx" style="display: block; padding: 0 10px"> $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
- $wp_the_query = $wp_query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $query );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->reset_content_media_count();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->reset_omit_loading_attr_filter();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3699,6 +3693,142 @@
</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 before loop but after get_header if not main query.
+ *
+ * @ticket 58211
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
+ * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop
+ *
+ * @param string $context Context for the element for which the `loading` attribute value is requested.
+ */
+ public function test_wp_get_loading_attr_default_before_loop_if_not_main_query( $context ) {
+ global $wp_query;
+
+ $wp_query = $this->get_new_wp_query_for_published_post();
+ $this->reset_content_media_count();
+ $this->reset_omit_loading_attr_filter();
+
+ do_action( 'get_header' );
+
+ // Lazy if not main query.
+ $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
+ }
+
+ /**
+ * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header in main query but header was not called.
+ *
+ * @ticket 58211
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
+ * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop
+ *
+ * @param string $context Context for the element for which the `loading` attribute value is requested.
+ */
+ public function test_wp_get_loading_attr_default_before_loop_in_main_query_but_header_not_called( $context ) {
+ global $wp_query;
+
+ $wp_query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $wp_query );
+ $this->reset_content_media_count();
+ $this->reset_omit_loading_attr_filter();
+
+ // Lazy if header not called.
+ $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
+ }
+
+ /**
+ * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header for main query.
+ *
+ * @ticket 58211
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
+ * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop
+ *
+ * @param string $context Context for the element for which the `loading` attribute value is requested.
+ */
+ public function test_wp_get_loading_attr_default_before_loop_if_main_query( $context ) {
+ global $wp_query;
+
+ $wp_query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $wp_query );
+ $this->reset_content_media_count();
+ $this->reset_omit_loading_attr_filter();
+
+ do_action( 'get_header' );
+ $this->assertFalse( wp_get_loading_attr_default( $context ) );
+ }
+
+ /**
+ * Tests that wp_get_loading_attr_default() returns the expected loading attribute value after get_header and after loop.
+ *
+ * @ticket 58211
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
+ * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop
+ *
+ * @param string $context Context for the element for which the `loading` attribute value is requested.
+ */
+ public function test_wp_get_loading_attr_default_after_loop( $context ) {
+ global $wp_query;
+
+ $wp_query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $wp_query );
+ $this->reset_content_media_count();
+ $this->reset_omit_loading_attr_filter();
+
+ do_action( 'get_header' );
+
+ while ( have_posts() ) {
+ the_post();
+ }
+ $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
+ }
+
+ /**
+ * Tests that wp_get_loading_attr_default() returns the expected loading attribute if no loop.
+ *
+ * @ticket 58211
+ *
+ * @covers ::wp_get_loading_attr_default
+ *
+ * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop
+ *
+ * @param string $context Context for the element for which the `loading` attribute value is requested.
+ */
+ public function test_wp_get_loading_attr_default_no_loop( $context ) {
+ global $wp_query;
+
+ $wp_query = $this->get_new_wp_query_for_published_post();
+ $this->set_main_query( $wp_query );
+ $this->reset_content_media_count();
+ $this->reset_omit_loading_attr_filter();
+
+ // Ensure header and footer is called.
+ do_action( 'get_header' );
+ do_action( 'get_footer' );
+
+ // Load lazy if the there is no loop and footer was called.
+ $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_wp_get_loading_attr_default_before_and_no_loop() {
+ return array(
+ array( 'wp_get_attachment_image' ),
+ array( 'the_post_thumbnail' ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Tests that wp_filter_content_tags() does not add loading="lazy" to the first
</span><span class="cx" style="display: block; padding: 0 10px"> * image in the loop when using a block theme.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4166,6 +4296,34 @@
</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">+
+ /**
+ * Returns a new WP_Query.
+ *
+ * @global WP_Query $wp_query WordPress Query object.
+ *
+ * @return WP_Query a new query.
+ */
+ public function get_new_wp_query_for_published_post() {
+ global $wp_query;
+
+ // New query to $wp_query. update global for the loop.
+ $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
+
+ return $wp_query;
+ }
+
+ /**
+ * Sets a query as main query.
+ *
+ * @global WP_Query $wp_the_query WordPress Query object.
+ *
+ * @param WP_Query $query query to be set as main query.
+ */
+ public function set_main_query( $query ) {
+ global $wp_the_query;
+ $wp_the_query = $query;
+ }
</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"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsqueryphp"></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/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query.php 2023-05-22 14:14:10 UTC (rev 55846)
+++ trunk/tests/phpunit/tests/query.php 2023-05-22 19:11:36 UTC (rev 55847)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -897,4 +897,71 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $q->is_tax() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $q->is_tag( 'non-existent-tag' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Test if $before_loop is true before loop.
+ *
+ * @ticket 58211
+ */
+ public function test_before_loop_value_set_true_before_the_loop() {
+ // Get a new query with 3 posts.
+ $query = $this->get_new_wp_query_with_posts( 3 );
+
+ $this->assertTrue( $query->before_loop );
+ }
+
+ /**
+ * Test $before_loop value is set to false when the loop starts.
+ *
+ * @ticket 58211
+ *
+ * @covers WP_Query::the_post
+ */
+ public function test_before_loop_value_set_to_false_in_loop_with_post() {
+ // Get a new query with 2 posts.
+ $query = $this->get_new_wp_query_with_posts( 2 );
+
+ while ( $query->have_posts() ) {
+ // $before_loop should be set false as soon as the_post is called for the first time.
+ $query->the_post();
+
+ $this->assertFalse( $query->before_loop );
+ break;
+ }
+ }
+
+ /**
+ * Test $before_loop value is set to false when there is no post in the loop.
+ *
+ * @ticket 58211
+ *
+ * @covers WP_Query::have_posts
+ */
+ public function test_before_loop_set_false_after_loop_with_no_post() {
+ // New query without any posts in the result.
+ $query = new WP_Query(
+ array(
+ 'category_name' => 'non-existent-category',
+ )
+ );
+
+ // There will not be any posts, so the loop will never actually enter.
+ while ( $query->have_posts() ) {
+ $query->the_post();
+ }
+
+ // Still, this should be false as there are no results and entering the loop was attempted.
+ $this->assertFalse( $query->before_loop );
+ }
+
+ /**
+ * Get a new query with a given number of posts.
+ *
+ * @param int $no_of_posts Number of posts to be added in the query.
+ */
+ public function get_new_wp_query_with_posts( $no_of_posts ) {
+ $post_ids = self::factory()->post->create_many( $no_of_posts );
+ $query = new WP_Query( array( 'post__in' => $post_ids ) );
+ return $query;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>