From 1c4a732f5fd49f86995e34df5688b4123675126b Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 4 Jun 2024 14:52:13 +0000 Subject: [PATCH] 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. Props thekt12, spacedmonkey, pereirinha, mukesh27, isabel_brison, oandregal, andrewserong, ramonjd. Fixes #59595. Built from https://develop.svn.wordpress.org/trunk@58334 git-svn-id: http://core.svn.wordpress.org/trunk@57790 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/global-styles-and-settings.php | 42 +++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index fbf4fe2c52..b413273a64 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/wp-includes/global-styles-and-settings.php @@ -307,8 +307,44 @@ function wp_add_global_styles_for_blocks() { $tree = WP_Theme_JSON_Resolver::get_merged_data(); $block_nodes = $tree->get_styles_block_nodes(); + + $can_use_cached = ! wp_is_development_mode( 'theme' ); + if ( $can_use_cached ) { + // Hash global settings and block nodes together to optimize performance of key generation. + $hash = md5( + wp_json_encode( + array( + 'global_setting' => wp_get_global_settings(), + 'block_nodes' => $block_nodes, + ) + ) + ); + + $cache_key = "wp_styles_for_blocks:$hash"; + $cached = get_site_transient( $cache_key ); + if ( ! is_array( $cached ) ) { + $cached = array(); + } + } + + $update_cache = false; + foreach ( $block_nodes as $metadata ) { - $block_css = $tree->get_styles_for_block( $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[ $cache_node_key ] ) ) { + $block_css = $cached[ $cache_node_key ]; + } else { + $block_css = $tree->get_styles_for_block( $metadata ); + $cached[ $cache_node_key ] = $block_css; + $update_cache = true; + } + } else { + $block_css = $tree->get_styles_for_block( $metadata ); + } if ( ! wp_should_load_separate_core_block_assets() ) { wp_add_inline_style( 'global-styles', $block_css ); @@ -354,6 +390,10 @@ function wp_add_global_styles_for_blocks() { } } } + + if ( $update_cache ) { + set_site_transient( $cache_key, $cached, HOUR_IN_SECONDS ); + } } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index e68f3c08ac..ace5ed3e94 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6-alpha-58333'; +$wp_version = '6.6-alpha-58334'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.