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 #6555

Draft
wants to merge 12 commits into
base: trunk
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/wp-includes/block-supports/generated-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ function wp_apply_generated_classname_support( $block_type ) {
if ( $has_generated_classname_support ) {
$block_classname = wp_get_block_default_classname( $block_type->name );

// Add the canonical blocks class name.
if ( block_is_variation( $block_type->name ) ) {
$block_classname .= ' ' . wp_get_block_default_classname( get_canonical_block_name( $block_type->name ) );
}

if ( $block_classname ) {
$attributes['class'] = $block_classname;
}
Expand Down
33 changes: 33 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2356,3 +2356,36 @@ 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;
}
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
6 changes: 3 additions & 3 deletions src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public function register( $block_support_name, $block_support_config ) {
* @return string[] Array of HTML attribute values keyed by their name.
*/
public function apply_block_supports() {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
self::$block_to_render['blockName']
);
// Block variations don't have a registered WP_Block_Type so we need to get the canonical block name.
$block_name = block_is_variation( self::$block_to_render['blockName'] ) ? get_canonical_block_name( self::$block_to_render['blockName'] ) : self::$block_to_render['blockName'];
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );

// If no render_callback, assume styles have been previously handled.
if ( ! $block_type || empty( $block_type ) ) {
Expand Down
15 changes: 13 additions & 2 deletions src/wp-includes/class-wp-block-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,22 @@ public function unregister( $name ) {
* @return WP_Block_Type|null The registered block type, or null if it is not registered.
*/
public function get_registered( $name ) {
if ( ! $this->is_registered( $name ) ) {
// If this is the name of a variation block, get the canonical blocks name since thats the one in the registry.
$block_is_variation = block_is_variation( $name );
$block_name = $block_is_variation ? get_canonical_block_name( $name ) : $name;

if ( ! $this->is_registered( $block_name ) ) {
return null;
}

return $this->registered_block_types[ $name ];
$block_type = $this->registered_block_types[ $block_name ];

if ( $block_is_variation ) {
// If this is a variation block, we set the name to the variation name.
$block_type->name = $name;
}

return $block_type;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ class WP_Block {
* @param WP_Block_Type_Registry $registry Optional block type registry.
*/
public function __construct( $block, $available_context = array(), $registry = null ) {
// Block variations don't have a registered WP_Block_Type so we need to get the canonical block name.
$block_name = block_is_variation( $block['blockName'] ) ? get_canonical_block_name( $block['blockName'] ) : $block['blockName'];
$this->parsed_block = $block;
$this->name = $block['blockName'];
$this->name = $block_name;

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