<!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>[49808] trunk: Media: Enable lazy-loading of iframes by adding the `loading="lazy"` attribute to iframe 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/49808">49808</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/49808","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>2020-12-16 21:17:24 +0000 (Wed, 16 Dec 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 iframes by adding the `loading="lazy"` attribute to iframe tags on the front-end.
* Expands the capabilities of `wp_filter_content_tags()` to add the attribute to iframe tags if enabled.
* Modifies the default behavior of `wp_lazy_loading_enabled()` so that it returns `true` for `iframe` tags.
* Introduces a `wp_iframe_tag_add_loading_attr()` function.
* Introduces a `wp_iframe_tag_add_loading_attr` filter.
Like for images, the attribute is only added to iframes which have both `width` and `height` specified (see related <a href="https://core.trac.wordpress.org/ticket/50367">#50367</a>).
Props azaozz, flixos90, westonruter.
Fixes <a href="https://core.trac.wordpress.org/ticket/50756">#50756</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<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="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-12-16 15:23:35 UTC (rev 49807)
+++ trunk/src/wp-includes/media.php 2020-12-16 21:17:24 UTC (rev 49808)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1703,6 +1703,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Determines whether to add the `loading` attribute to the specified tag in the specified context.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 Now returns `true` by default for `iframe` tags.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag_name The tag name.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $context Additional context, like the current filter name
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1710,9 +1711,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool Whether to add the attribute.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_lazy_loading_enabled( $tag_name, $context ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // By default add to all 'img' tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // By default add to all 'img' and 'iframe' tags.
</ins><span class="cx" style="display: block; padding: 0 10px"> // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $default = ( 'img' === $tag_name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading
+ $default = ( 'img' === $tag_name || 'iframe' === $tag_name );
</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"> * Filters whether to add the `loading` attribute to the specified tag in the specified context.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1732,14 +1734,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Modifies HTML tags in post content to include new browser and HTML technologies
</span><span class="cx" style="display: block; padding: 0 10px"> * that may not have existed at the time of post creation. These modifications currently
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well
+ * as adding `loading` attributes to `iframe` HTML tags.
</ins><span class="cx" style="display: block; padding: 0 10px"> * Future similar optimizations should be added/expected here.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see wp_img_tag_add_width_and_height_attr()
</span><span class="cx" style="display: block; padding: 0 10px"> * @see wp_img_tag_add_srcset_and_sizes_attr()
</span><span class="cx" style="display: block; padding: 0 10px"> * @see wp_img_tag_add_loading_attr()
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @see wp_iframe_tag_add_loading_attr()
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $content The HTML content to be filtered.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $context Optional. Additional context to pass to the filters.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1751,32 +1756,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $context = current_filter();
</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">- $add_loading_attr = wp_lazy_loading_enabled( 'img', $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context );
+ $add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context );
</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 ( false === strpos( $content, '<img' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $content;
</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">- if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) {
- return $content;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // List of the unique `img` tags found in $content.
</span><span class="cx" style="display: block; padding: 0 10px"> $images = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach ( $matches[0] as $image ) {
- if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
- $attachment_id = absint( $class_id[1] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // List of the unique `iframe` tags found in $content.
+ $iframes = array();
</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 ( $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;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $matches as $match ) {
+ list( $tag, $tag_name ) = $match;
+
+ switch ( $tag_name ) {
+ case 'img':
+ if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $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[ $tag ] = $attachment_id;
+ break;
+ }
+ }
+ $images[ $tag ] = 0;
+ break;
+ case 'iframe':
+ $iframes[ $tag ] = 0;
+ break;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- $images[ $image ] = 0;
</del><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"> // Reduce the array to unique attachment IDs.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1804,7 +1817,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"> // Add 'loading' attribute if applicable.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
</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">@@ -1813,6 +1826,19 @@
</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">+ foreach ( $iframes as $iframe => $attachment_id ) {
+ $filtered_iframe = $iframe;
+
+ // Add 'loading' attribute if applicable.
+ if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) {
+ $filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context );
+ }
+
+ if ( $filtered_iframe !== $iframe ) {
+ $content = str_replace( $iframe, $filtered_iframe, $content );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $content;
</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">@@ -1827,7 +1853,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_img_tag_add_loading_attr( $image, $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">- * Filters the `loading` attribute value. Default `lazy`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters the `loading` attribute value to add to an image. Default `lazy`.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Returning `false` or an empty string will not add the attribute.
</span><span class="cx" style="display: block; padding: 0 10px"> * Returning `true` will add the default value.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1937,6 +1963,47 @@
</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">+ * Adds `loading` attribute to an `iframe` HTML tag.
+ *
+ * @since 5.7.0
+ *
+ * @param string $iframe The HTML `iframe` tag where the attribute should be added.
+ * @param string $context Additional context to pass to the filters.
+ * @return string Converted `iframe` tag with `loading` attribute added.
+ */
+function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
+ /**
+ * Filters the `loading` attribute value to add to an iframe. Default `lazy`.
+ *
+ * Returning `false` or an empty string will not add the attribute.
+ * Returning `true` will add the default value.
+ *
+ * @since 5.7.0
+ *
+ * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in
+ * the attribute being omitted for the iframe. Default 'lazy'.
+ * @param string $iframe The HTML `iframe` tag to be filtered.
+ * @param string $context Additional context about how the function was called or where the iframe tag is.
+ */
+ $value = apply_filters( 'wp_iframe_tag_add_loading_attr', 'lazy', $iframe, $context );
+
+ if ( $value ) {
+ if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
+ $value = 'lazy';
+ }
+
+ // Iframes should have source and dimension attributes for the `loading` attribute to be added.
+ if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
+ return $iframe;
+ }
+
+ return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe );
+ }
+
+ return $iframe;
+}
+
+/**
</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="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-12-16 15:23:35 UTC (rev 49807)
+++ trunk/tests/phpunit/tests/media.php 2020-12-16 21:17:24 UTC (rev 49808)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2736,6 +2736,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50367
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 50756
</ins><span class="cx" style="display: block; padding: 0 10px"> * @requires function imagejpeg
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_wp_filter_content_tags_loading_lazy() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2742,19 +2743,22 @@
</span><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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $img_xhtml = str_replace( ' />', '/>', $img );
- $img_html5 = str_replace( ' />', '>', $img );
- $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
- $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
- $iframe = '<iframe src="https://www.example.com"></iframe>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $img_xhtml = str_replace( ' />', '/>', $img );
+ $img_html5 = str_replace( ' />', '>', $img );
+ $img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
+ $img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
+ $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
+ $iframe_no_width_height = '<iframe src="https://www.example.com"></iframe>';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px"> $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px"> $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // The following should not be modified because there already is a 'loading' attribute.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $img_eager = str_replace( ' />', ' loading="eager" />', $img );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img_eager = str_replace( ' />', ' loading="eager" />', $img );
+ $iframe_eager = str_replace( '">', '" loading="eager">', $iframe );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $content = '
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Image, standard.</p>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2767,11 +2771,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> %4$s
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Image, without dimension attributes. Should not be modified.</p>
</span><span class="cx" style="display: block; padding: 0 10px"> %5$s
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- <p>Iframe, standard. Should not be modified.</p>
- %6$s';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ <p>Iframe, standard.</p>
+ %6$s
+ <p>Iframe, with pre-existing "loading" attribute. Should not be modified.</p>
+ %7$s
+ <p>Iframe, without dimension attributes. Should not be modified.</p>
+ %8$s';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe );
- $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
+ $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do not add width, height, srcset, and sizes.
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2785,17 +2793,22 @@
</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 44427
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 50756
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_wp_filter_content_tags_loading_lazy_opted_in() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
- $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
+ $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
+ $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $content = '
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Image, standard.</p>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- %1$s';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ %1$s
+ <p>Iframe, standard.</p>
+ %2$s';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_unfiltered = sprintf( $content, $img );
- $content_filtered = sprintf( $content, $lazy_img );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content_unfiltered = sprintf( $content, $img, $iframe );
+ $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do not add srcset and sizes while testing.
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2810,14 +2823,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"> * @ticket 44427
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 50756
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function test_wp_filter_content_tags_loading_lazy_opted_out() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $content = '
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Image, standard.</p>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- %1$s';
- $content = sprintf( $content, $img );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ %1$s
+ <p>Iframe, standard.</p>
+ %2$s';
+ $content = sprintf( $content, $img, $iframe );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do not add srcset and sizes while testing.
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2879,6 +2896,50 @@
</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">+ * @ticket 50756
+ */
+ function test_wp_iframe_tag_add_loading_attr() {
+ $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
+ $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
+
+ $this->assertContains( ' loading="lazy"', $iframe );
+ }
+
+ /**
+ * @ticket 50756
+ */
+ function test_wp_iframe_tag_add_loading_attr_without_src() {
+ $iframe = '<iframe width="640" height="360"></iframe>';
+ $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
+
+ $this->assertNotContains( ' loading=', $iframe );
+ }
+
+ /**
+ * @ticket 50756
+ */
+ function test_wp_iframe_tag_add_loading_attr_with_single_quotes() {
+ $iframe = "<iframe src='https://www.example.com' width='640' height='360'></iframe>";
+ $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
+
+ $this->assertNotContains( ' loading=', $iframe );
+
+ // Test specifically that the attribute is not there with double-quotes,
+ // to avoid regressions.
+ $this->assertNotContains( ' loading="lazy"', $iframe );
+ }
+
+ /**
+ * @ticket 50756
+ */
+ function test_wp_iframe_tag_add_loading_attr_opt_out() {
+ $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
+ add_filter( 'wp_iframe_tag_add_loading_attr', '__return_false' );
+
+ $this->assertNotContains( ' loading=', $iframe );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50425
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2918,6 +2979,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 44427
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50425
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 50756
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $tag_name Tag name.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2934,7 +2996,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function data_wp_lazy_loading_enabled_tag_name_defaults() {
</span><span class="cx" style="display: block; padding: 0 10px"> return array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'img => true' => array( 'img', true ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'iframe => false' => array( 'iframe', false ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'iframe => true' => array( 'iframe', true ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'arbitrary tag => false' => array( 'blink', false ),
</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>