Block Hooks API: Insert metadata at the same time as hooked blocks.
The Block Hooks UI relies on the `ignoredHookedBlocks` metadata when determining whether to show the toggle in the Site Editor. The problem is that for uncustomized templates we don't add this metadata. Currently the visitor functions have a default callback of `insert_hooked_blocks` and we only add the metadata when we're writing to the database via an available filter in the template controller. This changeset creates a new callback which both inserts the hooked blocks and adds the `ignoredHookedBlocks` metadata to the anchor block, and uses this new callback explicitly in the visitor functions that are run upon reading from the database. We continue to set the `ignoredHookedBlocks` metadata when writing to the database, i.e. this operation happens twice. Although not ideal, this is necessary to cover the following scenarios: * When the user adds an anchor block within the editor, we still need to add the `ignoredHookedBlocks` meta to it to prevent hooked blocks hooking on to it unexpectedly on the frontend. This is required to keep parity between the frontend and editor. * When a user writes template data to the database directly through the API (instead of the editor), we need to again ensure we're not inserting hooked blocks unexpectedly. It is worth noting that with this change, the first hooked block to insert relative to its anchor block will be accepted. Any additional blocks of the same type (e.g. a second `core/loginout` block) trying to hook onto the same anchor block will be ignored, irrespective of the position. Props tomjcafferkey, bernhard-reiter, gziolo. Fixes #59574. Built from https://develop.svn.wordpress.org/trunk@58186 git-svn-id: http://core.svn.wordpress.org/trunk@57649 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
bdebc89143
commit
031dbe0b4d
|
@ -598,8 +598,8 @@ function _build_block_template_result_from_file( $template_file, $template_type
|
|||
$after_block_visitor = null;
|
||||
$hooked_blocks = get_hooked_blocks();
|
||||
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
}
|
||||
$blocks = parse_blocks( $template->content );
|
||||
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
|
||||
|
@ -984,8 +984,8 @@ function _build_block_template_result_from_post( $post ) {
|
|||
|
||||
$hooked_blocks = get_hooked_blocks();
|
||||
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
$blocks = parse_blocks( $template->content );
|
||||
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
|
||||
}
|
||||
|
|
|
@ -858,11 +858,11 @@ function get_hooked_blocks() {
|
|||
* @since 6.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||
* @param string $relative_position The relative position of the hooked blocks.
|
||||
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
|
||||
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
|
||||
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
|
||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||
* @param string $relative_position The relative position of the hooked blocks.
|
||||
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
|
||||
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
|
||||
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern that the anchor block belongs to.
|
||||
* @return string
|
||||
*/
|
||||
function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
|
||||
|
@ -949,12 +949,12 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke
|
|||
* @since 6.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||
* @param string $relative_position The relative position of the hooked blocks.
|
||||
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
|
||||
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
|
||||
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
|
||||
* @return string An empty string.
|
||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||
* @param string $relative_position The relative position of the hooked blocks.
|
||||
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
|
||||
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
|
||||
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern that the anchor block belongs to.
|
||||
* @return string Empty string.
|
||||
*/
|
||||
function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
|
||||
$anchor_block_type = $parsed_anchor_block['blockName'];
|
||||
|
@ -1002,6 +1002,29 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po
|
|||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the markup for blocks hooked to the given anchor block in a specific relative position and then
|
||||
* adds a list of hooked block types to an anchor block's ignored hooked block types.
|
||||
*
|
||||
* This function is meant for internal use only.
|
||||
*
|
||||
* @since 6.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||
* @param string $relative_position The relative position of the hooked blocks.
|
||||
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
|
||||
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
|
||||
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern that the anchor block belongs to.
|
||||
* @return string
|
||||
*/
|
||||
function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
|
||||
$markup = insert_hooked_blocks( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
|
||||
$markup .= set_ignored_hooked_blocks_metadata( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
|
||||
|
||||
return $markup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
|
||||
*
|
||||
|
|
|
@ -174,8 +174,8 @@ final class WP_Block_Patterns_Registry {
|
|||
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
|
||||
$after_block_visitor = null;
|
||||
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern );
|
||||
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
|
||||
}
|
||||
$blocks = parse_blocks( $content );
|
||||
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.6-alpha-58185';
|
||||
$wp_version = '6.6-alpha-58186';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue