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