Make WordPress Core


Ignore:
Timestamp:
03/07/2024 02:10:31 PM (4 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Use new Templates Controller filter instead of action.

This changeset adds a new rest_pre_insert_{$this->post_type} filter in the WP_REST_Templates_Controller, where it is applied to the return value of the prepare_item_for_database method. (This is consistent with the WP_REST_Post_Controller, where that filter has existed before.)

The new filter is then used to inject hooked blocks into the template (or template part) content received via the endpoint, prior to persisting it to the database.

This supersedes the previous mechanism, which was using the rest_after_insert_{$this->post_type} action, from which it performed an additional wp_update_post call to update the template (part) content with the hooked blocks injected. The new technique eschews that additional call and the resulting extra revision it created, as well as a problem with regard to duplicated escaping and sanitization, which had caused some special characters to be garbled.

Props tomjcafferkey, gziolo, swissspidy, karolmanijak.
Fixes #60671.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r57366 r57790  
    4848    public static function wpTearDownAfterClass() {
    4949        wp_delete_post( self::$post->ID );
     50    }
     51
     52    /**
     53     * Tear down after each test.
     54     *
     55     * @since 6.5.0
     56     */
     57    public function tear_down() {
     58        if ( has_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ) ) {
     59            remove_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10 );
     60        }
     61        if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/block' ) ) {
     62            unregister_block_type( 'tests/hooked-block' );
     63        }
     64
     65        parent::tear_down();
    5066    }
    5167
     
    912928        $this->assertEmpty( $prepared->post_content, 'The content was not correct in the prepared template part.' );
    913929    }
     930
     931    /**
     932     * @ticket 60671
     933     *
     934     * @covers WP_REST_Templates_Controller::prepare_item_for_database
     935     * @covers inject_ignored_hooked_blocks_metadata_attributes
     936     */
     937    public function test_prepare_item_for_database_injects_hooked_block() {
     938        register_block_type(
     939            'tests/hooked-block',
     940            array(
     941                'block_hooks' => array(
     942                    'tests/anchor-block' => 'after',
     943                ),
     944            )
     945        );
     946
     947        add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );
     948
     949        $endpoint = new WP_REST_Templates_Controller( 'wp_template_part' );
     950
     951        $prepare_item_for_database = new ReflectionMethod( $endpoint, 'prepare_item_for_database' );
     952        $prepare_item_for_database->setAccessible( true );
     953
     954        $body_params = array(
     955            'title'   => 'Untitled Template Part',
     956            'slug'    => 'untitled-template-part',
     957            'content' => '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->',
     958        );
     959
     960        $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' );
     961        $request->set_body_params( $body_params );
     962
     963        $prepared = $prepare_item_for_database->invoke( $endpoint, $request );
     964        $this->assertSame(
     965            '<!-- wp:tests/anchor-block {"metadata":{"ignoredHookedBlocks":["tests/hooked-block"]}} -->Hello<!-- /wp:tests/anchor-block -->',
     966            $prepared->post_content,
     967            'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.'
     968        );
     969    }
    914970}
Note: See TracChangeset for help on using the changeset viewer.