From 559e6cecf49505ecc3a21fbff647fdc5c3f892a3 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 13 Jun 2023 09:51:11 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55909 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/script-loader.php | 5 ++++- tests/phpunit/tests/dependencies/styles.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 9cd877a50f..76155ac226 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -2872,8 +2872,11 @@ function wp_maybe_inline_styles() { // Build an array of styles that have a path defined. foreach ( $wp_styles->queue as $handle ) { + if ( ! isset( $wp_styles->registered[ $handle ] ) ) { + continue; + } $src = $wp_styles->registered[ $handle ]->src; - $path = wp_styles()->get_data( $handle, 'path' ); + $path = $wp_styles->get_data( $handle, 'path' ); if ( $path && $src ) { $size = wp_filesize( $path ); if ( ! $size ) { diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 798b75a252..4b1e46f21d 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -537,6 +537,26 @@ CSS; $this->assertSameSets( $GLOBALS['wp_styles']->registered['test-handle']->extra['after'], array( $css ), 'Source of style should set to after property' ); } + /** + * @ticket 58394 + * + * @covers ::wp_maybe_inline_styles + */ + public function test_wp_maybe_inline_styles_dequeue_styles() { + $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_deregister_style( 'test-handle' ); + + wp_maybe_inline_styles(); + + $this->assertSame( 0, $filter->get_call_count() ); + } + /** * 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.