Skip to content

Commit

Permalink
Cache for a minute using a transient (#25680)
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath authored Nov 13, 2020
1 parent db96c4e commit 8e4b94d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,24 @@ function gutenberg_experimental_global_styles_get_preset_classes( $selector, $se
* @return string Stylesheet.
*/
function gutenberg_experimental_global_styles_get_stylesheet( $tree ) {

// Check if we can use cached.
$can_use_cached = (
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
! is_admin()
);

if ( $can_use_cached ) {

// Check if we have the styles already cached.
$cached = get_transient( 'global_styles' );
if ( $cached ) {
return $cached;
}
}

$stylesheet = '';
$block_data = gutenberg_experimental_global_styles_get_block_data();
foreach ( array_keys( $tree ) as $block_name ) {
Expand Down Expand Up @@ -828,6 +846,13 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree ) {
$stylesheet .= 'a{color:var(--wp--style--color--link, #00e);}';
}

if ( $can_use_cached ) {

// Cache for a minute.
// This cache doesn't need to be any longer, we only want to avoid spikes on high-trafic sites.
set_transient( 'global_styles', $stylesheet, MINUTE_IN_SECONDS );
}

return $stylesheet;
}

Expand Down

0 comments on commit 8e4b94d

Please sign in to comment.