Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Hooks: Add new hooked_block filter #6136

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,20 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke
'innerContent' => array(),
);

/**
* Filters the parsed block array for a given 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', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );

/**
* Filters the parsed block array for a given hooked block.
*
Expand All @@ -900,15 +914,16 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke
* @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, $relative_position, $parsed_anchor_block, $context );
$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 `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.
// 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 (
! isset( $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ||
! in_array( $hooked_block_type, $parsed_anchor_block['attrs']['metadata']['ignoredHookedBlocks'], true )
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/blocks/insertHookedBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() {
'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?
if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
return $parsed_hooked_block;
Expand All @@ -124,9 +124,9 @@ public function test_insert_hooked_blocks_filter_can_set_attributes() {

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() );
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
remove_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10 );

$this->assertSame(
'<!-- wp:' . self::HOOKED_BLOCK_TYPE . ' {"layout":{"type":"constrained"}} /-->',
Expand Down Expand Up @@ -172,7 +172,7 @@ public function test_insert_hooked_blocks_filter_can_wrap_block() {
};
add_filter( 'hooked_block_' . self::HOOKED_BLOCK_TYPE, $filter, 10, 3 );
$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, 10 );

$this->assertSame(
'<!-- wp:group --><div class="wp-block-group"><!-- wp:' . self::HOOKED_BLOCK_TYPE . ' /--></div><!-- /wp:group -->',
Expand Down
Loading