Make WordPress Core


Ignore:
Timestamp:
09/21/2023 04:35:30 PM (3 years ago)
Author:
flixos90
Message:

Media: Introduce filters to customize the results from wp_get_loading_optimization_attributes().

This changeset introduces two filters that allow customizing the loading optimization attributes array returned from wp_get_loading_optimization_attributes() for individual HTML tags:

  • The wp_get_loading_optimization_attributes filter can be used to modify the results from the WordPress core logic.
  • The pre_wp_get_loading_optimization_attributes filter can be used to use entirely custom logic and effectively short-circuit the core function.

Props pereirinha, mukesh27, spacedmonkey, joemcgill.
Fixes #58893.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r56616 r56651  
    54425442
    54435443        /**
     5444         * Tests for pre_wp_get_loading_optimization_attributes filter.
     5445         *
     5446         * @ticket 58893
     5447         */
     5448        public function test_pre_wp_get_loading_optimization_attributes_filter() {
     5449                add_filter(
     5450                        'pre_wp_get_loading_optimization_attributes',
     5451                        static function ( $loading_attrs ) {
     5452                                if ( false === $loading_attrs ) {
     5453                                        // Initialize as an empty array.
     5454                                        $loading_attrs = array();
     5455                                }
     5456                                $loading_attrs['fetchpriority'] = 'high';
     5457
     5458                                return $loading_attrs;
     5459                        },
     5460                        10,
     5461                        1
     5462                );
     5463
     5464                $attr = $this->get_width_height_for_high_priority();
     5465
     5466                $this->assertSame(
     5467                        array( 'fetchpriority' => 'high' ),
     5468                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5469                        'The filter did not return early fetchpriority attribute'
     5470                );
     5471
     5472                // Clean up the filter.
     5473                add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_false' );
     5474
     5475                $this->assertSameSets(
     5476                        array( 'loading' => 'lazy' ),
     5477                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5478                        'The filter did not return the default attributes.'
     5479                );
     5480
     5481                // Return no loading attributes.
     5482                add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' );
     5483
     5484                $this->assertSameSets(
     5485                        array(),
     5486                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5487                        'The filter did not clean up all attributes.'
     5488                );
     5489
     5490                // Modify the loading attributes with any custom attributes.
     5491                add_filter(
     5492                        'pre_wp_get_loading_optimization_attributes',
     5493                        static function ( $loading_attrs ) {
     5494                                if ( false === $loading_attrs ) {
     5495                                        // Initialize as an empty array.
     5496                                        $loading_attrs = array();
     5497                                }
     5498                                $loading_attrs['custom_attr'] = 'custom_value';
     5499
     5500                                return $loading_attrs;
     5501                        },
     5502                        10,
     5503                        1
     5504                );
     5505
     5506                $this->assertSameSets(
     5507                        array( 'custom_attr' => 'custom_value' ),
     5508                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5509                        'The filter did not return custom attributes.'
     5510                );
     5511        }
     5512
     5513        /**
     5514         * Tests for wp_get_loading_optimization_attributes filter.
     5515         *
     5516         * @ticket 58893
     5517         */
     5518        public function test_wp_get_loading_optimization_attributes_filter() {
     5519                $attr = $this->get_width_height_for_high_priority();
     5520
     5521                $this->assertSameSets(
     5522                        array( 'loading' => 'lazy' ),
     5523                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5524                        'Before the filter it will not return the loading attribute.'
     5525                );
     5526
     5527                add_filter(
     5528                        'wp_get_loading_optimization_attributes',
     5529                        static function ( $loading_attrs ) {
     5530                                unset( $loading_attrs['loading'] );
     5531                                $loading_attrs['fetchpriority'] = 'high';
     5532
     5533                                return $loading_attrs;
     5534                        },
     5535                        10,
     5536                        1
     5537                );
     5538
     5539                $this->assertSameSets(
     5540                        array( 'fetchpriority' => 'high' ),
     5541                        wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
     5542                        'After the filter it will not return the fetchpriority attribute.'
     5543                );
     5544        }
     5545
     5546        /**
    54445547         * Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
    54455548         *
Note: See TracChangeset for help on using the changeset viewer.