Make WordPress Core

Changeset 55909


Ignore:
Timestamp:
06/13/2023 09:51:11 AM (18 months ago)
Author:
spacedmonkey
Message:

Script Loader: Add a check to see in style is registered in wp_maybe_inline_styles.

Add a check in wp_maybe_inline_styles to check that style is registered before processing items in queue. It is possible that developers may have called wp_deregister_style, unregistering style but the style still be in the queue to be processed. Without this check, typing to access the src property would result in a notice error.

Follow on from [55888].

Props spacedmonkey, flixos90, dd32, kebbet.
See #58394.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r55900 r55909  
    28732873    // Build an array of styles that have a path defined.
    28742874    foreach ( $wp_styles->queue as $handle ) {
     2875        if ( ! isset( $wp_styles->registered[ $handle ] ) ) {
     2876            continue;
     2877        }
    28752878        $src  = $wp_styles->registered[ $handle ]->src;
    2876         $path = wp_styles()->get_data( $handle, 'path' );
     2879        $path = $wp_styles->get_data( $handle, 'path' );
    28772880        if ( $path && $src ) {
    28782881            $size = wp_filesize( $path );
  • trunk/tests/phpunit/tests/dependencies/styles.php

    r55888 r55909  
    539539
    540540    /**
     541     * @ticket 58394
     542     *
     543     * @covers ::wp_maybe_inline_styles
     544     */
     545    public function test_wp_maybe_inline_styles_dequeue_styles() {
     546        $filter = new MockAction();
     547        add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
     548        wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' );
     549        wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' );
     550
     551        wp_enqueue_style( 'test-handle' );
     552
     553        wp_deregister_style( 'test-handle' );
     554
     555        wp_maybe_inline_styles();
     556
     557        $this->assertSame( 0, $filter->get_call_count() );
     558    }
     559
     560    /**
    541561     * wp_filesize should be only be called once, as on the second run of wp_maybe_inline_styles,
    542562     * src will be set to false and filesize will not be requested.
Note: See TracChangeset for help on using the changeset viewer.