Make WordPress Core


Ignore:
Timestamp:
10/07/2025 06:22:02 AM (12 months ago)
Author:
westonruter
Message:

Media: Switch to enqueueing contain-intrinsic-size CSS fix for IMG tags having sizes=auto.

This deprecates the wp_print_auto_sizes_contain_css_fix() function running at wp_head priority 1, in favor of a wp_enqueue_img_auto_sizes_contain_css_fix() function which runs just before at wp_head priority 0. The latter function unhooks the former while also enqueueing an inline style to be printed with all other styles but up front to preserve the cascade. This eliminates directly printing the STYLE tag, which was a change done similarly before for the emoji styles. See #58775.

For backwards compatibility, the CSS can still be prevented from being enqueued/printed via:

remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );

This change ensures that all styles are printed together using the correct API for emitting styles.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/8954.

Follow-up to [59435].

Props westonruter, sabernhardt, SirLouen, flixos90, joemcgill, SergeyBiryukov, superpoincare.
See #62413.
Fixes #62731.

File:
1 edited

Legend:

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

    r60736 r60910  
    2929        protected static $large_filename = 'test-image-large.jpg';
    3030        protected static $post_ids;
     31
     32        /**
     33         * @var WP_Styles|null
     34         */
     35        protected static $original_wp_styles;
    3136
    3237        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    … …  
    6772        }
    6873
     74        public function set_up(): void {
     75                global $wp_styles;
     76                self::$original_wp_styles = $wp_styles;
     77                $wp_styles                = null;
     78                parent::set_up();
     79        }
     80
    6981        public static function wpTearDownAfterClass() {
    7082                $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
    … …  
    8092         */
    8193        public function tear_down() {
    82                 global $_wp_current_template_id, $_wp_current_template_content;
     94                global $_wp_current_template_id, $_wp_current_template_content, $wp_styles;
    8395                unset( $_wp_current_template_id, $_wp_current_template_content );
     96
     97                $wp_styles = self::$original_wp_styles;
    8498
    8599                parent::tear_down();
    … …  
    65786592
    65796593        /**
     6594         * Provides data to test wp_enqueue_img_auto_sizes_contain_css_fix().
     6595         *
     6596         * @return array<string, array>
     6597         */
     6598        public function data_provider_data_provider_to_test_wp_enqueue_img_auto_sizes_contain_css_fix(): array {
     6599                return array(
     6600                        'default'                     => array(
     6601                                'set_up'   => null,
     6602                                'expected' => true,
     6603                        ),
     6604                        'filtered_off'                => array(
     6605                                'set_up'   => static function (): void {
     6606                                        add_filter( 'wp_img_tag_add_auto_sizes', '__return_false' );
     6607                                },
     6608                                'expected' => false,
     6609                        ),
     6610                        'filtered_on'                 => array(
     6611                                'set_up'   => static function (): void {
     6612                                        add_filter( 'wp_img_tag_add_auto_sizes', '__return_false' );
     6613                                        add_filter( 'wp_img_tag_add_auto_sizes', '__return_true', 100 );
     6614                                },
     6615                                'expected' => true,
     6616                        ),
     6617                        'deprecated_function_removed' => array(
     6618                                'set_up'   => static function (): void {
     6619                                        remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
     6620                                },
     6621                                'expected' => false,
     6622                        ),
     6623                        'new_function_removed'        => array(
     6624                                'set_up'              => static function (): void {
     6625                                        remove_action( 'wp_head', 'wp_enqueue_img_auto_sizes_contain_css_fix', 0 );
     6626                                },
     6627                                'expected'            => false,
     6628                                'expected_deprecated' => 'wp_print_auto_sizes_contain_css_fix',
     6629                        ),
     6630                        'both_functions_removed'      => array(
     6631                                'set_up'   => static function (): void {
     6632                                        remove_action( 'wp_head', 'wp_enqueue_img_auto_sizes_contain_css_fix', 0 );
     6633                                        remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );
     6634                                },
     6635                                'expected' => false,
     6636                        ),
     6637                );
     6638        }
     6639
     6640        /**
     6641         * Tests that IMG auto-sizes CSS fix is enqueued (and printed) when expected.
     6642         *
     6643         * @covers ::wp_enqueue_img_auto_sizes_contain_css_fix
     6644         * @ticket 62731
     6645         *
     6646         * @dataProvider data_provider_data_provider_to_test_wp_enqueue_img_auto_sizes_contain_css_fix
     6647         */
     6648        public function test_wp_enqueue_img_auto_sizes_contain_css_fix( ?Closure $set_up, bool $expected, ?string $expected_deprecated = null ): void {
     6649                if ( $set_up ) {
     6650                        $set_up();
     6651                }
     6652                if ( isset( $expected_deprecated ) ) {
     6653                        $this->setExpectedDeprecated( $expected_deprecated );
     6654                }
     6655
     6656                $this->assertCount( 0, wp_styles()->queue );
     6657                wp_enqueue_style( 'very-early-enqueued', home_url( '/very-early-enqueued.css' ) );
     6658                add_action(
     6659                        'wp_enqueue_scripts',
     6660                        static function () {
     6661                                wp_enqueue_style( 'wp-block-library' );
     6662                        }
     6663                );
     6664
     6665                $wp_head_output           = get_echo( 'wp_head' );
     6666                $html_processor           = new WP_HTML_Tag_Processor( $wp_head_output );
     6667                $found_style_text_content = null;
     6668                while ( $html_processor->next_tag( array( 'tag_name' => 'STYLE' ) ) ) {
     6669                        if ( $html_processor->get_attribute( 'id' ) === 'wp-img-auto-sizes-contain-inline-css' ) {
     6670                                $found_style_text_content = $html_processor->get_modifiable_text();
     6671                                break;
     6672                        }
     6673                }
     6674
     6675                $enqueued = wp_styles()->queue;
     6676                if ( $expected ) {
     6677                        $this->assertSame( 'wp-img-auto-sizes-contain', array_shift( $enqueued ) );
     6678                        $this->assertIsString( $found_style_text_content );
     6679                        $this->assertStringContainsString( 'contain-intrinsic-size', $found_style_text_content );
     6680                } else {
     6681                        $this->assertNull( $found_style_text_content );
     6682                }
     6683                $this->assertSame( 'very-early-enqueued', array_shift( $enqueued ) );
     6684                $this->assertContains( 'wp-emoji-styles', $enqueued );
     6685                $this->assertContains( 'wp-block-library', $enqueued );
     6686        }
     6687
     6688        /**
    65806689         * Data provider for test_wp_img_tag_add_auto_sizes().
    65816690         *
Note: See TracChangeset for help on using the changeset viewer.