Skip to content

Commit

Permalink
Update wp_theme_has_theme_json to use WP_Object_Cache (#45543)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Miguel Torres <miguel.torres@automattic.com>
Co-authored-by: Jonny Harris <spacedmonkey@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 17, 2022
1 parent 7ec665c commit dd166c1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
11 changes: 3 additions & 8 deletions lib/compat/wordpress-6.2/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
* @package gutenberg
*/

/**
* Note for backport: we should also remove the existing filters:
*
* > add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
* > add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
*/
add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' );
add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' );
add_action( 'switch_theme', 'wp_theme_has_theme_json_clean_cache' );
add_action( 'start_previewing_theme', 'wp_theme_has_theme_json_clean_cache' );
add_action( 'upgrader_process_complete', '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme' );
66 changes: 48 additions & 18 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,69 @@
/**
* Whether a theme or its parent have a theme.json file.
*
* @param boolean $clear_cache Whether the cache should be cleared and theme support recomputed. Default is false.
* The result would be cached via the WP_Object_Cache.
* It can be cleared by calling wp_theme_has_theme_json_clean_cache().
*
* @return boolean
*/
function wp_theme_has_theme_json( $clear_cache = false ) {
static $theme_has_support = null;
function wp_theme_has_theme_json() {
$cache_group = 'theme_json';
$cache_key = 'wp_theme_has_theme_json';
$theme_has_support = wp_cache_get( $cache_key, $cache_group );

if ( true === $clear_cache ) {
$theme_has_support = null;
}

if ( null !== $theme_has_support ) {
return $theme_has_support;
/**
* $theme_has_support is stored as a int in the cache.
*
* The reason not to store it as a boolean is to avoid working
* with the $found parameter which apparently had some issues in some implementations
* https://developer.wordpress.org/reference/functions/wp_cache_get/
*/
if ( 0 === $theme_has_support || 1 === $theme_has_support ) {
return (bool) $theme_has_support;
}

// Has the own theme a theme.json?
$theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' );
$theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ) ? 1 : 0;

// Look up the parent if the child does not have a theme.json.
if ( ! $theme_has_support ) {
$theme_has_support = is_readable( get_template_directory() . '/theme.json' );
if ( 0 === $theme_has_support ) {
$theme_has_support = is_readable( get_template_directory() . '/theme.json' ) ? 1 : 0;
}

return $theme_has_support;
wp_cache_set( $cache_key, $theme_has_support, $cache_group );

return (bool) $theme_has_support;
}
}

if ( ! function_exists( 'wp_theme_has_theme_json_clean_cache' ) ) {
/**
* Function to clean the cache used by wp_theme_has_theme_json method.
*/
function wp_theme_has_theme_json_clean_cache() {
wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' );
}
}

if ( ! function_exists( 'wp_theme_clean_theme_json_cached_data' ) ) {
if ( ! function_exists( '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme' ) ) {
/**
* Clean theme.json related cached data.
* Private function to clean the cache used by wp_theme_has_theme_json method.
*
* It is hooked into the `upgrader_process_complete` action.
*
* @see default-filters.php
*
* @param WP_Upgrader $upgrader Instance of WP_Upgrader class.
* @param array $options Metadata that identifies the data that is updated.
*/
function wp_theme_clean_theme_json_cached_data() {
wp_theme_has_theme_json( true );
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
function _wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme( $upgrader, $options ) {
// The cache only needs cleaning when the active theme was updated.
if (
'update' === $options['action'] &&
'theme' === $options['type'] &&
( isset( $options['themes'][ get_stylesheet() ] ) || isset( $options['themes'][ get_template() ] ) )
) {
wp_theme_has_theme_json_clean_cache();
}
}
}

0 comments on commit dd166c1

Please sign in to comment.