-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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: Make work with modified templates/parts/patterns #5523
Changes from all commits
d7e47ba
12a5251
f29ba55
711c933
e1ffbdc
3d56c9b
a9e3fbe
29cea57
a6d9c0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -757,6 +757,57 @@ | |
return $hooked_blocks; | ||
} | ||
|
||
/** | ||
* 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(); | ||
|
||
$markup = ''; | ||
/** | ||
* 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 ) { | ||
if ( | ||
isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) && | ||
in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) | ||
) { | ||
continue; | ||
} | ||
|
||
$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
|
||
// TODO: The following is only needed for the REST API endpoint. | ||
// Ideally, the code should thus be moved into the controller. | ||
if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any issue with running it always? It's a simple operation, so I don't expect it would impact the front end. I would be in favor of keeping it simple unless I miss something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No issue, other than it adds a few bytes per block (and it might be somewhat confusing for people). I just thought if we revisit #5514 (which requires different treatment of frontend and REST API), we might also look into this. Definitely okay to leave it in though. |
||
$anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); | ||
} | ||
$anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; | ||
} | ||
return $markup; | ||
} | ||
|
||
/** | ||
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block. | ||
* | ||
|
@@ -794,40 +845,10 @@ | |
|
||
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_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
} | ||
$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_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
} | ||
$markup .= insert_hooked_blocks( $block, 'before', $hooked_blocks, $context ); | ||
|
||
return $markup; | ||
}; | ||
|
@@ -863,33 +884,11 @@ | |
* @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_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
} | ||
$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_comment_delimited_block_content( $hooked_block_type, array(), '' ); | ||
} | ||
$markup .= insert_hooked_blocks( $parent_block, 'last_child', $hooked_blocks, $context ); | ||
} | ||
|
||
return $markup; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be quite convenient for all templates stored in the database 👍🏻