Make WordPress Core


Ignore:
Timestamp:
07/07/2023 06:06:49 PM (8 months ago)
Author:
flixos90
Message:

Media: Avoid programmatically created images within post content from incorrectly receiving fetchpriority="high".

Follow-up to [56037], as that changeset accidentally did not consider the changes made in [55825]: Images that are programmatically injected into post content (e.g. through a block, or shortcode, or any hook calling a function like wp_get_attachment_image()) must be treated as part of the whole post content blob since otherwise the heuristics for applying fetchpriority="high" and loading="lazy" are skewed by parsing certain images before others rather than sequentially parsing the entire post content. [55825] addressed that for lazy-loading, but when [56037] introduced fetchpriority support, the related refactor missed making the same consideration for that attribute.

Props flixos90, spacedmonkey, thekt12, mukesh27.
Fixes #58235.
See #58089.

File:
1 edited

Legend:

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

    r56154 r56164  
    41354135     * @covers ::wp_get_loading_optimization_attributes
    41364136     */
    4137     public function test_wp_filter_content_tags_does_not_lazy_load_special_images_within_the_content() {
     4137    public function test_wp_filter_content_tags_does_not_apply_loading_optimization_to_special_images_within_the_content() {
    41384138        global $wp_query, $wp_the_query;
    41394139
    4140         // Force no lazy-loading on the image tag expected in the content.
    4141         $expected_content = wpautop(
    4142             wp_get_attachment_image(
    4143                 self::$large_id,
    4144                 'large',
    4145                 false,
    4146                 array(
    4147                     'loading'       => false,
    4148                     'fetchpriority' => 'high',
    4149                 )
     4140        // Force no lazy-loading or fetchpriority on the image tag expected in the content.
     4141        $expected_image = wp_get_attachment_image(
     4142            self::$large_id,
     4143            'large',
     4144            false,
     4145            array(
     4146                'loading'       => false,
     4147                'fetchpriority' => false,
    41504148            )
    41514149        );
     
    41534151        // Reset high priority flag as the forced `fetchpriority="high"` above already modified it.
    41544152        $this->reset_high_priority_element_flag();
     4153
     4154        $image_within_content = '';
    41554155
    41564156        // Overwrite post content with an image.
    41574157        add_filter(
    41584158            'the_content',
    4159             static function() {
     4159            static function() use ( &$image_within_content ) {
    41604160                // Replace content with an image tag, i.e. the 'wp_get_attachment_image' context is used while running 'the_content' filter.
    4161                 return wp_get_attachment_image( self::$large_id, 'large', false );
     4161                $image_within_content = wp_get_attachment_image( self::$large_id, 'large', false );
     4162                return $image_within_content;
    41624163            },
    41634164            9 // Run before wp_filter_content_tags().
     
    41794180        }
    41804181
    4181         // Ensure that parsed content has the image without lazy-loading.
    4182         $this->assertSame( $expected_content, $content );
     4182        // Ensure that parsed image within content does not receive any loading optimization attributes.
     4183        $this->assertSame( $expected_image, $image_within_content, 'Image with wp_get_attachment_image context within post content should not receive loading optimization attributes' );
     4184
     4185        // Ensure that parsed content has the image with fetchpriority as it is the first large image.
     4186        $expected_content = wpautop( str_replace( '<img ', '<img fetchpriority="high" ', $expected_image ) );
     4187        $this->assertSame( $expected_content, $content, 'Post content with programmatically injected image is missing loading optimization attributes' );
    41834188    }
    41844189
     
    45044509
    45054510    /**
    4506      * Tests that wp_get_loading_optimization_attributes() returns false for special contexts when they're used within 'the_content' filter.
     4511     * Tests that wp_get_loading_optimization_attributes() does not modify any attributes for special contexts when they're used within 'the_content' filter.
    45074512     *
    45084513     * @ticket 58089
     
    45154520     * @param string $context Context for the element for which the `loading` attribute value is requested.
    45164521     */
    4517     public function test_wp_get_loading_optimization_attributes_should_return_false_for_special_contexts_within_the_content( $context ) {
     4522    public function test_wp_get_loading_optimization_attributes_should_not_modify_images_for_special_contexts_within_the_content( $context ) {
    45184523        remove_all_filters( 'the_content' );
    45194524
     
    45294534        apply_filters( 'the_content', '' );
    45304535
    4531         $this->assertSame(
    4532             array( 'fetchpriority' => 'high' ),
    4533             $result,
    4534             'First large image is loaded with high fetchpriority.'
    4535         );
     4536        $this->assertSame( array(), $result );
    45364537    }
    45374538
Note: See TracChangeset for help on using the changeset viewer.