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.