From 2ceb5edc351f27c2620266fa30751f8b501a51f4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 15:20:48 +0200 Subject: [PATCH 1/8] Cache read_json_file() --- .../class-wp-theme-json-resolver.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index a92f5c1e09f4f..4dcbdf587b79d 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -70,24 +70,39 @@ class WP_Theme_JSON_Resolver { */ protected static $i18n_schema = null; + /** + * `theme.json` file cache. + * + * @since 6.1.0 + * @var array + * @access private + */ + protected static $theme_json_file_cache = array(); + /** * Processes a file that adheres to the theme.json schema * and returns an array with its contents, or a void array if none found. * * @since 5.8.0 + * @since 6.1.0 Added caching. * * @param string $file_path Path to file. Empty if no file. * @return array Contents that adhere to the theme.json schema. */ protected static function read_json_file( $file_path ) { - $config = array(); if ( $file_path ) { + if ( array_key_exists( $file_path, static::$theme_json_file_cache ) ) { + return static::$theme_json_file_cache[ $file_path ]; + } + $decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) ); if ( is_array( $decoded_file ) ) { - $config = $decoded_file; + static::$theme_json_file_cache[ $file_path ] = $decoded_file; + return static::$theme_json_file_cache[ $file_path ]; } + } else { + return array(); } - return $config; } /** From a596043ff36a09b0aa427a91d99fb4ca620fd5d7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 15:21:41 +0200 Subject: [PATCH 2/8] Remove caching from read_json_file() --- .../class-wp-theme-json-resolver.php | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 4dcbdf587b79d..387f9dd6c40eb 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -193,31 +193,29 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme ) { - $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); - /** - * Filters the data provided by the theme for global styles & settings. - * - * @since 6.1.0 - * - * @param WP_Theme_JSON_Data Class to access and update the underlying data. - */ - $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); - $theme_json_data = $theme_json->get_data(); - static::$theme = new WP_Theme_JSON( $theme_json_data ); - - if ( wp_get_theme()->parent() ) { - // Get parent theme.json. - $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); - $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); - $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); - - // Merge the child theme.json into the parent theme.json. - // The child theme takes precedence over the parent. - $parent_theme->merge( static::$theme ); - static::$theme = $parent_theme; - } + $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + /** + * Filters the data provided by the theme for global styles & settings. + * + * @since 6.1.0 + * + * @param WP_Theme_JSON_Data Class to access and update the underlying data. + */ + $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $theme_json_data = $theme_json->get_data(); + static::$theme = new WP_Theme_JSON( $theme_json_data ); + + if ( wp_get_theme()->parent() ) { + // Get parent theme.json. + $parent_theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json', true ) ); + $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); + $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); + + // Merge the child theme.json into the parent theme.json. + // The child theme takes precedence over the parent. + $parent_theme->merge( static::$theme ); + static::$theme = $parent_theme; } if ( ! $options['with_supports'] ) { From dd78dbc3ab1b00390d42797512bd5671ddc218f0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 16:14:50 +0200 Subject: [PATCH 3/8] Proper multi-line comment Co-authored-by: David B --- 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 387f9dd6c40eb..9d7472653af5b 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -212,8 +212,10 @@ public static function get_theme_data( $deprecated = array(), $options = array() $parent_theme_json_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); - // Merge the child theme.json into the parent theme.json. - // The child theme takes precedence over the parent. + /* + * Merge the child theme.json into the parent theme.json. + * The child theme takes precedence over the parent. + */ $parent_theme->merge( static::$theme ); static::$theme = $parent_theme; } From 7da8bf781e61c0fa86aa4e528dd9e26656b74913 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 16:15:05 +0200 Subject: [PATCH 4/8] Whitespace Co-authored-by: David B --- src/wp-includes/class-wp-theme-json-resolver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 9d7472653af5b..36cbf3d173a17 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -195,6 +195,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + /** * Filters the data provided by the theme for global styles & settings. * From e3a2d689f5a7753b614af6b608d64ccab90a0785 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 17:04:13 +0200 Subject: [PATCH 5/8] More whitespace --- src/wp-includes/class-wp-theme-json-resolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 36cbf3d173a17..9bb68702d9f55 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -195,7 +195,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); - + /** * Filters the data provided by the theme for global styles & settings. * From bf1a86c8d41550ca4a1ed91e5393285ad8bfdac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:11:51 +0200 Subject: [PATCH 6/8] Remove core theme.json caching --- src/wp-includes/class-wp-theme-json-resolver.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 9bb68702d9f55..0f78d1e7f2bb7 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -146,12 +146,9 @@ protected static function translate( $theme_json, $domain = 'default' ) { * @return WP_Theme_JSON Entity that holds core data. */ public static function get_core_data() { - if ( null !== static::$core ) { - return static::$core; - } - $config = static::read_json_file( __DIR__ . '/theme.json' ); $config = static::translate( $config ); + /** * Filters the default data provided by WordPress for global styles & settings. * From d10ac9ac0c3281cc81a7ce7e492a2f992251b07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:12:25 +0200 Subject: [PATCH 7/8] Remove user theme.json caching --- src/wp-includes/class-wp-theme-json-resolver.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 0f78d1e7f2bb7..6a3b2df20b0cd 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -410,10 +410,6 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post * @return WP_Theme_JSON Entity that holds styles for user data. */ public static function get_user_data() { - if ( null !== static::$user ) { - return static::$user; - } - $config = array(); $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); From 66b66f645d0ae99664a142fc77e70efb7dce9fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:12:56 +0200 Subject: [PATCH 8/8] Make sure there is a empty array fallback for all paths --- src/wp-includes/class-wp-theme-json-resolver.php | 4 ++-- 1 file changed, 2 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 6a3b2df20b0cd..ab117c6c7eb2d 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -100,9 +100,9 @@ protected static function read_json_file( $file_path ) { static::$theme_json_file_cache[ $file_path ] = $decoded_file; return static::$theme_json_file_cache[ $file_path ]; } - } else { - return array(); } + + return array(); } /**