Make WordPress Core


Ignore:
Timestamp:
09/14/2023 01:23:13 PM (3 years ago)
Author:
Bernhard Reiter
Message:

General: Add block_hooks field to block type registration, REST API.

In order to implement Block Hooks, we need to add a new block_hooks field to the WP_Block_Type class, as well as to block registration related functions, the block types REST API controller, etc.

Props gziolo.
Fixes #59346. See #59313.

File:
1 edited

Legend:

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

    r56560 r56587  
    343343 * @since 6.1.0 Added support for `render` field.
    344344 * @since 6.3.0 Added `selectors` field.
     345 * @since 6.4.0 Added support for `blockHooks` field.
    345346 *
    346347 * @param string $file_or_folder Path to the JSON file with metadata definition for
     
    514515        }
    515516
     517        if ( ! empty( $metadata['blockHooks'] ) ) {
     518                /**
     519                 * Map camelCased position string (from block.json) to snake_cased block type position.
     520                 *
     521                 * @var array
     522                 */
     523                $position_mappings = array(
     524                        'before'     => 'before',
     525                        'after'      => 'after',
     526                        'firstChild' => 'first_child',
     527                        'lastChild'  => 'last_child',
     528                );
     529
     530                $settings['block_hooks'] = array();
     531                foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
     532                        // Avoid infinite recursion (hooking to itself).
     533                        if ( $metadata['name'] === $anchor_block_name ) {
     534                                _doing_it_wrong(
     535                                        __METHOD__,
     536                                        __( 'Cannot hook block to itself.', 'gutenberg' ),
     537                                        '6.4.0'
     538                                );
     539                                continue;
     540                        }
     541
     542                        if ( ! isset( $position_mappings[ $position ] ) ) {
     543                                continue;
     544                        }
     545
     546                        $settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
     547                }
     548        }
     549
    516550        if ( ! empty( $metadata['render'] ) ) {
    517551                $template_path = wp_normalize_path(
Note: See TracChangeset for help on using the changeset viewer.