Skip to content

Commit

Permalink
Editor: Extend register_block_type to accept the path file or folde…
Browse files Browse the repository at this point in the history
…r with `block.json`

Rather than using two distinct methods to register block types in WordPress core, let's make `register_block_type` the canonical method to deal with all use cases. In practice, the patch proposed extends its usage to work as a proxy to `register_block_type_from_metadata`. It should remove some confusion that we observed and let us be more explicit what's the latest recommendation.

Props matveb, mcsf.
Fixes #53233.



git-svn-id: https://develop.svn.wordpress.org/trunk@50927 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed May 19, 2021
1 parent fe9ab38 commit 6ad4e34
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
72 changes: 40 additions & 32 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,6 @@
* @since 5.0.0
*/

/**
* Registers a block type.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively
* a complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args Optional. Array of block type arguments. Accepts any public property
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information
* on accepted arguments. Default empty array.
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type( $name, $args = array() ) {
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
}

/**
* Unregisters a block type.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively
* a complete WP_Block_Type instance.
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
*/
function unregister_block_type( $name ) {
return WP_Block_Type_Registry::get_instance()->unregister( $name );
}

/**
* Removes the block asset's path prefix if provided.
*
Expand Down Expand Up @@ -204,7 +174,7 @@ function register_block_style_handle( $metadata, $field_name ) {
}

/**
* Registers a block type from metadata stored in the `block.json` file.
* Registers a block type from the metadata stored in the `block.json` file.
*
* @since 5.5.0
*
Expand Down Expand Up @@ -348,12 +318,50 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$metadata
);

return register_block_type(
return WP_Block_Type_Registry::get_instance()->register(
$metadata['name'],
$settings
);
}

/**
* Registers a block type. The recommended way is to register a block type using
* the metadata stored in the `block.json` file.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively
* a path to the JSON file with metadata definition for the block,
* or a path to the folder where the `block.json` file is located,
* or a complete WP_Block_Type instance.
* In case a WP_Block_Type is provided, the $args parameter will be ignored.
* @param array $args Optional. Array of block type arguments. Accepts any public property
* of `WP_Block_Type`. See WP_Block_Type::__construct() for information
* on accepted arguments. Default empty array.
*
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type( $block_type, $args = array() ) {
if ( is_string( $block_type ) && file_exists( $block_type ) ) {
return register_block_type_from_metadata( $block_type, $args );
}

return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
}

/**
* Unregisters a block type.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively
* a complete WP_Block_Type instance.
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
*/
function unregister_block_type( $name ) {
return WP_Block_Type_Registry::get_instance()->unregister( $name );
}

/**
* Determine whether a post or content string has blocks.
*
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function register_core_block_types_from_metadata() {
);

foreach ( $block_folders as $block_folder ) {
register_block_type_from_metadata(
register_block_type(
ABSPATH . WPINC . '/blocks/' . $block_folder
);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ function test_block_registers_with_metadata_fixture() {
);
}

/**
* @ticket 53233
*/
function test_block_register_block_type_proxy_for_metadata() {
$result = register_block_type(
DIR_TESTDATA . '/blocks/notice'
);

$this->assertInstanceOf( 'WP_Block_Type', $result );
$this->assertSame( 'tests/notice', $result->name );
}

/**
* @ticket 52301
*/
Expand Down

0 comments on commit 6ad4e34

Please sign in to comment.