Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 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
17 changes: 17 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1248,8 +1248,25 @@
$content
);

/*
* We need to avoid inserting any blocks hooked into the `before` and `after` positions
* of the temporary wrapper block that we create to wrap the content.
* See https://core.trac.wordpress.org/ticket/63287 for more details.
*/
$suppress_blocks_from_insertion_before_and_after_wrapper_block = static function ( $hooked_block_types, $relative_position, $anchor_block_type ) use ( $wrapper_block_type ) {
if (
$wrapper_block_type === $anchor_block_type &&
in_array( $relative_position, array( 'before', 'after' ), true )
) {
return array();
}
return $hooked_block_types;
};

// Apply Block Hooks.
add_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX, 3);

Check failure on line 1267 in src/wp-includes/blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Expected 1 spaces before closing parenthesis; 0 found
$content = apply_block_hooks_to_content( $content, $post, $callback );
remove_filter( 'hooked_block_types', $suppress_blocks_from_insertion_before_and_after_wrapper_block, PHP_INT_MAX );

// Finally, we need to remove the temporary wrapper block.
$content = remove_serialized_parent_block( $content );
Expand Down
13 changes: 8 additions & 5 deletions tests/phpunit/tests/blocks/applyBlockHooksToContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Tests_Blocks_ApplyBlockHooksToContent extends WP_UnitTestCase {
* Set up.
*
* @ticket 61902.
* @ticket 63287.
*/
public static function wpSetUpBeforeClass() {
register_block_type(
'tests/hooked-block',
array(
'block_hooks' => array(
'tests/anchor-block' => 'after',
'core/post-content' => 'after',
),
)
);
Expand Down Expand Up @@ -79,23 +80,25 @@ public function test_apply_block_hooks_to_content_sets_theme_attribute_on_templa

/**
* @ticket 61902
* @ticket 63287
*/
public function test_apply_block_hooks_to_content_inserts_hooked_block() {
$context = new WP_Block_Template();
$context->content = '<!-- wp:tests/anchor-block /-->';
$context->content = '<!-- wp:post-content /-->';

$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->',
'<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
$actual
);
}

/**
* @ticket 61074
* @ticket 63287
*/
public function test_apply_block_hooks_to_content_with_context_set_to_null() {
$content = '<!-- wp:tests/anchor-block /-->';
$content = '<!-- wp:post-content /-->';

/*
* apply_block_hooks_to_content() will fall back to the global $post object (via get_post())
Expand All @@ -106,7 +109,7 @@ public function test_apply_block_hooks_to_content_with_context_set_to_null() {

$actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:tests/anchor-block /--><!-- wp:tests/hooked-block /-->',
'<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
$actual
);
}
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 Expand Up @@ -130,6 +140,33 @@ public function test_apply_block_hooks_to_content_from_post_object_respects_igno
$this->assertSame( $expected, $actual );
}

/**
* @ticket 63287
*/
public function test_apply_block_hooks_to_content_from_post_object_does_not_insert_hooked_block_before_container_block() {
$filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) {
if ( 'core/post-content' === $anchor_block_type && 'before' === $relative_position ) {
$hooked_block_types[] = 'tests/dynamically-hooked-block-before-post-content';
Comment thread
gziolo marked this conversation as resolved.
}

return $hooked_block_types;
};

$expected = '<!-- wp:tests/hooked-block-first-child /-->' .
self::$post->post_content .
'<!-- wp:tests/hooked-block /-->';

add_filter( 'hooked_block_types', $filter, 10, 3 );
$actual = apply_block_hooks_to_content_from_post_object(
self::$post->post_content,
self::$post,
'insert_hooked_blocks'
);
remove_filter( 'hooked_block_types', $filter, 10 );

$this->assertSame( $expected, $actual );
}

/**
* @ticket 62716
*/
Expand Down
Loading