Media: Introduce wp_get_attachment_image_context filter.

Since WordPress 5.9, a "context" value of "wp_get_attachment_image" has been used in the `wp_get_attachment_image()` function to provide context to underlying functions where that is relevant, e.g. `wp_get_loading_attr_default()`. Since that value used to be not customizable, it required a workaround in `get_the_post_thumbnail()` to avoid calling those functions in `wp_get_attachment_image()`, which resulted in unnecessary complexity and was prone to errors.

This changeset introduces a `wp_get_attachment_image_context` filter and leverages it with private filter callback functions that are leveraged by default when `get_the_post_thumbnail()` is called. This avoids the need for the previous workaround and furthermore provides flexibility for other callers of `wp_get_attachment_image()` to provide their own contexts.

Props flixos90, costdev, thekt12, westonruter, spacedmonkey.
Fixes #58212.


git-svn-id: https://develop.svn.wordpress.org/trunk@55821 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2023-05-17 18:29:41 +00:00
parent 261ab3fa5d
commit 5db31259ec
5 changed files with 240 additions and 19 deletions
+52 -2
View File
@@ -1050,9 +1050,18 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
'decoding' => 'async',
);
/**
* Filters the context in which wp_get_attachment_image() is used.
*
* @since 6.3.0
*
* @param string $context The context. Default 'wp_get_attachment_image'.
*/
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
// Add `loading` attribute.
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
$default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
if ( wp_lazy_loading_enabled( 'img', $context ) ) {
$default_attr['loading'] = wp_get_loading_attr_default( $context );
}
$attr = wp_parse_args( $attr, $default_attr );
@@ -2170,6 +2179,47 @@ function _wp_post_thumbnail_class_filter_remove( $attr ) {
remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
}
/**
* Overrides the context used in {@see wp_get_attachment_image()}. Internal use only.
*
* Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
* action hooks to dynamically add/remove itself so as to only filter post thumbnails.
*
* @ignore
* @since 6.3.0
* @access private
*
* @param string $context The context for rendering an attachment image.
* @return string Modified context set to 'the_post_thumbnail'.
*/
function _wp_post_thumbnail_context_filter( $context ) {
return 'the_post_thumbnail';
}
/**
* Adds the '_wp_post_thumbnail_context_filter' callback to the 'wp_get_attachment_image_context'
* filter hook. Internal use only.
*
* @ignore
* @since 6.3.0
* @access private
*/
function _wp_post_thumbnail_context_filter_add() {
add_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
}
/**
* Removes the '_wp_post_thumbnail_context_filter' callback from the 'wp_get_attachment_image_context'
* filter hook. Internal use only.
*
* @ignore
* @since 6.3.0
* @access private
*/
function _wp_post_thumbnail_context_filter_remove() {
remove_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
}
add_shortcode( 'wp_caption', 'img_caption_shortcode' );
add_shortcode( 'caption', 'img_caption_shortcode' );