From f52d476bc6dcfe4c5d011f335361500f80708620 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 9 Jun 2022 16:19:39 +0000 Subject: [PATCH] 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 [53480]. See #53232. git-svn-id: https://develop.svn.wordpress.org/trunk@53481 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 13 ++++-- tests/phpunit/tests/media.php | 88 +++++++++++++++++++++++++++++++---- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b3e73ed944..a383c5b282 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1003,6 +1003,7 @@ function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon * @since 2.5.0 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. * @since 5.5.0 The `$loading` attribute was added. + * @since 6.1.0 The `$decoding` attribute was added. * * @param int $attachment_id Image attachment ID. * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array @@ -1943,7 +1944,7 @@ function wp_img_tag_add_loading_attr( $image, $context ) { } /** - * Add `decoding` attribute to an `img` HTML tag. + * Adds `decoding` attribute to an `img` HTML tag. * * The `decoding` attribute allows developers to indicate whether the * browser can decode the image off the main thread (`async`), on the @@ -1968,13 +1969,15 @@ function wp_img_tag_add_decoding_attr( $image, $context ) { * * @since 6.1.0 * - * @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'. + * @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'. * @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. + * @param string $context Additional context about how the function was called + * or where the img tag is. */ $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context ); + if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) { $image = str_replace( 'assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) ); @@ -2556,7 +2563,19 @@ EOF; $html = '%4$s'; $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]'; - $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption ); + $expected = sprintf( + $html, + $url, + 'attachment wp-att-' . $id, + $attachment[0], + $alt, + $attachment[1], + $attachment[2], + $align, + $size, + $id, + $caption + ); $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) ); } @@ -2579,7 +2598,17 @@ EOF; $html = '%3$s'; - $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id ); + $expected = sprintf( + $html, + $url, + $attachment[0], + $alt, + $attachment[1], + $attachment[2], + $align, + $size, + $id + ); $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) ); } @@ -2913,8 +2942,22 @@ EOF;

Image, no height but width attribute. Should NOT be modified.

%4$s'; - $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' ); + $content_unfiltered = sprintf( + $content, + $img, + $img_no_width_height, + $img_no_width, + $img_no_height + ); + + $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' ); // Do not add loading, srcset, and sizes. add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); @@ -2971,8 +3014,30 @@ EOF;

Iframe, without dimension attributes. Should not be modified.

%8$s'; - $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' ); + $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 + ); + $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' ); // Do not add width, height, srcset, and sizes. add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); @@ -2991,6 +3056,7 @@ EOF; public function test_wp_filter_content_tags_loading_lazy_opted_in() { $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); + $lazy_img = wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' ); $iframe = ''; $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' ); @@ -3001,7 +3067,7 @@ EOF; %2$s'; $content_unfiltered = sprintf( $content, $img, $iframe ); - $content_filtered = sprintf( $content, wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' ), $lazy_iframe, 'the_content' ); + $content_filtered = sprintf( $content, $lazy_img, $lazy_iframe ); // Do not add srcset and sizes while testing. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); @@ -3019,7 +3085,8 @@ EOF; * @ticket 50756 */ public function test_wp_filter_content_tags_loading_lazy_opted_out() { - $img = wp_img_tag_add_decoding_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ), 'the_content' ); + $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); + $img = wp_img_tag_add_decoding_attr( $img, 'the_content' ); $iframe = ''; $content = ' @@ -3483,7 +3550,8 @@ EOF; // Following the threshold of 2, the first two content media elements should not be lazy-loaded. $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2; - $content_expected = wp_img_tag_add_decoding_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2, 'the_content' ); + $content_expected = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2; + $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' ); $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); $wp_the_query = $wp_query;