From 2a57dd7f86e96c6dbb01b1c7b48047fe15ddd075 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 10 Nov 2022 22:16:15 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-theme-json-resolver.php | 52 ++++++++++++++------ wp-includes/class-wp-theme-json.php | 3 +- wp-includes/version.php | 2 +- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/wp-includes/class-wp-theme-json-resolver.php b/wp-includes/class-wp-theme-json-resolver.php index cf14486f67..c166283af3 100644 --- a/wp-includes/class-wp-theme-json-resolver.php +++ b/wp-includes/class-wp-theme-json-resolver.php @@ -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_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); + $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,20 +264,23 @@ 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_data = static::translate( $parent_theme_json_data, wp_get_theme()->parent()->get( 'TextDomain' ) ); - $parent_theme = new WP_Theme_JSON( $parent_theme_json_data ); + if ( wp_get_theme()->parent() ) { + // Get parent theme.json. + $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 ); - /* - * 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; + /* + * 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'] ) { @@ -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 ) !== '' ); } diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index d0fcdeb52a..df5bd20d84 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -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'] ) diff --git a/wp-includes/version.php b/wp-includes/version.php index ba059b1142..16d3aa9116 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.