<!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>[53481] trunk: Media: Some documentation and test improvements for `wp_img_tag_add_decoding_attr()`:</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/53481">53481</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/53481","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-06-09 16:19:39 +0000 (Thu, 09 Jun 2022)</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: Some documentation and test improvements for `wp_img_tag_add_decoding_attr()`:
* Add a `@since` note for the `decoding` attribute in `wp_get_attachment_image()`.
* Adjust `wp_img_tag_add_decoding_attr()` DocBlocks per the documentation standards.
* Wrap some long `sprintf()` calls in unit tests for better readability. In at least one case, `the_content` was unnecessarily passed to `sprintf()` as an extra (unused) parameter.
Follow-up to <a href="https://core.trac.wordpress.org/changeset/53480">[53480]</a>.
See <a href="https://core.trac.wordpress.org/ticket/53232">#53232</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 2022-06-09 05:29:57 UTC (rev 53480)
+++ trunk/src/wp-includes/media.php 2022-06-09 16:19:39 UTC (rev 53481)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1003,6 +1003,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0 The `$loading` attribute was added.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.1.0 The `$decoding` attribute was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $attachment_id Image attachment ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1943,7 +1944,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"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Add `decoding` attribute to an `img` HTML tag.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Adds `decoding` attribute to an `img` HTML tag.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * The `decoding` attribute allows developers to indicate whether the
</span><span class="cx" style="display: block; padding: 0 10px"> * browser can decode the image off the main thread (`async`), on the
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1968,13 +1969,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.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|false|null $value The `decoding` attribute value. Returning a falsey value will result in
- * the attribute being omitted for the image. Otherwise, it may be:
- * 'async' (default), 'sync', or 'auto'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string|false|null $value The `decoding` attribute value. Returning a falsey value
+ * will result in the attribute being omitted for the image.
+ * Otherwise, it may be: 'async' (default), 'sync', or 'auto'.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param string $image The HTML `img` tag to be filtered.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $context Additional context about how the function was called or where the img tag is.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $context Additional context about how the function was called
+ * or where the img tag is.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );
</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 ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
</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 2022-06-09 05:29:57 UTC (rev 53480)
+++ trunk/tests/phpunit/tests/media.php 2022-06-09 16:19:39 UTC (rev 53481)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2530,7 +2530,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $attachment = wp_get_attachment_image_src( $id, 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = sprintf(
+ $html,
+ $attachment[0],
+ $attachment[1],
+ $attachment[2],
+ $align,
+ $id
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2556,7 +2563,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $html = '<a href="%1$s" rel="%2$s"><img src="%3$s" alt="%4$s" width="%5$d" height="%6$d" class="size-%8$s wp-image-%9$d" /></a>';
</span><span class="cx" style="display: block; padding: 0 10px"> $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = sprintf(
+ $html,
+ $url,
+ 'attachment wp-att-' . $id,
+ $attachment[0],
+ $alt,
+ $attachment[1],
+ $attachment[2],
+ $align,
+ $size,
+ $id,
+ $caption
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2579,7 +2598,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $html = '<a href="%1$s"><img src="%2$s" alt="%3$s" width="%4$d" height="%5$d" class="align%6$s size-%7$s wp-image-%8$d" /></a>';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $expected = sprintf(
+ $html,
+ $url,
+ $attachment[0],
+ $alt,
+ $attachment[1],
+ $attachment[2],
+ $align,
+ $size,
+ $id
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2913,9 +2942,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Image, no height but width attribute. Should NOT be modified.</p>
</span><span class="cx" style="display: block; padding: 0 10px"> %4$s';
</span><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_no_width_height, $img_no_width, $img_no_height );
- $content_filtered = wp_img_tag_add_decoding_attr( sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height ), 'the_content' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content_unfiltered = sprintf(
+ $content,
+ $img,
+ $img_no_width_height,
+ $img_no_width,
+ $img_no_height
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content_filtered = sprintf(
+ $content,
+ $img,
+ $respimg_no_width_height,
+ $img_no_width,
+ $img_no_height
+ );
+ $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Do not add loading, srcset, and sizes.
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
</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">@@ -2971,9 +3014,31 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <p>Iframe, without dimension attributes. Should not be modified.</p>
</span><span class="cx" style="display: block; padding: 0 10px"> %8$s';
</span><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, $iframe_eager, $iframe_no_width_height );
- $content_filtered = wp_img_tag_add_decoding_attr( sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $lazy_iframe, $iframe_eager, $iframe_no_width_height ), 'the_content' );
</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
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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
+ );
+ $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );
+
</ins><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="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">@@ -2991,6 +3056,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_wp_filter_content_tags_loading_lazy_opted_in() {
</span><span class="cx" style="display: block; padding: 0 10px"> $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
</span><span class="cx" style="display: block; padding: 0 10px"> $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $lazy_img = wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
</span><span class="cx" style="display: block; padding: 0 10px"> $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3001,7 +3067,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> %2$s';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $content_unfiltered = sprintf( $content, $img, $iframe );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_filtered = sprintf( $content, wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' ), $lazy_iframe, 'the_content' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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">@@ -3019,7 +3085,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50756
</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_loading_lazy_opted_out() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $img = wp_img_tag_add_decoding_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ), 'the_content' );
</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_decoding_attr( $img, 'the_content' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $content = '
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3483,7 +3550,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Following the threshold of 2, the first two content media elements should not be lazy-loaded.
</span><span class="cx" style="display: block; padding: 0 10px"> $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $content_expected = wp_img_tag_add_decoding_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2, 'the_content' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $content_expected = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;
+ $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_the_query = $wp_query;
</span></span></pre>
</div>
</div>
</body>
</html>