WP_Theme_JSON::unwrap_shared_block_style_variations( array $theme_json, array $valid_variations ): array

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Unwraps shared block style variations.

Description

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” } } } } } } }

Parameters

$theme_jsonarrayrequired
A structure that follows the theme.json schema.
$valid_variationsarrayrequired
Valid block style variations.

Return

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

Source

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;
}

Changelog

VersionDescription
6.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.