Skip to content

Commit

Permalink
Fix block style registry variations
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Jun 21, 2024
1 parent 9018714 commit 9d8cf0e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();

Check warning on line 922 in lib/class-wp-theme-json-resolver-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Unused variable $variations_data.

/*
* 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();

Check warning on line 931 in lib/class-wp-theme-json-resolver-gutenberg.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Variable $theme_json_data is undefined.
$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;
}
}

0 comments on commit 9d8cf0e

Please sign in to comment.