Skip to content

Commit f29f594

Browse files
committed
Script Loader: Move wp_load_classic_theme_block_styles_on_demand() from init to wp_default_styles.
This ensures the filters to opt in to loading separate block styles on demand are added at the moment `WP_Styles` is constructed. This accounts for styles being registered at the `init` action before `register_core_block_style_handles()` runs at priority 9. Without this, the `wp-block-library` stylesheet may get registered with the full combined block styles as `style.css` instead of just `common.css`, due to `wp_should_load_block_assets_on_demand()` still returning false. The `wp_default_styles` action still runs during `init`. Developed in #11232 Follow-up to r61008. Props westonruter, adamsilverstein. See #64099. Fixes #64846. git-svn-id: https://develop.svn.wordpress.org/trunk@61981 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d577970 commit f29f594

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/wp-includes/default-filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@
607607
add_action( 'enqueue_block_assets', 'wp_enqueue_classic_theme_styles' );
608608
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
609609
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
610-
add_action( 'init', 'wp_load_classic_theme_block_styles_on_demand', 8 ); // Must happen before register_core_block_style_handles() at priority 9.
610+
add_action( 'wp_default_styles', 'wp_load_classic_theme_block_styles_on_demand', 0 ); // Must happen before wp_default_styles() and register_core_block_style_handles().
611611
/*
612612
* `wp_enqueue_registered_block_scripts_and_styles` is bound to both
613613
* `enqueue_block_editor_assets` and `enqueue_block_assets` hooks

src/wp-includes/script-loader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3674,11 +3674,15 @@ function wp_remove_surrounding_empty_script_tags( $contents ) {
36743674
/**
36753675
* Adds hooks to load block styles on demand in classic themes.
36763676
*
3677+
* This function must be called before {@see wp_default_styles()} and {@see register_core_block_style_handles()} so that
3678+
* the filters are added to cause {@see wp_should_load_separate_core_block_assets()} to return true.
3679+
*
36773680
* @since 6.9.0
3681+
* @since 7.0.0 This is now invoked at the `wp_default_styles` action with priority 0 instead of at `init` with priority 8.
36783682
*
36793683
* @see _add_default_theme_supports()
36803684
*/
3681-
function wp_load_classic_theme_block_styles_on_demand() {
3685+
function wp_load_classic_theme_block_styles_on_demand(): void {
36823686
// This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports().
36833687
if ( wp_is_block_theme() ) {
36843688
return;

tests/phpunit/tests/template.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,10 @@ public function test_wp_hoist_late_printed_styles( ?Closure $set_up, string $con
19631963
// `_print_emoji_detection_script()` assumes `wp-includes/js/wp-emoji-loader.js` is present:
19641964
self::touch( ABSPATH . WPINC . '/js/wp-emoji-loader.js' );
19651965

1966+
if ( $set_up ) {
1967+
$set_up();
1968+
}
1969+
19661970
switch_theme( 'default' );
19671971
global $wp_styles;
19681972
$wp_styles = null;
@@ -2001,10 +2005,6 @@ static function () {
20012005
}
20022006
);
20032007

2004-
if ( $set_up ) {
2005-
$set_up();
2006-
}
2007-
20082008
wp_load_classic_theme_block_styles_on_demand();
20092009

20102010
// Ensure that separate core block assets get registered.

0 commit comments

Comments
 (0)