Make WordPress Core

Ticket #53232: 53232.8.diff

File 53232.8.diff, 16.8 KB (added by adamsilverstein, 3 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 2b46167afc..32dca8ef1e 100644
    function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f 
    10771077                        }
    10781078                }
    10791079
     1080                // Add a `decoding` attribute.
     1081                $attr['decoding'] = 'async';
     1082
    10801083                /**
    10811084                 * Filters the list of attachment image attributes.
    10821085                 *
    function wp_filter_content_tags( $content, $context = null ) { 
    18541857                         */
    18551858                        $filtered_image = apply_filters( 'wp_content_img_tag', $filtered_image, $context, $attachment_id );
    18561859
     1860                        // Add 'decoding=async' attribute unless a 'decoding' attribute is already present.
     1861                        if ( false === strpos( $filtered_image, ' decoding=' ) ) {
     1862                                $filtered_image = wp_img_tag_add_decoding_async_attr( $filtered_image, $context );
     1863                        }
     1864
    18571865                        if ( $filtered_image !== $match[0] ) {
    18581866                                $content = str_replace( $match[0], $filtered_image, $content );
    18591867                        }
    function wp_img_tag_add_loading_attr( $image, $context ) { 
    19341942        return $image;
    19351943}
    19361944
     1945/**
     1946 * Add `decoding` attribute to an `img` HTML tag.
     1947 *
     1948 * The `decoding` attribute allows developers to indicate whether the
     1949 * browser can decode the image off the main thread (`async`), on the
     1950 * main thread (`sync`) or as determined by the browser (`auto`).
     1951 *
     1952 * By default WordPress adds `decoding="async"` to images but developers
     1953 * can use the {@see 'wp_img_tag_add_decoding_attr'} filter to modify this
     1954 * to remove the attribute or set it to another accepted value.
     1955 *
     1956 * @since 6.0.0
     1957 *
     1958 * @param string $image   The HTML `img` tag where the attribute should be added.
     1959 * @param string $context Additional context to pass to the filters.
     1960 * @return string Converted `img` tag with `decoding` attribute added.
     1961 */
     1962function wp_img_tag_add_decoding_async_attr( $image, $context ) {
     1963        /**
     1964         * Filters the `decoding` attribute value to add to an image. Default `async`.
     1965         *
     1966         * Returning `false` or an empty string will not add the attribute.
     1967         *
     1968         * @since 6.0.0
     1969         *
     1970         * @param string|bool $value The `decoding` attribute value. Returning a falsey value will result in
     1971         *                           the attribute being omitted for the image. Otherwise, it may be:
     1972         *                           'async' (default), 'sync', or 'auto'.
     1973         * @param string      $image The HTML `img` tag to be filtered.
     1974         * @param string      $context Additional context about how the function was called or where the img tag is.
     1975         */
     1976        $value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );
     1977        if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
     1978                $image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
     1979        }
     1980
     1981        return $image;
     1982}
     1983
    19371984/**
    19381985 * Adds `width` and `height` attributes to an `img` HTML tag.
    19391986 *
  • src/wp-includes/pluggable.php

    diff --git src/wp-includes/pluggable.php src/wp-includes/pluggable.php
    index fdbfdea250..de4ced9bd9 100644
    if ( ! function_exists( 'get_avatar' ) ) : 
    26942694                        'force_display' => false,
    26952695                        'loading'       => null,
    26962696                        'extra_attr'    => '',
     2697                        'decoding'      => 'async',
    26972698                );
    26982699
    26992700                if ( wp_lazy_loading_enabled( 'img', 'get_avatar' ) ) {
    if ( ! function_exists( 'get_avatar' ) ) : 
    27812782                        $extra_attr .= "loading='{$loading}'";
    27822783                }
    27832784
     2785                if ( isset( $args['decoding'] ) && in_array( $args['decoding'], array( 'async', 'sync', 'auto' ) ) && ! preg_match( '/\bdecoding\s*=/', $extra_attr ) ) {
     2786                        if ( ! empty( $extra_attr ) ) {
     2787                                $extra_attr .= ' ';
     2788                        }
     2789                        $extra_attr .= "decoding='{$args['decoding']}'";
     2790                }
     2791
    27842792                $avatar = sprintf(
    27852793                        "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>",
    27862794                        esc_attr( $args['alt'] ),
  • tests/phpunit/tests/avatar.php

    diff --git tests/phpunit/tests/avatar.php tests/phpunit/tests/avatar.php
    index 336ab611e2..026809b4a1 100644
    class Tests_Avatar extends WP_UnitTestCase { 
    169169
    170170        public function test_get_avatar() {
    171171                $img = get_avatar( 1 );
    172                 $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy'/>$|", $img ), 1 );
     172                $this->assertSame( preg_match( "|^<img alt='[^']*' src='[^']*' srcset='[^']*' class='[^']*' height='[^']*' width='[^']*' loading='lazy' decoding='async'/>$|", $img ), 1 );
    173173        }
    174174
    175175        public function test_get_avatar_size() {
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 8f3340aaf7..8b4ebce620 100644
    EOF; 
    14741474        public function test_wp_get_attachment_image_defaults() {
    14751475                $image    = image_downsize( self::$large_id, 'thumbnail' );
    14761476                $expected = sprintf(
    1477                         '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" />',
     1477                        '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" />',
    14781478                        $image[1],
    14791479                        $image[2],
    14801480                        $image[0]
    EOF; 
    15121512
    15131513                $image    = image_downsize( self::$large_id, 'thumbnail' );
    15141514                $expected = sprintf(
    1515                         '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" />',
     1515                        '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" loading="lazy" decoding="async" />',
    15161516                        $image[1],
    15171517                        $image[2],
    15181518                        $image[0]
    EOF; 
    22482248                        $respimg_xhtml,
    22492249                        $respimg_html5
    22502250                );
     2251                $content_filtered = wp_img_tag_add_decoding_async_attr( $content_filtered, 'the_content' );
    22512252
    22522253                // Do not add width, height, and loading.
    22532254                add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    EOF; 
    22732274        public function test_wp_filter_content_tags_srcset_sizes_wrong() {
    22742275                $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
    22752276                $img = wp_img_tag_add_loading_attr( $img, 'test' );
     2277                $img = wp_img_tag_add_decoding_async_attr( $img, 'the_content' );
    22762278
    22772279                // Replace the src URL.
    22782280                $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img );
    EOF; 
    22872289                // Generate HTML and add a dummy srcset attribute.
    22882290                $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
    22892291                $img = wp_img_tag_add_loading_attr( $img, 'test' );
     2292                $img = wp_img_tag_add_decoding_async_attr( $img, 'the_content' );
    22902293                $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
    22912294
    22922295                // The content filter should return the image unchanged.
    EOF; 
    23342337         */
    23352338        public function test_wp_filter_content_tags_filter_with_identical_image_tags_custom_attributes() {
    23362339                $img     = get_image_tag( self::$large_id, '', '', '', 'large' );
    2337                 $img     = str_replace( '<img ', '<img srcset="custom" sizes="custom" loading="custom" ', $img );
     2340                $img     = str_replace( '<img ', '<img srcset="custom" sizes="custom" loading="custom" decoding="custom"', $img );
    23382341                $content = "$img\n$img";
    23392342
    23402343                add_filter(
    EOF; 
    23592362                add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    23602363                add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    23612364                add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     2365                add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
    23622366
    23632367                add_filter(
    23642368                        'wp_content_img_tag',
    EOF; 
    24612465                        $respimg_https,
    24622466                        $respimg_relative
    24632467                );
     2468                $expected = wp_img_tag_add_decoding_async_attr( $expected, 'the_content' );
    24642469
    24652470                $actual = wp_filter_content_tags( $unfiltered );
    24662471
    EOF; 
    26052610                        'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
    26062611                        'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .
    26072612                        'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' .
    2608                         'sizes="(max-width: 999px) 100vw, 999px" />';
     2613                        'sizes="(max-width: 999px) 100vw, 999px" decoding="async" />';
    26092614
    26102615                $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
    26112616
    EOF; 
    29102915                        %4$s';
    29112916
    29122917                $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
    2913                 $content_filtered   = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height );
     2918                $content_filtered   = wp_img_tag_add_decoding_async_attr( sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height ), 'the_content' );
    29142919
    29152920                // Do not add loading, srcset, and sizes.
    29162921                add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    EOF; 
    29682973                        %8$s';
    29692974
    29702975                $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
    2971                 $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 );
     2976                $content_filtered   = wp_img_tag_add_decoding_async_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' );
    29722977
    29732978                // Do not add width, height, srcset, and sizes.
    29742979                add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    EOF; 
    29973002                        %2$s';
    29983003
    29993004                $content_unfiltered = sprintf( $content, $img, $iframe );
    3000                 $content_filtered   = sprintf( $content, $lazy_img, $lazy_iframe );
     3005                $content_filtered   = sprintf( $content, wp_img_tag_add_decoding_async_attr( $lazy_img ), $lazy_iframe, 'the_content' );
    30013006
    30023007                // Do not add srcset and sizes while testing.
    30033008                add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
    EOF; 
    30153020         * @ticket 50756
    30163021         */
    30173022        public function test_wp_filter_content_tags_loading_lazy_opted_out() {
    3018                 $img    = get_image_tag( self::$large_id, '', '', '', 'medium' );
     3023                $img    = wp_img_tag_add_decoding_async_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ), 'the_content' );
    30193024                $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
    30203025
    30213026                $content = '
    EOF; 
    34793484
    34803485                // Following the threshold of 2, the first two content media elements should not be lazy-loaded.
    34813486                $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2;
    3482                 $content_expected   = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;
     3487                $content_expected   = wp_img_tag_add_decoding_async_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2, 'the_content' );
    34833488
    34843489                $wp_query     = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
    34853490                $wp_the_query = $wp_query;
  • tests/phpunit/tests/media/getAdjacentImageLink.php

    diff --git tests/phpunit/tests/media/getAdjacentImageLink.php tests/phpunit/tests/media/getAdjacentImageLink.php
    index 735c0ff15c..1c4133d6e7 100644
    class Tests_Media_GetAdjacentImageLink extends WP_Test_Adjacent_Image_Link_TestC 
    3232                        'when has previous link'           => array(
    3333                                'current_attachment_index'  => 3,
    3434                                'expected_attachment_index' => 2,
    35                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     35                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    3636                        ),
    3737                        'with text when has previous link' => array(
    3838                                'current_attachment_index'  => 3,
    class Tests_Media_GetAdjacentImageLink extends WP_Test_Adjacent_Image_Link_TestC 
    4343                        'when has next link'               => array(
    4444                                'current_attachment_index'  => 4,
    4545                                'expected_attachment_index' => 5,
    46                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     46                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    4747                                'args'                      => array( 'prev' => false ),
    4848                        ),
    4949                        'with text when has next link'     => array(
  • tests/phpunit/tests/media/getNextImageLink.php

    diff --git tests/phpunit/tests/media/getNextImageLink.php tests/phpunit/tests/media/getNextImageLink.php
    index 86a843cbfc..88ee51a434 100644
    class Tests_Media_GetNextImageLink extends WP_Test_Adjacent_Image_Link_TestCase 
    3131                        'when has next link'           => array(
    3232                                'current_attachment_index'  => 4,
    3333                                'expected_attachment_index' => 5,
    34                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     34                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    3535                        ),
    3636                        'with text when has next link' => array(
    3737                                'current_attachment_index'  => 4,
  • tests/phpunit/tests/media/getPreviousImageLink.php

    diff --git tests/phpunit/tests/media/getPreviousImageLink.php tests/phpunit/tests/media/getPreviousImageLink.php
    index 2d2d511e4f..d0eba54295 100644
    class Tests_Media_GetPreviousImageLink extends WP_Test_Adjacent_Image_Link_TestC 
    3131                        'when has previous link'           => array(
    3232                                'current_attachment_index'  => 3,
    3333                                'expected_attachment_index' => 2,
    34                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     34                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    3535                        ),
    3636                        'with text when has previous link' => array(
    3737                                'current_attachment_index'  => 3,
  • tests/phpunit/tests/media/nextImageLink.php

    diff --git tests/phpunit/tests/media/nextImageLink.php tests/phpunit/tests/media/nextImageLink.php
    index 7799779fa8..f6da4605ca 100644
    class Tests_Media_NextImageLink extends WP_Test_Adjacent_Image_Link_TestCase { 
    3030                        'when has next link'           => array(
    3131                                'current_attachment_index'  => 4,
    3232                                'expected_attachment_index' => 5,
    33                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     33                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image5.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    3434                        ),
    3535                        'with text when has next link' => array(
    3636                                'current_attachment_index'  => 4,
  • tests/phpunit/tests/media/previousImageLink.php

    diff --git tests/phpunit/tests/media/previousImageLink.php tests/phpunit/tests/media/previousImageLink.php
    index 11d6583d6a..de422e0379 100644
    class Tests_Media_PreviousImageLink extends WP_Test_Adjacent_Image_Link_TestCase 
    3030                        'when has previous link'           => array(
    3131                                'current_attachment_index'  => 3,
    3232                                'expected_attachment_index' => 2,
    33                                 'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="http://example.org/wp-content/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" /></a>',
     33                                'expected'                  => '<a href=\'http://example.org/?attachment_id=%%ID%%\'><img width="1" height="1" src="' . WP_CONTENT_URL . '/uploads/image2.jpg" class="attachment-thumbnail size-thumbnail" alt="" loading="lazy" decoding="async" /></a>',
    3434                        ),
    3535                        'with text when has previous link' => array(
    3636                                'current_attachment_index'  => 3,