Make WordPress Core


Ignore:
Timestamp:
04/03/2024 03:09:38 PM (12 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Pass correct context to filters.

The $context argument passed to filters such as hooked_block_types, hooked_block, and hooked_block_{$hooked_block_type} allows them to conditionally insert a hooked block. If the anchor block is contained in a template or template part, $context will be set to a WP_Block_Template object reflecting that template or part.

The aforementioned filters are applied when hooked block insertion is run upon reading a template (or part) from the DB (and before sending the template/part content with hooked blocks inserted over the REST API to the client), but also upon writing to the DB, as that's when the ignoredHookedBlocks metadata attribute is set.

Prior to this changeset, the $context passed to Block Hooks related filters in the latter case reflected the template/part that was already stored in the database (if any), which is a bug; instead, it needs to reflect the template/part that will result from the incoming POST network request that will trigger a database update.

Those incoming changes are encapsulated in the $changes argument passed to the reset_pre_insert_template and reset_pre_insert_template_part filters, respectively, and thus to the inject_ignored_hooked_blocks_metadata_attributes function that is hooked to them. $changes is of type stdClass and only contains the fields that need to be updated. That means that in order to create a WP_Block_Template object, a two-step process is needed:

  • Emulate what the updated wp_template or wp_template_part post object in the database will look like by merging $changes on top of the existing $post object fetched from the DB, or from the theme's block template (part) file, if any.
  • Create a WP_Block_Template from the resulting object.

To achieve the latter, a new helper method (_build_block_template_object_from_post_object) is extracted from the existing _build_block_template_result_from_post function. (The latter cannot be used directly as it includes a few database calls that will fail if no post object for the template has existed yet in the database.)

While somewhat complicated to implement, the overall change allows for better separation of concerns and isolation of entities. This is visible e.g. in the fact that inject_ignored_hooked_blocks_metadata_attributes no longer requires a $request argument, which is reflected by unit tests no longer needing to create a $request object to pass to it, thus decoupling the function from the templates endpoint controller.

Unit tests for inject_ignored_hooked_blocks_metadata_attributes have been moved to a new, separate file. Test coverage has been added such that now, all three relevant scenarios are covered:

  • The template doesn't exist in the DB, nor is there a block theme template file for it.
  • The template doesn't exist in the DB, but there is a block theme template file for it.
  • The template already exists in the DB.

Those scenarios also correspond to the logical branching inside WP_REST_Templates_Controller::prepare_item_for_database, which is where inject_ignored_hooked_blocks_metadata_attributes gets its data from.

Props tomjcafferkey, bernhard-reiter, gziolo, swissspidy.
Fixes #60754.

File:
1 edited

Legend:

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

    r57790 r57919  
    1515     */
    1616    protected static $admin_id;
    17     private static $post;
     17    private static $template_post;
     18    private static $template_part_post;
    1819
    1920    /**
     
    3031
    3132        // Set up template post.
    32         $args       = array(
     33        $args                = array(
    3334            'post_type'    => 'wp_template',
    3435            'post_name'    => 'my_template',
     
    4243            ),
    4344        );
    44         self::$post = self::factory()->post->create_and_get( $args );
    45         wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' );
     45        self::$template_post = self::factory()->post->create_and_get( $args );
     46        wp_set_post_terms( self::$template_post->ID, get_stylesheet(), 'wp_theme' );
     47
     48        // Set up template part post.
     49        $args                     = array(
     50            'post_type'    => 'wp_template_part',
     51            'post_name'    => 'my_template_part',
     52            'post_title'   => 'My Template Part',
     53            'post_content' => 'Content',
     54            'post_excerpt' => 'Description of my template part.',
     55            'tax_input'    => array(
     56                'wp_theme'              => array(
     57                    get_stylesheet(),
     58                ),
     59                'wp_template_part_area' => array(
     60                    WP_TEMPLATE_PART_AREA_HEADER,
     61                ),
     62            ),
     63        );
     64        self::$template_part_post = self::factory()->post->create_and_get( $args );
     65        wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' );
     66        wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
    4667    }
    4768
    4869    public static function wpTearDownAfterClass() {
    49         wp_delete_post( self::$post->ID );
     70        wp_delete_post( self::$template_post->ID );
    5071    }
    5172
     
    5778    public function tear_down() {
    5879        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 );
     80            remove_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
    6081        }
    6182        if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/block' ) ) {
     
    131152                ),
    132153                'status'          => 'publish',
    133                 'wp_id'           => self::$post->ID,
     154                'wp_id'           => self::$template_post->ID,
    134155                'has_theme_file'  => false,
    135156                'is_custom'       => true,
    136157                'author'          => 0,
    137                 'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     158                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
    138159                'author_text'     => 'Test Blog',
    139160                'original_source' => 'site',
     
    178199                ),
    179200                'status'          => 'publish',
    180                 'wp_id'           => self::$post->ID,
     201                'wp_id'           => self::$template_post->ID,
    181202                'has_theme_file'  => false,
    182203                'is_custom'       => true,
    183204                'author'          => 0,
    184                 'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     205                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
    185206                'author_text'     => 'Test Blog',
    186207                'original_source' => 'site',
     
    217238                ),
    218239                'status'          => 'publish',
    219                 'wp_id'           => self::$post->ID,
     240                'wp_id'           => self::$template_post->ID,
    220241                'has_theme_file'  => false,
    221242                'is_custom'       => true,
    222243                'author'          => 0,
    223                 'modified'        => mysql_to_rfc3339( self::$post->post_modified ),
     244                'modified'        => mysql_to_rfc3339( self::$template_post->post_modified ),
    224245                'author_text'     => 'Test Blog',
    225246                'original_source' => 'site',
     
    945966        );
    946967
    947         add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );
     968        add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
    948969
    949970        $endpoint = new WP_REST_Templates_Controller( 'wp_template_part' );
     
    952973        $prepare_item_for_database->setAccessible( true );
    953974
     975        $id          = get_stylesheet() . '//' . 'my_template_part';
    954976        $body_params = array(
    955             'title'   => 'Untitled Template Part',
    956             'slug'    => 'untitled-template-part',
     977            'id'      => $id,
     978            'slug'    => 'my_template_part',
    957979            'content' => '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->',
    958980        );
Note: See TracChangeset for help on using the changeset viewer.