Skip to content
Closed
Next Next commit
Register block style variations defined by the theme on init
  • Loading branch information
oandregal committed Jun 12, 2024
commit cdba16a8a62c096c305813adc5d79ee8a0ae36ef
80 changes: 80 additions & 0 deletions src/wp-includes/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,83 @@ function wp_enqueue_block_style_variation_styles() {
add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 );

add_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation', 10, 1 );

/**
* @access private
*/
function wp_register_block_style_variations_from_theme_json_data( $variations ) {
$variations_data = array();

if ( empty( $variations ) ) {
return $variations_data;
}

$registry = WP_Block_Styles_Registry::get_instance();
$have_named_variations = ! wp_is_numeric_array( $variations );

foreach ( $variations as $key => $variation ) {
$supported_blocks = $variation['blockTypes'] ?? array();

/*
* Standalone theme.json partial files for block style variations
* will have their styles under a top-level property by the same name.
* Variations defined within an existing theme.json or theme style
* variation will themselves already be the required styles data.
*/
$variation_data = $variation['styles'] ?? $variation;

if ( empty( $variation_data ) ) {
continue;
}

/*
* Block style variations read in via standalone theme.json partials
* need to have their name set to the kebab case version of their title.
*/
$variation_name = $have_named_variations ? $key : _wp_to_kebab_case( $variation['title'] );
$variation_label = $variation['title'] ?? $variation_name;

foreach ( $supported_blocks as $block_type ) {
$registered_styles = $registry->get_registered_styles_for_block( $block_type );

// Register block style variation if it hasn't already been registered.
if ( ! array_key_exists( $variation_name, $registered_styles ) ) {
register_block_style(
$block_type,
array(
'name' => $variation_name,
'label' => $variation_label,
)
);
}
}
}
}

/**
* @access private
*
* Register the block style variations defined by the theme.
* These can come in three forms:
*
* - the theme's theme.json
* - the theme's partials (standalone files in styles that only define block style variations)
* - the user's theme.json (for example, theme style variations the user selected)
*
*/
function wp_register_block_style_variations_from_theme() {
// Partials.
$variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
wp_register_block_style_variations_from_theme_json_data( $variations_partials );

// theme.json of the theme.
$theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data();
$variations_theme = $theme_json_theme->get_data()['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations_theme );

// User data linked for this theme.
$theme_json_user = WP_Theme_JSON_Resolver::get_user_data();
$variations_user = $theme_json_user->get_data()['styles']['blocks']['variations'] ?? array();
wp_register_block_style_variations_from_theme_json_data( $variations_user );
}
add_filter( 'init', 'wp_register_block_style_variations_from_theme' );