Skip to content

Commit

Permalink
Themes: Add 'theme_files' cache group to block pattern cache operations.
Browse files Browse the repository at this point in the history
Use 'theme_files' cache group for block pattern caches. Previously, block pattern cache data was not stored in a cache group and used the default group. This new cache group, is setup as a global cache group, meaning that sites using multisite, will have a single cache for block pattern data per theme. This change also no longer invalidate block pattern caches in multisite instances, meaning block pattern caches can be shared between sites on a network, meaning less repeated data in the object cache. 

Props spacedmonkey, flixos90, joemcgill.
Fixes #60120.

git-svn-id: https://develop.svn.wordpress.org/trunk@57608 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
spacedmonkey committed Feb 13, 2024
1 parent 26f8360 commit 9a8d2c4
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/wp-includes/class-wp-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ private function get_pattern_cache() {
if ( ! $this->exists() ) {
return false;
}
$pattern_data = wp_cache_get( 'wp_theme_patterns_' . $this->stylesheet );
$pattern_data = wp_cache_get( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' );
if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) {
return $pattern_data['patterns'];
}
Expand All @@ -1993,7 +1993,7 @@ private function set_pattern_cache( array $patterns ) {
'version' => $this->get( 'Version' ),
'patterns' => $patterns,
);
wp_cache_set( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data );
wp_cache_set( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data, 'theme_files' );
}

/**
Expand All @@ -2002,7 +2002,7 @@ private function set_pattern_cache( array $patterns ) {
* @since 6.4.0
*/
public function delete_pattern_cache() {
wp_cache_delete( 'wp_theme_patterns_' . $this->stylesheet );
wp_cache_delete( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' );
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ function wp_start_object_cache() {
'site-options',
'site-queries',
'site-transient',
'theme_files',
'rss',
'users',
'user-queries',
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/ms-blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
'site-options',
'site-queries',
'site-transient',
'theme_files',
'rss',
'users',
'user-queries',
Expand Down Expand Up @@ -658,6 +659,7 @@ function restore_current_blog() {
'site-options',
'site-queries',
'site-transient',
'theme_files',
'rss',
'users',
'user-queries',
Expand Down
6 changes: 4 additions & 2 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,10 @@ function switch_theme( $stylesheet ) {
update_option( 'theme_switched', $old_theme->get_stylesheet() );

// Clear pattern caches.
$new_theme->delete_pattern_cache();
$old_theme->delete_pattern_cache();
if ( ! is_multisite() ) {
$new_theme->delete_pattern_cache();
$old_theme->delete_pattern_cache();
}

// Set autoload=no for the old theme, autoload=yes for the switched theme.
$theme_mods_options = array(
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public static function flush_cache() {
'site-options',
'site-queries',
'site-transient',
'theme_files',
'rss',
'users',
'user-queries',
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function test_delete_pattern_cache() {

/**
* @ticket 59490
* @group ms-excluded
*/
public function test_should_clear_cache_after_switching_theme() {
switch_theme( 'block-theme' );
Expand Down

0 comments on commit 9a8d2c4

Please sign in to comment.