diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 04fb27608a..0da68e3d43 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -5643,6 +5643,11 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { return $postprocess( $loading_attrs, true ); } + // The custom header image is always expected to be in the header. + if ( 'get_header_image_tag' === $context ) { + return $postprocess( $loading_attrs, true ); + } + // Special handling for programmatically created image tags. if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) { /* diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 32fd55df5e..dfc0a4e063 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -1237,10 +1237,11 @@ function get_header_image_tag( $attr = array() ) { $attr = wp_parse_args( $attr, array( - 'src' => $header->url, - 'width' => $width, - 'height' => $height, - 'alt' => $alt, + 'src' => $header->url, + 'width' => $width, + 'height' => $height, + 'alt' => $alt, + 'decoding' => 'async', ) ); @@ -1265,6 +1266,29 @@ function get_header_image_tag( $attr = array() ) { } } + $attr = array_merge( + $attr, + wp_get_loading_optimization_attributes( 'img', $attr, 'get_header_image_tag' ) + ); + + /* + * If the default value of `lazy` for the `loading` attribute is overridden + * to omit the attribute for this image, ensure it is not included. + */ + if ( isset( $attr['loading'] ) && ! $attr['loading'] ) { + unset( $attr['loading'] ); + } + + // If the `fetchpriority` attribute is overridden and set to false or an empty string. + if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) { + unset( $attr['fetchpriority'] ); + } + + // If the `decoding` attribute is overridden and set to false or an empty string. + if ( isset( $attr['decoding'] ) && ! $attr['decoding'] ) { + unset( $attr['decoding'] ); + } + /** * Filters the list of header image attributes. * diff --git a/tests/phpunit/tests/theme/customHeader.php b/tests/phpunit/tests/theme/customHeader.php index 21350deeda..13455df663 100644 --- a/tests/phpunit/tests/theme/customHeader.php +++ b/tests/phpunit/tests/theme/customHeader.php @@ -174,6 +174,82 @@ class Tests_Theme_CustomHeader extends WP_UnitTestCase { $this->assertStringContainsString( sprintf( 'src="%s"', $custom ), $html ); } + /** + * Tests default values of performance attributes for "get_header_image_tag". + * + * @ticket 58680 + */ + public function test_get_header_image_tag_with_default_performance_attributes() { + $this->add_theme_support( + array( + 'default-image' => 'http://localhost/default-header.jpg', + 'width' => 60, + 'height' => 60, + ) + ); + + add_filter( + 'wp_min_priority_img_pixels', + static function() { + return 2500; // 50*50=2500 + } + ); + + wp_high_priority_element_flag( true ); + + $html = get_header_image_tag(); + $this->assertStringNotContainsString( ' loading="lazy"', $html ); + $this->assertStringContainsString( ' fetchpriority="high"', $html ); + $this->assertStringContainsString( ' decoding="async"', $html ); + } + + /** + * Tests custom values of performance attributes for "get_header_image_tag". + * + * @ticket 58680 + */ + public function test_get_header_image_tag_with_custom_performance_attributes() { + $this->add_theme_support( + array( + 'default-image' => 'http://localhost/default-header.jpg', + 'width' => 500, + 'height' => 500, + ) + ); + + $html = get_header_image_tag( + array( + 'fetchpriority' => '', + 'decoding' => '', + ) + ); + $this->assertStringNotContainsString( ' fetchpriority="high"', $html ); + $this->assertStringNotContainsString( ' decoding="async"', $html ); + } + + /** + * Tests custom lazy loading for "get_header_image_tag". + * + * @ticket 58680 + */ + public function test_get_header_image_tag_with_custom_lazy_loading() { + $this->add_theme_support( + array( + 'default-image' => 'http://localhost/default-header.jpg', + 'width' => 500, + 'height' => 500, + ) + ); + + $html = get_header_image_tag( + array( + 'loading' => 'lazy', + ) + ); + $this->assertStringNotContainsString( ' fetchpriority="high"', $html ); + $this->assertStringContainsString( ' loading="lazy"', $html ); + } + public function test_get_custom_header_markup_without_registered_default_image() { $this->add_theme_support();