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/src/wp-includes/block-template-utils.php

    r58768 r58785  
    16031603    }
    16041604
     1605    if ( ! isset( $changes->post_content ) ) {
     1606        return $changes;
     1607    }
     1608
    16051609    $hooked_blocks = get_hooked_blocks();
    16061610    if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
Note: See TracChangeset for help on using the changeset viewer.