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

Improved caching. #3624

Closed
wants to merge 6 commits into from
Closed
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
21 changes: 18 additions & 3 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class WP_Theme_JSON_Resolver {
*/
protected static $theme = null;

/**
* Container for data coming from the theme with supports.
*
* @since 6.1.2
* @var WP_Theme_JSON
*/
protected static $with_theme_supports = null;

/**
* Whether or not the theme supports theme.json.
*
Expand Down Expand Up @@ -288,6 +296,10 @@ public static function get_theme_data( $deprecated = array(), $options = array()
return static::$theme;
}

if ( null !== static::$with_theme_supports ) {
return static::$with_theme_supports;
}

/*
* We want the presets and settings declared in theme.json
* to override the ones declared via theme supports.
Expand Down Expand Up @@ -323,9 +335,9 @@ public static function get_theme_data( $deprecated = array(), $options = array()
// Classic themes without a theme.json don't support global duotone.
$theme_support_data['settings']['color']['defaultDuotone'] = false;
}
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
$with_theme_supports->merge( static::$theme );
return $with_theme_supports;
static::$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
static::$with_theme_supports->merge( static::$theme );
return static::$with_theme_supports;
}

/**
Expand Down Expand Up @@ -638,6 +650,8 @@ protected static function get_file_path_from_theme( $file_name, $template = fals
* and `$i18n_schema` variables to reset.
* @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables
* to reset.
* @since 6.1.2 Added the `$with_theme_supports` variables
* to reset.
*/
public static function clean_cached_data() {
static::$core = null;
Expand All @@ -649,6 +663,7 @@ public static function clean_cached_data() {
'user' => array(),
);
static::$theme = null;
static::$with_theme_supports = null;
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
static::$user = null;
static::$user_custom_post_type_id = null;
static::$theme_has_support = null;
Expand Down
6 changes: 4 additions & 2 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,15 @@
add_action( 'init', '_register_core_block_patterns_and_categories' );
add_action( 'init', 'check_theme_switched', 99 );
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
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( 'after_switch_theme', '_wp_menus_changed' );
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
add_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' );

foreach ( array( 'switch_theme', 'start_previewing_theme', 'add_theme_support', 'remove_theme_support' ) as $action ) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the add_theme_support and remove_theme_support integration should be removed. I understand the concern, and I agree this is a problem we need to address.

However, invalidating the cache on add_theme_support means that it will happen on every request, since that function is typically called in every request.

Addressing this problem is not easy, which is why we need to IMO take some more time to think about the best way to address it. The root problem is that data that is cached preferably shouldn't depend on add_theme_support or any filters FWIW.

add_action( $action, array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) );
}

if ( isset( $_GET['replytocom'] ) ) {
add_filter( 'wp_robots', 'wp_robots_no_robots' );
}
Expand Down
19 changes: 19 additions & 0 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,16 @@ function add_theme_support( $feature, ...$args ) {
}

$_wp_theme_features[ $feature ] = $args;

/**
* Fires after theme support is added.
*
* @since 6.1.2
*
* @param string $feature The feature being added.
* @param mixed $args Optional extra arguments to pass along with certain features.
*/
do_action( 'add_theme_support', $feature, $args );
}

/**
Expand Down Expand Up @@ -3033,6 +3043,15 @@ function _remove_theme_support( $feature ) {

unset( $_wp_theme_features[ $feature ] );

/**
* Fires after theme support is removed.
*
* @since 6.1.2
*
* @param string $feature The feature being added.
*/
do_action( 'remove_theme_support', $feature );

return true;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/theme/wpGetGlobalStylesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function set_up() {

// Clear caches.
wp_clean_themes_cache();
WP_Theme_JSON_Resolver::clean_cached_data();
unset( $GLOBALS['wp_themes'] );
}

Expand All @@ -49,6 +50,7 @@ public function tear_down() {
remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );

wp_clean_themes_cache();
WP_Theme_JSON_Resolver::clean_cached_data();
unset( $GLOBALS['wp_themes'] );

parent::tear_down();
Expand Down