Make WordPress Core


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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.