From 5c8c27b24b10f6bbd39336e8b7bb1684ae712577 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 18 Oct 2024 21:55:18 +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. 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 --- wp-includes/global-styles-and-settings.php | 44 +++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index f40d4a5e98..34fc28694b 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/wp-includes/global-styles-and-settings.php @@ -257,8 +257,46 @@ function wp_add_global_styles_for_blocks() { $tree = WP_Theme_JSON_Resolver::get_merged_data(); $tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree ); $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 ) { - $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['blocks'][ $cache_node_key ] ) ) { + $block_css = $cached['blocks'][ $cache_node_key ]; + } else { + $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() ) { 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 ); + } } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index ec211019a9..dbc1475c48 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.