Make WordPress Core


Ignore:
Timestamp:
05/17/2023 06:29:41 PM (10 months ago)
Author:
flixos90
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r55816 r55821  
    10511051        );
    10521052
     1053        /**
     1054         * Filters the context in which wp_get_attachment_image() is used.
     1055         *
     1056         * @since 6.3.0
     1057         *
     1058         * @param string $context The context. Default 'wp_get_attachment_image'.
     1059         */
     1060        $context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
     1061
    10531062        // Add `loading` attribute.
    1054         if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
    1055             $default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
     1063        if ( wp_lazy_loading_enabled( 'img', $context ) ) {
     1064            $default_attr['loading'] = wp_get_loading_attr_default( $context );
    10561065        }
    10571066
     
    21692178function _wp_post_thumbnail_class_filter_remove( $attr ) {
    21702179    remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
     2180}
     2181
     2182/**
     2183 * Overrides the context used in {@see wp_get_attachment_image()}. Internal use only.
     2184 *
     2185 * Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
     2186 * action hooks to dynamically add/remove itself so as to only filter post thumbnails.
     2187 *
     2188 * @ignore
     2189 * @since 6.3.0
     2190 * @access private
     2191 *
     2192 * @param string $context The context for rendering an attachment image.
     2193 * @return string Modified context set to 'the_post_thumbnail'.
     2194 */
     2195function _wp_post_thumbnail_context_filter( $context ) {
     2196    return 'the_post_thumbnail';
     2197}
     2198
     2199/**
     2200 * Adds the '_wp_post_thumbnail_context_filter' callback to the 'wp_get_attachment_image_context'
     2201 * filter hook. Internal use only.
     2202 *
     2203 * @ignore
     2204 * @since 6.3.0
     2205 * @access private
     2206 */
     2207function _wp_post_thumbnail_context_filter_add() {
     2208    add_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
     2209}
     2210
     2211/**
     2212 * Removes the '_wp_post_thumbnail_context_filter' callback from the 'wp_get_attachment_image_context'
     2213 * filter hook. Internal use only.
     2214 *
     2215 * @ignore
     2216 * @since 6.3.0
     2217 * @access private
     2218 */
     2219function _wp_post_thumbnail_context_filter_remove() {
     2220    remove_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
    21712221}
    21722222
Note: See TracChangeset for help on using the changeset viewer.