Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
20 changes: 18 additions & 2 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,25 @@ function apply_block_hooks_to_content( $content, $context = null, $callback = 'i
* We also need to cover the case where the hooked block is not present in
* `$content` at first and we're allowed to insert it once -- but not again.
*/
$suppress_single_instance_blocks = static function ( $hooked_block_types ) use ( &$block_allows_multiple_instances, $content ) {
$suppress_single_instance_blocks = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( &$block_allows_multiple_instances, $content, $context ) {
Comment thread
ockham marked this conversation as resolved.
Outdated
static $single_instance_blocks_present_in_content = array();
foreach ( $hooked_block_types as $index => $hooked_block_type ) {
if ( $context instanceof WP_Post ) {
$wrapper_block_type = 'core/post-content';
if ( 'wp_navigation' === $context->post_type ) {
$wrapper_block_type = 'core/navigation';
} elseif ( 'wp_block' === $context->post_type ) {
$wrapper_block_type = 'core/block';
}

if (
$wrapper_block_type === $anchor_block_type &&
in_array( $relative_position, array( 'before', 'after' ), true )
) {
$hooked_block_types = array();
}
}

Comment thread
ockham marked this conversation as resolved.
Outdated
if ( ! isset( $block_allows_multiple_instances[ $hooked_block_type ] ) ) {
$hooked_block_type_definition =
WP_Block_Type_Registry::get_instance()->get_registered( $hooked_block_type );
Expand All @@ -1157,7 +1173,7 @@ function apply_block_hooks_to_content( $content, $context = null, $callback = 'i
}
return $hooked_block_types;
};
add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX );
add_filter( 'hooked_block_types', $suppress_single_instance_blocks, PHP_INT_MAX, 3 );
$content = traverse_and_serialize_blocks(
parse_blocks( $content ),
$before_block_visitor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public static function wpSetUpBeforeClass() {
),
)
);

register_block_type(
'tests/hooked-block-after-post-content',
array(
'block_hooks' => array(
'core/post-content' => 'after',
),
)
);
}

/**
Expand All @@ -100,6 +109,7 @@ public static function wpTearDownAfterClass() {

$registry->unregister( 'tests/hooked-block' );
$registry->unregister( 'tests/hooked-block-first-child' );
$registry->unregister( 'tests/hooked-block-after-post-content' );
}

/**
Expand Down
Loading