Skip to content

Commit

Permalink
Block Hooks: Don't erase post content if it isn't changed by client.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@58785 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ockham committed Jul 23, 2024
1 parent d6d1437 commit ddd78e3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,10 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
_deprecated_argument( __FUNCTION__, '6.5.3' );
}

if ( ! isset( $changes->post_content ) ) {
return $changes;
}

$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $changes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,59 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ
'The template part\'s post content was modified.'
);
}

/**
* @ticket 61550
*/
public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_with_no_changes_to_post_content() {
register_block_type(
'tests/hooked-block',
array(
'block_hooks' => array(
'core/heading' => 'after',
),
)
);

$id = self::TEST_THEME . '//' . 'my_template';
$template = get_block_template( $id, 'wp_template' );

$changes = new stdClass();
$changes->ID = $template->wp_id;

// Note that we're not setting `$changes->post_content`!

$post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
$this->assertFalse(
isset( $post->post_content ),
"post_content shouldn't have been set."
);
}

/**
* @ticket 61550
*/
public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_with_no_changes_to_post_content() {
register_block_type(
'tests/hooked-block',
array(
'block_hooks' => array(
'core/heading' => 'after',
),
)
);

$id = self::TEST_THEME . '//' . 'my_template_part';
$template = get_block_template( $id, 'wp_template_part' );

$changes = new stdClass();
$changes->ID = $template->wp_id;
// Note that we're not setting `$changes->post_content`!

$post = inject_ignored_hooked_blocks_metadata_attributes( $changes );
$this->assertFalse(
isset( $post->post_content ),
"post_content shouldn't have been set."
);
}
}

0 comments on commit ddd78e3

Please sign in to comment.