From 42bca5e5d8265898109123e5ec560a7167739587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Sun, 22 Jan 2023 19:31:15 +0800 Subject: [PATCH 1/5] Cache wp_theme_has_theme_json via static variable --- src/wp-includes/global-styles-and-settings.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index c625166524b63..f684a3239078b 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -264,6 +264,11 @@ function ( $item ) { * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. */ function wp_theme_has_theme_json() { + static $theme_has_support = null; + if ( null !== $theme_has_support ) { + return $theme_has_support; + } + // Does the theme have its own theme.json? $theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ); From 51f5d3afdb2ba7da20dc75f9757ecbf31dd12460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:52:28 +0100 Subject: [PATCH 2/5] Clean static cache upon events --- src/wp-includes/global-styles-and-settings.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index f684a3239078b..e58c6d91e06ce 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -261,11 +261,15 @@ function ( $item ) { * * @since 6.2.0 * + * @param bool $can_use_cache Whether the cached value can be used or should be recalculated. + * True by default. + * * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. */ -function wp_theme_has_theme_json() { +function wp_theme_has_theme_json( $can_use_cache = true ) { static $theme_has_support = null; - if ( null !== $theme_has_support ) { + + if ( $can_use_cache && ( null !== $theme_has_support ) ) { return $theme_has_support; } @@ -286,5 +290,6 @@ function wp_theme_has_theme_json() { * @since 6.2.0 */ function wp_clean_theme_json_cache() { + wp_theme_has_theme_json( false ); WP_Theme_JSON_Resolver::clean_cached_data(); } From f8d09bcbc8a030e4050e4517586bacc8e6b0ef0b Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 23 Jan 2023 13:26:12 -0600 Subject: [PATCH 3/5] Remove cache reset --- src/wp-includes/global-styles-and-settings.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index e58c6d91e06ce..a36181e05318e 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -261,15 +261,12 @@ function ( $item ) { * * @since 6.2.0 * - * @param bool $can_use_cache Whether the cached value can be used or should be recalculated. - * True by default. - * * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. */ -function wp_theme_has_theme_json( $can_use_cache = true ) { +function wp_theme_has_theme_json() { static $theme_has_support = null; - if ( $can_use_cache && ( null !== $theme_has_support ) ) { + if ( null !== $theme_has_support ) { return $theme_has_support; } @@ -290,6 +287,5 @@ function wp_theme_has_theme_json( $can_use_cache = true ) { * @since 6.2.0 */ function wp_clean_theme_json_cache() { - wp_theme_has_theme_json( false ); WP_Theme_JSON_Resolver::clean_cached_data(); } From a67086a3ad1822c7d1d54e702f9fe4e99417fa3e Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 23 Jan 2023 13:37:39 -0600 Subject: [PATCH 4/5] Reintroduce WP_DEBUG to reset static cache --- src/wp-includes/global-styles-and-settings.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index a36181e05318e..adf99045b42bd 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -266,6 +266,17 @@ function ( $item ) { function wp_theme_has_theme_json() { static $theme_has_support = null; + /* + * Ignore cache when `WP_DEBUG` is enabled. Why? + * - to avoid interfering with the theme developer's workflow. + * - to reset between each automated test. + * + * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. + */ + if ( WP_DEBUG ) { + $theme_has_support = null; + } + if ( null !== $theme_has_support ) { return $theme_has_support; } From d0ee04474df46845fa554870a47fc0a861b5705f Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Tue, 24 Jan 2023 07:16:49 -0600 Subject: [PATCH 5/5] Ignore cache when in dev or test mode --- .../global-styles-and-settings.php | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index adf99045b42bd..a0b91a4d219ed 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -266,18 +266,21 @@ function ( $item ) { function wp_theme_has_theme_json() { static $theme_has_support = null; - /* - * Ignore cache when `WP_DEBUG` is enabled. Why? - * - to avoid interfering with the theme developer's workflow. - * - to reset between each automated test. - * - * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. - */ - if ( WP_DEBUG ) { - $theme_has_support = null; - } - - if ( null !== $theme_has_support ) { + if ( + null !== $theme_has_support && + /* + * Ignore static cache when `WP_DEBUG` is enabled. Why? To avoid interfering with + * the theme developer's workflow. + * + * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. + */ + ! WP_DEBUG && + /* + * Ignore cache when automated test suites are running. Why? To ensure + * the static cache is reset between each test. + */ + ! ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) + ) { return $theme_has_support; }