Skip to content

Commit

Permalink
Infer block variation name from attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed May 21, 2024
1 parent 52d6b3d commit d7735f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/wp-includes/block-supports/generated-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function wp_get_block_default_classname( $block_name ) {
* @param WP_Block_Type $block_type Block Type.
* @return array Block CSS classes and inline styles.
*/
function wp_apply_generated_classname_support( $block_type ) {
function wp_apply_generated_classname_support( $block_type, $block_attributes ) {
$attributes = array();
$has_generated_classname_support = block_has_support( $block_type, 'className', true );
if ( $has_generated_classname_support ) {
Expand All @@ -57,6 +57,11 @@ function wp_apply_generated_classname_support( $block_type ) {
if ( $block_classname ) {
$attributes['class'] = $block_classname;
}

$variation = infer_block_variation( $block_type, $block_attributes );
if ( $variation ) {
$attributes['class'] .= ' ' . $block_classname . '-' . $variation;
}
}

return $attributes;
Expand Down
12 changes: 12 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2389,3 +2389,15 @@ function get_canonical_block_name( $block_name ) {

return $block_name;
}

function infer_block_variation( $block_type, $attributes ) {
$variations = $block_type->get_variations();
foreach ( $variations as $variation ) {
foreach ( $variation['attributes'] as $attribute => $value ) {
if ( ! isset( $attributes[ $attribute ] ) || $attributes[ $attribute ] !== $value ) {
continue 2;
}
}
return $variation['name'];
}
}

0 comments on commit d7735f8

Please sign in to comment.