Make WordPress Core


Ignore:
Timestamp:
02/26/2025 07:38:08 PM (15 months ago)
Author:
flixos90
Message:

Editor: Allow registering block type collections with a single function call.

[59132] introduced the wp_register_block_metadata_collection() function and underlying WP_Block_Metadata_Registry class to allow central registration of a block metadata PHP manifest file in favor of parsing individual JSON files. While this improves performance, it only increases the amount of APIs and code that plugin developers need to use to register their block types properly.

This changeset introduces a new function wp_register_block_types_from_metadata_collection() that improves the developer experience of registering block types from a single source, by handling it in only a single function call.

Developers that already use a generated block metadata PHP manifest file (e.g. via the wp-scripts build-blocks-manifest tool) can now call wp_register_block_types_from_metadata_collection() with that file to automatically register all block types from that block metadata collection. Individual calls to register_block_type() or register_block_type_from_metadata() are no longer necessary when the new function is used.

Props flixos90, gziolo, joemcgill, mreishus, mukesh27, swissspidy.
Fixes #62267.
See #62002.

File:
1 edited

Legend:

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

    r59866 r59874  
    374374
    375375    return $i18n_block_schema;
     376}
     377
     378/**
     379 * Registers all block types from a block metadata collection.
     380 *
     381 * This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided,
     382 * register the metadata collection directly within the same function call.
     383 *
     384 * @since 6.8.0
     385 * @see wp_register_block_metadata_collection()
     386 * @see register_block_type_from_metadata()
     387 *
     388 * @param string $path     The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ).
     389 * @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in
     390 *                         order to register the collection. If this parameter is not provided, the `$path` parameter
     391 *                         must reference a previously registered block metadata collection.
     392 */
     393function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) {
     394    if ( $manifest ) {
     395        wp_register_block_metadata_collection( $path, $manifest );
     396    }
     397
     398    $block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path );
     399    foreach ( $block_metadata_files as $block_metadata_file ) {
     400        register_block_type_from_metadata( $block_metadata_file );
     401    }
    376402}
    377403
Note: See TracChangeset for help on using the changeset viewer.