From 787b2c20874b2361137b822d29b33048c2724c73 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 27 Oct 2022 13:21:50 -0700 Subject: [PATCH 1/9] Avoid parsing core's theme.json and theme-i18n.json when using a classic theme without support. --- src/wp-includes/class-wp-theme-json-resolver.php | 16 ++++++++++++---- src/wp-includes/class-wp-theme-json.php | 3 ++- 2 files 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 18b997561cf05..88f8907ed9ca9 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -170,8 +170,12 @@ public static function get_core_data() { return static::$core; } - $config = static::read_json_file( __DIR__ . '/theme.json' ); - $config = static::translate( $config ); + if ( static::theme_has_support() ) { + $config = static::read_json_file( __DIR__ . '/theme.json' ); + $config = static::translate( $config ); + } else { + $config = array(); + } /** * Filters the default data provided by WordPress for global styles & settings. @@ -246,8 +250,12 @@ public static function get_theme_data( $deprecated = array(), $options = array() $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' ) ); + if ( static::theme_has_support() ) { + $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' ) ); + } else { + $theme_json_data = array(); + } /** * Filters the data provided by the theme for global styles and settings. diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 555898370f863..47ce05512645f 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2934,7 +2934,8 @@ public function get_data() { 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'] ) From aff634740769140f5f9f2961ba741c544ee92f57 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 27 Oct 2022 13:22:32 -0700 Subject: [PATCH 2/9] Avoid DB query for global styles when using a classic theme without support. --- src/wp-includes/class-wp-theme-json-resolver.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 88f8907ed9ca9..d44a5186e17df 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -408,6 +408,9 @@ private static function remove_json_comments( $array ) { * @return array Custom Post Type for the user's origin config. */ public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) { + if ( ! static::theme_has_support() ) { + return array(); + } if ( ! $theme instanceof WP_Theme ) { $theme = wp_get_theme(); } From 0c768d3744ed9c7da57647a3d5aee0285cb8a141 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 2 Nov 2022 14:47:43 -0700 Subject: [PATCH 3/9] Further optimize WP_Theme_JSON_Resolver logic for classic themes, use pre-built PHP-parsed version of WP core theme.json if present. --- .../class-wp-theme-json-resolver.php | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 422a6b5637966..4badf9b1dba92 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -170,11 +170,13 @@ public static function get_core_data() { return static::$core; } - if ( static::theme_has_support() ) { + // TODO: Generate 'theme-json.php' at build time which returns fully parsed and translation-ready 'theme.json' + // as a PHP associative array. + if ( file_exists( __DIR__ . '/theme-json.php' ) ) { + $config = require __DIR__ . '/theme-json.php'; + } else { $config = static::read_json_file( __DIR__ . '/theme.json' ); $config = static::translate( $config ); - } else { - $config = array(); } /** @@ -271,16 +273,19 @@ public static function get_theme_data( $deprecated = array(), $options = array() 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; + $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; + } } if ( ! $options['with_supports'] ) { @@ -593,8 +598,8 @@ public static function get_user_global_styles_post_id() { 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 ) !== '' ); } From b9ff7748ccdfe0e2ad8a75c082742359e38054ef Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 2 Nov 2022 14:48:20 -0700 Subject: [PATCH 4/9] Add demo version of theme-json.php from WP core theme.json (via var_export()). --- src/wp-includes/theme-json.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/wp-includes/theme-json.php diff --git a/src/wp-includes/theme-json.php b/src/wp-includes/theme-json.php new file mode 100644 index 0000000000000..d7f88bc0a321f --- /dev/null +++ b/src/wp-includes/theme-json.php @@ -0,0 +1,12 @@ + 2, 'settings' => array ( 'appearanceTools' => false, 'useRootPaddingAwareAlignments' => false, 'border' => array ( 'color' => false, 'radius' => false, 'style' => false, 'width' => false, ), 'color' => array ( 'background' => true, 'custom' => true, 'customDuotone' => true, 'customGradient' => true, 'defaultDuotone' => true, 'defaultGradients' => true, 'defaultPalette' => true, 'duotone' => array ( 0 => array ( 'name' => _x( 'Dark grayscale', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#7f7f7f', ), 'slug' => 'dark-grayscale', ), 1 => array ( 'name' => _x( 'Grayscale', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#ffffff', ), 'slug' => 'grayscale', ), 2 => array ( 'name' => _x( 'Purple and yellow', 'Duotone name' ), 'colors' => array ( 0 => '#8c00b7', 1 => '#fcff41', ), 'slug' => 'purple-yellow', ), 3 => array ( 'name' => _x( 'Blue and red', 'Duotone name' ), 'colors' => array ( 0 => '#000097', 1 => '#ff4747', ), 'slug' => 'blue-red', ), 4 => array ( 'name' => _x( 'Midnight', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#00a5ff', ), 'slug' => 'midnight', ), 5 => array ( 'name' => _x( 'Magenta and yellow', 'Duotone name' ), 'colors' => array ( 0 => '#c7005a', 1 => '#fff278', ), 'slug' => 'magenta-yellow', ), 6 => array ( 'name' => _x( 'Purple and green' , 'Duotone name' ), 'colors' => array ( 0 => '#a60072', 1 => '#67ff66', ), 'slug' => 'purple-green', ), 7 => array ( 'name' => _x( 'Blue and orange', 'Duotone name' ), 'colors' => array ( 0 => '#1900d8', 1 => '#ffa96b', ), 'slug' => 'blue-orange', ), ), 'gradients' => array ( 0 => array ( 'name' => _x( 'Vivid cyan blue to vivid purple', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', 'slug' => 'vivid-cyan-blue-to-vivid-purple', ), 1 => array ( 'name' => _x( 'Light green cyan to vivid green cyan', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)', 'slug' => 'light-green-cyan-to-vivid-green-cyan', ), 2 => array ( 'name' => _x( 'Luminous vivid amber to luminous vivid orange', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%)', 'slug' => 'luminous-vivid-amber-to-luminous-vivid-orange', ), 3 => array ( 'name' => _x( 'Luminous vivid orange to vivid red', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%)', 'slug' => 'luminous-vivid-orange-to-vivid-red', ), 4 => array ( 'name' => _x( 'Very light gray to cyan bluish gray', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%)', 'slug' => 'very-light-gray-to-cyan-bluish-gray', ), 5 => array ( 'name' => _x( 'Cool to warm spectrum', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%)', 'slug' => 'cool-to-warm-spectrum', ), 6 => array ( 'name' => _x( 'Blush light purple', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%)', 'slug' => 'blush-light-purple', ), 7 => array ( 'name' => _x( 'Blush bordeaux', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)', 'slug' => 'blush-bordeaux', ), 8 => array ( 'name' => _x( 'Luminous dusk', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)', 'slug' => 'luminous-dusk', ), 9 => array ( 'name' => _x( 'Pale ocean', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%)', 'slug' => 'pale-ocean', ), 10 => array ( 'name' => _x( 'Electric grass', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%)', 'slug' => 'electric-grass', ), 11 => array ( 'name' => _x( 'Midnight', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%)', 'slug' => 'midnight', ), ), 'link' => false, 'palette' => array ( 0 => array ( 'name' => _x( 'Black', 'Color name' ), 'slug' => 'black', 'color' => '#000000', ), 1 => array ( 'name' => _x( 'Cyan bluish gray', 'Color name' ), 'slug' => 'cyan-bluish-gray', 'color' => '#abb8c3', ), 2 => array ( 'name' => _x( 'White', 'Color name' ), 'slug' => 'white', 'color' => '#ffffff', ), 3 => array ( 'name' => _x( 'Pale pink', 'Color name' ), 'slug' => 'pale-pink', 'color' => '#f78da7', ), 4 => array ( 'name' => _x( 'Vivid red', 'Color name' ), 'slug' => 'vivid-red', 'color' => '#cf2e2e', ), 5 => array ( 'name' => _x( 'Luminous vivid orange', 'Color name' ), 'slug' => 'luminous-vivid-orange', 'color' => '#ff6900', ), 6 => array ( 'name' => _x( 'Luminous vivid amber', 'Color name' ), 'slug' => 'luminous-vivid-amber', 'color' => '#fcb900', ), 7 => array ( 'name' => _x( 'Light green cyan', 'Color name' ), 'slug' => 'light-green-cyan', 'color' => '#7bdcb5', ), 8 => array ( 'name' => _x( 'Vivid green cyan', 'Color name' ), 'slug' => 'vivid-green-cyan', 'color' => '#00d084', ), 9 => array ( 'name' => _x( 'Pale cyan blue', 'Color name' ), 'slug' => 'pale-cyan-blue', 'color' => '#8ed1fc', ), 10 => array ( 'name' => _x( 'Vivid cyan blue', 'Color name' ), 'slug' => 'vivid-cyan-blue', 'color' => '#0693e3', ), 11 => array ( 'name' => _x( 'Vivid purple', 'Color name' ), 'slug' => 'vivid-purple', 'color' => '#9b51e0', ), ), 'text' => true, ), 'layout' => array ( 'definitions' => array ( 'default' => array ( 'name' => 'default', 'slug' => 'flow', 'className' => 'is-layout-flow', 'baseStyles' => array ( 0 => array ( 'selector' => ' > .alignleft', 'rules' => array ( 'float' => 'left', 'margin-inline-start' => '0', 'margin-inline-end' => '2em', ), ), 1 => array ( 'selector' => ' > .alignright', 'rules' => array ( 'float' => 'right', 'margin-inline-start' => '2em', 'margin-inline-end' => '0', ), ), 2 => array ( 'selector' => ' > .aligncenter', 'rules' => array ( 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => ' > *', 'rules' => array ( 'margin-block-start' => '0', 'margin-block-end' => '0', ), ), 1 => array ( 'selector' => ' > * + *', 'rules' => array ( 'margin-block-start' => NULL, 'margin-block-end' => '0', ), ), ), ), 'constrained' => array ( 'name' => 'constrained', 'slug' => 'constrained', 'className' => 'is-layout-constrained', 'baseStyles' => array ( 0 => array ( 'selector' => ' > .alignleft', 'rules' => array ( 'float' => 'left', 'margin-inline-start' => '0', 'margin-inline-end' => '2em', ), ), 1 => array ( 'selector' => ' > .alignright', 'rules' => array ( 'float' => 'right', 'margin-inline-start' => '2em', 'margin-inline-end' => '0', ), ), 2 => array ( 'selector' => ' > .aligncenter', 'rules' => array ( 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), 3 => array ( 'selector' => ' > :where(:not(.alignleft):not(.alignright):not(.alignfull))', 'rules' => array ( 'max-width' => 'var(--wp--style--global--content-size)', 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), 4 => array ( 'selector' => ' > .alignwide', 'rules' => array ( 'max-width' => 'var(--wp--style--global--wide-size)', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => ' > *', 'rules' => array ( 'margin-block-start' => '0', 'margin-block-end' => '0', ), ), 1 => array ( 'selector' => ' > * + *', 'rules' => array ( 'margin-block-start' => NULL, 'margin-block-end' => '0', ), ), ), ), 'flex' => array ( 'name' => 'flex', 'slug' => 'flex', 'className' => 'is-layout-flex', 'displayMode' => 'flex', 'baseStyles' => array ( 0 => array ( 'selector' => '', 'rules' => array ( 'flex-wrap' => 'wrap', 'align-items' => 'center', ), ), 1 => array ( 'selector' => ' > *', 'rules' => array ( 'margin' => '0', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => '', 'rules' => array ( 'gap' => NULL, ), ), ), ), ), ), 'spacing' => array ( 'blockGap' => NULL, 'margin' => false, 'padding' => false, 'customSpacingSize' => true, 'units' => array ( 0 => 'px', 1 => 'em', 2 => 'rem', 3 => 'vh', 4 => 'vw', 5 => '%', ), 'spacingScale' => array ( 'operator' => '*', 'increment' => 1.5, 'steps' => 7, 'mediumStep' => 1.5, 'unit' => 'rem', ), ), 'typography' => array ( 'customFontSize' => true, 'dropCap' => true, 'fontSizes' => array ( 0 => array ( 'name' => _x( 'Small', 'Font size name' ), 'slug' => 'small', 'size' => '13px', ), 1 => array ( 'name' => _x( 'Medium', 'Font size name' ), 'slug' => 'medium', 'size' => '20px', ), 2 => array ( 'name' => _x( 'Large', 'Font size name' ), 'slug' => 'large', 'size' => '36px', ), 3 => array ( 'name' => _x( 'Extra Large', 'Font size name' ), 'slug' => 'x-large', 'size' => '42px', ), ), 'fontStyle' => true, 'fontWeight' => true, 'letterSpacing' => true, 'lineHeight' => false, 'textDecoration' => true, 'textTransform' => true, ), 'blocks' => array ( 'core/button' => array ( 'border' => array ( 'radius' => true, ), ), 'core/pullquote' => array ( 'border' => array ( 'color' => true, 'radius' => true, 'style' => true, 'width' => true, ), ), ), ), 'styles' => array ( 'elements' => array ( 'button' => array ( 'color' => array ( 'text' => '#fff', 'background' => '#32373c', ), 'spacing' => array ( 'padding' => 'calc(0.667em + 2px) calc(1.333em + 2px)', ), 'typography' => array ( 'fontSize' => 'inherit', 'fontFamily' => 'inherit', 'lineHeight' => 'inherit', 'textDecoration' => 'none', ), 'border' => array ( 'width' => '0', ), ), 'link' => array ( 'typography' => array ( 'textDecoration' => 'underline', ), ), ), 'spacing' => array ( 'blockGap' => '24px', 'padding' => array ( 'top' => '0px', 'right' => '0px', 'bottom' => '0px', 'left' => '0px', ), ), ), ); From 6a835a4084b4f990cfe30724c5afa1185d09aeac Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 8 Nov 2022 15:44:14 -0800 Subject: [PATCH 5/9] Remove temporary theme.json in favor of https://github.com/WordPress/gutenberg/issues/45616. --- src/wp-includes/class-wp-theme-json-resolver.php | 10 ++-------- src/wp-includes/theme-json.php | 12 ------------ 2 files changed, 2 insertions(+), 20 deletions(-) delete mode 100644 src/wp-includes/theme-json.php diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 4badf9b1dba92..b59cee2b269b4 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -170,14 +170,8 @@ public static function get_core_data() { return static::$core; } - // TODO: Generate 'theme-json.php' at build time which returns fully parsed and translation-ready 'theme.json' - // as a PHP associative array. - if ( file_exists( __DIR__ . '/theme-json.php' ) ) { - $config = require __DIR__ . '/theme-json.php'; - } else { - $config = static::read_json_file( __DIR__ . '/theme.json' ); - $config = static::translate( $config ); - } + $config = static::read_json_file( __DIR__ . '/theme.json' ); + $config = static::translate( $config ); /** * Filters the default data provided by WordPress for global styles & settings. diff --git a/src/wp-includes/theme-json.php b/src/wp-includes/theme-json.php deleted file mode 100644 index d7f88bc0a321f..0000000000000 --- a/src/wp-includes/theme-json.php +++ /dev/null @@ -1,12 +0,0 @@ - 2, 'settings' => array ( 'appearanceTools' => false, 'useRootPaddingAwareAlignments' => false, 'border' => array ( 'color' => false, 'radius' => false, 'style' => false, 'width' => false, ), 'color' => array ( 'background' => true, 'custom' => true, 'customDuotone' => true, 'customGradient' => true, 'defaultDuotone' => true, 'defaultGradients' => true, 'defaultPalette' => true, 'duotone' => array ( 0 => array ( 'name' => _x( 'Dark grayscale', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#7f7f7f', ), 'slug' => 'dark-grayscale', ), 1 => array ( 'name' => _x( 'Grayscale', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#ffffff', ), 'slug' => 'grayscale', ), 2 => array ( 'name' => _x( 'Purple and yellow', 'Duotone name' ), 'colors' => array ( 0 => '#8c00b7', 1 => '#fcff41', ), 'slug' => 'purple-yellow', ), 3 => array ( 'name' => _x( 'Blue and red', 'Duotone name' ), 'colors' => array ( 0 => '#000097', 1 => '#ff4747', ), 'slug' => 'blue-red', ), 4 => array ( 'name' => _x( 'Midnight', 'Duotone name' ), 'colors' => array ( 0 => '#000000', 1 => '#00a5ff', ), 'slug' => 'midnight', ), 5 => array ( 'name' => _x( 'Magenta and yellow', 'Duotone name' ), 'colors' => array ( 0 => '#c7005a', 1 => '#fff278', ), 'slug' => 'magenta-yellow', ), 6 => array ( 'name' => _x( 'Purple and green' , 'Duotone name' ), 'colors' => array ( 0 => '#a60072', 1 => '#67ff66', ), 'slug' => 'purple-green', ), 7 => array ( 'name' => _x( 'Blue and orange', 'Duotone name' ), 'colors' => array ( 0 => '#1900d8', 1 => '#ffa96b', ), 'slug' => 'blue-orange', ), ), 'gradients' => array ( 0 => array ( 'name' => _x( 'Vivid cyan blue to vivid purple', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)', 'slug' => 'vivid-cyan-blue-to-vivid-purple', ), 1 => array ( 'name' => _x( 'Light green cyan to vivid green cyan', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)', 'slug' => 'light-green-cyan-to-vivid-green-cyan', ), 2 => array ( 'name' => _x( 'Luminous vivid amber to luminous vivid orange', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%)', 'slug' => 'luminous-vivid-amber-to-luminous-vivid-orange', ), 3 => array ( 'name' => _x( 'Luminous vivid orange to vivid red', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%)', 'slug' => 'luminous-vivid-orange-to-vivid-red', ), 4 => array ( 'name' => _x( 'Very light gray to cyan bluish gray', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%)', 'slug' => 'very-light-gray-to-cyan-bluish-gray', ), 5 => array ( 'name' => _x( 'Cool to warm spectrum', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%)', 'slug' => 'cool-to-warm-spectrum', ), 6 => array ( 'name' => _x( 'Blush light purple', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%)', 'slug' => 'blush-light-purple', ), 7 => array ( 'name' => _x( 'Blush bordeaux', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)', 'slug' => 'blush-bordeaux', ), 8 => array ( 'name' => _x( 'Luminous dusk', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)', 'slug' => 'luminous-dusk', ), 9 => array ( 'name' => _x( 'Pale ocean', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%)', 'slug' => 'pale-ocean', ), 10 => array ( 'name' => _x( 'Electric grass', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%)', 'slug' => 'electric-grass', ), 11 => array ( 'name' => _x( 'Midnight', 'Gradient name' ), 'gradient' => 'linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%)', 'slug' => 'midnight', ), ), 'link' => false, 'palette' => array ( 0 => array ( 'name' => _x( 'Black', 'Color name' ), 'slug' => 'black', 'color' => '#000000', ), 1 => array ( 'name' => _x( 'Cyan bluish gray', 'Color name' ), 'slug' => 'cyan-bluish-gray', 'color' => '#abb8c3', ), 2 => array ( 'name' => _x( 'White', 'Color name' ), 'slug' => 'white', 'color' => '#ffffff', ), 3 => array ( 'name' => _x( 'Pale pink', 'Color name' ), 'slug' => 'pale-pink', 'color' => '#f78da7', ), 4 => array ( 'name' => _x( 'Vivid red', 'Color name' ), 'slug' => 'vivid-red', 'color' => '#cf2e2e', ), 5 => array ( 'name' => _x( 'Luminous vivid orange', 'Color name' ), 'slug' => 'luminous-vivid-orange', 'color' => '#ff6900', ), 6 => array ( 'name' => _x( 'Luminous vivid amber', 'Color name' ), 'slug' => 'luminous-vivid-amber', 'color' => '#fcb900', ), 7 => array ( 'name' => _x( 'Light green cyan', 'Color name' ), 'slug' => 'light-green-cyan', 'color' => '#7bdcb5', ), 8 => array ( 'name' => _x( 'Vivid green cyan', 'Color name' ), 'slug' => 'vivid-green-cyan', 'color' => '#00d084', ), 9 => array ( 'name' => _x( 'Pale cyan blue', 'Color name' ), 'slug' => 'pale-cyan-blue', 'color' => '#8ed1fc', ), 10 => array ( 'name' => _x( 'Vivid cyan blue', 'Color name' ), 'slug' => 'vivid-cyan-blue', 'color' => '#0693e3', ), 11 => array ( 'name' => _x( 'Vivid purple', 'Color name' ), 'slug' => 'vivid-purple', 'color' => '#9b51e0', ), ), 'text' => true, ), 'layout' => array ( 'definitions' => array ( 'default' => array ( 'name' => 'default', 'slug' => 'flow', 'className' => 'is-layout-flow', 'baseStyles' => array ( 0 => array ( 'selector' => ' > .alignleft', 'rules' => array ( 'float' => 'left', 'margin-inline-start' => '0', 'margin-inline-end' => '2em', ), ), 1 => array ( 'selector' => ' > .alignright', 'rules' => array ( 'float' => 'right', 'margin-inline-start' => '2em', 'margin-inline-end' => '0', ), ), 2 => array ( 'selector' => ' > .aligncenter', 'rules' => array ( 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => ' > *', 'rules' => array ( 'margin-block-start' => '0', 'margin-block-end' => '0', ), ), 1 => array ( 'selector' => ' > * + *', 'rules' => array ( 'margin-block-start' => NULL, 'margin-block-end' => '0', ), ), ), ), 'constrained' => array ( 'name' => 'constrained', 'slug' => 'constrained', 'className' => 'is-layout-constrained', 'baseStyles' => array ( 0 => array ( 'selector' => ' > .alignleft', 'rules' => array ( 'float' => 'left', 'margin-inline-start' => '0', 'margin-inline-end' => '2em', ), ), 1 => array ( 'selector' => ' > .alignright', 'rules' => array ( 'float' => 'right', 'margin-inline-start' => '2em', 'margin-inline-end' => '0', ), ), 2 => array ( 'selector' => ' > .aligncenter', 'rules' => array ( 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), 3 => array ( 'selector' => ' > :where(:not(.alignleft):not(.alignright):not(.alignfull))', 'rules' => array ( 'max-width' => 'var(--wp--style--global--content-size)', 'margin-left' => 'auto !important', 'margin-right' => 'auto !important', ), ), 4 => array ( 'selector' => ' > .alignwide', 'rules' => array ( 'max-width' => 'var(--wp--style--global--wide-size)', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => ' > *', 'rules' => array ( 'margin-block-start' => '0', 'margin-block-end' => '0', ), ), 1 => array ( 'selector' => ' > * + *', 'rules' => array ( 'margin-block-start' => NULL, 'margin-block-end' => '0', ), ), ), ), 'flex' => array ( 'name' => 'flex', 'slug' => 'flex', 'className' => 'is-layout-flex', 'displayMode' => 'flex', 'baseStyles' => array ( 0 => array ( 'selector' => '', 'rules' => array ( 'flex-wrap' => 'wrap', 'align-items' => 'center', ), ), 1 => array ( 'selector' => ' > *', 'rules' => array ( 'margin' => '0', ), ), ), 'spacingStyles' => array ( 0 => array ( 'selector' => '', 'rules' => array ( 'gap' => NULL, ), ), ), ), ), ), 'spacing' => array ( 'blockGap' => NULL, 'margin' => false, 'padding' => false, 'customSpacingSize' => true, 'units' => array ( 0 => 'px', 1 => 'em', 2 => 'rem', 3 => 'vh', 4 => 'vw', 5 => '%', ), 'spacingScale' => array ( 'operator' => '*', 'increment' => 1.5, 'steps' => 7, 'mediumStep' => 1.5, 'unit' => 'rem', ), ), 'typography' => array ( 'customFontSize' => true, 'dropCap' => true, 'fontSizes' => array ( 0 => array ( 'name' => _x( 'Small', 'Font size name' ), 'slug' => 'small', 'size' => '13px', ), 1 => array ( 'name' => _x( 'Medium', 'Font size name' ), 'slug' => 'medium', 'size' => '20px', ), 2 => array ( 'name' => _x( 'Large', 'Font size name' ), 'slug' => 'large', 'size' => '36px', ), 3 => array ( 'name' => _x( 'Extra Large', 'Font size name' ), 'slug' => 'x-large', 'size' => '42px', ), ), 'fontStyle' => true, 'fontWeight' => true, 'letterSpacing' => true, 'lineHeight' => false, 'textDecoration' => true, 'textTransform' => true, ), 'blocks' => array ( 'core/button' => array ( 'border' => array ( 'radius' => true, ), ), 'core/pullquote' => array ( 'border' => array ( 'color' => true, 'radius' => true, 'style' => true, 'width' => true, ), ), ), ), 'styles' => array ( 'elements' => array ( 'button' => array ( 'color' => array ( 'text' => '#fff', 'background' => '#32373c', ), 'spacing' => array ( 'padding' => 'calc(0.667em + 2px) calc(1.333em + 2px)', ), 'typography' => array ( 'fontSize' => 'inherit', 'fontFamily' => 'inherit', 'lineHeight' => 'inherit', 'textDecoration' => 'none', ), 'border' => array ( 'width' => '0', ), ), 'link' => array ( 'typography' => array ( 'textDecoration' => 'underline', ), ), ), 'spacing' => array ( 'blockGap' => '24px', 'padding' => array ( 'top' => '0px', 'right' => '0px', 'bottom' => '0px', 'left' => '0px', ), ), ), ); From 1ab511c703e495ab3b5b98daff19be787cae4a0b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 8 Nov 2022 15:55:17 -0800 Subject: [PATCH 6/9] Include parsing parent theme.json logic in cached logic block. --- .../class-wp-theme-json-resolver.php | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index b59cee2b269b4..57e09e48162ce 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -246,8 +246,9 @@ public static function get_theme_data( $deprecated = array(), $options = array() $options = wp_parse_args( $options, array( 'with_supports' => true ) ); if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { - if ( static::theme_has_support() ) { - $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(); @@ -263,22 +264,22 @@ public static function get_theme_data( $deprecated = array(), $options = array() $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_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; + 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; + } } } From 496610945f826a490143a73016cea4029cc31684 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 8 Nov 2022 17:04:09 -0800 Subject: [PATCH 7/9] Add test coverage and fix relevant tests to require a block theme. --- .../tests/theme/wpThemeJsonResolver.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 524fade2d8f85..7798205520d1c 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -634,6 +634,8 @@ function test_merges_child_theme_json_into_parent_theme_json() { * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() { + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); wp_set_current_user( self::$administrator_id ); $theme = wp_get_theme(); WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); @@ -666,6 +668,8 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() { + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); $theme = wp_get_theme(); WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); add_filter( 'query', array( $this, 'filter_db_query' ) ); @@ -681,11 +685,36 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_ $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); } + /** + * @ticket 56945 + * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles + */ + function test_get_user_data_from_wp_global_styles_does_not_run_for_theme_without_support() { + // The 'default' theme does not support theme.json. + switch_theme( 'default' ); + wp_set_current_user( self::$administrator_id ); + $theme = wp_get_theme(); + + // Add filter to record DB query count. + add_filter( 'query', array( $this, 'filter_db_query' ) ); + + // When theme.json is not supported, the method should not run a query and always return an empty result. + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); + $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); + $this->assertSame( 0, count( $this->queries ), 'Unexpected SQL query detected for theme without theme.json support.' ); + + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); + $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); + $this->assertSame( 0, count( $this->queries ), 'Unexpected SQL query detected for theme without theme.json support.' ); + } + /** * @ticket 55392 * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_does_exist() { + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); $theme = wp_get_theme(); $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); $this->assertIsArray( $post1 ); @@ -701,6 +730,8 @@ function test_get_user_data_from_wp_global_styles_does_exist() { * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_create_post() { + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); $theme = wp_get_theme( 'testing' ); $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertIsArray( $post1 ); @@ -718,6 +749,8 @@ function test_get_user_data_from_wp_global_styles_create_post() { * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles */ function test_get_user_data_from_wp_global_styles_filter_state() { + // Switch to a theme that does have support. + switch_theme( 'block-theme' ); $theme = wp_get_theme( 'foo' ); $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true, array( 'publish' ) ); $this->assertIsArray( $post1 ); @@ -749,4 +782,29 @@ function test_get_theme_data_theme_supports_overrides_theme_json() { $line_height = $current_settings['typography']['lineHeight']; $this->assertTrue( $line_height, 'lineHeight setting after add_theme_support() should be true.' ); } + + /** + * @ticket 56945 + * @covers WP_Theme_JSON_Resolver::get_theme_data + */ + function test_get_theme_data_does_not_parse_theme_json_if_not_present() { + // The 'default' theme does not support theme.json. + switch_theme( 'default' ); + + $theme_json_resolver = new WP_Theme_JSON_Resolver(); + + // Force-unset $i18n_schema property to "unload" translation schema. + $property = new ReflectionProperty( $theme_json_resolver, 'i18n_schema' ); + $property->setAccessible( true ); + $property->setValue( null ); + + // A completely empty theme.json data set still has the 'version' key when parsed. + $empty_theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ); + + // Call using 'with_supports' set to false, so that the method only considers theme.json. + $theme_data = $theme_json_resolver->get_theme_data( array(), array( 'with_supports' => false ) ); + $this->assertInstanceOf( 'WP_Theme_JSON', $theme_data, 'Theme data should be an instance of WP_Theme_JSON.' ); + $this->assertSame( $empty_theme_json, $theme_data->get_raw_data(), 'Theme data should be empty without theme support.' ); + $this->assertNull( $property->getValue(), 'Theme i18n schema should not have been loaded without theme support.' ); + } } From 34c19573696aa3560503c6304b5e0c0f791dcfa1 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 9 Nov 2022 15:24:33 -0800 Subject: [PATCH 8/9] Address code review feedback. --- .../class-wp-theme-json-resolver.php | 16 ++++--- .../tests/theme/wpThemeJsonResolver.php | 48 ++++++++----------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index d9cefcb6f3edc..4b177c09c2f24 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -274,9 +274,9 @@ public static function get_theme_data( $deprecated = array(), $options = array() $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; } @@ -408,12 +408,16 @@ private static function remove_json_comments( $array ) { * @return array Custom Post Type for the user's origin config. */ public static function get_user_data_from_wp_global_styles( $theme, $create_post = false, $post_status_filter = array( 'publish' ) ) { - if ( ! static::theme_has_support() ) { - return array(); - } 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(); diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 7798205520d1c..1cbc519255cf4 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -33,13 +33,6 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase { */ private $orig_theme_dir; - /** - * Queries. - * - * @var array - */ - private $queries = array(); - /** * WP_Theme_JSON_Resolver::$blocks_cache property. * @@ -105,7 +98,7 @@ public function set_up() { add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); - $this->queries = array(); + // Clear caches. wp_clean_themes_cache(); unset( $GLOBALS['wp_themes'] ); @@ -129,13 +122,6 @@ public function filter_set_locale_to_polish() { return 'pl_PL'; } - function filter_db_query( $query ) { - if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) { - $this->queries[] = $query; - } - return $query; - } - /** * @ticket 52991 * @ticket 54336 @@ -639,14 +625,21 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( wp_set_current_user( self::$administrator_id ); $theme = wp_get_theme(); WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - add_filter( 'query', array( $this, 'filter_db_query' ) ); - $query_count = count( $this->queries ); + $global_styles_query_count = 0; + add_filter( + 'query', + function( $query ) use ( &$global_styles_query_count ) { + if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) { + $global_styles_query_count++; + } + return $query; + } + ); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); WP_Theme_JSON_Resolver::clean_cached_data(); } - $query_count = count( $this->queries ) - $query_count; - $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); + $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); @@ -654,14 +647,13 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries( $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); $this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' ); - $query_count = count( $this->queries ); + $global_styles_query_count = 0; for ( $i = 0; $i < 3; $i ++ ) { $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); WP_Theme_JSON_Resolver::clean_cached_data(); $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." ); } - $query_count = count( $this->queries ) - $query_count; - $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); + $this->assertSame( 1, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' ); } /** @@ -672,13 +664,12 @@ function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_ switch_theme( 'block-theme' ); $theme = wp_get_theme(); WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); - add_filter( 'query', array( $this, 'filter_db_query' ) ); - $query_count = count( $this->queries ); + $query_count = get_num_queries(); for ( $i = 0; $i < 3; $i++ ) { WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); WP_Theme_JSON_Resolver::clean_cached_data(); } - $query_count = count( $this->queries ) - $query_count; + $query_count = get_num_queries() - $query_count; $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); @@ -695,17 +686,16 @@ function test_get_user_data_from_wp_global_styles_does_not_run_for_theme_without wp_set_current_user( self::$administrator_id ); $theme = wp_get_theme(); - // Add filter to record DB query count. - add_filter( 'query', array( $this, 'filter_db_query' ) ); + $start_queries = get_num_queries(); // When theme.json is not supported, the method should not run a query and always return an empty result. $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); - $this->assertSame( 0, count( $this->queries ), 'Unexpected SQL query detected for theme without theme.json support.' ); + $this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); - $this->assertSame( 0, count( $this->queries ), 'Unexpected SQL query detected for theme without theme.json support.' ); + $this->assertSame( 0, get_num_queries() - $start_queries, 'Unexpected SQL query detected for theme without theme.json support.' ); } /** From 392391a5bb909ad5adae3358b263380d1db13973 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 9 Nov 2022 16:31:52 -0800 Subject: [PATCH 9/9] Limit comment to 80 characters per line. Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- src/wp-includes/class-wp-theme-json-resolver.php | 9 +++++++-- 1 file changed, 7 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 4b177c09c2f24..c166283af30f1 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -412,8 +412,13 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post $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. + /* + * 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(); }