|
17 | 17 | * @return void |
18 | 18 | */ |
19 | 19 | function gutenberg_enqueue_global_styles() { |
20 | | - $separate_assets = wp_should_load_separate_core_block_assets(); |
| 20 | + $assets_on_demand = wp_should_load_block_assets_on_demand(); |
21 | 21 | $is_block_theme = wp_is_block_theme(); |
22 | 22 | $is_classic_theme = ! $is_block_theme; |
23 | 23 |
|
24 | | - /* |
25 | | - * Global styles should be printed in the head when loading all styles combined. |
26 | | - * The footer should only be used to print global styles for classic themes with separate core assets enabled. |
| 24 | + /** |
| 25 | + * Global styles should be printed in the HEAD for block themes, or for classic themes when loading assets on |
| 26 | + * demand is disabled (which is no longer the default since WordPress 6.9). |
27 | 27 | * |
28 | | - * See https://core.trac.wordpress.org/ticket/53494. |
| 28 | + * @link https://core.trac.wordpress.org/ticket/53494 |
| 29 | + * @link https://core.trac.wordpress.org/ticket/61965 |
29 | 30 | */ |
30 | 31 | if ( |
31 | | - ( $is_block_theme && doing_action( 'wp_footer' ) ) || |
32 | | - ( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) || |
33 | | - ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets ) |
| 32 | + doing_action( 'wp_footer' ) && |
| 33 | + ( |
| 34 | + $is_block_theme || |
| 35 | + ( $is_classic_theme && ! $assets_on_demand ) |
| 36 | + ) |
34 | 37 | ) { |
35 | 38 | return; |
36 | 39 | } |
37 | 40 |
|
| 41 | + /** |
| 42 | + * The footer should only be used for classic themes when loading assets on demand is enabled. In WP 6.9 this is the |
| 43 | + * default with the introduction of hoisting late-printed styles (via {@see wp_load_classic_theme_block_styles_on_demand()}). |
| 44 | + * So even though the main global styles are not printed here in the HEAD for classic themes with on-demand asset |
| 45 | + * loading, a placeholder for the global styles is still enqueued. Then when {@see wp_hoist_late_printed_styles()} |
| 46 | + * processes the output buffer, it can locate the placeholder and inject the global styles from the footer into the |
| 47 | + * HEAD, replacing the placeholder. |
| 48 | + * |
| 49 | + * @link https://core.trac.wordpress.org/ticket/64099 |
| 50 | + */ |
| 51 | + if ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $assets_on_demand ) { |
| 52 | + if ( has_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' ) ) { |
| 53 | + wp_register_style( 'wp-global-styles-placeholder', false ); |
| 54 | + wp_add_inline_style( 'wp-global-styles-placeholder', ':root { --wp-internal-comment: "Placeholder for wp_hoist_late_printed_styles() to replace with the global-styles printed at wp_footer." }' ); |
| 55 | + wp_enqueue_style( 'wp-global-styles-placeholder' ); |
| 56 | + } |
| 57 | + return; |
| 58 | + } |
| 59 | + |
38 | 60 | /* |
39 | 61 | * If loading the CSS for each block separately, then load the theme.json CSS conditionally. |
40 | 62 | * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. |
|
0 commit comments