Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a36ac67
Block Hooks: Extract `insert_hooked_blocks()` function
ockham Jan 3, 2024
5350ae2
Introduce hooked_block filter
ockham Jan 3, 2024
e63213c
get_hooked_block_markup: Change argument order
ockham Jan 3, 2024
a891d3c
Fix get_hooked_block_markup tests
ockham Jan 3, 2024
fabd9c2
Make block type name part of filter name
ockham Jan 3, 2024
afd95d0
Set attrs to empty array
ockham Jan 3, 2024
f797f8a
Allow passing inner blocks
ockham Jan 10, 2024
01f30ab
Use correct block type in ignoredHookedBlocks
ockham Jan 10, 2024
962eaf5
Update comment
ockham Jan 11, 2024
1a214a7
Mark insert_hooked_blocks as private
ockham Jan 11, 2024
8b08619
Slight rewording of a comment
ockham Jan 11, 2024
0658500
Clarify PHPDoc
ockham Jan 11, 2024
45ff3da
Fix tests
ockham Jan 11, 2024
520f67e
Cover new hooked_block_type arg in tests
ockham Jan 11, 2024
df3dc99
Add ticket references to tests
ockham Jan 11, 2024
7b9d6f2
Another ticket reference
ockham Jan 11, 2024
c962fc2
Add assertion to insert_hooked_blocks test
ockham Jan 11, 2024
cf1297d
insert_hooked_blocks Test: Turn vars into class consts
ockham Jan 16, 2024
311c2e6
Add more test coverage
ockham Jan 16, 2024
ace54d4
Add ticket references to 59572
ockham Jan 16, 2024
e3df476
Amend PHPDoc
ockham Jan 25, 2024
275a96a
Change variable names to emphasize parsed block array format
ockham Jan 25, 2024
e21c8e9
Remove unnecessary test mocks
ockham Jan 25, 2024
c2912f8
Ooops
ockham Jan 25, 2024
13f1c5d
Add test coverage for filter
ockham Jan 25, 2024
b9f71ad
Add test to cover wrapping block
ockham Jan 25, 2024
30c2e97
Add assertion messages
ockham Jan 25, 2024
5015617
More assertion messages
ockham Jan 25, 2024
3092b85
Coding standards
ockham Jan 25, 2024
47e80e1
More coding standards
ockham Jan 25, 2024
9cac78c
Tweak PHPDoc
ockham Jan 25, 2024
7fdf956
More PHPDoc tweaking
ockham Jan 25, 2024
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
Next Next commit
Block Hooks: Extract insert_hooked_blocks() function
  • Loading branch information
ockham committed Jan 25, 2024
commit a36ac67f2d370aec648afb801d37d184e9ee1baa
99 changes: 43 additions & 56 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,45 @@ function get_hooked_block_markup( &$anchor_block, $hooked_block_type ) {
return get_comment_delimited_block_content( $hooked_block_type, array(), '' );
}

/**
* Returns the markup for blocks hooked to the given anchor block in a specific relative position.
*
* @since 6.5.0
*
* @param array $anchor_block The anchor block.
* @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 blocks hooked to the given anchor block.
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
* @return string
*/
function insert_hooked_blocks( &$anchor_block, $relative_position, $hooked_blocks, $context ) {
$anchor_block_type = $anchor_block['blockName'];
$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();

/**
* Filters the list of hooked block types for a given anchor block type and relative position.
*
* @since 6.4.0
*
* @param string[] $hooked_block_types The list of hooked block types.
* @param string $relative_position The relative position of the hooked blocks.
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
* @param string $anchor_block_type The anchor block type.
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
*/
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );

$markup = '';
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $anchor_block, $hooked_block_type );
}

return $markup;
}

/**
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
*
Expand Down Expand Up @@ -826,40 +865,10 @@ function make_before_block_visitor( $hooked_blocks, $context ) {

if ( $parent_block && ! $prev ) {
// Candidate for first-child insertion.
$relative_position = 'first_child';
$anchor_block_type = $parent_block['blockName'];
$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();

/**
* Filters the list of hooked block types for a given anchor block type and relative position.
*
* @since 6.4.0
*
* @param string[] $hooked_block_types The list of hooked block types.
* @param string $relative_position The relative position of the hooked blocks.
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
* @param string $anchor_block_type The anchor block type.
* @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to.
*/
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $parent_block, $hooked_block_type );
}
$markup .= insert_hooked_blocks( $parent_block, 'first_child', $hooked_blocks, $context );
}

$relative_position = 'before';
$anchor_block_type = $block['blockName'];
$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();

/** This filter is documented in wp-includes/blocks.php */
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $block, $hooked_block_type );
}
$markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context );

return $markup;
};
Expand Down Expand Up @@ -895,33 +904,11 @@ function make_after_block_visitor( $hooked_blocks, $context ) {
* @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
*/
return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context ) {
$markup = '';

$relative_position = 'after';
$anchor_block_type = $block['blockName'];
$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();

/** This filter is documented in wp-includes/blocks.php */
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $block, $hooked_block_type );
}
$markup = insert_hooked_blocks( $block, 'after', $hooked_blocks, $context );

if ( $parent_block && ! $next ) {
// Candidate for last-child insertion.
$relative_position = 'last_child';
$anchor_block_type = $parent_block['blockName'];
$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
: array();

/** This filter is documented in wp-includes/blocks.php */
$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
foreach ( $hooked_block_types as $hooked_block_type ) {
$markup .= get_hooked_block_markup( $parent_block, $hooked_block_type );
}
$markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context );
}

return $markup;
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/tests/blocks/insertHookedBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Tests for the insert_hooked_blocks function.
*
* @package WordPress
* @subpackage Blocks
*
* @since 6.5.0
*
* @group blocks
* @group block-hooks
*/
class Tests_Blocks_InsertHookedBlocks extends WP_UnitTestCase {
/**
* @covers ::insert_hooked_blocks
*/
public function test_insert_hooked_blocks() {
$anchor_block_name = 'tests/anchor-block';
$anchor_block = array(
'blockName' => $anchor_block_name,
);

// Maybe move to class level and include other relative positions?
// And/or data provider?
$hooked_blocks = array(
$anchor_block_name => array(
'after' => array( 'tests/hooked-before' ),
),
);

$actual = insert_hooked_blocks( $anchor_block, 'after', $hooked_blocks, array() );
$this->assertSame( '<!-- wp:tests/hooked-before /-->', $actual );
}
}