Make WordPress Core

Changeset 58578

Timestamp:
06/26/2024 08:52:12 AM (6 weeks ago)
Author:
Bernhard Reiter
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r58561 r58578  
    10961096    );
    10971097
    1098     $serialized_block = apply_block_hooks_to_content( $markup, get_post( $post->ID ), 'set_ignored_hooked_blocks_metadata' );
     1098    $existing_post = get_post( $post->ID );
     1099    // Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
     1100    $context          = (object) array_merge( (array) $existing_post, (array) $post );
     1101    $serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
    10991102    $root_block       = parse_blocks( $serialized_block )[0];
    11001103
     
    11091112            $ignored_hooked_blocks          = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
    11101113        }
    1111         update_post_meta( $post->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) );
     1114
     1115        if ( ! isset( $post->meta_input ) ) {
     1116            $post->meta_input = array();
     1117        }
     1118        $post->meta_input['_wp_ignored_hooked_blocks'] = json_encode( $ignored_hooked_blocks );
    11121119    }
    11131120
  • trunk/tests/phpunit/tests/blocks/updateIgnoredHookedBlocksPostMeta.php

    r58291 r58578  
    7676        $this->assertSame(
    7777            array( 'tests/my-block' ),
    78             json_decode( get_post_meta( self::$navigation_post->ID, '_wp_ignored_hooked_blocks', true ), true ),
     78            json_decode( , true ),
    7979            'Block was not added to ignored hooked blocks metadata.'
    8080        );
Note: See TracChangeset for help on using the changeset viewer.