Make WordPress Core


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