Make WordPress Core

Changeset 59132


Ignore:
Timestamp:
09/30/2024 05:05:37 PM (2 months ago)
Author:
flixos90
Message:

Editor: Allow registering PHP manifest file for block metadata collections for enhanced performance.

Typically, when registering a new block type, its metadata is read from the provided block.json file. The more block types are registered on a site, the more costly becomes this process, as it involves filesystem reads and parsing JSON.

WordPress Core's built-in blocks have in the past worked around that by having a auto-generated PHP manifest file that includes the already parsed JSON data for all blocks. This changeset effectively allows plugins to do the same, by introducing a new API function wp_register_block_metadata_collection(). The WordPress Core block manifest is now handled using this API as well, rather than custom logic baked into register_block_type_from_metadata().

The wp_register_block_metadata_collection() function requires two parameters:

  • $path: The base path in which block files for the collection reside.
  • $manifest: The path to the manifest file for the collection.

Every block.json file that is supposed to be part of the collection must reside within the provided $path, within its own block-specific directory matching the block name (without the block namespace). For example, for a collection $path of /wp-content/plugins/test-plugin and a block test-plugin/testimonial, the block file could be /wp-content/plugins/test-plugins/blocks/testimonial/block.json.

It is recommended that plugins use the new API function for enhanced performance, especially if they register several block types. However, the use of the function is entirely optional. Not using it will not result in any difference in user-facing behavior.

Props mreishus, flixos90, gziolo, spacedmonkey, azaozz, mukesh27.
Fixes #62002.

Location:
trunk
Files:
3 added
3 edited

Legend:

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

    r59124 r59132  
    377377
    378378/**
     379 * Registers a block metadata collection.
     380 *
     381 * This function allows core and third-party plugins to register their block metadata
     382 * collections in a centralized location. Registering collections can improve performance
     383 * by avoiding multiple reads from the filesystem and parsing JSON.
     384 *
     385 * @since 6.7.0
     386 *
     387 * @param string $path     The base path in which block files for the collection reside.
     388 * @param string $manifest The path to the manifest file for the collection.
     389 */
     390function wp_register_block_metadata_collection( $path, $manifest ) {
     391    WP_Block_Metadata_Registry::register_collection( $path, $manifest );
     392}
     393
     394/**
    379395 * Registers a block type from the metadata stored in the `block.json` file.
    380396 *
     
    403419     * Using a static variable ensures that the metadata is only read once per request.
    404420     */
    405     static $core_blocks_meta;
    406     if ( ! $core_blocks_meta ) {
    407         $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
    408     }
    409421
    410422    $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
     
    412424        $file_or_folder;
    413425
    414     $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC );
    415     // If the block is not a core block, the metadata file must exist.
     426    $is_core_block        = str_starts_with( $file_or_folder, ABSPATH . WPINC );
    416427    $metadata_file_exists = $is_core_block || file_exists( $metadata_file );
    417     if ( ! $metadata_file_exists && empty( $args['name'] ) ) {
    418         return false;
    419     }
    420 
    421     // Try to get metadata from the static cache for core blocks.
    422     $metadata = array();
    423     if ( $is_core_block ) {
    424         $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
    425         if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
    426             $metadata = $core_blocks_meta[ $core_block_name ];
    427         }
    428     }
    429 
    430     // If metadata is not found in the static cache, read it from the file.
    431     if ( $metadata_file_exists && empty( $metadata ) ) {
     428    $registry_metadata    = WP_Block_Metadata_Registry::get_metadata( $file_or_folder );
     429
     430    if ( $registry_metadata ) {
     431        $metadata = $registry_metadata;
     432    } elseif ( $metadata_file_exists ) {
    432433        $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
     434    } else {
     435        $metadata = array();
    433436    }
    434437
  • trunk/src/wp-includes/blocks/index.php

    r59117 r59132  
    161161}
    162162add_action( 'init', 'register_core_block_types_from_metadata' );
     163
     164/**
     165 * Registers the core block metadata collection.
     166 *
     167 * This function is hooked into the 'init' action with a priority of 9,
     168 * ensuring that the core block metadata is registered before the regular
     169 * block initialization that happens at priority 10.
     170 *
     171 * @since 6.7.0
     172 */
     173function wp_register_core_block_metadata_collection() {
     174    wp_register_block_metadata_collection(
     175        BLOCKS_PATH,
     176        BLOCKS_PATH . 'blocks-json.php'
     177    );
     178}
     179add_action( 'init', 'wp_register_core_block_metadata_collection', 9 );
  • trunk/src/wp-settings.php

    r59107 r59132  
    356356require ABSPATH . WPINC . '/class-wp-block.php';
    357357require ABSPATH . WPINC . '/class-wp-block-list.php';
     358require ABSPATH . WPINC . '/class-wp-block-metadata-registry.php';
    358359require ABSPATH . WPINC . '/class-wp-block-parser-block.php';
    359360require ABSPATH . WPINC . '/class-wp-block-parser-frame.php';
Note: See TracChangeset for help on using the changeset viewer.