Skip to content

Commit

Permalink
Editor: Revert caching of global styles for blocks.
Browse files Browse the repository at this point in the history
This reverts [58334] to fix a bug where edits to block styles made in the site editor were not showing in the front end.

Props joemcgill, spacedmonkey, andrewserong, hellofromtonya, audrasjb.
See #59595.


git-svn-id: https://develop.svn.wordpress.org/trunk@58710 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
joemcgill committed Jul 12, 2024
1 parent ad135d5 commit 7f697bc
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 143 deletions.
42 changes: 1 addition & 41 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,44 +255,8 @@ 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 ) {

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 );
}
$block_css = $tree->get_styles_for_block( $metadata );

if ( ! wp_should_load_separate_core_block_assets() ) {
wp_add_inline_style( 'global-styles', $block_css );
Expand Down Expand Up @@ -338,10 +302,6 @@ function wp_add_global_styles_for_blocks() {
}
}
}

if ( $update_cache ) {
set_site_transient( $cache_key, $cached, HOUR_IN_SECONDS );
}
}

/**
Expand Down
102 changes: 0 additions & 102 deletions tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,87 +75,6 @@ public function test_third_party_blocks_inline_styles_get_registered_to_global_s
);
}

/**
* Ensure that the block cache is set for global styles.
*
* @ticket 59595
*/
public function test_styles_for_blocks_cache_is_set() {
$this->set_up_third_party_block();

wp_register_style( 'global-styles', false, array(), true, true );

$cache_key = $this->get_wp_styles_for_blocks_cache_key();
$styles_for_blocks_before = get_site_transient( $cache_key );
$this->assertFalse( $styles_for_blocks_before );

wp_add_global_styles_for_blocks();

$styles_for_blocks_after = get_site_transient( $cache_key );
$this->assertNotEmpty( $styles_for_blocks_after );
}

/**
* Confirm that the block cache is skipped when in dev mode for themes.
*
* @ticket 59595
*/
public function test_styles_for_blocks_skips_cache_in_dev_mode() {
global $_wp_tests_development_mode;

$orig_dev_mode = $_wp_tests_development_mode;

// Setting development mode to theme should skip the cache.
$_wp_tests_development_mode = 'theme';

wp_register_style( 'global-styles', false, array(), true, true );

// Initial register of global styles.
wp_add_global_styles_for_blocks();

$cache_key = $this->get_wp_styles_for_blocks_cache_key();
$styles_for_blocks_initial = get_site_transient( $cache_key );

// Cleanup.
$_wp_tests_development_mode = $orig_dev_mode;

$this->assertFalse( $styles_for_blocks_initial );
}

/**
* Confirm that the block cache is updated if the block meta has changed.
*
* @ticket 59595
*/
public function test_styles_for_blocks_cache_is_skipped() {
wp_register_style( 'global-styles', false, array(), true, true );

// Initial register of global styles.
wp_add_global_styles_for_blocks();

$cache_key = $this->get_wp_styles_for_blocks_cache_key();
$styles_for_blocks_initial = get_site_transient( $cache_key );
$this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' );

$this->set_up_third_party_block();

/*
* Call register of global styles again to ensure the cache is updated.
* In normal conditions, this function is only called once per request.
*/
wp_add_global_styles_for_blocks();

$cache_key = $this->get_wp_styles_for_blocks_cache_key();
$styles_for_blocks_updated = get_site_transient( $cache_key );
$this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' );

$this->assertNotEquals(
$styles_for_blocks_initial,
$styles_for_blocks_updated,
'Block style cache was not updated.'
);
}

/**
* @ticket 56915
* @ticket 61165
Expand Down Expand Up @@ -334,25 +253,4 @@ private function get_global_styles() {
$actual = wp_styles()->get_data( 'global-styles', 'after' );
return is_array( $actual ) ? $actual : array();
}

/**
* Get cache key for `wp_styles_for_blocks`.
*
* @return string The cache key.
*/
private function get_wp_styles_for_blocks_cache_key() {
$tree = WP_Theme_JSON_Resolver::get_merged_data();
$block_nodes = $tree->get_styles_block_nodes();
// md5 is a costly operation, so we hashing global settings and block_node in a single call.
$hash = md5(
wp_json_encode(
array(
'global_setting' => wp_get_global_settings(),
'block_nodes' => $block_nodes,
)
)
);

return "wp_styles_for_blocks:$hash";
}
}

0 comments on commit 7f697bc

Please sign in to comment.