Make WordPress Core

Changeset 56142


Ignore:
Timestamp:
07/05/2023 07:28:02 PM (15 months ago)
Author:
flixos90
Message:

Media: Ensure custom header image tag supports loading optimization attributes.

This changeset is a follow up to [56037] and ensures that the get_header_image_tag() function receives the benefits of wp_get_loading_optimization_attributes() as well. Prior to fetchpriority support, this was not needed since the header image should never be lazy-loaded, but the image certainly is a fetchpriority candidate, so therefore it is crucial to have it supported.

Props felipeelia, spacedmonkey, mukesh27, westonruter, flixos90.
Fixes #58680.

Location:
trunk
Files:
3 edited

Legend:

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

    r56122 r56142  
    56445644    }
    56455645
     5646    // The custom header image is always expected to be in the header.
     5647    if ( 'get_header_image_tag' === $context ) {
     5648        return $postprocess( $loading_attrs, true );
     5649    }
     5650
    56465651    // Special handling for programmatically created image tags.
    56475652    if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) {
  • trunk/src/wp-includes/theme.php

    r55977 r56142  
    12381238        $attr,
    12391239        array(
    1240             'src'    => $header->url,
    1241             'width'  => $width,
    1242             'height' => $height,
    1243             'alt'    => $alt,
     1240            'src'      => $header->url,
     1241            'width'    => $width,
     1242            'height'   => $height,
     1243            'alt'      => $alt,
     1244            'decoding' => 'async',
    12441245        )
    12451246    );
     
    12641265            }
    12651266        }
     1267    }
     1268
     1269    $attr = array_merge(
     1270        $attr,
     1271        wp_get_loading_optimization_attributes( 'img', $attr, 'get_header_image_tag' )
     1272    );
     1273
     1274    /*
     1275     * If the default value of `lazy` for the `loading` attribute is overridden
     1276     * to omit the attribute for this image, ensure it is not included.
     1277     */
     1278    if ( isset( $attr['loading'] ) && ! $attr['loading'] ) {
     1279        unset( $attr['loading'] );
     1280    }
     1281
     1282    // If the `fetchpriority` attribute is overridden and set to false or an empty string.
     1283    if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) {
     1284        unset( $attr['fetchpriority'] );
     1285    }
     1286
     1287    // If the `decoding` attribute is overridden and set to false or an empty string.
     1288    if ( isset( $attr['decoding'] ) && ! $attr['decoding'] ) {
     1289        unset( $attr['decoding'] );
    12661290    }
    12671291
  • trunk/tests/phpunit/tests/theme/customHeader.php

    r53948 r56142  
    175175    }
    176176
     177    /**
     178     * Tests default values of performance attributes for "get_header_image_tag".
     179     *
     180     * @ticket 58680
     181     */
     182    public function test_get_header_image_tag_with_default_performance_attributes() {
     183        $this->add_theme_support(
     184            array(
     185                'default-image' => 'http://localhost/default-header.jpg',
     186                'width'         => 60,
     187                'height'        => 60,
     188            )
     189        );
     190
     191        add_filter(
     192            'wp_min_priority_img_pixels',
     193            static function() {
     194                return 2500; // 50*50=2500
     195            }
     196        );
     197
     198        wp_high_priority_element_flag( true );
     199
     200        $html = get_header_image_tag();
     201        $this->assertStringNotContainsString( ' loading="lazy"', $html );
     202        $this->assertStringContainsString( ' fetchpriority="high"', $html );
     203        $this->assertStringContainsString( ' decoding="async"', $html );
     204    }
     205
     206    /**
     207     * Tests custom values of performance attributes for "get_header_image_tag".
     208     *
     209     * @ticket 58680
     210     */
     211    public function test_get_header_image_tag_with_custom_performance_attributes() {
     212        $this->add_theme_support(
     213            array(
     214                'default-image' => 'http://localhost/default-header.jpg',
     215                'width'         => 500,
     216                'height'        => 500,
     217            )
     218        );
     219
     220        $html = get_header_image_tag(
     221            array(
     222                'fetchpriority' => '',
     223                'decoding'      => '',
     224            )
     225        );
     226        $this->assertStringNotContainsString( ' fetchpriority="high"', $html );
     227        $this->assertStringNotContainsString( ' decoding="async"', $html );
     228    }
     229
     230    /**
     231     * Tests custom lazy loading for "get_header_image_tag".
     232     *
     233     * @ticket 58680
     234     */
     235    public function test_get_header_image_tag_with_custom_lazy_loading() {
     236        $this->add_theme_support(
     237            array(
     238                'default-image' => 'http://localhost/default-header.jpg',
     239                'width'         => 500,
     240                'height'        => 500,
     241            )
     242        );
     243
     244        $html = get_header_image_tag(
     245            array(
     246                'loading' => 'lazy',
     247            )
     248        );
     249        $this->assertStringNotContainsString( ' fetchpriority="high"', $html );
     250        $this->assertStringContainsString( ' loading="lazy"', $html );
     251    }
     252
    177253    public function test_get_custom_header_markup_without_registered_default_image() {
    178254        $this->add_theme_support();
Note: See TracChangeset for help on using the changeset viewer.