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

Block API: Add support for block aliases (PoC) #6523

Draft
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/wp-includes/class-wp-block-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ final class WP_Block_Type_Registry {
*/
private $registered_block_types = array();


/**
* Registered block aliases, as `$alias_name => $instance` pairs.
*
* @since 6.6.0
* var WP_Block_Type[]
*/
private $registered_block_type_aliases = array();

/**
* Container for the main instance of the class.
*
Expand Down Expand Up @@ -94,6 +103,21 @@ public function register( $name, $args = array() ) {
$block_type = new WP_Block_Type( $name, $args );
}

// Register aliases for the block type.
$aliases = $block_type->get_aliases();
if ( ! empty( $aliases ) ) {
foreach ( $aliases as $alias_name => $alias_args ) {
if ( ! $this->is_registered( $alias_name ) ) {
$new_args = array_merge( $args, $alias_args );
$new_args['alias_of'] = $name;
$alias_args['attributes']['metadata'] = array( 'canonicalBlock' => $name );

// Register the alias block.
$this->registered_block_type_aliases[ $alias_name ] = new WP_Block_Type( $alias_name, array_merge( $args, $alias_args ) );
}
}
}

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

return $block_type;
Expand Down Expand Up @@ -142,6 +166,10 @@ public function get_registered( $name ) {
return null;
}

if ( isset( $this->registered_block_type_aliases[ $name ] ) ) {
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
return $this->registered_block_type_aliases[ $name ];
}

return $this->registered_block_types[ $name ];
}

Expand All @@ -153,7 +181,7 @@ public function get_registered( $name ) {
* @return WP_Block_Type[] Associative array of `$block_type_name => $block_type` pairs.
*/
public function get_all_registered() {
return $this->registered_block_types;
return array_merge( $this->registered_block_types, $this->registered_block_type_aliases );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -165,7 +193,7 @@ public function get_all_registered() {
* @return bool True if the block type is registered, false otherwise.
*/
public function is_registered( $name ) {
return isset( $this->registered_block_types[ $name ] );
return isset( $this->registered_block_types[ $name ] ) || isset( $this->registered_block_type_aliases[ $name ] );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}

public function __wakeup() {
Expand Down
54 changes: 54 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ class WP_Block_Type {
*/
public $variation_callback = null;

/**
* List of aliases for this block type. Only accessible through magic getter. null by default.
*
* @since 6.6.0
* @var array[]|null
*/
private $aliases = null;

/**
* If this is an alias block type, this property will contain the name of the block type it is an alias of.
*
* @since 6.6.0
* @var string
*/
public $alias_of = null;

/**
* Custom CSS selectors for theme.json style generation.
*
Expand Down Expand Up @@ -366,6 +382,10 @@ public function __get( $name ) {
return $this->get_variations();
}

if ( 'aliases' === $name ) {
return $this->get_aliases();
}

if ( 'uses_context' === $name ) {
return $this->get_uses_context();
}
Expand Down Expand Up @@ -616,6 +636,40 @@ public function get_variations() {
return apply_filters( 'get_block_type_variations', $this->variations, $this );
}

/**
* Get block aliases.
*
* @since 6.6.0
*
* @return array[]
*/
public function get_aliases() {
if ( ! isset( $this->aliases ) ) {
$this->aliases = array();
$variations = array();
if ( is_callable( $this->variation_callback ) ) {
$variations = call_user_func( $this->variation_callback );
}

foreach ( $variations as $variation ) {
if ( ! empty( $variation['alias'] ) && true === $variation['alias'] ) {
unset( $variation['alias'] );
$this->aliases[][ $variation['name'] ] = $variation;
}
}
}

/**
* Filters the registered aliases for a block type.
*
* @since 6.6.0
*
* @param array $aliases Array of registered aliases for a block type.
* @param WP_Block_Type $block_type The full block type object.
*/
return apply_filters( 'get_block_type_aliases', $this->aliases, $this );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this already matches the existing pattern of using a filter similar to get_block_type_variations, but it's always felt odd to me that there are not more declarative APIs to register variations or in this case, aliases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a method that is a bit more declarative, you can use the variation_callback when registering the block itself which you can see an example of here.

It still feels different to the client-side registration where you can register them separately using an imported function.

}

/**
* Get block uses context.
*
Expand Down
Loading