Make WordPress Core


Ignore:
Timestamp:
07/23/2024 01:41:15 PM (3 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Don't erase post content if it isn't changed by client.

The inject_ignored_hooked_blocks_metadata_attributes filter that is attached to both the rest_pre_insert_wp_template and rest_pre_insert_wp_template_part hooks receives a stdClass object from the Templates REST API controller that contains all fields that the client would like to modify when making a POST request (plus the id to identify the relevant template or template part, respectively).

There are cases when the post_content field is not set, e.g. when the client would like to rename an existing template (in which case it would only set the title field).

Prior to this changeset, the filter would erroneously apply the Block Hooks algorithm to the non-existent post_content field regardless, which would result in it being set to the empty string ''. As a consequence, renaming a template would have the unwanted side effect of wiping its contents.

This changeset fixes the issue by returning early from the filter if the post_content field is not set.

Props alshakero, bernhard-reiter.
Fixes #61550.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php

    r58614 r58785  
    622622        );
    623623    }
     624
     625    /**
     626     * @ticket 61550
     627     */
     628    public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_with_no_changes_to_post_content() {
     629        register_block_type(
     630            'tests/hooked-block',
     631            array(
     632                'block_hooks' => array(
     633                    'core/heading' => 'after',
     634                ),
     635            )
     636        );
     637
     638        $id       = self::TEST_THEME . '//' . 'my_template';
     639        $template = get_block_template( $id, 'wp_template' );
     640
     641        $changes     = new stdClass();
     642        $changes->ID = $template->wp_id;
     643
     644        // Note that we're not setting `$changes->post_content`!
     645
     646        $post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
     647        $this->assertFalse(
     648            isset( $post->post_content ),
     649            "post_content shouldn't have been set."
     650        );
     651    }
     652
     653    /**
     654     * @ticket 61550
     655     */
     656    public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_with_no_changes_to_post_content() {
     657        register_block_type(
     658            'tests/hooked-block',
     659            array(
     660                'block_hooks' => array(
     661                    'core/heading' => 'after',
     662                ),
     663            )
     664        );
     665
     666        $id       = self::TEST_THEME . '//' . 'my_template_part';
     667        $template = get_block_template( $id, 'wp_template_part' );
     668
     669        $changes     = new stdClass();
     670        $changes->ID = $template->wp_id;
     671        // Note that we're not setting `$changes->post_content`!
     672
     673        $post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
     674        $this->assertFalse(
     675            isset( $post->post_content ),
     676            "post_content shouldn't have been set."
     677        );
     678    }
    624679}
Note: See TracChangeset for help on using the changeset viewer.