From df256e414c183ff001cdf26ebf37b19b960180de Mon Sep 17 00:00:00 2001 From: bernhard-reiter Date: Wed, 26 Jun 2024 08:52:12 +0000 Subject: [PATCH] Block Hooks: Refactor controller filter to use meta_input. Prior to this changeset, the function `update_ignored_hooked_blocks_postmeta()` used the core function `update_post_meta()` to write `_wp_ignored_hooked_blocks` data to the database during an operation that is preparing a post to be inserted. Since we have access to the incoming changes that are being prepared we can remove this database operation in favour of writing the data to the post object provided under `meta_input`. Doing this means two things: 1. It allows us to store postmeta for new posts that are about to be created since they don't have an `ID` yet (which is information `update_post_meta()` needs). 2. The core controller will take care of updating postmeta in a more predictable pattern. Props tomjcafferkey, bernhard-reiter. Fixes #61495. git-svn-id: https://develop.svn.wordpress.org/trunk@58578 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 11 +++++++++-- .../blocks/updateIgnoredHookedBlocksPostMeta.php | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 26e5e0c96b409..2edcaa8c1ba04 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1095,7 +1095,10 @@ function update_ignored_hooked_blocks_postmeta( $post ) { $post->post_content ); - $serialized_block = apply_block_hooks_to_content( $markup, get_post( $post->ID ), 'set_ignored_hooked_blocks_metadata' ); + $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 ); + $serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' ); $root_block = parse_blocks( $serialized_block )[0]; $ignored_hooked_blocks = isset( $root_block['attrs']['metadata']['ignoredHookedBlocks'] ) @@ -1108,7 +1111,11 @@ function update_ignored_hooked_blocks_postmeta( $post ) { $existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true ); $ignored_hooked_blocks = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) ); } - update_post_meta( $post->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) ); + + if ( ! isset( $post->meta_input ) ) { + $post->meta_input = array(); + } + $post->meta_input['_wp_ignored_hooked_blocks'] = json_encode( $ignored_hooked_blocks ); } $post->post_content = remove_serialized_parent_block( $serialized_block ); diff --git a/tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php b/tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php index 6f60243630002..7b0a830dd52dd 100644 --- a/tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php +++ b/tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php @@ -75,7 +75,7 @@ public function test_update_ignored_hooked_blocks_postmeta_preserves_entities() ); $this->assertSame( array( 'tests/my-block' ), - json_decode( get_post_meta( self::$navigation_post->ID, '_wp_ignored_hooked_blocks', true ), true ), + json_decode( $post->meta_input['_wp_ignored_hooked_blocks'], true ), 'Block was not added to ignored hooked blocks metadata.' ); }