From 2775e751146f431b5ff37d075bb2f52cf69bbc9b Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Tue, 4 Oct 2022 13:57:42 +0200 Subject: [PATCH 1/5] Try to cache the theme json file reading --- .../class-wp-theme-json-resolver.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 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..0b9bd1f82a8b0 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -70,6 +70,15 @@ class WP_Theme_JSON_Resolver { */ protected static $i18n_schema = null; + /** + * Stores the theme json styles from the theme.json file. + * @since 6.1.0 + * @var string + * @access private + */ + + protected static $theme_json_data = null; + /** * Processes a file that adheres to the theme.json schema * and returns an array with its contents, or a void array if none found. @@ -177,8 +186,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() } $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - - if ( null === static::$theme ) { + if ( null === static::$theme && null === static::$theme_json_data ) { $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' ) ); /** @@ -188,9 +196,10 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @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 ); + $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_json_data = $theme_json_data; + static::$theme = new WP_Theme_JSON( $theme_json_data ); if ( wp_get_theme()->parent() ) { // Get parent theme.json. From e2704ab2b98c9b7542efc798c9abdf8c11fcd945 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Tue, 4 Oct 2022 15:15:27 +0200 Subject: [PATCH 2/5] Cache only the theme json --- src/wp-includes/class-wp-theme-json-resolver.php | 9 ++++++--- 1 file changed, 6 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 0b9bd1f82a8b0..c3ad6cd546b27 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -186,9 +186,11 @@ public static function get_theme_data( $deprecated = array(), $options = array() } $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme && null === static::$theme_json_data ) { + if ( null === static::$theme_json_data ) { + $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. * @@ -196,9 +198,10 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ + + static::$theme_json_data = $theme_json_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_json_data = $theme_json_data; static::$theme = new WP_Theme_JSON( $theme_json_data ); if ( wp_get_theme()->parent() ) { @@ -213,7 +216,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() static::$theme = $parent_theme; } } - + static::$theme = new WP_Theme_JSON( static::$theme_json_data ); if ( ! $options['with_supports'] ) { return static::$theme; } From e686c5f5c38b260e61d9af85addb95181bad772b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 14:55:21 +0200 Subject: [PATCH 3/5] Whitespace, variable use --- src/wp-includes/class-wp-theme-json-resolver.php | 11 ++++------- 1 file changed, 4 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 c3ad6cd546b27..d60baf5f1f110 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -76,7 +76,6 @@ class WP_Theme_JSON_Resolver { * @var string * @access private */ - protected static $theme_json_data = null; /** @@ -186,10 +185,10 @@ public static function get_theme_data( $deprecated = array(), $options = array() } $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme_json_data ) { - $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' ) ); + if ( null === static::$theme_json_data ) { + static::$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + static::$theme_json_data = static::translate( static::$theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); /** * Filters the data provided by the theme for global styles & settings. @@ -198,9 +197,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @param WP_Theme_JSON_Data Class to access and update the underlying data. */ - - static::$theme_json_data = $theme_json_data; - $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); + $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( static::$theme_json_data, 'theme' ) ); $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON( $theme_json_data ); From 1a65ed291529568d53e395052a6adc23b5f1e9b1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 14:58:49 +0200 Subject: [PATCH 4/5] Small fix --- .../class-wp-theme-json-resolver.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index d60baf5f1f110..6da7c989b3f6f 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -189,31 +189,31 @@ public static function get_theme_data( $deprecated = array(), $options = array() if ( null === static::$theme_json_data ) { static::$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); static::$theme_json_data = static::translate( static::$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( static::$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; - } + /** + * 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( static::$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; } - static::$theme = new WP_Theme_JSON( static::$theme_json_data ); + if ( ! $options['with_supports'] ) { return static::$theme; } From 9665d71bde2b89fbe8dec56d8962e4a1d95cbe61 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 5 Oct 2022 15:01:48 +0200 Subject: [PATCH 5/5] Rename to theme_json_file_data --- src/wp-includes/class-wp-theme-json-resolver.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 6da7c989b3f6f..0c683b611870b 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -76,7 +76,7 @@ class WP_Theme_JSON_Resolver { * @var string * @access private */ - protected static $theme_json_data = null; + protected static $theme_json_file_data = null; /** * Processes a file that adheres to the theme.json schema @@ -186,9 +186,9 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); - if ( null === static::$theme_json_data ) { - static::$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); - static::$theme_json_data = static::translate( static::$theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + if ( null === static::$theme_json_file_data ) { + static::$theme_json_file_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); + static::$theme_json_file_data = static::translate( static::$theme_json_file_data, wp_get_theme()->get( 'TextDomain' ) ); } /** @@ -198,7 +198,7 @@ public static function get_theme_data( $deprecated = array(), $options = array() * * @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( static::$theme_json_data, 'theme' ) ); + $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( static::$theme_json_file_data, 'theme' ) ); $theme_json_data = $theme_json->get_data(); static::$theme = new WP_Theme_JSON( $theme_json_data );