Make WordPress Core

Ticket #53232: 53232.diff

File 53232.diff, 13.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 b3204c2799..60a0d046f5 100644
    function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f 
    10771077                        }
    10781078                }
    10791079
     1080                $attr['decoding'] = 'async';
     1081
    10801082                /**
    10811083                 * Filters the list of attachment image attributes.
    10821084                 *
    function wp_filter_content_tags( $content, $context = null ) { 
    18431845                                $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context );
    18441846                        }
    18451847
     1848                        // Add 'decoding=async' attribute unless a 'decoding' attribute is already present.
     1849                        if ( false === strpos( $filtered_image, ' decoding=' ) ) {
     1850                                $filtered_image = wp_img_tag_add_decoding_async_attr( $filtered_image );
     1851                        }
     1852
    18461853                        if ( $filtered_image !== $match[0] ) {
    18471854                                $content = str_replace( $match[0], $filtered_image, $content );
    18481855                        }
    function wp_img_tag_add_loading_attr( $image, $context ) { 
    19111918        return $image;
    19121919}
    19131920
     1921/**
     1922 * Adds `decoding=async` attribute to an `img` HTML tag.
     1923 *
     1924 * @since 6.0.0
     1925 *
     1926 * @param string $image The HTML `img` tag where the attribute should be added.
     1927 * @return string Converted `img` tag with `decoding=async` attribute added.
     1928 */
     1929function wp_img_tag_add_decoding_async_attr( $image ) {
     1930        return str_replace( '<img ', '<img decoding="async" ', $image );
     1931}
     1932
    19141933/**
    19151934 * Adds `width` and `height` attributes to an `img` HTML tag.
    19161935 *
  • 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/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 5d92d9718b..4f6f4db36b 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 );
    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 );
    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 );
    22902293                $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
    22912294
    22922295                // The content filter should return the image unchanged.
    EOF; 
    23832386                        $respimg_https,
    23842387                        $respimg_relative
    23852388                );
     2389                $expected = wp_img_tag_add_decoding_async_attr( $expected );
    23862390
    23872391                $actual = wp_filter_content_tags( $unfiltered );
    23882392
    EOF; 
    25272531                        'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
    25282532                        'class="attachment-testsize size-testsize" alt="" loading="lazy" ' .
    25292533                        'srcset="' . $uploads_url . 'test-image-testsize-999x999.jpg 999w, ' . $uploads_url . $basename . '-150x150.jpg 150w" ' .
    2530                         'sizes="(max-width: 999px) 100vw, 999px" />';
     2534                        'sizes="(max-width: 999px) 100vw, 999px" decoding="async" />';
    25312535
    25322536                $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
    25332537
    EOF; 
    28322836                        %4$s';
    28332837
    28342838                $content_unfiltered = sprintf( $content, $img, $img_no_width_height, $img_no_width, $img_no_height );
    2835                 $content_filtered   = sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height );
     2839                $content_filtered   = wp_img_tag_add_decoding_async_attr( sprintf( $content, $img, $respimg_no_width_height, $img_no_width, $img_no_height ) );
    28362840
    28372841                // Do not add loading, srcset, and sizes.
    28382842                add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    EOF; 
    28902894                        %8$s';
    28912895
    28922896                $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe, $iframe_eager, $iframe_no_width_height );
    2893                 $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 );
     2897                $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 ) );
    28942898
    28952899                // Do not add width, height, srcset, and sizes.
    28962900                add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    EOF; 
    29192923                        %2$s';
    29202924
    29212925                $content_unfiltered = sprintf( $content, $img, $iframe );
    2922                 $content_filtered   = sprintf( $content, $lazy_img, $lazy_iframe );
     2926                $content_filtered   = sprintf( $content, wp_img_tag_add_decoding_async_attr( $lazy_img ), $lazy_iframe );
    29232927
    29242928                // Do not add srcset and sizes while testing.
    29252929                add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
    EOF; 
    29372941         * @ticket 50756
    29382942         */
    29392943        public function test_wp_filter_content_tags_loading_lazy_opted_out() {
    2940                 $img    = get_image_tag( self::$large_id, '', '', '', 'medium' );
     2944                $img    = wp_img_tag_add_decoding_async_attr( get_image_tag( self::$large_id, '', '', '', 'medium' ) );
    29412945                $iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
    29422946
    29432947                $content = '
    EOF; 
    34013405
    34023406                // Following the threshold of 2, the first two content media elements should not be lazy-loaded.
    34033407                $content_unfiltered = $img1 . $iframe1 . $img2 . $img3 . $iframe2;
    3404                 $content_expected   = $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2;
     3408                $content_expected   = wp_img_tag_add_decoding_async_attr( $img1 . $iframe1 . $lazy_img2 . $lazy_img3 . $lazy_iframe2 );
    34053409
    34063410                $wp_query     = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) );
    34073411                $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,