From ad493ea841c47e4a7fd0c46b41e7ad997013d9b6 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:39:36 +1000 Subject: [PATCH 1/4] Global Styles: Avoid unnecessary processing of theme.json variation partialss --- .../block-supports/block-style-variations.php | 7 ++ .../class-wp-theme-json-resolver.php | 92 +++++++++++++------ 2 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index b85ab6aad655a..bf8122e838d69 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -474,6 +474,13 @@ function wp_register_block_style_variations_from_theme_json_data( $variations ) * @access private */ function wp_register_block_style_variations_from_theme() { + $has_partials_directory = is_dir( get_stylesheet_directory() . '/styles' ) || is_dir( get_template_directory() . '/styles' ); + + // Skip any registration of styles if no theme.json or variation partials. + if ( ! wp_theme_has_theme_json() && ! $has_partials_directory ) { + return; + } + // Partials from `/styles`. $variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' ); wp_register_block_style_variations_from_theme_json_data( $variations_partials ); diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index fca687cfb9f1b..d586f1104a2fa 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -91,6 +91,14 @@ class WP_Theme_JSON_Resolver { */ protected static $theme_json_file_cache = array(); + /** + * Cache of parsed and translated style variation theme.json partials. + * + * @since 6.6.0 + * @var array + */ + protected static $style_variations_cache = array(); + /** * Processes a file that adheres to the theme.json schema * and returns an array with its contents, or a void array if none found. @@ -693,7 +701,7 @@ private static function recursively_iterate_json( $dir ) { } /** - * Determines if a supplied style variation matches the provided scope. + * Determines the scope of a style variation. * * For backwards compatibility, if a variation does not define any scope * related property, e.g. `blockTypes`, it is assumed to be a theme style @@ -701,37 +709,25 @@ private static function recursively_iterate_json( $dir ) { * * @since 6.6.0 * - * @param array $variation Theme.json shaped style variation object. - * @param string $scope Scope to check e.g. theme, block etc. - * @return boolean + * @param array $variation Theme.json shaped style variation object. + * + * @return string */ - private static function style_variation_has_scope( $variation, $scope ) { - if ( 'block' === $scope ) { - return isset( $variation['blockTypes'] ); + protected static function get_style_variation_scope( $variation ) { + if ( isset( $variation['blockTypes'] ) ) { + return 'block'; } - if ( 'theme' === $scope ) { - return ! isset( $variation['blockTypes'] ); - } - - return false; + return 'theme'; } /** - * Returns the style variations defined by the theme. + * Retrieves all style variation partials defined by the theme (parent and child). * - * @since 6.0.0 - * @since 6.2.0 Returns parent theme variations if theme is a child. - * @since 6.6.0 Added configurable scope parameter to allow filtering - * theme.json partial files by the scope to which they - * can be applied e.g. theme vs block etc. - * - * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. - * @return array + * @since 6.6.0 */ - public static function get_style_variations( $scope = 'theme' ) { + protected static function get_style_variation_files() { $variation_files = array(); - $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; $template_directory = get_template_directory() . '/styles'; if ( is_dir( $base_directory ) ) { @@ -750,18 +746,54 @@ public static function get_style_variations( $scope = 'theme' ) { $variation_files = array_merge( $variation_files, $variation_files_parent ); } ksort( $variation_files ); + + return $variation_files; + } + + /** + * Returns the style variations defined by the theme (parent and child). + * + * @since 6.2.0 Returns parent theme variations if theme is a child. + * @since 6.6.0 Added cache and configurable scope parameter to allow + * filtering theme.json partial files by the scope to + * which they can be applied e.g. theme vs block etc. + * + * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. + * @return array + */ + public static function get_style_variations( $scope = 'theme' ) { + $theme_dir = get_stylesheet_directory(); + $locale = get_locale(); + if ( isset( static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ] ) ) { + return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; + } + + $variation_files = static::get_style_variation_files(); + $variations = array( + 'theme' => array(), + 'block' => array(), + ); + foreach ( $variation_files as $path => $file ) { $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); - if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) { - $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); - $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); - if ( empty( $variation['title'] ) ) { - $variation['title'] = basename( $path, '.json' ); + if ( is_array( $decoded_file ) ) { + $variation_scope = static::get_style_variation_scope( $decoded_file ); + + if ( $variation_scope ) { + $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); + $variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); + + if ( empty( $variation['title'] ) ) { + $variation['title'] = basename( $path, '.json' ); + } + + $variations[ $variation_scope ][] = $variation; } - $variations[] = $variation; } } - return $variations; + static::$style_variations_cache[ $theme_dir ][ $locale ] = $variations; + + return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; } /** From fb4809a6caa5a21dd8f90d1332a0a8d9aef1c147 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:44:54 +1000 Subject: [PATCH 2/4] Address feedback --- src/wp-includes/class-wp-theme-json-resolver.php | 3 +-- 1 file changed, 1 insertion(+), 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 d586f1104a2fa..96b1c911d28cc 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -710,7 +710,6 @@ private static function recursively_iterate_json( $dir ) { * @since 6.6.0 * * @param array $variation Theme.json shaped style variation object. - * * @return string */ protected static function get_style_variation_scope( $variation ) { @@ -781,7 +780,7 @@ public static function get_style_variations( $scope = 'theme' ) { if ( $variation_scope ) { $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); - $variation = ( new WP_Theme_JSON_Gutenberg( $translated ) )->get_raw_data(); + $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); if ( empty( $variation['title'] ) ) { $variation['title'] = basename( $path, '.json' ); From 5e08be582106f2c4de3ed2c3a33863465123e2ad Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:43:44 +1000 Subject: [PATCH 3/4] Add more detailed explanatory comment --- .../block-supports/block-style-variations.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-supports/block-style-variations.php b/src/wp-includes/block-supports/block-style-variations.php index bf8122e838d69..43a50f1fe65d0 100644 --- a/src/wp-includes/block-supports/block-style-variations.php +++ b/src/wp-includes/block-supports/block-style-variations.php @@ -474,9 +474,19 @@ function wp_register_block_style_variations_from_theme_json_data( $variations ) * @access private */ function wp_register_block_style_variations_from_theme() { + /* + * Skip any registration of styles if no theme.json or variation partials are present. + * + * Given the possibility of hybrid themes, this check can't rely on if the theme + * is a block theme or not. Instead: + * - If there is a primary theme.json, continue. + * - If there is a partials directory, continue. + * - The only variations to be registered from the global styles user origin, + * are those that have been copied in from the selected theme style variation. + * For a theme style variation to be selected it would have to have a partial + * theme.json file covered by the previous check. + */ $has_partials_directory = is_dir( get_stylesheet_directory() . '/styles' ) || is_dir( get_template_directory() . '/styles' ); - - // Skip any registration of styles if no theme.json or variation partials. if ( ! wp_theme_has_theme_json() && ! $has_partials_directory ) { return; } From 646df131ed574d3bfcc1f9dc7fcf5ab231733c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Mon, 17 Jun 2024 11:52:34 +0200 Subject: [PATCH 4/4] Remove changes unrelated to gutenberg/62529. --- .../class-wp-theme-json-resolver.php | 91 ++++++------------- 1 file changed, 30 insertions(+), 61 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json-resolver.php b/src/wp-includes/class-wp-theme-json-resolver.php index 96b1c911d28cc..fca687cfb9f1b 100644 --- a/src/wp-includes/class-wp-theme-json-resolver.php +++ b/src/wp-includes/class-wp-theme-json-resolver.php @@ -91,14 +91,6 @@ class WP_Theme_JSON_Resolver { */ protected static $theme_json_file_cache = array(); - /** - * Cache of parsed and translated style variation theme.json partials. - * - * @since 6.6.0 - * @var array - */ - protected static $style_variations_cache = array(); - /** * Processes a file that adheres to the theme.json schema * and returns an array with its contents, or a void array if none found. @@ -701,7 +693,7 @@ private static function recursively_iterate_json( $dir ) { } /** - * Determines the scope of a style variation. + * Determines if a supplied style variation matches the provided scope. * * For backwards compatibility, if a variation does not define any scope * related property, e.g. `blockTypes`, it is assumed to be a theme style @@ -709,24 +701,37 @@ private static function recursively_iterate_json( $dir ) { * * @since 6.6.0 * - * @param array $variation Theme.json shaped style variation object. - * @return string + * @param array $variation Theme.json shaped style variation object. + * @param string $scope Scope to check e.g. theme, block etc. + * @return boolean */ - protected static function get_style_variation_scope( $variation ) { - if ( isset( $variation['blockTypes'] ) ) { - return 'block'; + private static function style_variation_has_scope( $variation, $scope ) { + if ( 'block' === $scope ) { + return isset( $variation['blockTypes'] ); + } + + if ( 'theme' === $scope ) { + return ! isset( $variation['blockTypes'] ); } - return 'theme'; + return false; } /** - * Retrieves all style variation partials defined by the theme (parent and child). + * Returns the style variations defined by the theme. * - * @since 6.6.0 + * @since 6.0.0 + * @since 6.2.0 Returns parent theme variations if theme is a child. + * @since 6.6.0 Added configurable scope parameter to allow filtering + * theme.json partial files by the scope to which they + * can be applied e.g. theme vs block etc. + * + * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. + * @return array */ - protected static function get_style_variation_files() { + public static function get_style_variations( $scope = 'theme' ) { $variation_files = array(); + $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; $template_directory = get_template_directory() . '/styles'; if ( is_dir( $base_directory ) ) { @@ -745,54 +750,18 @@ protected static function get_style_variation_files() { $variation_files = array_merge( $variation_files, $variation_files_parent ); } ksort( $variation_files ); - - return $variation_files; - } - - /** - * Returns the style variations defined by the theme (parent and child). - * - * @since 6.2.0 Returns parent theme variations if theme is a child. - * @since 6.6.0 Added cache and configurable scope parameter to allow - * filtering theme.json partial files by the scope to - * which they can be applied e.g. theme vs block etc. - * - * @param string $scope The scope or type of style variation to retrieve e.g. theme, block etc. - * @return array - */ - public static function get_style_variations( $scope = 'theme' ) { - $theme_dir = get_stylesheet_directory(); - $locale = get_locale(); - if ( isset( static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ] ) ) { - return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; - } - - $variation_files = static::get_style_variation_files(); - $variations = array( - 'theme' => array(), - 'block' => array(), - ); - foreach ( $variation_files as $path => $file ) { $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); - if ( is_array( $decoded_file ) ) { - $variation_scope = static::get_style_variation_scope( $decoded_file ); - - if ( $variation_scope ) { - $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); - $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); - - if ( empty( $variation['title'] ) ) { - $variation['title'] = basename( $path, '.json' ); - } - - $variations[ $variation_scope ][] = $variation; + if ( is_array( $decoded_file ) && static::style_variation_has_scope( $decoded_file, $scope ) ) { + $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) ); + $variation = ( new WP_Theme_JSON( $translated ) )->get_raw_data(); + if ( empty( $variation['title'] ) ) { + $variation['title'] = basename( $path, '.json' ); } + $variations[] = $variation; } } - static::$style_variations_cache[ $theme_dir ][ $locale ] = $variations; - - return static::$style_variations_cache[ $theme_dir ][ $locale ][ $scope ]; + return $variations; } /**