Skip to content
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: Fix context in update_ignored_hooked_blocks_postmeta #7941

Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ function update_ignored_hooked_blocks_postmeta( $post ) {
$existing_post = get_post( $post->ID );
// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
$context = (object) array_merge( (array) $existing_post, (array) $post );
$context = new WP_Post( $context ); // Convert to WP_Post object.
$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
$root_block = parse_blocks( $serialized_block )[0];

Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,31 @@ public function test_update_ignored_hooked_blocks_postmeta_dont_modify_if_no_pos
'Post content did not match the original markup.'
);
}

/**
* @ticket 62639
*/
public function test_update_ignored_hooked_blocks_postmeta_sets_correct_context_type() {
$action = new MockAction();
add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 );

$original_markup = '<!-- wp:navigation-link {"label":"News","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
$post = new stdClass();
$post->ID = self::$navigation_post->ID;
$post->post_content = $original_markup;
$post->post_type = 'wp_navigation';

$post = update_ignored_hooked_blocks_postmeta( $post );

$args = $action->get_args();
$contexts = array_column( $args, 3 );

foreach ( $contexts as $context ) {
$this->assertInstanceOf(
WP_Post::class,
$context,
'The context passed to the hooked_block_types filter is not a WP_Post instance.'
);
}
}
}
Loading