Block Hooks: Allow hooked_block filters to return null.

Allow returning null from the `hooked_block` and `hooked_block_{$hooked_block_type}` filters to suppress a hooked block from being inserted. This is required to allow extenders conditionally inserting a hooked block based on e.g. the value of an attribute of the anchor block.

Props swissspidy, gziolo, joshuatf, tomjcafferkey.
Fixes 60580.

git-svn-id: https://develop.svn.wordpress.org/trunk@57668 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
bernhard-reiter
2024-02-20 15:20:39 +00:00
parent 804360948c
commit 3a206f74a1
3 changed files with 95 additions and 2 deletions
@@ -180,4 +180,42 @@ class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
"Markup wasn't generated correctly for hooked block wrapped in Group block by filter."
);
}
/**
* @ticket 60580
*
* @covers ::insert_hooked_blocks
*/
public function test_insert_hooked_blocks_filter_can_suppress_hooked_block() {
$anchor_block = array(
'blockName' => self::ANCHOR_BLOCK_TYPE,
'attrs' => array(
'layout' => array(
'type' => 'flex',
),
),
'innerContent' => array(),
);
$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;
}
if (
isset( $parsed_anchor_block['attrs']['layout']['type'] ) &&
'flex' === $parsed_anchor_block['attrs']['layout']['type']
) {
return null;
}
return $parsed_hooked_block;
};
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 );
$this->assertSame( '', $actual, "No markup should've been generated for hooked block suppressed by filter." );
}
}
@@ -144,4 +144,36 @@ class Tests_Blocks_SetIgnoredHookedBlocksMetadata extends WP_UnitTestCase {
$anchor_block['attrs']['metadata']['ignoredHookedBlocks']
);
}
/**
* @ticket 60580
*
* @covers ::set_ignored_hooked_blocks_metadata
*/
public function test_set_ignored_hooked_blocks_metadata_for_block_suppressed_by_filter() {
$anchor_block = array(
'blockName' => 'tests/anchor-block',
'attrs' => array(),
);
$hooked_blocks = array(
'tests/anchor-block' => array(
'after' => array( 'tests/hooked-block', 'tests/hooked-block-suppressed-by-filter' ),
),
);
$filter = function ( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block ) {
if ( 'tests/anchor-block' === $parsed_anchor_block['blockName'] && 'after' === $relative_position ) {
return null;
}
return $parsed_hooked_block;
};
add_filter( 'hooked_block_tests/hooked-block-suppressed-by-filter', $filter, 10, 4 );
set_ignored_hooked_blocks_metadata( $anchor_block, 'after', $hooked_blocks, null );
remove_filter( 'hooked_block_tests/hooked-block-suppressed-by-filter', $filter );
$this->assertSame( array( 'tests/hooked-block' ), $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] );
}
}