From 582ddb82f4524c15c675d32bd173215769a59728 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 7 Jun 2023 06:54:18 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55888 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/script-loader.php | 14 ++- tests/phpunit/tests/dependencies/styles.php | 95 +++++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 0ea531824b..713a0d3118 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2872,12 +2872,18 @@ function wp_maybe_inline_styles() { // Build an array of styles that have a path defined. foreach ( $wp_styles->queue as $handle ) { - if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) { + $src = $wp_styles->registered[ $handle ]->src; + $path = wp_styles()->get_data( $handle, 'path' ); + if ( $path && $src ) { + $size = wp_filesize( $path ); + if ( ! $size ) { + continue; + } $styles[] = array( 'handle' => $handle, - 'src' => $wp_styles->registered[ $handle ]->src, - 'path' => $wp_styles->registered[ $handle ]->extra['path'], - 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ), + 'src' => $src, + 'path' => $path, + 'size' => $size, ); } } diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index ad2e672dc0..798b75a252 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -517,4 +517,99 @@ CSS; $GLOBALS['wp_styles']->registered['wp-block-library']->src ); } + + /** + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_wp_maybe_inline_styles() { + wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' ); + wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' ); + + wp_enqueue_style( 'test-handle' ); + + wp_maybe_inline_styles(); + + $this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should be reset to false' ); + + $css = file_get_contents( ABSPATH . WPINC . '/css/classic-themes.css' ); + $this->assertSameSets( $GLOBALS['wp_styles']->registered['test-handle']->extra['after'], array( $css ), 'Source of style should set to after property' ); + } + + /** + * wp_filesize should be only be called once, as on the second run of wp_maybe_inline_styles, + * src will be set to false and filesize will not be requested. + * + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_wp_maybe_inline_styles_multiple_runs() { + $filter = new MockAction(); + add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) ); + wp_register_style( 'test-handle', '/' . WPINC . '/css/classic-themes.css' ); + wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' ); + + wp_enqueue_style( 'test-handle' ); + + wp_maybe_inline_styles(); + wp_maybe_inline_styles(); + + $this->assertSame( 1, $filter->get_call_count() ); + } + + /** + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_test_wp_maybe_inline_styles_missing_file() { + $filter = new MockAction(); + add_filter( 'pre_wp_filesize', array( $filter, 'filter' ) ); + $url = '/' . WPINC . '/css/invalid.css'; + wp_register_style( 'test-handle', $url ); + wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/invalid.css' ); + + wp_enqueue_style( 'test-handle' ); + + wp_maybe_inline_styles(); + + $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url, 'Source should not change' ); + $this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' ); + $this->assertSame( 1, $filter->get_call_count(), 'wp_filesize should only be called once' ); + } + + /** + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_wp_maybe_inline_styles_no_src() { + wp_register_style( 'test-handle', false ); + wp_style_add_data( 'test-handle', 'path', ABSPATH . WPINC . '/css/classic-themes.css' ); + + wp_enqueue_style( 'test-handle' ); + + wp_maybe_inline_styles(); + + $this->assertFalse( $GLOBALS['wp_styles']->registered['test-handle']->src, 'Source of style should remain false' ); + $this->assertArrayNotHasKey( 'after', $GLOBALS['wp_styles']->registered['test-handle']->extra, 'Source of style not should set to after property' ); + } + + /** + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_wp_maybe_inline_styles_no_path() { + $url = '/' . WPINC . '/css/classic-themes.css'; + wp_register_style( 'test-handle', $url ); + + wp_enqueue_style( 'test-handle' ); + + wp_maybe_inline_styles(); + + $this->assertSame( $GLOBALS['wp_styles']->registered['test-handle']->src, $url ); + } }