Skip to content

Commit b5e5f4d

Browse files
committed
Register block style variations defined by the theme on init
1 parent 5115c7a commit b5e5f4d

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

src/wp-includes/block-supports/block-style-variations.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,83 @@ function wp_enqueue_block_style_variation_styles() {
422422
add_filter( 'wp_theme_json_data_theme', 'wp_resolve_block_style_variations_from_styles_registry', 10, 1 );
423423

424424
add_filter( 'wp_theme_json_data_user', 'wp_resolve_block_style_variations_from_theme_style_variation', 10, 1 );
425+
426+
/**
427+
* @access private
428+
*/
429+
function wp_register_block_style_variations_from_theme_json_data( $variations ) {
430+
$variations_data = array();
431+
432+
if ( empty( $variations ) ) {
433+
return $variations_data;
434+
}
435+
436+
$registry = WP_Block_Styles_Registry::get_instance();
437+
$have_named_variations = ! wp_is_numeric_array( $variations );
438+
439+
foreach ( $variations as $key => $variation ) {
440+
$supported_blocks = $variation['blockTypes'] ?? array();
441+
442+
/*
443+
* Standalone theme.json partial files for block style variations
444+
* will have their styles under a top-level property by the same name.
445+
* Variations defined within an existing theme.json or theme style
446+
* variation will themselves already be the required styles data.
447+
*/
448+
$variation_data = $variation['styles'] ?? $variation;
449+
450+
if ( empty( $variation_data ) ) {
451+
continue;
452+
}
453+
454+
/*
455+
* Block style variations read in via standalone theme.json partials
456+
* need to have their name set to the kebab case version of their title.
457+
*/
458+
$variation_name = $have_named_variations ? $key : _wp_to_kebab_case( $variation['title'] );
459+
$variation_label = $variation['title'] ?? $variation_name;
460+
461+
foreach ( $supported_blocks as $block_type ) {
462+
$registered_styles = $registry->get_registered_styles_for_block( $block_type );
463+
464+
// Register block style variation if it hasn't already been registered.
465+
if ( ! array_key_exists( $variation_name, $registered_styles ) ) {
466+
register_block_style(
467+
$block_type,
468+
array(
469+
'name' => $variation_name,
470+
'label' => $variation_label,
471+
)
472+
);
473+
}
474+
}
475+
}
476+
}
477+
478+
/**
479+
* @access private
480+
*
481+
* Register the block style variations defined by the theme.
482+
* These can come in three forms:
483+
*
484+
* - the theme's theme.json
485+
* - the theme's partials (standalone files in styles that only define block style variations)
486+
* - the user's theme.json (for example, theme style variations the user selected)
487+
*
488+
*/
489+
function wp_register_block_style_variations_from_theme() {
490+
// Partials.
491+
$variations_partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
492+
wp_register_block_style_variations_from_theme_json_data( $variations_partials );
493+
494+
// theme.json of the theme.
495+
$theme_json_theme = WP_Theme_JSON_Resolver::get_theme_data();
496+
$variations_theme = $theme_json_theme->get_data()['styles']['blocks']['variations'] ?? array();
497+
wp_register_block_style_variations_from_theme_json_data( $variations_theme );
498+
499+
// User data linked for this theme.
500+
$theme_json_user = WP_Theme_JSON_Resolver::get_user_data();
501+
$variations_user = $theme_json_user->get_data()['styles']['blocks']['variations'] ?? array();
502+
wp_register_block_style_variations_from_theme_json_data( $variations_user );
503+
}
504+
add_filter( 'init', 'wp_register_block_style_variations_from_theme' );

0 commit comments

Comments
 (0)