From 9d8cf0ed19a45321502b71f1722936e64278282d Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:48:34 +1000 Subject: [PATCH] Fix block style registry variations --- ...class-wp-theme-json-resolver-gutenberg.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index b4033c6998071..daa05a43d52e0 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -260,6 +260,12 @@ public static function get_theme_data( $deprecated = array(), $options = array() */ $theme_json_data = static::inject_shared_variations_from_theme_json_partials( $theme_json_data, $variations ); + /* + * Merge any block style variations registered through the WP_Block_Styles_Registry + * with a style object. + */ + $theme_json_data = static::inject_shared_variations_from_style_registry( $theme_json_data ); + /** * Filters the data provided by the theme for global styles and settings. * @@ -865,6 +871,8 @@ public static function resolve_theme_file_uris( $theme_json ) { * Adds shared block style variation definitions sourced from theme.json partials * to the supplied theme.json data. * + * @since 6.6.0 + * * @param array $data Array following the theme.json specification. * @param array $variations Shared block style variations. * @return array Theme json data including shared block style variation definitions. @@ -899,4 +907,35 @@ private static function inject_shared_variations_from_theme_json_partials( $data return $data; } + + /** + * Adds shared block style variation definitions sourced from the WP_Block_Styles_Registry. + * + * @since 6.6.0 + * + * @param array $data Array following the theme.json specification. + * @return array Theme json data including shared block style variation definitions. + */ + private static function inject_shared_variations_from_style_registry( $data ) { + $registry = WP_Block_Styles_Registry::get_instance(); + $styles = $registry->get_all_registered(); + $variations_data = array(); + + /* + * As the block style registry stores the styles per block type we don't have + * a shared variation to inject. It will go directly into the block type's variations. + */ + foreach ( $styles as $block_type => $variations ) { + foreach ( $variations as $variation_name => $variation ) { + if ( ! empty( $variation['style_data'] ) ) { + $current_variation = $theme_json_data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array(); + $merged_variation = array_replace_recursive( $variation['style_data'], $current_variation ); + $path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name ); + _wp_array_set( $data, $path, $merged_variation ); + } + } + } + + return $data; + } }