Editor: Avoid running certain logic around `theme.json` parsing unnecessarily for classic themes.

Here's what it does:
* Do not load and parse `theme-i18n.json` schema if the theme does not have a `theme.json` file.
* Fix the variable caching layer around the theme's `theme.json` parsing so that a parent's theme `theme.json` is cached as well.
* Do not run a `WP_Query` for global styles for a user when the theme does not have a `theme.json`.

In a basic WordPress setup, this changeset improves `wp_head` execution time for classic themes by 10%, and overall response time for both block themes and classic themes by 4%. This may seem like a small win, but 4% reduced overall response time is actually quite a bit for one change, and it is worth mentioning that this is just one of several other little performance tweaks which are being worked on to improve performance of `theme.json` parsing further.

Props flixos90, manuilov, oandregal, peterwilsoncc, spacedmonkey.
Fixes #56945.

Built from https://develop.svn.wordpress.org/trunk@54799


git-svn-id: http://core.svn.wordpress.org/trunk@54351 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2022-11-10 22:16:15 +00:00
parent c9a4d274fc
commit 2a57dd7f86
3 changed files with 39 additions and 18 deletions

View File

@ -246,8 +246,13 @@ class WP_Theme_JSON_Resolver {
$options = wp_parse_args( $options, array( 'with_supports' => true ) );
if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
$theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) );
$theme_json_file = static::get_file_path_from_theme( 'theme.json' );
if ( '' !== $theme_json_file ) {
$theme_json_data = static::read_json_file( $theme_json_file );
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
} else {
$theme_json_data = array();
}
/**
* Filters the data provided by the theme for global styles and settings.
@ -259,11 +264,12 @@ class WP_Theme_JSON_Resolver {
$theme_json = apply_filters( 'wp_theme_json_data_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_file = static::get_file_path_from_theme( 'theme.json', true );
if ( '' !== $parent_theme_json_file ) {
$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
$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 );
@ -274,6 +280,8 @@ class WP_Theme_JSON_Resolver {
$parent_theme->merge( static::$theme );
static::$theme = $parent_theme;
}
}
}
if ( ! $options['with_supports'] ) {
return static::$theme;
@ -403,6 +411,18 @@ class WP_Theme_JSON_Resolver {
if ( ! $theme instanceof WP_Theme ) {
$theme = wp_get_theme();
}
/*
* Bail early if the theme does not support a theme.json.
*
* Since WP_Theme_JSON_Resolver::theme_has_support() only supports the active
* theme, the extra condition for whether $theme is the active theme is
* present here.
*/
if ( $theme->get_stylesheet() === get_stylesheet() && ! static::theme_has_support() ) {
return array();
}
$user_cpt = array();
$post_type_filter = 'wp_global_styles';
$stylesheet = $theme->get_stylesheet();
@ -582,8 +602,8 @@ class WP_Theme_JSON_Resolver {
public static function theme_has_support() {
if ( ! isset( static::$theme_has_support ) ) {
static::$theme_has_support = (
is_readable( static::get_file_path_from_theme( 'theme.json' ) ) ||
is_readable( static::get_file_path_from_theme( 'theme.json', true ) )
static::get_file_path_from_theme( 'theme.json' ) !== '' ||
static::get_file_path_from_theme( 'theme.json', true ) !== ''
);
}

View File

@ -2939,7 +2939,8 @@ class WP_Theme_JSON {
public function set_spacing_sizes() {
$spacing_scale = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() );
if ( ! is_numeric( $spacing_scale['steps'] )
if ( ! isset( $spacing_scale['steps'] )
|| ! is_numeric( $spacing_scale['steps'] )
|| ! isset( $spacing_scale['mediumStep'] )
|| ! isset( $spacing_scale['unit'] )
|| ! isset( $spacing_scale['operator'] )

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54797';
$wp_version = '6.2-alpha-54799';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.