-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sure base global styles are loaded before block styles. #5892
Changes from 5 commits
b643d7a
83367d8
00dc2c3
48ce988
be210d6
aef490b
0a7045a
bef9311
9010293
6d050e0
ca1698e
1226e21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -596,9 +596,11 @@ | |
add_filter( 'block_editor_settings_all', 'wp_add_editor_classic_theme_styles' ); | ||
|
||
// Global styles can be enqueued in both the header and the footer. See https://core.trac.wordpress.org/ticket/53494. | ||
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); | ||
add_action( 'init', 'wp_enqueue_global_styles', 1 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @scruffian @ajlende @aristath as they may have the most experience with all the scenarios, IIRC. I couldn't experiment/test this change because my wordpress-develop env wouldn't start up, so I'm trying to recall from memory / skimming through the code. There are two scenarios: the core blocks styles are loaded with a separate stylesheet or bundled as part of the block library. As I understand it, the order of styles after this change would be Single stylesheetBefore:
After
Separate stylesheetsBefore:
After:
Is this the goal / what happens? Notes:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change LGTM. Of course "global" styles should always be loaded before "local" styles :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for looking into this!
I tried and it didn't work, I think because @oandregal the separate stylesheets scenario you outline is correct; I'll need to double-check the single stylesheet but I think it's also right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is incorrect, it lacks a bit of nuance. Styles is a very loaded domain, the same words mean different things to people. It's important to me that we all share the same context, and clarify what we mean. This is how it works now:
My understanding is that we want to do this instead:
The top-level global styles should be: presets + some styles that are attached to the I mentioned above that I see still some "block styles" loaded in the "separate stylesheet" scenario (I understand this is a bug): with this change, they'll be enqueued in 1, and can create conflicts because they should be enqueued in 3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeaaa, can see that too :) In my mind "global styles" means styles that apply to all similar elements, for example blocks, or image blocks, or paragraph blocks, etc. "Local styles" means styles that apply to only one of many elements. For example only to one image block when there are 5 on the page, etc. Thinking it would be great to add some explanations in comments at the top of all CSS files in an attempt to get everybody on the same page. Can also be added in docblocks where the CSS is outputted from JS or PHP. Another idea might be to drop the "global" and "local" terms as inadequate and use something a bit more granular by specificity. Perhaps "Top level" "Level 1", "Level 2", etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, this is the documentation we have for styles (naming, how they work, etc.). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I looked at this carefully and can confirm the following order of styles: Single stylesheettrunk
this PR
Separate stylesheetstrunk
this PR
So your assessment is essentially correct @oandregal. I think the only suboptimal change in this PR is in the single stylesheet scenario, where block library CSS is now loaded after block-specific global styles. It should be the other way around, but ideally base global styles would also be added before block library styles. I'll look into splitting base and block-specific global styles into separate style tags as a workaround.
I'm probably missing something, but I don't see those styles appearing at all either on trunk or this PR branch. Tested by adding a Pullquote block to a page and checking that a line-height of
Those extra block styles added to
Good point. You mean they should be enqueued after the block library styles, right? I'll look into it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh, thanks for sharing. I think this is a bug: block styles should always be together. Why |
||
add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 ); | ||
|
||
add_action( 'wp_enqueue_scripts', 'wp_add_global_styles_for_blocks' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this add the block-level global styles twice to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh that's a good point, I'll have to test that scenario better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just confirmed and there's no duplication of block-level global styles in the single stylesheet scenario. I removed it from where it was called inside |
||
|
||
// Global styles custom CSS. | ||
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' ); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,6 +294,74 @@ function wp_get_global_styles_custom_css() { | |
return $stylesheet; | ||
} | ||
|
||
/** | ||
* Gets the global styles base custom CSS from theme.json. | ||
* Logic should follow wp_get_global_styles_custom_css. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @return string The global base custom CSS. | ||
*/ | ||
function wp_get_global_styles_base_custom_css() { | ||
if ( ! wp_theme_has_theme_json() ) { | ||
return ''; | ||
} | ||
|
||
$can_use_cached = ! wp_is_development_mode( 'theme' ); | ||
|
||
$cache_key = 'wp_get_global_styles_base_custom_css'; | ||
$cache_group = 'theme_json'; | ||
if ( $can_use_cached ) { | ||
$cached = wp_cache_get( $cache_key, $cache_group ); | ||
if ( $cached ) { | ||
return $cached; | ||
} | ||
} | ||
|
||
$tree = WP_Theme_JSON_Resolver::get_merged_data(); | ||
$stylesheet = $tree->get_custom_base_css(); | ||
|
||
if ( $can_use_cached ) { | ||
wp_cache_set( $cache_key, $stylesheet, $cache_group ); | ||
} | ||
|
||
return $stylesheet; | ||
} | ||
|
||
/** | ||
* Gets the global styles per-block custom CSS from theme.json. | ||
* Logic should follow wp_get_global_styles_custom_css. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @return string The global per-block custom CSS. | ||
*/ | ||
function wp_get_global_styles_block_custom_css() { | ||
if ( ! wp_theme_has_theme_json() ) { | ||
return ''; | ||
} | ||
|
||
$can_use_cached = ! wp_is_development_mode( 'theme' ); | ||
|
||
$cache_key = 'wp_get_global_styles_block_custom_css'; | ||
$cache_group = 'theme_json'; | ||
if ( $can_use_cached ) { | ||
$cached = wp_cache_get( $cache_key, $cache_group ); | ||
if ( $cached ) { | ||
return $cached; | ||
} | ||
} | ||
|
||
$tree = WP_Theme_JSON_Resolver::get_merged_data(); | ||
$stylesheet = $tree->get_custom_block_css(); | ||
|
||
if ( $can_use_cached ) { | ||
wp_cache_set( $cache_key, $stylesheet, $cache_group ); | ||
} | ||
|
||
return $stylesheet; | ||
} | ||
|
||
/** | ||
* Adds global style rules to the inline style for each block. | ||
* | ||
|
@@ -302,11 +370,15 @@ function wp_get_global_styles_custom_css() { | |
function wp_add_global_styles_for_blocks() { | ||
$tree = WP_Theme_JSON_Resolver::get_merged_data(); | ||
$block_nodes = $tree->get_styles_block_nodes(); | ||
|
||
if ( ! wp_should_load_separate_core_block_assets() ) { | ||
wp_register_style( 'global-styles-blocks', false ); | ||
} | ||
foreach ( $block_nodes as $metadata ) { | ||
$block_css = $tree->get_styles_for_block( $metadata ); | ||
|
||
if ( ! wp_should_load_separate_core_block_assets() ) { | ||
wp_add_inline_style( 'global-styles', $block_css ); | ||
wp_add_inline_style( 'global-styles-blocks', $block_css ); | ||
continue; | ||
} | ||
|
||
|
@@ -336,6 +408,10 @@ function wp_add_global_styles_for_blocks() { | |
} | ||
} | ||
} | ||
|
||
if ( ! wp_should_load_separate_core_block_assets() ) { | ||
wp_enqueue_style( 'global-styles-blocks' ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this should be enqueued here (or registered above) as it's a rather large change for a public function that just used to add styles. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm good point, I'll see if I can find a more appropriate place. |
||
} | ||
|
||
/** | ||
|
@@ -431,6 +507,8 @@ function wp_clean_theme_json_cache() { | |
wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' ); | ||
wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); | ||
wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); | ||
wp_cache_delete( 'wp_get_global_styles_base_custom_css', 'theme_json' ); | ||
wp_cache_delete( 'wp_get_global_styles_block_custom_css', 'theme_json' ); | ||
wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); | ||
WP_Theme_JSON_Resolver::clean_cached_data(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old
get_custom_css
function should be refactored to call these two to make it clear where the code came from.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to pull these changes out and do them in a separate PR.