Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Render variation alias blocks (set canonical name early) #6594

Draft
wants to merge 9 commits into
base: trunk
Choose a base branch
from
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'] .= ' ' . wp_get_block_default_classname( $block_type->name . '/' . $variation );
}
}

return $attributes;
Expand Down
49 changes: 49 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2356,3 +2356,52 @@ function _wp_footnotes_force_filtered_html_on_import_filter( $arg ) {
}
return $arg;
}

/**
* Checks if the block name is a variation of a block. Variations follow a specific format
* of `namespace/block-name/variation-name`.
*
* @access private
* @since 6.6.0
*
* @param string $block_name
* @return boolean
*/
function block_is_variation( $block_name ) {
return substr_count( $block_name, '/' ) === 2;
}

/**
* Returns the canonical block name for a block. If the block is a variation, it will return
* the block name without the variation name.
*
* @access private
* @since 6.6.0
*
* @param string $block_name
* @return string
*/
function get_canonical_block_name( $block_name ) {
if ( block_is_variation( $block_name ) ) {
$parts = explode( '/', $block_name );
return $parts[0] . '/' . $parts[1];
}

return $block_name;
}

function infer_block_variation( $block_type, $block_attributes ) {
$variations = $block_type->get_variations();
foreach ( $variations as $variation ) {
$attributes = $variation['attributes'];
if ( isset( $variation['isActive'] ) && is_array( $variation['isActive'] ) ) {
$attributes = array_intersect_key( $attributes, array_flip( $variation['isActive'] ) );
}
foreach ( $attributes as $attribute => $value ) {
if ( ! isset( $block_attributes[ $attribute ] ) || $block_attributes[ $attribute ] !== $value ) {
continue 2;
}
}
return $variation['name'];
}
}
17 changes: 9 additions & 8 deletions src/wp-includes/class-wp-block-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function next_token() {
* match back in PHP to see which one it was.
*/
$has_match = preg_match(
'/<!--\s+(?P<closer>\/)?wp:(?P<namespace>[a-z][a-z0-9_-]*\/)?(?P<name>[a-z][a-z0-9_-]*)\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s',
'/<!--\s+(?P<closer>\/)?wp:(?P<namespace>[a-z][a-z0-9_-]*\/)?(?P<name>[a-z][a-z0-9_-]*)(?:\/(?P<variation>[a-z][a-z0-9_-]*))?\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s',
$this->document,
$matches,
PREG_OFFSET_CAPTURE,
Expand All @@ -264,13 +264,14 @@ public function next_token() {

list( $match, $started_at ) = $matches[0];

$length = strlen( $match );
$is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1];
$is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1];
$namespace = $matches['namespace'];
$namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/';
$name = $namespace . $matches['name'][0];
$has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1];
$length = strlen( $match );
$is_closer = isset( $matches['closer'] ) && -1 !== $matches['closer'][1];
$is_void = isset( $matches['void'] ) && -1 !== $matches['void'][1];
$is_variation = isset( $matches['variation'] ) && -1 !== $matches['variation'][1];
$namespace = $matches['namespace'];
$namespace = ( isset( $namespace ) && -1 !== $namespace[1] ) ? $namespace[0] : 'core/';
$name = $is_variation ? $namespace . $matches['name'][0] . '/' . $matches['variation'][0] : $namespace . $matches['name'][0];
$has_attrs = isset( $matches['attrs'] ) && -1 !== $matches['attrs'][1];

/*
* Fun fact! It's not trivial in PHP to create "an empty associative array" since all arrays
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function register( $block_support_name, $block_support_config ) {
*/
public function apply_block_supports() {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
self::$block_to_render['blockName']
get_canonical_block_name( self::$block_to_render['blockName'] )
);

// If no render_callback, assume styles have been previously handled.
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class WP_Block {
*/
public function __construct( $block, $available_context = array(), $registry = null ) {
$this->parsed_block = $block;
$this->name = $block['blockName'];
$this->name = get_canonical_block_name( $block['blockName'] );

if ( is_null( $registry ) ) {
$registry = WP_Block_Type_Registry::get_instance();
Expand Down
Loading