<!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>[47554] trunk: Media: Enable lazy-loading of images by automatically adding the new `loading="lazy"` attribute to image tags on the front-end.</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/47554">47554</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/47554","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-04-08 00:53:18 +0000 (Wed, 08 Apr 2020)</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: Enable lazy-loading of images by automatically adding the new `loading="lazy"` attribute to image tags on the front-end.
- Introduces `wp_lazy_loading_enabled()`, `wp_filter_content_tags()`, `wp_img_tag_add_loading_attr()`, and `wp_img_tag_add_srcset_and_sizes_attr()` functions.
- Introduces `wp_lazy_loading_enabled`, `wp_img_tag_add_loading_attr`, and `wp_img_tag_add_srcset_and_sizes_attr` filters.
Props flixos90, addyosmani, mor10, swissspidy, pierlo, westonruter, spacedmonkey, mikeschroder, jonoaldersonwp, peterwilsoncc, narwen, jeffpaul, OptimizingMatters, futtta, mukeshpanchal27, azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/44427">#44427</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesdeprecatedphp">trunk/src/wp-includes/deprecated.php</a></li>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunktestsphpunittestsavatarphp">trunk/tests/phpunit/tests/avatar.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="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/src/wp-includes/default-filters.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,7 +175,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'wpautop' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'shortcode_unautop' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_content', 'prepend_attachment' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'the_content', 'wp_make_content_images_responsive' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'the_content', 'wp_filter_content_tags' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_excerpt', 'wptexturize' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_excerpt', 'convert_smilies' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -182,6 +182,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_excerpt', 'convert_chars' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_excerpt', 'wpautop' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_excerpt', 'shortcode_unautop' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'the_excerpt', 'wp_filter_content_tags' );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'the_post_thumbnail_caption', 'wptexturize' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -207,6 +208,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'convert_smilies', 20 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'wpautop' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'shortcode_unautop' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'widget_text_content', 'wp_filter_content_tags' );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run.
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // RSS filters.
</span></span></pre></div>
<a id="trunksrcwpincludesdeprecatedphp"></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/deprecated.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/deprecated.php 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/src/wp-includes/deprecated.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2509,11 +2509,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Determines whether the current admin page is generated by a plugin.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Use global $plugin_page and/or get_plugin_page_hookname() hooks.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * For more information on this and similar theme functions, check out
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
</ins><span class="cx" style="display: block; padding: 0 10px"> * Conditional Tags} article in the Theme Developer Handbook.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 1.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @deprecated 3.1.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3716,11 +3716,11 @@
</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"> * Determines whether the current URL is within the comments popup window.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * For more information on this and similar theme functions, check out
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
</ins><span class="cx" style="display: block; padding: 0 10px"> * Conditional Tags} article in the Theme Developer Handbook.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 1.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @deprecated 4.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3950,3 +3950,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_send_json_error( array( 'errorMessage' => __( 'The Press This plugin is required.' ) ) );
</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">+
+/**
+ * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
+ *
+ * @since 4.4.0
+ * @deprecated 5.5.0
+ *
+ * @see wp_image_add_srcset_and_sizes()
+ *
+ * @param string $content The raw post content to be filtered.
+ * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
+ */
+function wp_make_content_images_responsive( $content ) {
+ _deprecated_function( __FUNCTION__, '5.5.0', 'wp_filter_content_tags()' );
+
+ // This will also add the `loading` attribute to `img` tags, if enabled.
+ return wp_filter_content_tags( $content );
+}
</ins></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 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/src/wp-includes/media.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1021,14 +1021,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $html = '';
</span><span class="cx" style="display: block; padding: 0 10px"> $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $image ) {
</span><span class="cx" style="display: block; padding: 0 10px"> list( $src, $width, $height ) = $image;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $hwstring = image_hwstring( $width, $height );
- $size_class = $size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $attachment = get_post( $attachment_id );
+ $hwstring = image_hwstring( $width, $height );
+ $size_class = $size;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_array( $size_class ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $size_class = join( 'x', $size_class );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $attachment = get_post( $attachment_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $default_attr = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'src' => $src,
</span><span class="cx" style="display: block; padding: 0 10px"> 'class' => "attachment-$size_class size-$size_class",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1035,6 +1039,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
</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">+ // Add `loading` attribute.
+ if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
+ $default_attr['loading'] = 'lazy';
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $attr = wp_parse_args( $attr, $default_attr );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Generate 'srcset' and 'sizes' if not already present.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1061,7 +1070,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.8.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $attr Array of attribute values for the image markup, keyed by attribute name.
</ins><span class="cx" style="display: block; padding: 0 10px"> * See wp_get_attachment_image().
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $attachment Image attachment post.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string|array $size Requested size. Image size or array of width and height values
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1068,11 +1077,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * (in that order). Default 'thumbnail'.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $attr = array_map( 'esc_attr', $attr );
</span><span class="cx" style="display: block; padding: 0 10px"> $html = rtrim( "<img $hwstring" );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $attr as $name => $value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $html .= " $name=" . '"' . $value . '"';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $html .= ' />';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1478,56 +1490,6 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
- *
- * @since 4.4.0
- *
- * @see wp_image_add_srcset_and_sizes()
- *
- * @param string $content The raw post content to be filtered.
- * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
- */
-function wp_make_content_images_responsive( $content ) {
- if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
- return $content;
- }
-
- $selected_images = array();
- $attachment_ids = array();
-
- foreach ( $matches[0] as $image ) {
- if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
- $attachment_id = absint( $class_id[1] );
-
- if ( $attachment_id ) {
- /*
- * If exactly the same image tag is used more than once, overwrite it.
- * All identical tags will be replaced later with 'str_replace()'.
- */
- $selected_images[ $image ] = $attachment_id;
- // Overwrite the ID when the same image is included more than once.
- $attachment_ids[ $attachment_id ] = true;
- }
- }
- }
-
- if ( count( $attachment_ids ) > 1 ) {
- /*
- * Warm the object cache with post and meta information for all found
- * images to avoid making individual database calls.
- */
- _prime_post_caches( array_keys( $attachment_ids ), false, true );
- }
-
- foreach ( $selected_images as $image => $attachment_id ) {
- $image_meta = wp_get_attachment_metadata( $attachment_id );
- $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
- }
-
- return $content;
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px"> * Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1617,6 +1579,193 @@
</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">+ * Determine whether to add the `loading` attribute to the specified tag in the specified context.
+ *
+ * @since 5.5.0
+ *
+ * @param string $tag_name The tag name.
+ * @param string $context Additional context, like the current filter name or the function name from where this was called.
+ * @return bool Whether to add the attribute.
+ */
+function wp_lazy_loading_enabled( $tag_name, $context ) {
+ // By default add to all 'img' tags.
+ // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
+ $default = ( 'img' === $tag_name );
+
+ /**
+ * Filters whether to add the `loading` attribute to the specified tag in the specified context.
+ *
+ * @since 5.5.0
+ *
+ * @param bool $default Default value.
+ * @param string $tag_name The tag name.
+ * @param string $context Additional context, like the current filter name or the function name from where this was called.
+ */
+ return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
+}
+
+/**
+ * Filters specific tags in post content and modifies their markup.
+ *
+ * This function adds `srcset`, `sizes`, and `loading` attributes to `img` HTML tags.
+ *
+ * @since 5.5.0
+ *
+ * @see wp_img_tag_add_loading_attr()
+ * @see wp_img_tag_add_srcset_and_sizes_attr()
+ *
+ * @param string $content The HTML content to be filtered.
+ * @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set.
+ * @return string Converted content with images modified.
+ */
+function wp_filter_content_tags( $content, $context = null ) {
+ if ( null === $context ) {
+ $context = current_filter();
+ }
+
+ $add_loading_attr = wp_lazy_loading_enabled( 'img', $context );
+
+ if ( false === strpos( $content, '<img' ) ) {
+ return $content;
+ }
+
+ if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) {
+ return $content;
+ }
+
+ // List of the unique `img` tags found in $content.
+ $images = array();
+
+ foreach ( $matches[0] as $image ) {
+ if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
+ $attachment_id = absint( $class_id[1] );
+
+ if ( $attachment_id ) {
+ // If exactly the same image tag is used more than once, overwrite it.
+ // All identical tags will be replaced later with 'str_replace()'.
+ $images[ $image ] = $attachment_id;
+ continue;
+ }
+ }
+
+ $images[ $image ] = 0;
+ }
+
+ // Reduce the array to unique attachment IDs.
+ $attachment_ids = array_unique( array_filter( array_values( $images ) ) );
+
+ if ( count( $attachment_ids ) > 1 ) {
+ /*
+ * Warm the object cache with post and meta information for all found
+ * images to avoid making individual database calls.
+ */
+ _prime_post_caches( $attachment_ids, false, true );
+ }
+
+ foreach ( $images as $image => $attachment_id ) {
+ $filtered_image = $image;
+
+ // Add 'srcset' and 'sizes' attributes if applicable.
+ if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) {
+ $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id );
+ }
+
+ // Add 'loading' attribute if applicable.
+ if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
+ $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
+ }
+
+ if ( $filtered_image !== $image ) {
+ $content = str_replace( $image, $filtered_image, $content );
+ }
+ }
+
+ return $content;
+}
+
+/**
+ * Adds `loading` attribute to an `img` HTML tag.
+ *
+ * @since 5.5.0
+ *
+ * @param string $image The HTML `img` tag where the attribute should be added.
+ * @param string $context Additional context to pass to the filters.
+ * @return string Converted `img` tag with `loading` attribute added.
+ */
+function wp_img_tag_add_loading_attr( $image, $context ) {
+ /**
+ * Filters the `loading` attribute value. Default `lazy`.
+ *
+ * Returning `false` or an empty string will not add the attribute.
+ * Returning `true` will add the default value.
+ *
+ * @since 5.5.0
+ *
+ * @param string $value The `loading` attribute value, defaults to `lazy`.
+ * @param string $image The HTML `img` tag to be filtered.
+ * @param string $context Additional context about how the function was called or where the img tag is.
+ */
+ $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context );
+
+ if ( $value ) {
+ if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
+ $value = 'lazy';
+ }
+
+ $quote = null;
+
+ // Check if the img tag is valid (has `src` attribute) and get the quote character.
+ // In almost all cases it will have src and a double quote.
+ if ( false !== strpos( $image, ' src="' ) ) {
+ $quote = '"';
+ } elseif ( preg_match( '/\ssrc\s*=(["\'])/', $image, $matches ) ) {
+ $quote = $matches[1];
+ }
+
+ if ( $quote ) {
+ $loading = "loading={$quote}{$value}{$quote}";
+
+ return str_replace( '<img', "<img {$loading}", $image );
+ }
+ }
+
+ return $image;
+}
+
+/**
+ * Adds `srcset` and `sizes` attributes to an existing `img` HTML tag.
+ *
+ * @since 5.5.0
+ *
+ * @param string $image The HTML `img` tag where the attribute should be added.
+ * @param string $context Additional context to pass to the filters.
+ * @param int $attachment_id Image attachment ID.
+ * @return string Converted 'img' element with 'loading' attribute added.
+ */
+function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) {
+ /**
+ * Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`.
+ *
+ * Returning anything else than `true` will not add the attributes.
+ *
+ * @since 5.5.0
+ *
+ * @param bool $value The filtered value, defaults to `true`.
+ * @param string $image The HTML `img` tag where the attribute should be added.
+ * @param string $context Additional context about how the function was called or where the img tag is.
+ * @param int $attachment_id The image attachment ID.
+ */
+ $add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id );
+
+ if ( true === $add ) {
+ $image_meta = wp_get_attachment_metadata( $attachment_id );
+ return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
+ }
+
+ return $image;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Adds a 'wp-post-image' class to post thumbnails. Internal use only.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
</span></span></pre></div>
<a id="trunksrcwpincludespluggablephp"></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/pluggable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/pluggable.php 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/src/wp-includes/pluggable.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2606,6 +2606,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Default null.
</span><span class="cx" style="display: block; padding: 0 10px"> * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option.
</span><span class="cx" style="display: block; padding: 0 10px"> * Default false.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @type string $loading Value for the `loading` attribute.
+ * Default null.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty.
</span><span class="cx" style="display: block; padding: 0 10px"> * }
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|false `<img>` tag for the user's avatar. False on failure.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2623,9 +2625,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'alt' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'class' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'force_display' => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'loading' => null,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'extra_attr' => '',
</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">+ if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) {
+ $defaults['loading'] = 'lazy';
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $args = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2695,6 +2702,18 @@
</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">+ // Add `loading` attribute.
+ $extra_attr = $args['extra_attr'];
+ $loading = $args['loading'];
+
+ if ( in_array( $loading, array( 'lazy', 'eager' ), true ) && ! preg_match( '/\bloading\s*=/', $extra_attr ) ) {
+ if ( ! empty( $extra_attr ) ) {
+ $extra_attr .= ' ';
+ }
+
+ $extra_attr .= "loading='{$loading}'";
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $avatar = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
</span><span class="cx" style="display: block; padding: 0 10px"> esc_attr( $args['alt'] ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2703,7 +2722,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> esc_attr( join( ' ', $class ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> (int) $args['height'],
</span><span class="cx" style="display: block; padding: 0 10px"> (int) $args['width'],
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $args['extra_attr']
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $extra_attr
</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="trunktestsphpunittestsavatarphp"></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/avatar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/avatar.php 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/tests/phpunit/tests/avatar.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -169,7 +169,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_avatar() {
</span><span class="cx" style="display: block; padding: 0 10px"> $img = get_avatar( 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' />$|", $img ), 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 );
</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 test_get_avatar_size() {
</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 2020-04-07 16:12:22 UTC (rev 47553)
+++ trunk/tests/phpunit/tests/media.php 2020-04-08 00:53:18 UTC (rev 47554)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1332,7 +1332,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_wp_get_attachment_image_defaults() {
</span><span class="cx" style="display: block; padding: 0 10px"> $image = image_downsize( self::$large_id, 'thumbnail' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />', $image[1], $image[2], $image[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1347,7 +1347,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $image = image_downsize( self::$large_id, 'thumbnail' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" />', $image[1], $image[2], $image[0] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />', $image[1], $image[2], $image[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1964,7 +1964,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function test_wp_make_content_images_responsive() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $image_meta = wp_get_attachment_metadata( self::$large_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1973,6 +1973,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Function used to build HTML for the editor.
</span><span class="cx" style="display: block; padding: 0 10px"> $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = wp_img_tag_add_loading_attr( $img, 'test' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $img_no_size_in_class = str_replace( 'size-', '', $img );
</span><span class="cx" style="display: block; padding: 0 10px"> $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
</span><span class="cx" style="display: block; padding: 0 10px"> $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2014,7 +2015,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $content_unfiltered = sprintf( $content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5 );
</span><span class="cx" style="display: block; padding: 0 10px"> $content_filtered = sprintf( $content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2028,25 +2029,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 34898
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function test_wp_make_content_images_responsive_wrong() {
- $image = get_image_tag( self::$large_id, '', '', '', 'medium' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_wrong() {
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $img = wp_img_tag_add_loading_attr( $img, 'test' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Replace the src URL.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $image );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $image_wrong_src, wp_make_content_images_responsive( $image_wrong_src ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) );
</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 class="cx" style="display: block; padding: 0 10px"> * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function test_wp_make_content_images_responsive_with_preexisting_srcset() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_with_preexisting_srcset() {
</ins><span class="cx" style="display: block; padding: 0 10px"> // Generate HTML and add a dummy srcset attribute.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $image_html = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $image_html = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $img = wp_img_tag_add_loading_attr( $img, 'test' );
+ $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // The content filter should return the image unchanged.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $image_html, wp_make_content_images_responsive( $image_html ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $img, wp_filter_content_tags( $img ) );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2097,7 +2100,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 35045
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 33641
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function test_wp_make_content_images_responsive_schemes() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_wp_filter_content_tags_schemes() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $image_meta = wp_get_attachment_metadata( self::$large_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2106,6 +2109,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Build HTML for the editor.
</span><span class="cx" style="display: block; padding: 0 10px"> $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = wp_img_tag_add_loading_attr( $img, 'test' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $img_https = str_replace( 'http://', 'https://', $img );
</span><span class="cx" style="display: block; padding: 0 10px"> $img_relative = str_replace( 'http://', '//', $img );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2126,7 +2130,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual = wp_make_content_images_responsive( $unfiltered );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual = wp_filter_content_tags( $unfiltered );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2260,7 +2264,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $month = gmdate( 'm' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- ' class="attachment-testsize size-testsize" alt=""' .
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ ' class="attachment-testsize size-testsize" alt="" loading="lazy"' .
</ins><span class="cx" style="display: block; padding: 0 10px"> ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .
</span><span class="cx" style="display: block; padding: 0 10px"> ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .
</span><span class="cx" style="display: block; padding: 0 10px"> ' sizes="(max-width: 999px) 100vw, 999px" />';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2523,6 +2527,102 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, $url );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 44427
+ */
+ function test_wp_lazy_load_content_media() {
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $img_xhtml = str_replace( ' />', '/>', $img );
+ $img_html5 = str_replace( ' />', '>', $img );
+ $iframe = '<iframe src="https://www.example.com"></iframe>';
+
+ $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
+ $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' );
+ $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' );
+
+ // The following should not be modified because there already is a 'loading' attribute.
+ $img_eager = str_replace( ' />', ' loading="eager" />', $img );
+
+ $content = '
+ <p>Image, standard.</p>
+ %1$s
+ <p>Image, XHTML 1.0 style (no space before the closing slash).</p>
+ %2$s
+ <p>Image, HTML 5.0 style.</p>
+ %3$s
+ <p>Image, with pre-existing "loading" attribute.</p>
+ %5$s
+ <p>Iframe, standard. Should not be modified.</p>
+ %4$s';
+
+ $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager );
+ $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $iframe, $img_eager );
+
+ // Do not add srcset and sizes.
+ add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+
+ $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
+
+ remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+ }
+
+ /**
+ * @ticket 44427
+ */
+ function test_wp_lazy_load_content_media_opted_in() {
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
+
+ $content = '
+ <p>Image, standard.</p>
+ %1$s';
+
+ $content_unfiltered = sprintf( $content, $img );
+ $content_filtered = sprintf( $content, $lazy_img );
+
+ // Do not add srcset and sizes while testing.
+ add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+
+ // Enable globally for all tags.
+ add_filter( 'wp_lazy_loading_enabled', '__return_true' );
+
+ $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
+ remove_filter( 'wp_lazy_loading_enabled', '__return_true' );
+ remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+ }
+
+ /**
+ * @ticket 44427
+ */
+ function test_wp_lazy_load_content_media_opted_out() {
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+
+ $content = '
+ <p>Image, standard.</p>
+ %1$s';
+ $content = sprintf( $content, $img );
+
+ // Do not add srcset and sizes while testing.
+ add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+
+ // Disable globally for all tags.
+ add_filter( 'wp_lazy_loading_enabled', '__return_false' );
+
+ $this->assertSame( $content, wp_filter_content_tags( $content ) );
+ remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
+ remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
+ }
+
+ /**
+ * @ticket 44427
+ */
+ function test_wp_img_tag_add_loading_attr_single_quote() {
+ $img = "<img src='example.png' alt='' width='300' height='225' />";
+ $img = wp_img_tag_add_loading_attr( $img, 'test' );
+
+ $this->assertContains( " loading='lazy'", $img );
+ }
</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>
</div>
</body>
</html>