Make WordPress Core


Ignore:
Timestamp:
07/24/2024 02:38:24 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.

Reviewed by gziolo.
Merges [58785] to the 6.6 branch.

Props alshakero, bernhard-reiter.
Fixes #61550.

File:
1 edited

Legend:

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

    r58042 r58802  
    420420        );
    421421    }
     422
     423    /**
     424     * @ticket 61550
     425     */
     426    public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_with_no_changes_to_post_content() {
     427        register_block_type(
     428            'tests/hooked-block',
     429            array(
     430                'block_hooks' => array(
     431                    'core/heading' => 'after',
     432                ),
     433            )
     434        );
     435
     436        $id       = self::TEST_THEME . '//' . 'my_template';
     437        $template = get_block_template( $id, 'wp_template' );
     438
     439        $changes     = new stdClass();
     440        $changes->ID = $template->wp_id;
     441
     442        // Note that we're not setting `$changes->post_content`!
     443
     444        $post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
     445        $this->assertFalse(
     446            isset( $post->post_content ),
     447            "post_content shouldn't have been set."
     448        );
     449    }
     450
     451    /**
     452     * @ticket 61550
     453     */
     454    public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_with_no_changes_to_post_content() {
     455        register_block_type(
     456            'tests/hooked-block',
     457            array(
     458                'block_hooks' => array(
     459                    'core/heading' => 'after',
     460                ),
     461            )
     462        );
     463
     464        $id       = self::TEST_THEME . '//' . 'my_template_part';
     465        $template = get_block_template( $id, 'wp_template_part' );
     466
     467        $changes     = new stdClass();
     468        $changes->ID = $template->wp_id;
     469        // Note that we're not setting `$changes->post_content`!
     470
     471        $post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
     472        $this->assertFalse(
     473            isset( $post->post_content ),
     474            "post_content shouldn't have been set."
     475        );
     476    }
    422477}
Note: See TracChangeset for help on using the changeset viewer.