-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Global Styles: Load block CSS conditionally (#41160)
* Global Styles: Load block styles as part of the block CSS * Global Styles: Load block styles as part of the block CSS * tidy up * fix PHP linting * add comments
- Loading branch information
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/compat/wordpress-6.1/get-global-styles-and-settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* API to interact with global settings & styles. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Adds global style rules to the inline style for each block. | ||
*/ | ||
function wp_add_global_styles_for_blocks() { | ||
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); | ||
// TODO some nodes dont have a name... | ||
$block_nodes = $tree->get_styles_block_nodes(); | ||
foreach ( $block_nodes as $metadata ) { | ||
$block_css = $tree->get_styles_for_block( $metadata ); | ||
$block_name = str_replace( 'core/', '', $metadata['name'] ); | ||
// These block styles are added on block_render. | ||
// This hooks inline CSS to them so that they are loaded conditionally | ||
// based on whether or not the block is used on the page. | ||
wp_add_inline_style( 'wp-block-' . $block_name, $block_css ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Load global styles assets in the front-end. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* This applies a filter to the list of style nodes that comes from `get_style_nodes` in WP_Theme_JSON. | ||
* This particular filter removes all of the blocks from the array. | ||
* | ||
* We want WP_Theme_JSON to be ignorant of the implementation details of how the CSS is being used. | ||
* This filter allows us to modify the output of WP_Theme_JSON depending on whether or not we are loading separate assets, | ||
* without making the class aware of that detail. | ||
* | ||
* @since 6.1 | ||
* | ||
* @param array $nodes The nodes to filter. | ||
* @return array A filtered array of style nodes. | ||
*/ | ||
function filter_out_block_nodes( $nodes ) { | ||
return array_filter( | ||
$nodes, | ||
function( $node ) { | ||
return ! in_array( 'blocks', $node['path'], true ); | ||
}, | ||
ARRAY_FILTER_USE_BOTH | ||
); | ||
} | ||
|
||
/** | ||
* Enqueues the global styles defined via theme.json. | ||
* This should replace wp_enqueue_global_styles. | ||
* | ||
* @since 5.8.0 | ||
*/ | ||
function gutenberg_enqueue_global_styles() { | ||
$separate_assets = wp_should_load_separate_core_block_assets(); | ||
$is_block_theme = wp_is_block_theme(); | ||
$is_classic_theme = ! $is_block_theme; | ||
|
||
/* | ||
* Global styles should be printed in the head when loading all styles combined. | ||
* The footer should only be used to print global styles for classic themes with separate core assets enabled. | ||
* | ||
* See https://core.trac.wordpress.org/ticket/53494. | ||
*/ | ||
if ( | ||
( $is_block_theme && doing_action( 'wp_footer' ) ) || | ||
( $is_classic_theme && doing_action( 'wp_footer' ) && ! $separate_assets ) || | ||
( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) && $separate_assets ) | ||
) { | ||
return; | ||
} | ||
|
||
/** | ||
* If we are loading CSS for each block separately, then we can load the theme.json CSS conditionally. | ||
* This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. | ||
*/ | ||
if ( $separate_assets ) { | ||
add_filter( 'gutenberg_get_style_nodes', 'filter_out_block_nodes' ); | ||
// add each block as an inline css. | ||
wp_add_global_styles_for_blocks(); | ||
} | ||
|
||
$stylesheet = gutenberg_get_global_stylesheet(); | ||
|
||
if ( empty( $stylesheet ) ) { | ||
return; | ||
} | ||
|
||
wp_register_style( 'global-styles', false, array(), true, true ); | ||
wp_add_inline_style( 'global-styles', $stylesheet ); | ||
wp_enqueue_style( 'global-styles' ); | ||
} | ||
|
||
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); | ||
remove_action( 'wp_footer', 'wp_enqueue_global_styles' ); | ||
remove_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_assets' ); | ||
remove_action( 'wp_footer', 'gutenberg_enqueue_global_styles_assets' ); | ||
|
||
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' ); | ||
add_action( 'wp_footer', 'gutenberg_enqueue_global_styles', 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters