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

Global Styles: clean cached data when switching themes #30830

Merged
merged 3 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 25 additions & 5 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class WP_Theme_JSON_Resolver {
*/
private static $user_custom_post_type_id = null;

/**
* Structure to hold i18n metadata.
*
* @var Array
*/
private static $theme_json_i18n = null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was extracted out to the class so it can be cleaned upon theme switches.


/**
* Processes a file that adheres to the theme.json
* schema and returns an array with its contents,
Expand Down Expand Up @@ -143,12 +150,11 @@ private static function extract_paths_to_translate( $i18n_partial, $current_path
* @return array An array of theme.json fields that are translatable and the keys that are translatable
*/
public static function get_fields_to_translate() {
static $theme_json_i18n = null;
if ( null === $theme_json_i18n ) {
$file_structure = self::read_json_file( __DIR__ . '/experimental-i18n-theme.json' );
$theme_json_i18n = self::extract_paths_to_translate( $file_structure );
if ( null === self::$theme_json_i18n ) {
$file_structure = self::read_json_file( __DIR__ . '/experimental-i18n-theme.json' );
self::$theme_json_i18n = self::extract_paths_to_translate( $file_structure );
}
return $theme_json_i18n;
return self::$theme_json_i18n;
}

/**
Expand Down Expand Up @@ -494,4 +500,18 @@ private static function get_file_path_from_theme( $file_name ) {
return $located;
}

/**
* Cleans the cached data so it can be recalculated.
*/
public static function clean_cached_data() {
self::$core = null;
self::$theme = null;
self::$user = null;
self::$user_custom_post_type_id = null;
self::$theme_has_support = null;
self::$theme_json_i18n = null;
}

}

add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
14 changes: 14 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,18 @@ function test_translations_are_applied() {
)
);
}

function test_switching_themes_recalculates_data() {
// By default, the theme for unit tests is "default",
// which doesn't have theme.json support.
$default = WP_Theme_JSON_Resolver::theme_has_support();

// Switch to a theme that does have support.
switch_theme( 'fse' );
$fse = WP_Theme_JSON_Resolver::theme_has_support();

$this->assertSame( false, $default );
$this->assertSame( true, $fse );
}

}