Skip to content

Commit

Permalink
Backport: Caching of global styles for blocks from core (WordPress#66349
Browse files Browse the repository at this point in the history
)

Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: ramonjd <[email protected]>
  • Loading branch information
3 people authored and karthick-murugan committed Nov 13, 2024
1 parent 58bd7be commit 49ffaf4
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion lib/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function gutenberg_get_global_stylesheet( $types = array() ) {
* @see wp_add_global_styles_for_blocks
*/
$origins = array( 'default', 'theme', 'custom' );

/*
* If the theme doesn't have theme.json but supports both appearance tools and color palette,
* the 'theme' origin should be included so color palette presets are also output.
Expand Down Expand Up @@ -260,15 +261,53 @@ function gutenberg_add_global_styles_for_blocks() {
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$tree = WP_Theme_JSON_Resolver_Gutenberg::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 );
continue;
}

$stylesheet_handle = 'global-styles';

/*
* When `wp_should_load_separate_core_block_assets()` is true, block styles are
* enqueued for each block on the page in class WP_Block's render function.
Expand Down Expand Up @@ -306,6 +345,10 @@ function gutenberg_add_global_styles_for_blocks() {
}
}
}

if ( $update_cache ) {
set_transient( $cache_key, $cached );
}
}

/**
Expand Down

0 comments on commit 49ffaf4

Please sign in to comment.