Make WordPress Core

Changeset 55888


Ignore:
Timestamp:
06/07/2023 06:54:18 AM (16 months ago)
Author:
spacedmonkey
Message:

Script Loader: Improve performance of wp_maybe_inline_styles function.

The wp_maybe_inline_styles function is called twice on the average page load. On it's second run however, it did not check to see if the style had already been processed on the first run. This resulted in calling filesize and get_file_contents unnecessarily, which was bad for performance. Now, the loop around the queued styles, checks to see if the source is set to false, meaning it has already been processed. This change also replaces calls to filesize with the core function wp_filesize, which improves extensibility.

Props spacedmonkey, flixos90, peterwilsoncc, joemcgill.
Fixes #58394.

Location:
trunk
Files:
2 edited

Legend:

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

    r55870 r55888  
    28732873    // Build an array of styles that have a path defined.
    28742874    foreach ( $wp_styles->queue as $handle ) {
    2875         if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) {
     2875        $src  = $wp_styles->registered[ $handle ]->src;
     2876        $path = wp_styles()->get_data( $handle, 'path' );
     2877        if ( $path && $src ) {
     2878            $size = wp_filesize( $path );
     2879            if ( ! $size ) {
     2880                continue;
     2881            }
    28762882            $styles[] = array(
    28772883                'handle' => $handle,
    2878                 'src'    => $wp_styles->registered[ $handle ]->src,
    2879                 'path'   => $wp_styles->registered[ $handle ]->extra['path'],
    2880                 'size'   => filesize( $wp_styles->registered[ $handle ]->extra['path'] ),
     2884                'src'    => $src,
     2885                'path'   => $path,
     2886                'size'   => $size,
    28812887            );
    28822888        }
  • trunk/tests/phpunit/tests/dependencies/styles.php

    r55669 r55888  
    518518        );
    519519    }
     520
     521    /**
     522     * @ticket 58394
     523     *
     524     * @covers ::wp_maybe_inline_styles
     525     */
     526    public function test_wp_maybe_inline_styles() {
     527        wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' );
     528        wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' );
     529
     530        wp_enqueue_style( 'test-handle' );
     531
     532        wp_maybe_inline_styles();
     533
     534        $this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should be reset to false' );
     535
     536        $css = file_get_contents( ABSPATH . WPINC . '/css/classic-themes.css' );
     537        $this->assertSameSets( $GLOBALS['wp_styles']->registered['test-handle']->extra['after'], array( $css ), 'Source of style should set to after property' );
     538    }
     539
     540    /**
     541     * wp_filesize should be only be called once, as on the second run of wp_maybe_inline_styles,
     542     * src will be set to false and filesize will not be requested.
     543     *
     544     * @ticket 58394
     545     *
     546     * @covers ::wp_maybe_inline_styles
     547     */
     548    public function test_wp_maybe_inline_styles_multiple_runs() {
     549        $filter = new MockAction();
     550        add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
     551        wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' );
     552        wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' );
     553
     554        wp_enqueue_style( 'test-handle' );
     555
     556        wp_maybe_inline_styles();
     557        wp_maybe_inline_styles();
     558
     559        $this->assertSame( 1, $filter->get_call_count() );
     560    }
     561
     562    /**
     563     * @ticket 58394
     564     *
     565     * @covers ::wp_maybe_inline_styles
     566     */
     567    public function test_test_wp_maybe_inline_styles_missing_file() {
     568        $filter = new MockAction();
     569        add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) );
     570        $url = '/' . WPINC . '/css/invalid.css';
     571        wp_register_style( 'test-handle', $url );
     572        wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/invalid.css' );
     573
     574        wp_enqueue_style( 'test-handle' );
     575
     576        wp_maybe_inline_styles();
     577
     578        $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url, 'Source should not change' );
     579        $this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' );
     580        $this->assertSame( 1, $filter->get_call_count(), 'wp_filesize should only be called once' );
     581    }
     582
     583    /**
     584     * @ticket 58394
     585     *
     586     * @covers ::wp_maybe_inline_styles
     587     */
     588    public function test_wp_maybe_inline_styles_no_src() {
     589        wp_register_style( 'test-handle', false );
     590        wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' );
     591
     592        wp_enqueue_style( 'test-handle' );
     593
     594        wp_maybe_inline_styles();
     595
     596        $this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should remain false' );
     597        $this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' );
     598    }
     599
     600    /**
     601     * @ticket 58394
     602     *
     603     * @covers ::wp_maybe_inline_styles
     604     */
     605    public function test_wp_maybe_inline_styles_no_path() {
     606        $url = '/' . WPINC . '/css/classic-themes.css';
     607        wp_register_style( 'test-handle', $url );
     608
     609        wp_enqueue_style( 'test-handle' );
     610
     611        wp_maybe_inline_styles();
     612
     613        $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url );
     614    }
    520615}
Note: See TracChangeset for help on using the changeset viewer.