Make WordPress Core


Ignore:
Timestamp:
12/29/2025 06:59:20 PM (10 months ago)
Author:
westonruter
Message:

Script Loader: Warn when a registered style has invalid path data and allow inlining empty stylesheets.

When a stylesheet is registered with a path that does not exist or which is not readable, then a _doing_it_wrong() is now issued. Previously, paths that did not exist were silently skipped; paths for empty styles were also needlessly skipped, since wp_filesize() also returns 0 for the failure case.

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

Follow-up to [50836].

Props westonruter, jonsurrell, soyebsalar01.
See #52620.
Fixes #64447.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/styles.php

    r61411 r61416  
    717717        /**
    718718         * @ticket 58394
     719         * @ticket 64447
    719720         *
    720721         * @covers ::wp_maybe_inline_styles
    721          */
    722         public function test_test_wp_maybe_inline_styles_missing_file() {
     722         * @expectedIncorrectUsage wp_maybe_inline_styles
     723         */
     724        public function test_wp_maybe_inline_styles_missing_file() {
    723725                $filter = new MockAction();
    724726                add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
    … …  
    767769
    768770                $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url );
     771        }
     772
     773        /**
     774         * @ticket 64447
     775         *
     776         * @covers ::wp_maybe_inline_styles
     777         * @expectedIncorrectUsage wp_maybe_inline_styles
     778         */
     779        public function test_wp_maybe_inline_styles_bad_path_with_file_size_provided() {
     780                $style_path = '/css/invalid.css'; // Does not exist.
     781
     782                // This ensures the initial file size check is bypassed.
     783                add_filter(
     784                        'pre_wp_filesize',
     785                        static function ( $size, $path ) use ( $style_path ) {
     786                                if ( str_contains( $path, $style_path ) ) {
     787                                        $size = 1000;
     788                                }
     789                                return $size;
     790                        },
     791                        10,
     792                        2
     793                );
     794
     795                $handle = 'test-handle';
     796                $url    = '/' . WPINC . $style_path;
     797                wp_register_style( $handle, $url );
     798                wp_style_add_data( $handle, 'path', ABSPATH . WPINC . $style_path );
     799                wp_enqueue_style( $handle );
     800
     801                wp_maybe_inline_styles();
     802
     803                $this->assertSame( $GLOBALS['wp_styles']->registered[ $handle ]->src, $url );
     804        }
     805
     806        /**
     807         * @ticket 64447
     808         *
     809         * @covers ::wp_maybe_inline_styles
     810         */
     811        public function test_wp_maybe_inline_styles_good_path_with_zero_file_size_provided() {
     812                $style_path = '/css/classic-themes.css';
     813
     814                // This simulates the file having a zero size.
     815                add_filter(
     816                        'pre_wp_filesize',
     817                        static function ( $size, $path ) use ( $style_path ) {
     818                                if ( str_contains( $path, $style_path ) ) {
     819                                        $size = 0;
     820                                }
     821                                return $size;
     822                        },
     823                        10,
     824                        2
     825                );
     826
     827                $handle = 'test-handle';
     828                wp_register_style( $handle, '/' . WPINC . $style_path );
     829                wp_style_add_data( $handle, 'path', ABSPATH . WPINC . $style_path );
     830                wp_enqueue_style( $handle );
     831
     832                wp_maybe_inline_styles();
     833
     834                $this->assertFalse( $GLOBALS['wp_styles']->registered[ $handle ]->src );
    769835        }
    770836
Note: See TracChangeset for help on using the changeset viewer.