Skip to content

Commit

Permalink
Update unwrapping to include variations for core blocks not in registry
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Jun 22, 2024
1 parent 334ad2f commit bc758fc
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,11 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut
}

$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json );
$registry = WP_Block_Type_Registry::get_instance();
$valid_block_names = array_keys( $registry->get_all_registered() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations();
$this->theme_json = static::unwrap_shared_block_style_variations( $this->theme_json, $valid_variations );
$this->theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$this->theme_json = static::maybe_opt_in_into_settings( $this->theme_json );

Expand Down Expand Up @@ -830,25 +830,21 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut
*
* @since 6.6.0
*
* @param array $theme_json A structure that follows the theme.json schema.
* @param array $theme_json A structure that follows the theme.json schema.
* @param array $valid_variations Valid block style variations.
*
* @return array Theme json data with shared variation definitions unwrapped under appropriate block types.
*/
private static function unwrap_shared_block_style_variations( $theme_json ) {
if ( ! isset( $theme_json['styles']['variations'] ) ) {
return $theme_json;
}

$registered_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();

if ( empty( $registered_styles ) ) {
private static function unwrap_shared_block_style_variations( $theme_json, $valid_variations ) {
if ( empty( $theme_json['styles']['variations'] ) || empty( $valid_variations ) ) {
return $theme_json;
}

$new_theme_json = $theme_json;
$variations = $new_theme_json['styles']['variations'];

foreach ( $registered_styles as $block_type => $registered_variations ) {
foreach ( $registered_variations as $variation_name => $variation_data ) {
foreach ( $valid_variations as $block_type => $registered_variations ) {
foreach ( $registered_variations as $variation_name ) {
$block_level_data = $new_theme_json['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
$top_level_data = $variations[ $variation_name ] ?? array();
$merged_data = array_replace_recursive( $top_level_data, $block_level_data );
Expand Down

0 comments on commit bc758fc

Please sign in to comment.