Editor: Ensure block styles in `theme.json` are rendered.

This change removes the caching of theme data in `WP_Theme_JSON_Resolver::get_theme_data()`, instead freshly compiling theme data on each call.

Also, to prevent any performance degradation by the removal, the file contents of `theme.json` files are now cached in `WP_Theme_JSON_Resolver::read_json_file()`, preventing multiple filesystem reads.

Follow-up to [54385].

Props ndiego, bph, mikachan, andrewserong, oandregal, cbravobernal, bernhard-reiter, aristath.
Fixes #56736.
Built from https://develop.svn.wordpress.org/trunk@54399


git-svn-id: http://core.svn.wordpress.org/trunk@53958 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
davidbaumwald 2022-10-06 18:02:10 +00:00
parent 4943dbf577
commit 428915d739
2 changed files with 44 additions and 36 deletions

View File

@ -70,24 +70,38 @@ class WP_Theme_JSON_Resolver {
*/ */
protected static $i18n_schema = null; protected static $i18n_schema = null;
/**
* `theme.json` file cache.
*
* @since 6.1.0
* @var array
*/
protected static $theme_json_file_cache = array();
/** /**
* Processes a file that adheres to the theme.json schema * Processes a file that adheres to the theme.json schema
* and returns an array with its contents, or a void array if none found. * and returns an array with its contents, or a void array if none found.
* *
* @since 5.8.0 * @since 5.8.0
* @since 6.1.0 Added caching.
* *
* @param string $file_path Path to file. Empty if no file. * @param string $file_path Path to file. Empty if no file.
* @return array Contents that adhere to the theme.json schema. * @return array Contents that adhere to the theme.json schema.
*/ */
protected static function read_json_file( $file_path ) { protected static function read_json_file( $file_path ) {
$config = array();
if ( $file_path ) { 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 ) ); $decoded_file = wp_json_file_decode( $file_path, array( 'associative' => true ) );
if ( is_array( $decoded_file ) ) { 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 ];
} }
} }
return $config;
return array();
} }
/** /**
@ -131,12 +145,9 @@ class WP_Theme_JSON_Resolver {
* @return WP_Theme_JSON Entity that holds core data. * @return WP_Theme_JSON Entity that holds core data.
*/ */
public static function get_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::read_json_file( __DIR__ . '/theme.json' );
$config = static::translate( $config ); $config = static::translate( $config );
/** /**
* Filters the default data provided by WordPress for global styles & settings. * Filters the default data provided by WordPress for global styles & settings.
* *
@ -178,31 +189,32 @@ class WP_Theme_JSON_Resolver {
$options = wp_parse_args( $options, array( 'with_supports' => true ) ); $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::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
/** /**
* Filters the data provided by the theme for global styles & settings. * Filters the data provided by the theme for global styles and settings.
* *
* @since 6.1.0 * @since 6.1.0
* *
* @param WP_Theme_JSON_Data Class to access and update the underlying data. * @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.
*/ */
$theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); $parent_theme->merge( static::$theme );
$theme_json_data = $theme_json->get_data(); static::$theme = $parent_theme;
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'] ) { if ( ! $options['with_supports'] ) {
@ -397,10 +409,6 @@ class WP_Theme_JSON_Resolver {
* @return WP_Theme_JSON Entity that holds styles for user data. * @return WP_Theme_JSON Entity that holds styles for user data.
*/ */
public static function get_user_data() { public static function get_user_data() {
if ( null !== static::$user ) {
return static::$user;
}
$config = array(); $config = array();
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-beta3-54398'; $wp_version = '6.1-beta3-54399';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.