Editor: Cache global styles for blocks.

This caches the generated CSS from block nodes in merged Theme JSON data to avoid repeated costly operations required to compute style properties for blocks. The generated CSS is saved to a transient that expires every hour.

This is a follow-up that reimplements [58334], which was previously reverted in [58710].

Props thekt12, spacedmonkey, pereirinha, mukesh27, isabel_brison, oandregal, andrewserong, ramonjd, joemcgill, costdev, aaronrobertshaw, peterwilsoncc.
Fixes #61679. See #59595.

Built from https://develop.svn.wordpress.org/trunk@59256


git-svn-id: http://core.svn.wordpress.org/trunk@58648 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2024-10-18 21:55:18 +00:00
parent 51868a5e8c
commit 5c8c27b24b
2 changed files with 44 additions and 2 deletions

View File

@ -257,8 +257,46 @@ function wp_add_global_styles_for_blocks() {
$tree = WP_Theme_JSON_Resolver::get_merged_data(); $tree = WP_Theme_JSON_Resolver::get_merged_data();
$tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree ); $tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree );
$block_nodes = $tree->get_styles_block_nodes(); $block_nodes = $tree->get_styles_block_nodes();
$can_use_cached = ! wp_is_development_mode( 'theme' );
$update_cache = false;
if ( $can_use_cached ) {
// Hash the merged WP_Theme_JSON data to bust cache on settings or styles change.
$cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) );
$cache_key = 'wp_styles_for_blocks';
$cached = get_transient( $cache_key );
// Reset the cached data if there is no value or if the hash has changed.
if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) {
$cached = array(
'hash' => $cache_hash,
'blocks' => array(),
);
// Update the cache if the hash has changed.
$update_cache = true;
}
}
foreach ( $block_nodes as $metadata ) { foreach ( $block_nodes as $metadata ) {
if ( $can_use_cached ) {
// Use the block name as the key for cached CSS data. Otherwise, use a hash of the metadata.
$cache_node_key = isset( $metadata['name'] ) ? $metadata['name'] : md5( wp_json_encode( $metadata ) );
if ( isset( $cached['blocks'][ $cache_node_key ] ) ) {
$block_css = $cached['blocks'][ $cache_node_key ];
} else {
$block_css = $tree->get_styles_for_block( $metadata ); $block_css = $tree->get_styles_for_block( $metadata );
$cached['blocks'][ $cache_node_key ] = $block_css;
// Update the cache if the cache contents have changed.
$update_cache = true;
}
} else {
$block_css = $tree->get_styles_for_block( $metadata );
}
if ( ! wp_should_load_separate_core_block_assets() ) { if ( ! wp_should_load_separate_core_block_assets() ) {
wp_add_inline_style( 'global-styles', $block_css ); wp_add_inline_style( 'global-styles', $block_css );
@ -304,6 +342,10 @@ function wp_add_global_styles_for_blocks() {
} }
} }
} }
if ( $update_cache ) {
set_transient( $cache_key, $cached );
}
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-beta3-59255'; $wp_version = '6.7-beta3-59256';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.