Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for PR 6392 #6701

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b7ba582
add caching
kt-12 Apr 15, 2024
3bba97f
transient implementation
kt-12 Apr 15, 2024
97806ea
Improve caching key computation
kt-12 Apr 16, 2024
341e218
improving performace for non persistance cache.
kt-12 Apr 16, 2024
9c92f65
cache related call only for non development mode
kt-12 Apr 16, 2024
cce7c9a
cache clear code
kt-12 Apr 16, 2024
7c7bcec
change cache invalidating logic
kt-12 Apr 16, 2024
b55aa6f
remove get compute style from constructor
kt-12 Apr 16, 2024
2732050
remove uncessary check
kt-12 Apr 16, 2024
2afb765
attach clear_compute_style_properties_cache directly to hook
kt-12 Apr 16, 2024
974a292
Update src/wp-includes/class-wp-theme-json.php
kt-12 Apr 23, 2024
99ac114
global style caching
kt-12 May 17, 2024
fc6c235
update old test
kt-12 May 17, 2024
d1bd38a
remove additional function
kt-12 May 17, 2024
2957ae4
update function name
kt-12 May 17, 2024
ffa4771
cs fixes
kt-12 May 17, 2024
cbc7be5
cache is set check
kt-12 May 17, 2024
a4fc0b5
cache is set check
kt-12 May 17, 2024
23270b2
assert if it's not empty after
kt-12 May 17, 2024
672525f
Merge branch 'WordPress:trunk' into enhancement/object-cache-compute-…
joemcgill May 22, 2024
d0b243c
Update unit tests
joemcgill May 24, 2024
fbbb4cf
Fix typo
joemcgill May 24, 2024
8d8e99d
update cache related chanage
kt-12 May 27, 2024
5899f63
remove repetition
kt-12 May 27, 2024
23c518e
Merge branch 'trunk' into enhancement/object-cache-compute-style-prop…
kt-12 May 27, 2024
2c26302
update style block caching logic
kt-12 May 28, 2024
4c8dfe2
update test cases.
kt-12 May 28, 2024
b08183e
revert test changes
kt-12 May 28, 2024
48a310c
unregster bug fix
kt-12 May 29, 2024
8b16eb5
Revert "unregster bug fix"
joemcgill May 31, 2024
76435fc
Clear theme json data between theme unit tests
joemcgill May 31, 2024
a8c7453
Merge branch 'trunk' into update/59595-fix-tests
joemcgill May 31, 2024
21c2922
Don't cache nodes without names
joemcgill May 31, 2024
c938aa5
Merge branch 'trunk' into enhancement/object-cache-compute-style-prop…
joemcgill Jun 3, 2024
6becf8d
Merge branch 'enhancement/object-cache-compute-style-properties' into…
joemcgill Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,41 @@ 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 ) {
// 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,
)
)
);

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

if ( $can_use_cached && isset( $metadata['name'] ) ) {
if ( isset( $cached[ $metadata['name'] ] ) ) {
$block_css = $cached[ $metadata['name'] ];
} else {
$block_css = $tree->get_styles_for_block( $metadata );
$cached[ $metadata['name'] ] = $block_css;
$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 );
Expand Down Expand Up @@ -354,6 +387,10 @@ function wp_add_global_styles_for_blocks() {
}
}
}

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

/**
Expand Down
102 changes: 102 additions & 0 deletions tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,87 @@ 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 @@ -253,4 +334,25 @@ 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";
}
}
Loading