Skip to content

Commit 1d49212

Browse files
committed
Block Hooks: Fix context in update_ignored_hooked_blocks_postmeta.
Ensure that the `$context` arg passed from `update_ignored_hooked_blocks_postmeta` to `apply_block_hooks_to_content` (and from there, to filters such as `hooked_block_types` and `hooked_block`) has the correct type (`WP_Post`). Filters hooked to `hooked_block_types` etc can typically include checks that conditionally insert a hooked block depending on `$context`. Prior to this changeset, a check like `if ( $context instanceof WP_Post )` would incorrectly fail, as `$context` would be a `stdClass` instance rather than a `WP_Post`. As a consequence, a hooked block inside of a Navigation post object that was modified by the user would not be marked as ignored by `update_ignored_hooked_blocks_postmeta`, and thus be erroneosly re-inserted by the Block Hooks algorithm. Props bernhard-reiter. Fixes #62639. git-svn-id: https://develop.svn.wordpress.org/trunk@59482 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8eb8d63 commit 1d49212

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/wp-includes/blocks.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ function update_ignored_hooked_blocks_postmeta( $post ) {
12171217
$existing_post = get_post( $post->ID );
12181218
// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
12191219
$context = (object) array_merge( (array) $existing_post, (array) $post );
1220+
$context = new WP_Post( $context ); // Convert to WP_Post object.
12201221
$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
12211222
$root_block = parse_blocks( $serialized_block )[0];
12221223

tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,31 @@ public function test_update_ignored_hooked_blocks_postmeta_dont_modify_if_no_pos
193193
'Post content did not match the original markup.'
194194
);
195195
}
196+
197+
/**
198+
* @ticket 62639
199+
*/
200+
public function test_update_ignored_hooked_blocks_postmeta_sets_correct_context_type() {
201+
$action = new MockAction();
202+
add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 );
203+
204+
$original_markup = '<!-- wp:navigation-link {"label":"News","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
205+
$post = new stdClass();
206+
$post->ID = self::$navigation_post->ID;
207+
$post->post_content = $original_markup;
208+
$post->post_type = 'wp_navigation';
209+
210+
$post = update_ignored_hooked_blocks_postmeta( $post );
211+
212+
$args = $action->get_args();
213+
$contexts = array_column( $args, 3 );
214+
215+
foreach ( $contexts as $context ) {
216+
$this->assertInstanceOf(
217+
WP_Post::class,
218+
$context,
219+
'The context passed to the hooked_block_types filter is not a WP_Post instance.'
220+
);
221+
}
222+
}
196223
}

0 commit comments

Comments
 (0)