diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f7a10ab672904..cf7aeac29c3ba 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -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; diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index 1f8a66ee8546f..e9c03e00d9daf 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -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." + ); + } }