Make WordPress Core

Changeset 58974


Ignore:
Timestamp:
09/03/2024 05:05:33 PM (5 weeks ago)
Author:
flixos90
Message:

Media: Consistently pass 'src' attribute to wp_get_loading_optimization_attributes().

A common use-case for the 'wp_get_loading_optimization_attributes' filter is to modify attributes based on the 'src' attribute. However, the wp_img_tag_add_loading_optimization_attrs() was not passing that attribute to the function as expected, which would make such usage of the filter unreliable. This changeset ensures the 'src' attribute is also passed in this scenario. All other calls to wp_get_loading_optimization_attributes() already included the attribute.

Props deepakrohilla, prestonwordsworth, mukesh27, adamsilverstein, joemcgill, flixos90.
Fixes #61436.
See #58893.

Location:
trunk
Files:
2 edited

Legend:

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

    r58942 r58974  
    19741974 */
    19751975function wp_img_tag_add_loading_optimization_attrs( $image, $context ) {
     1976    $src               = preg_match( '/ src=["\']?([^"\']*)/i', $image, $matche_src ) ? $matche_src[1] : null;
    19761977    $width             = preg_match( '/ width=["\']([0-9]+)["\']/', $image, $match_width ) ? (int) $match_width[1] : null;
    19771978    $height            = preg_match( '/ height=["\']([0-9]+)["\']/', $image, $match_height ) ? (int) $match_height[1] : null;
     
    19881989        'img',
    19891990        array(
     1991            'src'           => $src,
    19901992            'width'         => $width,
    19911993            'height'        => $height,
  • trunk/tests/phpunit/tests/media.php

    r58773 r58974  
    55805580
    55815581    /**
     5582     * Tests that wp_img_tag_add_loading_optimization_attrs() passes the 'src' attribute to wp_get_loading_optimization_attributes().
     5583     *
     5584     * @ticket 61436
     5585     *
     5586     * @covers ::wp_img_tag_add_loading_optimization_attrs
     5587     */
     5588    public function test_wp_img_tag_add_loading_optimization_attrs_passes_src() {
     5589        add_filter(
     5590            'wp_get_loading_optimization_attributes',
     5591            static function ( $loading_attrs, $tag_name, $attr ) {
     5592                if (
     5593                    'img' === $tag_name &&
     5594                    isset( $attr['src'] ) &&
     5595                    'https://example.org/a-specific-image.jpg' === $attr['src']
     5596                ) {
     5597                    $loading_attrs['fetchpriority'] = 'low';
     5598                    $loading_attrs['loading']       = 'eager';
     5599                }
     5600                return $loading_attrs;
     5601            },
     5602            10,
     5603            3
     5604        );
     5605
     5606        $image    = '<img src="https://example.org/a-specific-image.jpg" width="1280" height="720">';
     5607        $expected = '<img fetchpriority="low" loading="eager" decoding="async" src="https://example.org/a-specific-image.jpg" width="1280" height="720">';
     5608
     5609        // Ensure attributes are modified because image src was matched.
     5610        $this->assertSame(
     5611            $expected,
     5612            wp_img_tag_add_loading_optimization_attrs( $image, 'the_content' ),
     5613            'fetchpriority should be low when src is matched.'
     5614        );
     5615    }
     5616
     5617    /**
    55825618     * @ticket 58235
    55835619     *
Note: See TracChangeset for help on using the changeset viewer.