Skip to content

Commit e4bd8c7

Browse files
committed
Avoid enabling separate/on-demand block styles if theme does not support wp-block-styles
1 parent 87aa447 commit e4bd8c7

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/wp-includes/script-loader.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,13 +3593,21 @@ function wp_load_classic_theme_block_styles_on_demand() {
35933593
}
35943594

35953595
/*
3596-
* Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally,
3597-
* and so that block-specific styles will only be enqueued when they are used on the page.
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.
35983600
*/
3599-
add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 );
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 );
36003607

3601-
// Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets).
3602-
add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 );
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 );
3610+
}
36033611

36043612
// Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early.
36053613
add_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' );

tests/phpunit/tests/template.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
7878
*/
7979
protected $original_wp_styles;
8080

81+
/**
82+
* @var array|null
83+
*/
84+
protected $original_theme_features;
85+
8186
public function set_up() {
8287
parent::set_up();
8388
$this->original_default_mimetype = ini_get( 'default_mimetype' );
@@ -110,13 +115,17 @@ public function set_up() {
110115
$wp_styles = null;
111116
wp_scripts();
112117
wp_styles();
118+
119+
$this->original_theme_features = $GLOBALS['_wp_theme_features'];
113120
}
114121

115122
public function tear_down() {
116123
global $wp_scripts, $wp_styles;
117124
$wp_scripts = $this->original_wp_scripts;
118125
$wp_styles = $this->original_wp_styles;
119126

127+
$GLOBALS['_wp_theme_features'] = $this->original_theme_features;
128+
120129
ini_set( 'default_mimetype', $this->original_default_mimetype );
121130
unregister_post_type( 'cpt' );
122131
unregister_taxonomy( 'taxo' );
@@ -930,12 +939,35 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_the
930939
}
931940

932941
/**
933-
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks for classic themes.
942+
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks for classic theme which supports block styles.
943+
*
944+
* @ticket 64099
945+
* @ticket 64150
946+
* @covers ::wp_load_classic_theme_block_styles_on_demand
947+
*/
948+
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme_with_block_styles_support(): void {
949+
switch_theme( 'default' );
950+
add_theme_support( 'wp-block-styles' );
951+
952+
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
953+
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
954+
$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.' );
955+
956+
wp_load_classic_theme_block_styles_on_demand();
957+
958+
$this->assertTrue( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() filters to return true because the theme does support wp-block-styles' );
959+
$this->assertTrue( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true because the theme does support wp-block-styles' );
960+
$this->assertNotFalse( 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.' );
961+
}
962+
963+
/**
964+
* Tests that wp_load_classic_theme_block_styles_on_demand() adds the expected hooks for classic theme which does not support block styles.
934965
*
935966
* @ticket 64099
967+
* @ticket 64150
936968
* @covers ::wp_load_classic_theme_block_styles_on_demand
937969
*/
938-
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme(): void {
970+
public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_theme_without_block_styles_support(): void {
939971
switch_theme( 'default' );
940972

941973
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() to return false initially.' );
@@ -944,8 +976,8 @@ public function test_wp_load_classic_theme_block_styles_on_demand_in_classic_the
944976

945977
wp_load_classic_theme_block_styles_on_demand();
946978

947-
$this->assertTrue( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() filters to return true' );
948-
$this->assertTrue( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return true' );
979+
$this->assertFalse( wp_should_load_separate_core_block_assets(), 'Expected wp_should_load_separate_core_block_assets() filters to return false because the theme does not support wp-block-styles' );
980+
$this->assertFalse( wp_should_load_block_assets_on_demand(), 'Expected wp_should_load_block_assets_on_demand() to return false because the theme does not support wp-block-styles' );
949981
$this->assertNotFalse( 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.' );
950982
}
951983

0 commit comments

Comments
 (0)