Skip to content

Commit a5d3f19

Browse files
committed
Remove wp-block-styles theme support precondition for loading block styles on demand
1 parent 5b03f1f commit a5d3f19

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/wp-includes/script-loader.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,8 +3575,11 @@ function wp_remove_surrounding_empty_script_tags( $contents ) {
35753575
* Adds hooks to load block styles on demand in classic themes.
35763576
*
35773577
* @since 6.9.0
3578+
*
3579+
* @see _add_default_theme_supports()
35783580
*/
35793581
function wp_load_classic_theme_block_styles_on_demand() {
3582+
// This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports().
35803583
if ( wp_is_block_theme() ) {
35813584
return;
35823585
}
@@ -3588,25 +3591,29 @@ function wp_load_classic_theme_block_styles_on_demand() {
35883591
*/
35893592
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 );
35903593

3594+
// If a site has opted out of the template enhancement output buffer, then we must abort.
35913595
if ( ! wp_should_output_buffer_template_for_enhancement() ) {
35923596
return;
35933597
}
35943598

3599+
// The following to filters are added by default for block themes in _add_default_theme_supports().
3600+
35953601
/*
3596-
* If the theme supports block styles, add filters to ensure they are loaded separately and on demand. Without this,
3597-
* if a theme does not want or support block styles, then enabling these filters can result in undesired separate
3598-
* block-specific styles being enqueued, though a theme may also be trying to nullify the wp-block-library
3599-
* stylesheet.
3602+
* Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3603+
* and so that block-specific styles will only be enqueued when they are used on the page.
3604+
* A priority of zero allows for this to be easily overridden by themes which wish to opt-out.
36003605
*/
3601-
if ( current_theme_supports( 'wp-block-styles' ) ) {
3602-
/*
3603-
* Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3604-
* and so that block-specific styles will only be enqueued when they are used on the page.
3605-
*/
3606-
add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
3606+
add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
36073607

3608-
// Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
3609-
add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
3608+
/*
3609+
* Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
3610+
* As above, a priority of zero allows for this to be easily overridden by themes which wish to opt-out.
3611+
*/
3612+
add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
3613+
3614+
// If even with the filtering
3615+
if ( ! wp_should_load_separate_core_block_assets() || ! wp_should_load_block_assets_on_demand() ) {
3616+
return;
36103617
}
36113618

36123619
// Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.

tests/phpunit/tests/template.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,35 +1393,39 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_block_theme
13931393
*/
13941394
public function data_wp_load_classic_theme_block_styles_on_demand(): array {
13951395
return array(
1396-
'block_theme' => array(
1396+
'block_theme' => array(
13971397
'theme' => 'block-theme',
13981398
'set_up' => static function () {},
1399-
'expected_on_demand' => false,
1399+
'expected_load_separate' => true,
1400+
'expected_on_demand' => true,
14001401
'expected_buffer_started' => false,
14011402
),
1402-
'classic_theme_with_output_buffer_blocked' => array(
1403+
'classic_theme_with_output_buffer_blocked' => array(
14031404
'theme' => 'default',
14041405
'set_up' => static function () {
14051406
add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_false' );
14061407
},
1408+
'expected_load_separate' => false,
14071409
'expected_on_demand' => false,
14081410
'expected_buffer_started' => false,
14091411
),
1410-
'classic_theme_with_block_styles_support' => array(
1412+
'classic_theme_with_should_load_separate_core_block_assets_opt_out' => array(
14111413
'theme' => 'default',
14121414
'set_up' => static function () {
1413-
add_theme_support( 'wp-block-styles' );
1415+
add_filter( 'should_load_separate_core_block_assets', '__return_false' );
14141416
},
1417+
'expected_load_separate' => false,
14151418
'expected_on_demand' => true,
1416-
'expected_buffer_started' => true,
1419+
'expected_buffer_started' => false,
14171420
),
1418-
'classic_theme_without_block_styles_support' => array(
1421+
'classic_theme_with_should_load_block_assets_on_demand_out_out' => array(
14191422
'theme' => 'default',
14201423
'set_up' => static function () {
1421-
remove_theme_support( 'wp-block-styles' );
1424+
add_filter( 'should_load_block_assets_on_demand', '__return_false' );
14221425
},
1426+
'expected_load_separate' => true,
14231427
'expected_on_demand' => false,
1424-
'expected_buffer_started' => true,
1428+
'expected_buffer_started' => false,
14251429
),
14261430
);
14271431
}
@@ -1436,7 +1440,7 @@ public function data_wp_load_classic_theme_block_styles_on_demand(): array {
14361440
*
14371441
* @dataProvider data_wp_load_classic_theme_block_styles_on_demand
14381442
*/
1439-
public function test_wp_load_classic_theme_block_styles_on_demand( string $theme, ?Closure $set_up, bool $expected_on_demand, bool $expected_buffer_started ) {
1443+
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 ) {
14401444
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
14411445
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
14421446
$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
14471451
}
14481452

14491453
wp_load_classic_theme_block_styles_on_demand();
1454+
_add_default_theme_supports();
14501455

1451-
$this->assertSame( $expected_on_demand, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' );
1456+
$this->assertSame( $expected_load_separate, wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() return value.' );
14521457
$this->assertSame( $expected_on_demand, wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() return value.' );
14531458
$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.' );
14541459
}

0 commit comments

Comments
 (0)