mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 21:00:21 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@56587 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -342,6 +342,7 @@ function get_block_metadata_i18n_schema() {
|
||||
* @since 5.9.0 Added support for `variations` and `viewScript` fields.
|
||||
* @since 6.1.0 Added support for `render` field.
|
||||
* @since 6.3.0 Added `selectors` field.
|
||||
* @since 6.4.0 Added support for `blockHooks` field.
|
||||
*
|
||||
* @param string $file_or_folder Path to the JSON file with metadata definition for
|
||||
* the block or path to the folder where the `block.json` file is located.
|
||||
@@ -513,6 +514,39 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $metadata['blockHooks'] ) ) {
|
||||
/**
|
||||
* Map camelCased position string (from block.json) to snake_cased block type position.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
$position_mappings = array(
|
||||
'before' => 'before',
|
||||
'after' => 'after',
|
||||
'firstChild' => 'first_child',
|
||||
'lastChild' => 'last_child',
|
||||
);
|
||||
|
||||
$settings['block_hooks'] = array();
|
||||
foreach ( $metadata['blockHooks'] as $anchor_block_name => $position ) {
|
||||
// Avoid infinite recursion (hooking to itself).
|
||||
if ( $metadata['name'] === $anchor_block_name ) {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'Cannot hook block to itself.', 'gutenberg' ),
|
||||
'6.4.0'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! isset( $position_mappings[ $position ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$settings['block_hooks'][ $anchor_block_name ] = $position_mappings[ $position ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $metadata['render'] ) ) {
|
||||
$template_path = wp_normalize_path(
|
||||
realpath(
|
||||
|
||||
Reference in New Issue
Block a user