Script Loader: Restore init hook so block theme styles load on demand in classic themes#11887
Script Loader: Restore init hook so block theme styles load on demand in classic themes#11887itzmekhokan wants to merge 4 commits into
Conversation
…mand. Register on-demand block asset filters at init priority 8 again so register_core_block_style_handles() can opt in before WP_Styles is constructed. Keep the wp_default_styles hook from [61981] for early WP_Styles instantiation and guard against duplicate hook registration. Fixes #65272.
…mand. Props Khokan Sardar. Fixes #65272.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Does this cause a regression in what r61981 was committed to fix Core-64846? |
| * wish to stream responses can more easily turn this off. | ||
| */ | ||
| add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 ); | ||
| if ( ! has_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true' ) ) { |
There was a problem hiding this comment.
This doesn't seem it will work properly. The has_filter() function returns false|int, with the int being the priority. Since the priority 0 is falsey, this will still end up adding the filter multiple times. So you'd need to instead do:
| if ( ! has_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true' ) ) { | |
| if ( false === has_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true' ) ) { |
| * separate block styles, then abort. | ||
| */ | ||
| add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 ); | ||
| if ( ! has_filter( 'should_load_separate_core_block_assets', '__return_true' ) ) { |
There was a problem hiding this comment.
| if ( ! has_filter( 'should_load_separate_core_block_assets', '__return_true' ) ) { | |
| if ( false === has_filter( 'should_load_separate_core_block_assets', '__return_true' ) ) { |
| * has explicitly opted out of loading block styles on demand, then abort. | ||
| */ | ||
| add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 ); | ||
| if ( ! has_filter( 'should_load_block_assets_on_demand', '__return_true' ) ) { |
There was a problem hiding this comment.
| if ( ! has_filter( 'should_load_block_assets_on_demand', '__return_true' ) ) { | |
| if ( false === has_filter( 'should_load_block_assets_on_demand', '__return_true' ) ) { |
|
|
||
| // Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early. | ||
| add_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ); | ||
| if ( ! has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) { |
There was a problem hiding this comment.
| if ( ! has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) { | |
| if ( false === has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) { |
…hooks. has_filter() and has_action() return the priority when a callback is registered, so priority 0 must be compared with false === instead of !. Adds regression tests for duplicate hook registration and for #64846 when WP_Styles is constructed before init. Props westonruter. Fixes #65272.
Thanks @westonruter — good catch on has_filter() / has_action() returning priority 0, which is falsy. Updated all four checks to use false ===. Re #64846: this should not regress r61981 because the wp_default_styles priority 0 hook is retained. That hook ensures filters are present when WP_Styles is constructed before init priority 9, so wp-block-library still registers as common.css. |
What
Restores the
initpriority 8 hook forwp_load_classic_theme_block_styles_on_demand()while keeping thewp_default_stylespriority 0 hook added in [61981]. Adds guards so filters and actions are not registered twice when the function runs from both hooks.Why
In WordPress 7.0, commit [61981] moved
wp_load_classic_theme_block_styles_on_demand()frominitpriority 8 towp_default_stylespriority 0 only. That introduced a regression (#65272):register_core_block_style_handles()runs atinitpriority 9.wp_should_load_separate_core_block_assets()before callingwp_styles().wp_default_styleshook, those filters are not added untilWP_Stylesis first constructed.register_core_block_style_handles()returns early, so per-block opinionated styles (blocks/*/theme.css, e.g.wp-block-quote-theme) are never registered or enqueued on the frontend.Symptom: In classic themes with
wp-block-stylessupport (e.g. Twenty Fifteen), Quote block right alignment works in the editor but the border stays on the left on the frontend becausetheme.cssnever loads.The
wp_default_styleshook remains so filters are still present whenWP_Stylesis constructed beforeinitpriority 9 (#64846).How
add_action( 'init', 'wp_load_classic_theme_block_styles_on_demand', 8 )indefault-filters.php.add_action( 'wp_default_styles', 'wp_load_classic_theme_block_styles_on_demand', 0 ).has_filter()/has_action()before adding hooks to avoid duplicate registration.Tests_Blocks_ClassicThemeBlockStylesOnDemand::test_register_core_block_style_handles_without_prior_wp_styles.Trac ticket: https://core.trac.wordpress.org/ticket/65272