mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Block Hooks: Introduce a new hooked_block filter.
This is a counterpart to the dynamic `hooked_block_{$block_type}` filter introduced in [57354],
which makes it easier to modify all hooked blocks prior to insertion.
Also adds the hooked block type as an additional argument to both filters for consistency.
Props bernhard-reiter, swissspidy.
Fixes #60574.
git-svn-id: https://develop.svn.wordpress.org/trunk@57660 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
51f0dd6bd8
commit
1280d7a3b5
@ -895,20 +895,35 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke
|
|||||||
/**
|
/**
|
||||||
* Filters the parsed block array for a given hooked block.
|
* Filters the parsed block array for a given hooked block.
|
||||||
*
|
*
|
||||||
* The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
|
|
||||||
*
|
|
||||||
* @since 6.5.0
|
* @since 6.5.0
|
||||||
*
|
*
|
||||||
* @param array $parsed_hooked_block The parsed block array for the given hooked block type.
|
* @param array $parsed_hooked_block The parsed block array for the given hooked block type.
|
||||||
|
* @param string $hooked_block_type The hooked block type name.
|
||||||
* @param string $relative_position The relative position of the hooked block.
|
* @param string $relative_position The relative position of the hooked block.
|
||||||
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||||
* @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
|
* @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
|
||||||
* or pattern that the anchor block belongs to.
|
* or pattern that the anchor block belongs to.
|
||||||
*/
|
*/
|
||||||
$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $relative_position, $parsed_anchor_block, $context );
|
$parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
|
||||||
|
|
||||||
// It's possible that the `hooked_block_{$hooked_block_type}` filter returned a block of a different type,
|
/**
|
||||||
// so we explicitly look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
|
* Filters the parsed block array for a given hooked block.
|
||||||
|
*
|
||||||
|
* The dynamic portion of the hook name, `$hooked_block_type`, refers to the block type name of the specific hooked block.
|
||||||
|
*
|
||||||
|
* @since 6.5.0
|
||||||
|
*
|
||||||
|
* @param array $parsed_hooked_block The parsed block array for the given hooked block type.
|
||||||
|
* @param string $hooked_block_type The hooked block type name.
|
||||||
|
* @param string $relative_position The relative position of the hooked block.
|
||||||
|
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
|
||||||
|
* @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type,
|
||||||
|
* or pattern that the anchor block belongs to.
|
||||||
|
*/
|
||||||
|
$parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
|
||||||
|
|
||||||
|
// It's possible that the filter returned a block of a different type, so we explicitly
|
||||||
|
// look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
|
||||||
if (
|
if (
|
||||||
! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
|
! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
|
||||||
! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
|
! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
|
||||||
|
|||||||
@ -110,7 +110,7 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
|
|||||||
'innerContent' => array(),
|
'innerContent' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
$filter = function ( $parsed_hooked_block, $relative_position, $parsed_anchor_block ) {
|
$filter = function ( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block ) {
|
||||||
// Is the hooked block adjacent to the anchor block?
|
// Is the hooked block adjacent to the anchor block?
|
||||||
if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
|
if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
|
||||||
return $parsed_hooked_block;
|
return $parsed_hooked_block;
|
||||||
@ -124,9 +124,9 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
|
|||||||
|
|
||||||
return $parsed_hooked_block;
|
return $parsed_hooked_block;
|
||||||
};
|
};
|
||||||
add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
|
add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 4 );
|
||||||
$actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
|
$actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
|
||||||
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
|
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter );
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
'<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' {"layout":{"type":"constrained"}} /-->',
|
'<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' {"layout":{"type":"constrained"}} /-->',
|
||||||
@ -172,7 +172,7 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
|
|||||||
};
|
};
|
||||||
add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
|
add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
|
||||||
$actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
|
$actual = insert_hooked_blocks( $anchor_block, 'after', self::HOOKED_BLOCKS, array() );
|
||||||
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
|
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter );
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
'<!-- wp:group --><div class="wp-block-group"><!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /--></div><!-- /wp:group -->',
|
'<!-- wp:group --><div class="wp-block-group"><!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /--></div><!-- /wp:group -->',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user