Media: Ensure that the image widget supports loading optimization attributes.

This changeset adds support for loading optimization attributes such as `loading="lazy"` and `fetchpriority="high"` to the image widget. A new context `widget_media_image` is introduced for that purpose.

Props spacedmonkey, thekt12, mukesh27, westonruter.
Fixes #58704.
See #58235.

Built from https://develop.svn.wordpress.org/trunk@56154


git-svn-id: http://core.svn.wordpress.org/trunk@55666 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-07-06 16:33:27 +00:00
parent 6625b149d9
commit 5b91824af0
3 changed files with 26 additions and 9 deletions

View File

@ -5662,7 +5662,7 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
} }
// Special handling for programmatically created image tags. // Special handling for programmatically created image tags.
if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context ) { if ( 'the_post_thumbnail' === $context || 'wp_get_attachment_image' === $context || 'widget_media_image' === $context ) {
/* /*
* Skip programmatically created images within post content as they need to be handled together with the other * Skip programmatically created images within post content as they need to be handled together with the other
* images within the post content. * images within the post content.

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-beta3-56153'; $wp_version = '6.3-beta3-56154';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -239,14 +239,31 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
$instance['height'] = ''; $instance['height'] = '';
} }
$image = sprintf( $attr = array(
'<img class="%1$s" src="%2$s" alt="%3$s" width="%4$s" height="%5$s" />', 'class' => $classes,
esc_attr( $classes ), 'src' => $instance['url'],
esc_url( $instance['url'] ), 'alt' => $instance['alt'],
esc_attr( $instance['alt'] ), 'width' => $instance['width'],
esc_attr( $instance['width'] ), 'height' => $instance['height'],
esc_attr( $instance['height'] ) 'decoding' => 'async',
); );
$loading_optimization_attr = wp_get_loading_optimization_attributes(
'img',
$attr,
'widget_media_image'
);
$attr = array_merge( $attr, $loading_optimization_attr );
$attr = array_map( 'esc_attr', $attr );
$image = '<img';
foreach ( $attr as $name => $value ) {
$image .= ' ' . $name . '="' . $value . '"';
}
$image .= ' />';
} // End if(). } // End if().
$url = ''; $url = '';