From 9ecfdd8e5a42ed4b66ffecb88321242717c89831 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 5 Jul 2023 19:30:25 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@56142 git-svn-id: http://core.svn.wordpress.org/trunk@55654 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 5 +++++ wp-includes/theme.php | 32 ++++++++++++++++++++++++++++---- wp-includes/version.php | 2 +- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 04fb27608a..0da68e3d43 100644 --- a/wp-includes/media.php +++ b/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/wp-includes/theme.php b/wp-includes/theme.php index 32fd55df5e..dfc0a4e063 100644 --- a/wp-includes/theme.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 2560c7180a..44d90d40dd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-beta3-56141'; +$wp_version = '6.3-beta3-56142'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.