Make WordPress Core

Changeset 53481


Ignore:
Timestamp:
06/09/2022 04:19:39 PM (2 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r53480 r53481  
    10041004 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
    10051005 * @since 5.5.0 The `$loading` attribute was added.
     1006 * @since 6.1.0 The `$decoding` attribute was added.
    10061007 *
    10071008 * @param int          $attachment_id Image attachment ID.
     
    19441945
    19451946/**
    1946  * Add `decoding` attribute to an `img` HTML tag.
     1947 * Adds `decoding` attribute to an `img` HTML tag.
    19471948 *
    19481949 * The `decoding` attribute allows developers to indicate whether the
     
    19691970     * @since 6.1.0
    19701971     *
    1971      * @param string|false|null $value   The `decoding` attribute value. Returning a falsey value will result in
    1972      *                                   the attribute being omitted for the image. Otherwise, it may be:
    1973      *                                   'async' (default), 'sync', or 'auto'.
     1972     * @param string|false|null $value   The `decoding` attribute value. Returning a falsey value
     1973     *                                   will result in the attribute being omitted for the image.
     1974     *                                   Otherwise, it may be: 'async' (default), 'sync', or 'auto'.
    19741975     * @param string            $image   The HTML `img` tag to be filtered.
    1975      * @param string            $context Additional context about how the function was called or where the img tag is.
     1976     * @param string            $context Additional context about how the function was called
     1977     *                                   or where the img tag is.
    19761978     */
    19771979    $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );
     1980
    19781981    if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
    19791982        $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
  • trunk/tests/phpunit/tests/media.php

    r53480 r53481  
    25312531
    25322532        $html     = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
    2533         $expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
     2533        $expected = sprintf(
     2534            $html,
     2535            $attachment[0],
     2536            $attachment[1],
     2537            $attachment[2],
     2538            $align,
     2539            $id
     2540        );
    25342541
    25352542        $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
     
    25572564        $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
    25582565
    2559         $expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
     2566        $expected = sprintf(
     2567            $html,
     2568            $url,
     2569            'attachment wp-att-' . $id,
     2570            $attachment[0],
     2571            $alt,
     2572            $attachment[1],
     2573            $attachment[2],
     2574            $align,
     2575            $size,
     2576            $id,
     2577            $caption
     2578        );
    25602579
    25612580        $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
     
    25802599        $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>';
    25812600
    2582         $expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
     2601        $expected = sprintf(
     2602            $html,
     2603            $url,
     2604            $attachment[0],
     2605            $alt,
     2606            $attachment[1],
     2607            $attachment[2],
     2608            $align,
     2609            $size,
     2610            $id
     2611        );
    25832612
    25842613        $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
     
    29142943            %4$s';
    29152944
    2916         $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
    2917         $content_filtered   = wp_img_tag_add_decoding_attr( sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height ), 'the_content' );
     2945        $content_unfiltered = sprintf(
     2946            $content,
     2947            $img,
     2948            $img_no_width_height,
     2949            $img_no_width,
     2950            $img_no_height
     2951        );
     2952
     2953        $content_filtered = sprintf(
     2954            $content,
     2955            $img,
     2956            $respimg_no_width_height,
     2957            $img_no_width,
     2958            $img_no_height
     2959        );
     2960        $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );
    29182961
    29192962        // Do not add loading, srcset, and sizes.
     
    29723015            %8$s';
    29733016
    2974         $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
    2975         $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' );
     3017        $content_unfiltered = sprintf(
     3018            $content,
     3019            $img,
     3020            $img_xhtml,
     3021            $img_html5,
     3022            $img_eager,
     3023            $img_no_width_height,
     3024            $iframe,
     3025            $iframe_eager,
     3026            $iframe_no_width_height
     3027        );
     3028
     3029        $content_filtered = sprintf(
     3030            $content,
     3031            $lazy_img,
     3032            $lazy_img_xhtml,
     3033            $lazy_img_html5,
     3034            $img_eager,
     3035            $img_no_width_height,
     3036            $lazy_iframe,
     3037            $iframe_eager,
     3038            $iframe_no_width_height
     3039        );
     3040        $content_filtered = wp_img_tag_add_decoding_attr( $content_filtered, 'the_content' );
    29763041
    29773042        // Do not add width, height, srcset, and sizes.
     
    29923057        $img         = get_image_tag( self::$large_id, '', '', '', 'medium' );
    29933058        $lazy_img    = wp_img_tag_add_loading_attr( $img, 'test' );
     3059        $lazy_img    = wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' );
    29943060        $iframe      = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
    29953061        $lazy_iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
     
    30023068
    30033069        $content_unfiltered = sprintf( $content, $img, $iframe );
    3004         $content_filtered   = sprintf( $content, wp_img_tag_add_decoding_attr( $lazy_img, 'the_content' ), $lazy_iframe, 'the_content' );
     3070        $content_filtered   = sprintf( $content, $lazy_img, $lazy_iframe );
    30053071
    30063072        // Do not add srcset and sizes while testing.
     
    30203086     */
    30213087    public function test_wp_filter_content_tags_loading_lazy_opted_out() {
    3022         $img    = wp_img_tag_add_decoding_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ), 'the_content' );
     3088        $img    = get_image_tag( self::$large_id, '', '', '', 'medium' );
     3089        $img    = wp_img_tag_add_decoding_attr( $img, 'the_content' );
    30233090        $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
    30243091
     
    34843551        // Following the threshold of 2, the first two content media elements should not be lazy-loaded.
    34853552        $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2;
    3486         $content_expected   = wp_img_tag_add_decoding_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2, 'the_content' );
     3553        $content_expected   = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;
     3554        $content_expected   = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' );
    34873555
    34883556        $wp_query     = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
Note: See TracChangeset for help on using the changeset viewer.