Make WordPress Core


Ignore:
Timestamp:
09/03/2024 05:05:33 PM (3 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.