WP_Theme_JSON::unwrap_shared_block_style_variations()private staticWP 6.6.0

Unwraps shared block style variations.

It takes the shared variations (styles.variations.variationName) and applies them to all the blocks that have the given variation registered (styles.blocks.blockType.variations.variationName).

For example, given the core/paragraph and core/group blocks have registered the section-a style variation, and given the following input:

{ "styles": {

"variations": {
  "section-a": { "color": { "background": "backgroundColor" } }
}

} }

It returns the following output:

{ "styles": {

"blocks": {
  "core/paragraph": {
	"variations": {
		"section-a": { "color": { "background": "backgroundColor" } }
	},
  },
  "core/group": {
	"variations": {
	  "section-a": { "color": { "background": "backgroundColor" } }
	}
  }
}

} }

Method of the class: WP_Theme_JSON{}

No Hooks.

Return

Array. Theme json data with shared variation definitions unwrapped under appropriate block types.

Usage

$result = WP_Theme_JSON::unwrap_shared_block_style_variations( $theme_json, $valid_variations );
$theme_json(array) (required)
A structure that follows the theme.json schema.
$valid_variations(array) (required)
Valid block style variations.

Changelog

Since 6.6.0 Introduced.

WP_Theme_JSON::unwrap_shared_block_style_variations() code WP 6.6.2

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 ( $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 );
			if ( ! empty( $merged_data ) ) {
				_wp_array_set( $new_theme_json, array( 'styles', 'blocks', $block_type, 'variations', $variation_name ), $merged_data );
			}
		}
	}

	unset( $new_theme_json['styles']['variations'] );

	return $new_theme_json;
}