Make WordPress Core

Changeset 50927


Ignore:
Timestamp:
05/19/2021 01:50:09 PM (3 years ago)
Author:
gziolo
Message:

Editor: Extend register_block_type to accept the path file or folder 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r50919 r50927  
    77 * @since 5.0.0
    88 */
    9 
    10 /**
    11  * Registers a block type.
    12  *
    13  * @since 5.0.0
    14  *
    15  * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
    16  *                                   a complete WP_Block_Type instance. In case a WP_Block_Type
    17  *                                   is provided, the $args parameter will be ignored.
    18  * @param array                $args Optional. Array of block type arguments. Accepts any public property
    19  *                                   of `WP_Block_Type`. See WP_Block_Type::__construct() for information
    20  *                                   on accepted arguments. Default empty array.
    21  * @return WP_Block_Type|false The registered block type on success, or false on failure.
    22  */
    23 function register_block_type( $name, $args = array() ) {
    24     return WP_Block_Type_Registry::get_instance()->register( $name, $args );
    25 }
    26 
    27 /**
    28  * Unregisters a block type.
    29  *
    30  * @since 5.0.0
    31  *
    32  * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
    33  *                                   a complete WP_Block_Type instance.
    34  * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
    35  */
    36 function unregister_block_type( $name ) {
    37     return WP_Block_Type_Registry::get_instance()->unregister( $name );
    38 }
    399
    4010/**
     
    205175
    206176/**
    207  * Registers a block type from metadata stored in the `block.json` file.
     177 * Registers a block type from the metadata stored in the `block.json` file.
    208178 *
    209179 * @since 5.5.0
     
    349319    );
    350320
    351     return register_block_type(
     321    return WP_Block_Type_Registry::get_instance()->register(
    352322        $metadata['name'],
    353323        $settings
    354324    );
     325}
     326
     327/**
     328 * Registers a block type. The recommended way is to register a block type using
     329 * the metadata stored in the `block.json` file.
     330 *
     331 * @since 5.0.0
     332 *
     333 * @param string|WP_Block_Type $block_type Block type name including namespace, or alternatively
     334 *                                         a path to the JSON file with metadata definition for the block,
     335 *                                         or a path to the folder where the `block.json` file is located,
     336 *                                         or a complete WP_Block_Type instance.
     337 *                                         In case a WP_Block_Type is provided, the $args parameter will be ignored.
     338 * @param array                $args       Optional. Array of block type arguments. Accepts any public property
     339 *                                         of `WP_Block_Type`. See WP_Block_Type::__construct() for information
     340 *                                         on accepted arguments. Default empty array.
     341 *
     342 * @return WP_Block_Type|false The registered block type on success, or false on failure.
     343 */
     344function register_block_type( $block_type, $args = array() ) {
     345    if ( is_string( $block_type ) && file_exists( $block_type ) ) {
     346        return register_block_type_from_metadata( $block_type, $args );
     347    }
     348
     349    return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
     350}
     351
     352/**
     353 * Unregisters a block type.
     354 *
     355 * @since 5.0.0
     356 *
     357 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
     358 *                                   a complete WP_Block_Type instance.
     359 * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
     360 */
     361function unregister_block_type( $name ) {
     362    return WP_Block_Type_Registry::get_instance()->unregister( $name );
    355363}
    356364
  • trunk/src/wp-includes/blocks/index.php

    r50824 r50927  
    6060
    6161    foreach ( $block_folders as $block_folder ) {
    62         register_block_type_from_metadata(
     62        register_block_type(
    6363            ABSPATH . WPINC . '/blocks/' . $block_folder
    6464        );
  • trunk/tests/phpunit/tests/blocks/register.php

    r50838 r50927  
    384384
    385385    /**
     386     * @ticket 53233
     387     */
     388    function test_block_register_block_type_proxy_for_metadata() {
     389        $result = register_block_type(
     390            DIR_TESTDATA . '/blocks/notice'
     391        );
     392
     393        $this->assertInstanceOf( 'WP_Block_Type', $result );
     394        $this->assertSame( 'tests/notice', $result->name );
     395    }
     396
     397    /**
    386398     * @ticket 52301
    387399     */
Note: See TracChangeset for help on using the changeset viewer.