Make WordPress Core

Changeset 59874


Ignore:
Timestamp:
02/26/2025 07:38:08 PM (18 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.

Location:
trunk
Files:
3 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
  • trunk/src/wp-includes/class-wp-block-metadata-registry.php

    r59730 r59874  
    181181
    182182        /**
     183         * Gets the list of absolute paths to all block metadata files that are part of the given collection.
     184         *
     185         * For instance, if a block metadata collection is registered with path `WP_PLUGIN_DIR . '/my-plugin/blocks/'`,
     186         * and the manifest file includes metadata for two blocks `'block-a'` and `'block-b'`, the result of this method
     187         * will be an array containing:
     188         * * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-a/block.json'`
     189         * * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-b/block.json'`
     190         *
     191         * @since 6.8.0
     192         *
     193         * @param string $path The absolute base path for a previously registered collection.
     194         * @return string[] List of block metadata file paths, or an empty array if the given `$path` is invalid.
     195         */
     196        public static function get_collection_block_metadata_files( $path ) {
     197                $path = wp_normalize_path( rtrim( $path, '/' ) );
     198
     199                if ( ! isset( self::$collections[ $path ] ) ) {
     200                        _doing_it_wrong(
     201                                __METHOD__,
     202                                __( 'No registered block metadata collection was found for the provided path.' ),
     203                                '6.8.0'
     204                        );
     205                        return array();
     206                }
     207
     208                $collection = &self::$collections[ $path ];
     209
     210                if ( null === $collection['metadata'] ) {
     211                        // Load the manifest file if not already loaded.
     212                        $collection['metadata'] = require $collection['manifest'];
     213                }
     214
     215                return array_map(
     216                        static function ( $block_name ) use ( $path ) {
     217                                return "{$path}/{$block_name}/block.json";
     218                        },
     219                        array_keys( $collection['metadata'] )
     220                );
     221        }
     222
     223        /**
    183224         * Finds the collection path for a given file or folder.
    184225         *
  • trunk/tests/phpunit/tests/blocks/wpBlockMetadataRegistry.php

    r59730 r59874  
    55 *
    66 * @group blocks
     7 * @coversDefaultClass WP_Block_Metadata_Registry
    78 */
    89class Tests_Blocks_WpBlockMetadataRegistry extends WP_UnitTestCase {
     
    189190                $this->assertFalse( $result, 'Non-existent manifest should not be registered' );
    190191        }
     192
     193        /**
     194         * Tests that the `get_collection_block_metadata_files()` method returns the expected list of block metadata files.
     195         *
     196         * @ticket 62267
     197         * @covers ::get_collection_block_metadata_files
     198         */
     199        public function test_get_collection_block_metadata_files() {
     200                $path          = WP_PLUGIN_DIR . '/test-plugin/data/block-types';
     201                $manifest_data = array(
     202                        'a-block'       => array(
     203                                'name'  => 'a-block',
     204                                'title' => 'A Block',
     205                        ),
     206                        'another-block' => array(
     207                                'name'  => 'another-block',
     208                                'title' => 'Another Block',
     209                        ),
     210                );
     211
     212                file_put_contents( $this->temp_manifest_file, '<?php return ' . var_export( $manifest_data, true ) . ';' );
     213
     214                $this->assertTrue( WP_Block_Metadata_Registry::register_collection( $path, $this->temp_manifest_file ) );
     215                $this->assertSame(
     216                        array(
     217                                WP_PLUGIN_DIR . '/test-plugin/data/block-types/a-block/block.json',
     218                                WP_PLUGIN_DIR . '/test-plugin/data/block-types/another-block/block.json',
     219                        ),
     220                        WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path )
     221                );
     222        }
    191223}
Note: See TracChangeset for help on using the changeset viewer.