From 979219863114208a2b60dd42c8bef7a267e227be Mon Sep 17 00:00:00 2001 From: Omar Alshaker Date: Tue, 2 Jul 2024 19:37:15 +0200 Subject: [PATCH 1/6] Fix wp_template renaming when `hooked_block_types` filter exists Fixes: https://core.trac.wordpress.org/ticket/61550 --- src/wp-includes/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f7a10ab672904..dc589c825c5ca 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1603,7 +1603,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated } $hooked_blocks = get_hooked_blocks(); - if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { + if ( empty ( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { return $changes; } From 9a31adaee1d75808cac5541d5e0284a0e7663f1c Mon Sep 17 00:00:00 2001 From: Omar Alshaker Date: Tue, 2 Jul 2024 19:41:52 +0200 Subject: [PATCH 2/6] Lint --- src/wp-includes/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index dc589c825c5ca..5177ca668056f 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1603,7 +1603,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated } $hooked_blocks = get_hooked_blocks(); - if ( empty ( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { + if ( empty( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { return $changes; } From 85d4e2987bd1182e32a9d263188a9e69675934f8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 22 Jul 2024 17:38:50 +0200 Subject: [PATCH 3/6] Basic test coverage --- ...tIgnoredHookedBlocksMetadataAttributes.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) 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." + ); + } } From f632c7f7c70dfb1244f6492959ae67ec0b4c3a54 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 22 Jul 2024 17:50:17 +0200 Subject: [PATCH 4/6] Change empty() to ! isset() --- src/wp-includes/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 5177ca668056f..16b248802b2b7 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1603,7 +1603,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated } $hooked_blocks = get_hooked_blocks(); - if ( empty( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { + if ( ! isset( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { return $changes; } From 617e4309b09a4ca37efb6b161803adb15b0aee45 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 22 Jul 2024 17:52:26 +0200 Subject: [PATCH 5/6] Break out into separate conditional --- src/wp-includes/block-template-utils.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 16b248802b2b7..0199bd1ab0e88 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1602,8 +1602,12 @@ 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 ( ! isset( $changes->post_content ) || ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { + if ( ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { return $changes; } From 6f95346a22faca400c05a16e10fb9215c64fa33e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 23 Jul 2024 15:05:14 +0200 Subject: [PATCH 6/6] Remove unnecessary pair of extra parens --- src/wp-includes/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 0199bd1ab0e88..cf7aeac29c3ba 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1607,7 +1607,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated } $hooked_blocks = get_hooked_blocks(); - if ( ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) ) { + if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { return $changes; }