From 5b91824af03669f4022dd8052555b38979627a6b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 6 Jul 2023 16:33:27 +0000 Subject: [PATCH] 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 --- wp-includes/media.php | 2 +- wp-includes/version.php | 2 +- .../widgets/class-wp-widget-media-image.php | 31 ++++++++++++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 11815e65fa..f8b0f9c1b1 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -5662,7 +5662,7 @@ function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) { } // 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 * images within the post content. diff --git a/wp-includes/version.php b/wp-includes/version.php index 95fbecadb5..6aa9e9a16e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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. diff --git a/wp-includes/widgets/class-wp-widget-media-image.php b/wp-includes/widgets/class-wp-widget-media-image.php index 77df8050d8..31a8bef16f 100644 --- a/wp-includes/widgets/class-wp-widget-media-image.php +++ b/wp-includes/widgets/class-wp-widget-media-image.php @@ -239,14 +239,31 @@ class WP_Widget_Media_Image extends WP_Widget_Media { $instance['height'] = ''; } - $image = sprintf( - '%3$s', - esc_attr( $classes ), - esc_url( $instance['url'] ), - esc_attr( $instance['alt'] ), - esc_attr( $instance['width'] ), - esc_attr( $instance['height'] ) + $attr = array( + 'class' => $classes, + 'src' => $instance['url'], + 'alt' => $instance['alt'], + 'width' => $instance['width'], + 'height' => $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 = ' $value ) { + $image .= ' ' . $name . '="' . $value . '"'; + } + + $image .= ' />'; } // End if(). $url = '';