From a5d3f19a3882a96e172df22f210e71145c6e7078 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 3 Nov 2025 22:25:08 -0800 Subject: [PATCH 1/4] Remove wp-block-styles theme support precondition for loading block styles on demand --- src/wp-includes/script-loader.php | 31 +++++++++++++++++++------------ tests/phpunit/tests/template.php | 27 ++++++++++++++++----------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index eda19a8b1f86c..2123c12293dc0 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -3575,8 +3575,11 @@ function wp_remove_surrounding_empty_script_tags( $contents ) { * Adds hooks to load block styles on demand in classic themes. * * @since 6.9.0 + * + * @see _add_default_theme_supports() */ function wp_load_classic_theme_block_styles_on_demand() { + // This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports(). if ( wp_is_block_theme() ) { return; } @@ -3588,25 +3591,29 @@ function wp_load_classic_theme_block_styles_on_demand() { */ add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 ); + // If a site has opted out of the template enhancement output buffer, then we must abort. if ( ! wp_should_output_buffer_template_for_enhancement() ) { return; } + // The following to filters are added by default for block themes in _add_default_theme_supports(). + /* - * If the theme supports block styles, add filters to ensure they are loaded separately and on demand. Without this, - * if a theme does not want or support block styles, then enabling these filters can result in undesired separate - * block-specific styles being enqueued, though a theme may also be trying to nullify the wp-block-library - * stylesheet. + * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, + * and so that block-specific styles will only be enqueued when they are used on the page. + * A priority of zero allows for this to be easily overridden by themes which wish to opt-out. */ - if ( current_theme_supports( 'wp-block-styles' ) ) { - /* - * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, - * and so that block-specific styles will only be enqueued when they are used on the page. - */ - add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 ); + add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 ); - // Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets). - add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 ); + /* + * Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets). + * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt-out. + */ + add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 ); + + // If even with the filtering + if ( ! wp_should_load_separate_core_block_assets() || ! wp_should_load_block_assets_on_demand() ) { + return; } // Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early. diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index e954f68b21923..8f18061e8822e 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -1393,35 +1393,39 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_block_theme */ public function data_wp_load_classic_theme_block_styles_on_demand(): array { return array( - 'block_theme' => array( + 'block_theme' => array( 'theme' => 'block-theme', 'set_up' => static function () {}, - 'expected_on_demand' => false, + 'expected_load_separate' => true, + 'expected_on_demand' => true, 'expected_buffer_started' => false, ), - 'classic_theme_with_output_buffer_blocked' => array( + 'classic_theme_with_output_buffer_blocked' => array( 'theme' => 'default', 'set_up' => static function () { add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' ); }, + 'expected_load_separate' => false, 'expected_on_demand' => false, 'expected_buffer_started' => false, ), - 'classic_theme_with_block_styles_support' => array( + 'classic_theme_with_should_load_separate_core_block_assets_opt_out' => array( 'theme' => 'default', 'set_up' => static function () { - add_theme_support( 'wp-block-styles' ); + add_filter( 'should_load_separate_core_block_assets', '__return_false' ); }, + 'expected_load_separate' => false, 'expected_on_demand' => true, - 'expected_buffer_started' => true, + 'expected_buffer_started' => false, ), - 'classic_theme_without_block_styles_support' => array( + 'classic_theme_with_should_load_block_assets_on_demand_out_out' => array( 'theme' => 'default', 'set_up' => static function () { - remove_theme_support( 'wp-block-styles' ); + add_filter( 'should_load_block_assets_on_demand', '__return_false' ); }, + 'expected_load_separate' => true, 'expected_on_demand' => false, - 'expected_buffer_started' => true, + 'expected_buffer_started' => false, ), ); } @@ -1436,7 +1440,7 @@ public function data_wp_load_classic_theme_block_styles_on_demand(): array { * * @dataProvider data_wp_load_classic_theme_block_styles_on_demand */ - public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_on_demand, bool $expected_buffer_started ) { + public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_load_separate, bool $expected_on_demand, bool $expected_buffer_started ) { $this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' ); $this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' ); $this->assertFalse( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action to be added for classic themes.' ); @@ -1447,8 +1451,9 @@ public function test_wp_load_classic_theme_block_styles_on_demand( string $theme } wp_load_classic_theme_block_styles_on_demand(); + _add_default_theme_supports(); - $this->assertSame( $expected_on_demand, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' ); + $this->assertSame( $expected_load_separate, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' ); $this->assertSame( $expected_on_demand, wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() return value.' ); $this->assertSame( $expected_buffer_started, (bool) has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ), 'Expected wp_template_enhancement_output_buffer_started action added status.' ); } From 9ed62e3570b91b1ac94cad0bcc1989145fbc4645 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 3 Nov 2025 23:25:42 -0800 Subject: [PATCH 2/4] Fix comment typos --- src/wp-includes/script-loader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 2123c12293dc0..5989ceaad8399 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -3596,7 +3596,7 @@ function wp_load_classic_theme_block_styles_on_demand() { return; } - // The following to filters are added by default for block themes in _add_default_theme_supports(). + // The following two filters are added by default for block themes in _add_default_theme_supports(). /* * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, @@ -3611,7 +3611,7 @@ function wp_load_classic_theme_block_styles_on_demand() { */ add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 ); - // If even with the filtering + // If a site has explicitly opted out of loading block styles on demand via filters with priorities higher than above, then abort. if ( ! wp_should_load_separate_core_block_assets() || ! wp_should_load_block_assets_on_demand() ) { return; } From e274e28f764e52a593d62eae8733820f1d6fbf05 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 3 Nov 2025 23:48:14 -0800 Subject: [PATCH 3/4] Add missing test case --- tests/phpunit/tests/template.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index 8f18061e8822e..2d96c71f8e54c 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -1427,6 +1427,13 @@ public function data_wp_load_classic_theme_block_styles_on_demand(): array { 'expected_on_demand' => false, 'expected_buffer_started' => false, ), + 'classic_theme_without_any_opt_out' => array( + 'theme' => 'default', + 'set_up' => static function () {}, + 'expected_load_separate' => true, + 'expected_on_demand' => true, + 'expected_buffer_started' => true, + ), ); } From 64d7d8ed8624eb6c89ea66a4fabb628923ee2d9c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 3 Nov 2025 23:50:54 -0800 Subject: [PATCH 4/4] Tweak comments --- src/wp-includes/script-loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 5989ceaad8399..7660f55aa027b 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -3591,7 +3591,7 @@ function wp_load_classic_theme_block_styles_on_demand() { */ add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 ); - // If a site has opted out of the template enhancement output buffer, then we must abort. + // If a site has opted out of the template enhancement output buffer, then bail. if ( ! wp_should_output_buffer_template_for_enhancement() ) { return; } @@ -3601,13 +3601,13 @@ function wp_load_classic_theme_block_styles_on_demand() { /* * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, * and so that block-specific styles will only be enqueued when they are used on the page. - * A priority of zero allows for this to be easily overridden by themes which wish to opt-out. + * A priority of zero allows for this to be easily overridden by themes which wish to opt out. */ add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 ); /* * Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets). - * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt-out. + * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt out. */ add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );