Make WordPress Core


Ignore:
Timestamp:
05/17/2023 06:29:41 PM (3 years 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/tests/phpunit/tests/media.php

    r55816 r55821  
    39653965
    39663966        /**
     3967         * Tests that `wp_get_attachment_image()` uses the correct default context.
     3968         *
     3969         * @ticket 58212
     3970         *
     3971         * @covers ::wp_get_attachment_image()
     3972         */
     3973        public function test_wp_get_attachment_image_context_filter_default() {
     3974                $last_context = '';
     3975                $this->track_last_attachment_image_context( $last_context );
     3976
     3977                wp_get_attachment_image( self::$large_id );
     3978                $this->assertSame( 'wp_get_attachment_image', $last_context );
     3979        }
     3980
     3981        /**
     3982         * Tests that `wp_get_attachment_image()` allows overriding the context via filter.
     3983         *
     3984         * @ticket 58212
     3985         *
     3986         * @covers ::wp_get_attachment_image()
     3987         */
     3988        public function test_wp_get_attachment_image_context_filter_value_is_passed_correctly() {
     3989                $last_context = '';
     3990                $this->track_last_attachment_image_context( $last_context );
     3991
     3992                // Add a filter that modifies the context.
     3993                add_filter(
     3994                        'wp_get_attachment_image_context',
     3995                        static function() {
     3996                                return 'my_custom_context';
     3997                        }
     3998                );
     3999
     4000                wp_get_attachment_image( self::$large_id );
     4001                $this->assertSame( 'my_custom_context', $last_context );
     4002        }
     4003
     4004        /**
     4005         * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
     4006         *
     4007         * The method parameter is passed by reference and therefore will always contain the last context value.
     4008         *
     4009         * @param mixed $last_context Variable to track last context. Passed by reference.
     4010         */
     4011        private function track_last_attachment_image_context( &$last_context ) {
     4012                add_filter(
     4013                        'wp_get_attachment_image_context',
     4014                        static function( $context ) use ( &$last_context ) {
     4015                                $last_context = $context;
     4016                                return $context;
     4017                        },
     4018                        11
     4019                );
     4020        }
     4021
     4022        /**
    39674023         * Add threshold to create a `-scaled` output image for testing.
    39684024         */
Note: See TracChangeset for help on using the changeset viewer.