WP_Theme_JSON_Resolver::inject_variations_from_block_styles_registryprivate staticWP 6.6.0

Adds variations sourced from the block styles registry to the supplied theme.json data.

Method of the class: WP_Theme_JSON_Resolver{}

No Hooks.

Returns

Array. Theme json data including shared block style variation definitions.

Usage

$result = WP_Theme_JSON_Resolver::inject_variations_from_block_styles_registry( $data );
$data(array) (required)
Array following the theme.json specification.

Changelog

Since 6.6.0 Introduced.

WP_Theme_JSON_Resolver::inject_variations_from_block_styles_registry() code WP 6.8.1

private static function inject_variations_from_block_styles_registry( $data ) {
	$registry = WP_Block_Styles_Registry::get_instance();
	$styles   = $registry->get_all_registered();

	foreach ( $styles as $block_type => $variations ) {
		foreach ( $variations as $variation_name => $variation ) {
			if ( empty( $variation['style_data'] ) ) {
				continue;
			}

			// First, override registry styles with any top-level styles.
			$top_level_data = $data['styles']['variations'][ $variation_name ] ?? array();
			if ( ! empty( $top_level_data ) ) {
				$variation['style_data'] = array_replace_recursive( $variation['style_data'], $top_level_data );
			}

			// Then, override styles so far with any block-level styles.
			$block_level_data = $data['styles']['blocks'][ $block_type ]['variations'][ $variation_name ] ?? array();
			if ( ! empty( $block_level_data ) ) {
				$variation['style_data'] = array_replace_recursive( $variation['style_data'], $block_level_data );
			}

			$path = array( 'styles', 'blocks', $block_type, 'variations', $variation_name );
			_wp_array_set( $data, $path, $variation['style_data'] );
		}
	}

	return $data;
}