From d09a7123458ed5c53883dc342a0e1d9789c8b23c Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 14 Nov 2022 22:39:15 +0000 Subject: [PATCH 1/5] Improved caching. --- src/wp-includes/class-wp-theme-json-resolver.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index b1f15897b1af5..c34696f0d19d4 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -243,7 +243,9 @@ public static function get_theme_data( $deprecated = array(), $options = array() _deprecated_argument( __METHOD__, '5.9.0' ); } - $options = wp_parse_args( $options, array( 'with_supports' => true ) ); + if ( static::$theme ) { + return static::$theme; + } if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); @@ -284,10 +286,6 @@ public static function get_theme_data( $deprecated = array(), $options = array() } } - if ( ! $options['with_supports'] ) { - return static::$theme; - } - /* * We want the presets and settings declared in theme.json * to override the ones declared via theme supports. @@ -324,8 +322,8 @@ public static function get_theme_data( $deprecated = array(), $options = array() $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::$theme->merge( $with_theme_supports ); + return static::$theme; } /** From 202ee64babbb0977be5e65919d51f162a0cbed13 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Mon, 14 Nov 2022 23:23:33 +0000 Subject: [PATCH 2/5] Two caches. --- .../class-wp-theme-json-resolver.php | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index c34696f0d19d4..093992c850d78 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -57,6 +57,14 @@ class WP_Theme_JSON_Resolver { */ protected static $theme = null; + /** + * Container for data coming from the theme with settings. + * + * @since 6.1.1 + * @var WP_Theme_JSON + */ + protected static $with_theme_supports = null; + /** * Whether or not the theme supports theme.json. * @@ -243,9 +251,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() _deprecated_argument( __METHOD__, '5.9.0' ); } - if ( static::$theme ) { - return static::$theme; - } + $options = wp_parse_args( $options, array( 'with_supports' => true ) ); if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { $theme_json_file = static::get_file_path_from_theme( 'theme.json' ); @@ -286,6 +292,14 @@ public static function get_theme_data( $deprecated = array(), $options = array() } } + if ( ! $options['with_supports'] ) { + 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. @@ -321,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 ); - static::$theme->merge( $with_theme_supports ); - return static::$theme; + static::$with_theme_supports = new WP_Theme_JSON( $theme_support_data ); + static::$with_theme_supports->merge( static::$theme ); + return static::$with_theme_supports; } /** @@ -647,6 +661,7 @@ public static function clean_cached_data() { 'user' => array(), ); static::$theme = null; + static::$with_theme_supports = null; static::$user = null; static::$user_custom_post_type_id = null; static::$theme_has_support = null; From 18c0ca9b2f513157102f3c86b5536b192382baab Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 15 Nov 2022 11:32:43 +0000 Subject: [PATCH 3/5] Fix tests. --- tests/phpunit/tests/theme/wpGetGlobalStylesheet.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php b/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php index 710f77d0caf0d..ebb61415a164c 100644 --- a/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php +++ b/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php @@ -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'] ); } @@ -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(); From b566dcc262e6ba846418562ed5875c0bb3413246 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 22 Nov 2022 11:35:37 +0000 Subject: [PATCH 4/5] Feedback. --- src/wp-includes/class-wp-theme-json-resolver.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 093992c850d78..ac36f2d4a0f6a 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -58,9 +58,9 @@ class WP_Theme_JSON_Resolver { protected static $theme = null; /** - * Container for data coming from the theme with settings. + * Container for data coming from the theme with supports. * - * @since 6.1.1 + * @since 6.1.2 * @var WP_Theme_JSON */ protected static $with_theme_supports = null; @@ -650,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; From 8e2af75191500d6b896bb6ef2ec6a4dd878bf4c9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 22 Nov 2022 11:37:04 +0000 Subject: [PATCH 5/5] Clean caches when theme feature is added / removed. --- src/wp-includes/default-filters.php | 6 ++++-- src/wp-includes/theme.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index e9ca31f5f7908..e3b568290f363 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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 ) { + add_action( $action, array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); +} + if ( isset( $_GET['replytocom'] ) ) { add_filter( 'wp_robots', 'wp_robots_no_robots' ); } diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 61d5c13284a10..21c3ee191cd11 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -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 ); } /** @@ -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; }