From 3034818c7a0344950674251b560510499a0bef77 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 12 Mar 2024 12:14:17 +0100 Subject: [PATCH 001/111] Block Hooks: Remove references to files in test --- tests/phpunit/tests/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 4d92c277a9730..4c4e2c88e74dd 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -412,7 +412,7 @@ public function test_wp_generate_block_templates_export_file() { public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { global $wp_current_filter; // Mock currently set filter. The $wp_current_filter global is reset during teardown by - // WP_UnitTestCase_Base::_restore_hooks() in tests/phpunit/includes/abstract-testcase.php. + // the unit test base class. $wp_current_filter[] = 'rest_pre_insert_wp_template'; register_block_type( @@ -446,7 +446,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { global $wp_current_filter; // Mock currently set filter. The $wp_current_filter global is reset during teardown by - // WP_UnitTestCase_Base::_restore_hooks() in tests/phpunit/includes/abstract-testcase.php. + // the unit test base class. $wp_current_filter[] = 'rest_pre_insert_wp_template_part'; register_block_type( From 7feddde1656675d4cd166a0bb9d145fde6033795 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 12 Mar 2024 12:14:54 +0100 Subject: [PATCH 002/111] Block Hooks: Add test to verify that correct context is passed to filters --- tests/phpunit/tests/block-template-utils.php | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 4c4e2c88e74dd..7ea6ce2e0ea24 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -471,4 +471,39 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' ); } + + /** + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + global $wp_current_filter; + // Mock currently set filter. The $wp_current_filter global is reset during teardown by + // the unit test base class. + $wp_current_filter[] = 'rest_pre_insert_wp_template'; + + $id = self::TEST_THEME . '//' . 'my_template'; + $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); + + $changes = new stdClass(); + $changes->post_content = 'Hello'; + + //$this->assertSame( 'rest_pre_insert_wp_template', current_filter() ); + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + + $args = $action->get_args(); + $anchor_block_type = $args[0][2]; + $context = $args[0][3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + $this->assertInstanceOf( 'WP_Block_Template', $context ); // FIXME: Currently failing :/ + $this->assertSame( + $changes->post_content, + $context->post_content, + 'The context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + } } From 5c99e4f28ba9605252769db2fc6d1a67717a8ec8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 12 Mar 2024 14:26:36 +0100 Subject: [PATCH 003/111] Coding Standards --- tests/phpunit/tests/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 7ea6ce2e0ea24..da7f19be59474 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -492,7 +492,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi //$this->assertSame( 'rest_pre_insert_wp_template', current_filter() ); - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $args = $action->get_args(); $anchor_block_type = $args[0][2]; From 2d9d585b16a1988ffc96d7f415d2b04e06ac3753 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 13 Mar 2024 10:14:04 +0000 Subject: [PATCH 004/111] Fix failing block-template-utils test --- tests/phpunit/tests/block-template-utils.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index da7f19be59474..f62be207e3be2 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -24,7 +24,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => 'Content', + 'post_content' => 'Hello', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -42,7 +42,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => 'Content', + 'post_content' => 'Hello', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -60,7 +60,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', - 'post_content' => 'Content', + 'post_content' => 'Hello', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( 'wp_theme' => array( @@ -426,6 +426,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $id = self::TEST_THEME . '//' . 'my_template'; $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); + $request->set_param( 'id', $id ); $changes = new stdClass(); $changes->post_content = 'Hello'; @@ -460,6 +461,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $id = self::TEST_THEME . '//' . 'my_template_part'; $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); + $request->set_param( 'id', $id ); $changes = new stdClass(); $changes->post_content = 'Hello'; @@ -486,6 +488,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $id = self::TEST_THEME . '//' . 'my_template'; $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); + $request->set_param( 'id', $id ); $changes = new stdClass(); $changes->post_content = 'Hello'; @@ -499,10 +502,10 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $context = $args[0][3]; $this->assertSame( 'tests/anchor-block', $anchor_block_type ); - $this->assertInstanceOf( 'WP_Block_Template', $context ); // FIXME: Currently failing :/ + $this->assertInstanceOf( 'WP_Block_Template', $context ); $this->assertSame( $changes->post_content, - $context->post_content, + $context->content, 'The context passed to the hooked_block_types filter doesn\'t match the template changes.' ); } From 1bfb8928639feadbf688a7427fa882e1b67f5a57 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 13 Mar 2024 11:17:23 +0000 Subject: [PATCH 005/111] Map incoming data to build object --- src/wp-includes/block-template-utils.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index cfae702dd6c46..831ab1b7b15f4 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1468,6 +1468,22 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + $post_to_template_key_map = array( + 'post_content' => 'content', + 'post_title' => 'title', + 'post_excerpt' => 'description', + 'post_type' => 'type', + 'post_status' => 'status', + ); + + // We need to overwrite the built template object with the incoming one from the request. + // This is so we can provide the correct context to the Block Hooks API which expects the `WP_Block_Template` object. + foreach ( $post_to_template_key_map as $post_key => $template_key ) { + if ( isset( $post->$post_key ) ) { + $template->{$template_key} = $post->$post_key; + } + } + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From cd928317f87b025da77f26632e80fddf5c2e0c8f Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 13 Mar 2024 14:55:19 +0000 Subject: [PATCH 006/111] Revert mock post_content values back to 'Content' --- tests/phpunit/tests/block-template-utils.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index f62be207e3be2..2f00f9c4bb23e 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -24,7 +24,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => 'Hello', + 'post_content' => 'Content', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -42,7 +42,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => 'Hello', + 'post_content' => 'Content', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -60,7 +60,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', - 'post_content' => 'Hello', + 'post_content' => 'Content', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( 'wp_theme' => array( @@ -429,7 +429,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $request->set_param( 'id', $id ); $changes = new stdClass(); - $changes->post_content = 'Hello'; + $changes->post_content = 'Content'; $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $this->assertSame( @@ -498,8 +498,8 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $args = $action->get_args(); - $anchor_block_type = $args[0][2]; - $context = $args[0][3]; + $anchor_block_type = end($args)[2]; + $context = end($args)[3]; $this->assertSame( 'tests/anchor-block', $anchor_block_type ); $this->assertInstanceOf( 'WP_Block_Template', $context ); From fc4b1cfa2740b21dfba64179c46e3a22ed1b3e7e Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Wed, 13 Mar 2024 14:56:07 +0000 Subject: [PATCH 007/111] Coding standards --- tests/phpunit/tests/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 2f00f9c4bb23e..8c7b06f9fd8e9 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -498,8 +498,8 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $args = $action->get_args(); - $anchor_block_type = end($args)[2]; - $context = end($args)[3]; + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; $this->assertSame( 'tests/anchor-block', $anchor_block_type ); $this->assertInstanceOf( 'WP_Block_Template', $context ); From b00199a2add1ba363bc059b4f6a3db58b7f68f11 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 16:22:51 +0100 Subject: [PATCH 008/111] Fix test --- tests/phpunit/tests/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 8c7b06f9fd8e9..348e84ef7c83c 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -429,7 +429,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $request->set_param( 'id', $id ); $changes = new stdClass(); - $changes->post_content = 'Content'; + $changes->post_content = 'Hello'; $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $this->assertSame( From 3cda15c81c83052b24037eb54b3d7de147a13803 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 16:30:06 +0100 Subject: [PATCH 009/111] Add some more coverage --- tests/phpunit/tests/block-template-utils.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 348e84ef7c83c..2f88e41e8e5c8 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -491,7 +491,9 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request->set_param( 'id', $id ); $changes = new stdClass(); + $changes->post_title = 'My updated Template'; $changes->post_content = 'Hello'; + $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; //$this->assertSame( 'rest_pre_insert_wp_template', current_filter() ); @@ -503,10 +505,20 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $this->assertSame( 'tests/anchor-block', $anchor_block_type ); $this->assertInstanceOf( 'WP_Block_Template', $context ); + $this->assertSame( + $changes->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); $this->assertSame( $changes->post_content, $context->content, - 'The context passed to the hooked_block_types filter doesn\'t match the template changes.' + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); } } From 4800ae5dd4f465d5728d20564f168dcf4460d905 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 16:30:56 +0100 Subject: [PATCH 010/111] Remove commented-out line --- tests/phpunit/tests/block-template-utils.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 2f88e41e8e5c8..7d75d36f932f6 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -495,8 +495,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $changes->post_content = 'Hello'; $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; - //$this->assertSame( 'rest_pre_insert_wp_template', current_filter() ); - inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); $args = $action->get_args(); From b906ae19955de8c69e993d055d7fb89b0c58c2d1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 16:32:16 +0100 Subject: [PATCH 011/111] Move global to top of function --- tests/phpunit/tests/block-template-utils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 7d75d36f932f6..3d6872f3508ca 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -478,14 +478,14 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { - $action = new MockAction(); - add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - global $wp_current_filter; // Mock currently set filter. The $wp_current_filter global is reset during teardown by // the unit test base class. $wp_current_filter[] = 'rest_pre_insert_wp_template'; + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + $id = self::TEST_THEME . '//' . 'my_template'; $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); $request->set_param( 'id', $id ); From 97424500c09eaf7f7273c60c5e475d6dfd768036 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:03:38 +0100 Subject: [PATCH 012/111] Rearrange comment --- tests/phpunit/tests/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 3d6872f3508ca..31ff4835381d3 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -479,8 +479,8 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ */ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { global $wp_current_filter; - // Mock currently set filter. The $wp_current_filter global is reset during teardown by - // the unit test base class. + // Mock currently set filter. + // The $wp_current_filter global is reset during teardown by the unit test base class. $wp_current_filter[] = 'rest_pre_insert_wp_template'; $action = new MockAction(); From ee06af0e61902988366f227fa9623faba59baa58 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:03:51 +0100 Subject: [PATCH 013/111] Include author --- src/wp-includes/block-template-utils.php | 1 + tests/phpunit/tests/block-template-utils.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 831ab1b7b15f4..b2a93b8e5bc37 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1469,6 +1469,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); $post_to_template_key_map = array( + 'post_author' => 'author', 'post_content' => 'content', 'post_title' => 'title', 'post_excerpt' => 'description', diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 31ff4835381d3..54f32f50534a5 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -491,6 +491,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request->set_param( 'id', $id ); $changes = new stdClass(); + $changes->post_author = 123; $changes->post_title = 'My updated Template'; $changes->post_content = 'Hello'; $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; From ba39f47fbced059f31713d09161a5ed4e4cd013b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:04:02 +0100 Subject: [PATCH 014/111] Rewrite assertion --- tests/phpunit/tests/block-template-utils.php | 31 +++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 54f32f50534a5..3783ac8967447 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -504,20 +504,23 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $this->assertSame( 'tests/anchor-block', $anchor_block_type ); $this->assertInstanceOf( 'WP_Block_Template', $context ); - $this->assertSame( - $changes->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_content, - $context->content, - 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + + $post_to_template_key_map = array( + 'post_author' => 'author', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_excerpt' => 'description', + 'post_type' => 'type', + 'post_status' => 'status', ); + + $expected = array(); + foreach ( $post_to_template_key_map as $post_key => $template_key ) { + if ( isset( $changes->$post_key ) ) { + $expected[ $template_key ] = $changes->$post_key; + } + } + + $this->assertEquals( $expected, (array) $context ); } } From 9e8f963d452a685ca49a84ab468f3e75eacbb1a7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:05:09 +0100 Subject: [PATCH 015/111] Add TODO comment --- src/wp-includes/block-template-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b2a93b8e5bc37..47a513fb87612 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1468,6 +1468,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + // TODO: Should maybe make this static. E.g. in the Templates Controller? $post_to_template_key_map = array( 'post_author' => 'author', 'post_content' => 'content', From 33ebc94dab69da87e9d25aff8f7ed52bef335969 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:05:50 +0100 Subject: [PATCH 016/111] Make sure template exists --- src/wp-includes/block-template-utils.php | 30 +++++++++++++----------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 47a513fb87612..2c6003f3d285d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1468,21 +1468,23 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); - // TODO: Should maybe make this static. E.g. in the Templates Controller? - $post_to_template_key_map = array( - 'post_author' => 'author', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_excerpt' => 'description', - 'post_type' => 'type', - 'post_status' => 'status', - ); + if ( $template ) { + // TODO: Should maybe make this static. E.g. in the Templates Controller? + $post_to_template_key_map = array( + 'post_author' => 'author', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_excerpt' => 'description', + 'post_type' => 'type', + 'post_status' => 'status', + ); - // We need to overwrite the built template object with the incoming one from the request. - // This is so we can provide the correct context to the Block Hooks API which expects the `WP_Block_Template` object. - foreach ( $post_to_template_key_map as $post_key => $template_key ) { - if ( isset( $post->$post_key ) ) { - $template->{$template_key} = $post->$post_key; + // We need to overwrite the built template object with the incoming one from the request. + // This is so we can provide the correct context to the Block Hooks API which expects the `WP_Block_Template` object. + foreach ( $post_to_template_key_map as $post_key => $template_key ) { + if ( isset( $post->$post_key ) ) { + $template->{$template_key} = $post->$post_key; + } } } From 95dc55455996cdee9b369d7ecdf418b1fb385f20 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:15:50 +0100 Subject: [PATCH 017/111] Cover origin --- src/wp-includes/block-template-utils.php | 4 ++++ tests/phpunit/tests/block-template-utils.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2c6003f3d285d..5a2f1adc17068 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1486,6 +1486,10 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { $template->{$template_key} = $post->$post_key; } } + + if ( isset( $post->meta_input['origin'] ) ) { + $template->origin = $post->meta_input['origin']; + } } $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 3783ac8967447..96c184f65c338 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -495,6 +495,9 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $changes->post_title = 'My updated Template'; $changes->post_content = 'Hello'; $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; + $changes->meta_input = array( + 'origin' => 'custom', + ); inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); @@ -520,6 +523,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $expected[ $template_key ] = $changes->$post_key; } } + $expected['origin'] = 'custom'; $this->assertEquals( $expected, (array) $context ); } From fc78209c886ddcb9835e6b8d88369563a9dae5ee Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:23:02 +0100 Subject: [PATCH 018/111] Test post type --- tests/phpunit/tests/block-template-utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 96c184f65c338..51d5e5dd1af1a 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -491,6 +491,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request->set_param( 'id', $id ); $changes = new stdClass(); + $changes->post_type = 'wp_template'; $changes->post_author = 123; $changes->post_title = 'My updated Template'; $changes->post_content = 'Hello'; From 218e33d2bf5660358cb7affccddca5ca28007a9c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:23:13 +0100 Subject: [PATCH 019/111] Cover post name --- src/wp-includes/block-template-utils.php | 1 + tests/phpunit/tests/block-template-utils.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 5a2f1adc17068..08593f0f8b30e 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1472,6 +1472,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { // TODO: Should maybe make this static. E.g. in the Templates Controller? $post_to_template_key_map = array( 'post_author' => 'author', + 'post_name' => 'slug', 'post_content' => 'content', 'post_title' => 'title', 'post_excerpt' => 'description', diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 51d5e5dd1af1a..e067d4ee071ef 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -493,6 +493,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $changes = new stdClass(); $changes->post_type = 'wp_template'; $changes->post_author = 123; + $changes->post_name = 'my-updated-template'; $changes->post_title = 'My updated Template'; $changes->post_content = 'Hello'; $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; @@ -511,6 +512,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $post_to_template_key_map = array( 'post_author' => 'author', + 'post_name' => 'slug', 'post_content' => 'content', 'post_title' => 'title', 'post_excerpt' => 'description', From 0b9bc726ce31a60b9355c82a10201d04eac64309 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Mar 2024 17:30:57 +0100 Subject: [PATCH 020/111] Cover theme and ID --- src/wp-includes/block-template-utils.php | 6 ++++++ tests/phpunit/tests/block-template-utils.php | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 08593f0f8b30e..bc5060b442eb1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1491,6 +1491,12 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { if ( isset( $post->meta_input['origin'] ) ) { $template->origin = $post->meta_input['origin']; } + + if ( isset( $post->tax_input['wp_theme'] ) ) { + $template->theme = $post->tax_input['wp_theme']; + } + + $template->id = $template->theme . '//' . $template->slug; } $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index e067d4ee071ef..92ac1d077a079 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -500,6 +500,9 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $changes->meta_input = array( 'origin' => 'custom', ); + $changes->tax_input = array( + 'wp_theme' => self::TEST_THEME, + ); inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); @@ -526,7 +529,9 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $expected[ $template_key ] = $changes->$post_key; } } + $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; $expected['origin'] = 'custom'; + $expected['theme'] = self::TEST_THEME; $this->assertEquals( $expected, (array) $context ); } From b0591afceba5e7b0e7ab856d0e3349ad3281e5c6 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 14 Mar 2024 08:28:45 +0000 Subject: [PATCH 021/111] Remove whitespace --- src/wp-includes/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index bc5060b442eb1..99441af42ccd1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1488,11 +1488,11 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { } } - if ( isset( $post->meta_input['origin'] ) ) { + if ( isset( $post->meta_input['origin'] ) ) { $template->origin = $post->meta_input['origin']; } - if ( isset( $post->tax_input['wp_theme'] ) ) { + if ( isset( $post->tax_input['wp_theme'] ) ) { $template->theme = $post->tax_input['wp_theme']; } From 67e1c4770b2f809865885e70877ffb50877f3db6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 10:58:22 +0100 Subject: [PATCH 022/111] Rephrase comment --- src/wp-includes/block-template-utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 99441af42ccd1..84f21e8ff1ee7 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1469,7 +1469,9 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); if ( $template ) { - // TODO: Should maybe make this static. E.g. in the Templates Controller? + // TODO: We might want to extract the mapping logic below into a separate function + // and reuse it in unit tests, and in `_build_block_template_result_from_post`. + $post_to_template_key_map = array( 'post_author' => 'author', 'post_name' => 'slug', From 945670af743f38eb4d9d2c4dbf5ad54d1c6ed267 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 10:59:07 +0100 Subject: [PATCH 023/111] Whitespace, my raison d'etre --- tests/phpunit/tests/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 92ac1d077a079..88f607164c834 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -491,7 +491,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request->set_param( 'id', $id ); $changes = new stdClass(); - $changes->post_type = 'wp_template'; + $changes->post_type = 'wp_template'; $changes->post_author = 123; $changes->post_name = 'my-updated-template'; $changes->post_title = 'My updated Template'; @@ -500,7 +500,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $changes->meta_input = array( 'origin' => 'custom', ); - $changes->tax_input = array( + $changes->tax_input = array( 'wp_theme' => self::TEST_THEME, ); From dee0a86b736cce25285bc4f1c9e1ac46dbab4bc8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 14:39:32 +0100 Subject: [PATCH 024/111] Madness --- src/wp-includes/block-template-utils.php | 56 ++++++++++---------- tests/phpunit/tests/block-template-utils.php | 48 ++++++++++------- 2 files changed, 56 insertions(+), 48 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 84f21e8ff1ee7..dcab3bd183849 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1461,45 +1461,45 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { return $post; } - // At this point, the post has already been created. // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. // To that end, we need to suppress hooked blocks from getting inserted into the template. add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); - $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; - remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); - if ( $template ) { - // TODO: We might want to extract the mapping logic below into a separate function - // and reuse it in unit tests, and in `_build_block_template_result_from_post`. - - $post_to_template_key_map = array( - 'post_author' => 'author', - 'post_name' => 'slug', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_excerpt' => 'description', - 'post_type' => 'type', - 'post_status' => 'status', - ); + // We also need to mimic terms and meta for the post based on the corresponding + // `terms_input` and `meta_input` properties in the changes object. - // We need to overwrite the built template object with the incoming one from the request. - // This is so we can provide the correct context to the Block Hooks API which expects the `WP_Block_Template` object. - foreach ( $post_to_template_key_map as $post_key => $template_key ) { - if ( isset( $post->$post_key ) ) { - $template->{$template_key} = $post->$post_key; - } + $terms_filter = function( $terms, $post_id, $taxonomy ) use ( $post ) { + if ( 'wp_theme' === $taxonomy && isset( $post->tax_input['wp_theme'] ) ) { + // TODO: Verify it's not an error object. + // TODO: Verify that $post_id matches (or isn't set). + $term = new stdClass; + $term->name = $post->tax_input['wp_theme']; + + $term = new WP_Term( $term ); + return array( $term ); } + return $terms; + }; - if ( isset( $post->meta_input['origin'] ) ) { - $template->origin = $post->meta_input['origin']; + $meta_filter = function( $value, $post_id, $meta_key, $single ) use ( $post ) { + if ( 'origin' === $meta_key && isset( $post->meta_input['origin'] ) ) { + return $single ? $post->meta_input['origin'] : array( $post->meta_input['origin'] ); } - if ( isset( $post->tax_input['wp_theme'] ) ) { - $template->theme = $post->tax_input['wp_theme']; + if ( 'is_wp_suggestion' === $meta_key && isset( $post->meta_input['is_wp_suggestion'] ) ) { + return $single ? $post->meta_input['is_wp_suggestion'] : array( $post->meta_input['is_wp_suggestion'] ); } - $template->id = $template->theme . '//' . $template->slug; - } + return $value; + }; + + add_filter( 'get_the_terms', $terms_filter, 10, 3 ); + add_filter( 'get_post_metadata', $meta_filter, 10, 4 ); + $template = _build_block_template_result_from_post( $post ); + remove_filter( 'get_post_metadata', $meta_filter ); + remove_filter( 'get_the_terms', $terms_filter ); + + remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 88f607164c834..19f8222a99d02 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -490,17 +490,21 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); $request->set_param( 'id', $id ); - $changes = new stdClass(); - $changes->post_type = 'wp_template'; - $changes->post_author = 123; - $changes->post_name = 'my-updated-template'; - $changes->post_title = 'My updated Template'; - $changes->post_content = 'Hello'; - $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; - $changes->meta_input = array( - 'origin' => 'custom', + $changes = new stdClass(); + $changes->ID = self::$template_post->ID; + $changes->post_type = 'wp_template'; + $changes->post_author = 123; + $changes->post_name = 'my-updated-template'; + $changes->post_title = 'My updated Template'; + $changes->post_content = 'Hello'; + $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; + $changes->post_status = 'publish'; + $changes->post_modified = '2021-07-01 12:00:00'; // FIXME + $changes->meta_input = array( + 'origin' => 'custom', + 'is_wp_suggestion' => true, ); - $changes->tax_input = array( + $changes->tax_input = array( 'wp_theme' => self::TEST_THEME, ); @@ -514,13 +518,15 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $this->assertInstanceOf( 'WP_Block_Template', $context ); $post_to_template_key_map = array( - 'post_author' => 'author', - 'post_name' => 'slug', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_excerpt' => 'description', - 'post_type' => 'type', - 'post_status' => 'status', + 'ID' => 'wp_id', + 'post_author' => 'author', + 'post_name' => 'slug', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_excerpt' => 'description', + 'post_type' => 'type', + 'post_status' => 'status', + 'post_modified' => 'modified', ); $expected = array(); @@ -529,9 +535,11 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $expected[ $template_key ] = $changes->$post_key; } } - $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; - $expected['origin'] = 'custom'; - $expected['theme'] = self::TEST_THEME; + $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; + $expected['origin'] = 'custom'; + $expected['source'] = 'custom'; + $expected['theme'] = self::TEST_THEME; + $expected['is_custom'] = false; $this->assertEquals( $expected, (array) $context ); } From 045ebeeadfd238ec2475334fedfeb121ff82e89f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 14:40:31 +0100 Subject: [PATCH 025/111] Rewrite a bit --- src/wp-includes/block-template-utils.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index dcab3bd183849..3a2f30fcce44a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1469,16 +1469,17 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { // `terms_input` and `meta_input` properties in the changes object. $terms_filter = function( $terms, $post_id, $taxonomy ) use ( $post ) { - if ( 'wp_theme' === $taxonomy && isset( $post->tax_input['wp_theme'] ) ) { - // TODO: Verify it's not an error object. - // TODO: Verify that $post_id matches (or isn't set). - $term = new stdClass; - $term->name = $post->tax_input['wp_theme']; - - $term = new WP_Term( $term ); - return array( $term ); + if ( 'wp_theme' !== $taxonomy || ! isset( $post->tax_input['wp_theme'] ) ) { + return $terms; } - return $terms; + + // TODO: Verify it's not an error object. + // TODO: Verify that $post_id matches (or isn't set). + $term = new stdClass; + $term->name = $post->tax_input['wp_theme']; + + $term = new WP_Term( $term ); + return array( $term ); }; $meta_filter = function( $value, $post_id, $meta_key, $single ) use ( $post ) { From d70d1fe757499a2739069c59d47db3e901ad9620 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 14:49:56 +0100 Subject: [PATCH 026/111] Add ticket number to test --- tests/phpunit/tests/block-template-utils.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 19f8222a99d02..ab1f305b44be3 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -475,6 +475,8 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ } /** + * @ticket 60754 + * * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { From d25f35a019e4fcf5a5ce3f050545bb5a7f2e3ea3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 15:09:32 +0100 Subject: [PATCH 027/111] Get test to pass --- tests/phpunit/tests/block-template-utils.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index ab1f305b44be3..990cb837ef51c 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -537,11 +537,14 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $expected[ $template_key ] = $changes->$post_key; } } - $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; - $expected['origin'] = 'custom'; - $expected['source'] = 'custom'; - $expected['theme'] = self::TEST_THEME; - $expected['is_custom'] = false; + $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; + $expected['origin'] = 'custom'; + $expected['source'] = 'custom'; + $expected['theme'] = self::TEST_THEME; + $expected['is_custom'] = false; + $expected['has_theme_file'] = false; + $expected['post_types'] = null; + $expected['area'] = null; $this->assertEquals( $expected, (array) $context ); } From 6a76a0728cf0afcd9fc399f648bdb0a294c6ab29 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 19:28:21 +0100 Subject: [PATCH 028/111] Rename static post var to template_post --- .../rest-api/wpRestTemplatesController.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 9665939dd6975..3effc651e3a2c 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -14,7 +14,7 @@ class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testc * @var int */ protected static $admin_id; - private static $post; + private static $template_post; /** * Create fake data before our tests run. @@ -29,7 +29,7 @@ public static function wpSetupBeforeClass( $factory ) { ); // Set up template post. - $args = array( + $args = array( 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', @@ -41,12 +41,12 @@ public static function wpSetupBeforeClass( $factory ) { ), ), ); - self::$post = self::factory()->post->create_and_get( $args ); - wp_set_post_terms( self::$post->ID, get_stylesheet(), 'wp_theme' ); + self::$template_post = self::factory()->post->create_and_get( $args ); + wp_set_post_terms( self::$template_post->ID, get_stylesheet(), 'wp_theme' ); } public static function wpTearDownAfterClass() { - wp_delete_post( self::$post->ID ); + wp_delete_post( self::$template_post->ID ); } /** @@ -130,11 +130,11 @@ public function test_get_items() { 'rendered' => 'My Template', ), 'status' => 'publish', - 'wp_id' => self::$post->ID, + 'wp_id' => self::$template_post->ID, 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, - 'modified' => mysql_to_rfc3339( self::$post->post_modified ), + 'modified' => mysql_to_rfc3339( self::$template_post->post_modified ), 'author_text' => 'Test Blog', 'original_source' => 'site', ), @@ -177,11 +177,11 @@ public function test_get_item() { 'rendered' => 'My Template', ), 'status' => 'publish', - 'wp_id' => self::$post->ID, + 'wp_id' => self::$template_post->ID, 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, - 'modified' => mysql_to_rfc3339( self::$post->post_modified ), + 'modified' => mysql_to_rfc3339( self::$template_post->post_modified ), 'author_text' => 'Test Blog', 'original_source' => 'site', ), @@ -216,11 +216,11 @@ public function test_get_item_works_with_a_single_slash( $endpoint_url ) { 'rendered' => 'My Template', ), 'status' => 'publish', - 'wp_id' => self::$post->ID, + 'wp_id' => self::$template_post->ID, 'has_theme_file' => false, 'is_custom' => true, 'author' => 0, - 'modified' => mysql_to_rfc3339( self::$post->post_modified ), + 'modified' => mysql_to_rfc3339( self::$template_post->post_modified ), 'author_text' => 'Test Blog', 'original_source' => 'site', ), From a7579956c58156072c4c81cd4f2f630190002745 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 14 Mar 2024 20:43:42 +0100 Subject: [PATCH 029/111] Attempt fixing controller test --- .../class-wp-rest-templates-controller.php | 1 + .../rest-api/wpRestTemplatesController.php | 33 ++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index cec8a77e04ef9..ac4ea293536bf 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -555,6 +555,7 @@ protected function prepare_item_for_database( $request ) { ); } else { $changes->post_name = $template->slug; + $changes->post_type = $this->post_type; $changes->ID = $template->wp_id; $changes->post_status = 'publish'; } diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 3effc651e3a2c..4d6c7f1d4649a 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -15,6 +15,7 @@ class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testc */ protected static $admin_id; private static $template_post; + private static $template_part_post; /** * Create fake data before our tests run. @@ -43,6 +44,26 @@ public static function wpSetupBeforeClass( $factory ) { ); self::$template_post = self::factory()->post->create_and_get( $args ); wp_set_post_terms( self::$template_post->ID, get_stylesheet(), 'wp_theme' ); + + // Set up template part post. + $args = array( + 'post_type' => 'wp_template_part', + 'post_name' => 'my_template_part', + 'post_title' => 'My Template Part', + 'post_content' => 'Content', + 'post_excerpt' => 'Description of my template part.', + 'tax_input' => array( + 'wp_theme' => array( + get_stylesheet(), + ), + 'wp_template_part_area' => array( + WP_TEMPLATE_PART_AREA_HEADER, + ), + ), + ); + self::$template_part_post = self::factory()->post->create_and_get( $args ); + wp_set_post_terms( self::$template_part_post->ID, get_stylesheet(), 'wp_theme' ); + wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' ); } public static function wpTearDownAfterClass() { @@ -951,13 +972,17 @@ public function test_prepare_item_for_database_injects_hooked_block() { $prepare_item_for_database = new ReflectionMethod( $endpoint, 'prepare_item_for_database' ); $prepare_item_for_database->setAccessible( true ); + $id = get_stylesheet() . '//' . 'my_template_part'; $body_params = array( - 'title' => 'Untitled Template Part', - 'slug' => 'untitled-template-part', - 'content' => 'Hello', + 'id' => $id, + 'title' => 'Untitled Template Part', + 'slug' => 'my_template_part', + 'description' => 'Description of my template part.', + 'author' => 1, + 'content' => 'Hello', ); - $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); $request->set_body_params( $body_params ); $prepared = $prepare_item_for_database->invoke( $endpoint, $request ); From 79f715f3a6cc844ce5b386f8262290a1b0b2ee82 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 15 Mar 2024 10:25:17 +0100 Subject: [PATCH 030/111] Rename argument to changes --- src/wp-includes/block-template-utils.php | 32 ++++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 3a2f30fcce44a..065e3552ee0bc 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1444,21 +1444,21 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @since 6.5.0 * @access private * - * @param stdClass $post An object representing a template or template part + * @param stdClass $changes An object representing a template or template part * prepared for inserting or updating the database. * @param WP_REST_Request $request Request object. * @return stdClass The updated object representing a template or template part. */ -function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { +function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) { $filter_name = current_filter(); if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) { - return $post; + return $changes; } $post_type = str_replace( 'rest_pre_insert_', '', $filter_name ); $hooked_blocks = get_hooked_blocks(); if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { - return $post; + return $changes; } // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. @@ -1468,27 +1468,27 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { // We also need to mimic terms and meta for the post based on the corresponding // `terms_input` and `meta_input` properties in the changes object. - $terms_filter = function( $terms, $post_id, $taxonomy ) use ( $post ) { - if ( 'wp_theme' !== $taxonomy || ! isset( $post->tax_input['wp_theme'] ) ) { + $terms_filter = function( $terms, $post_id, $taxonomy ) use ( $changes ) { + if ( 'wp_theme' !== $taxonomy || ! isset( $changes->tax_input['wp_theme'] ) ) { return $terms; } // TODO: Verify it's not an error object. // TODO: Verify that $post_id matches (or isn't set). $term = new stdClass; - $term->name = $post->tax_input['wp_theme']; + $term->name = $changes->tax_input['wp_theme']; $term = new WP_Term( $term ); return array( $term ); }; - $meta_filter = function( $value, $post_id, $meta_key, $single ) use ( $post ) { - if ( 'origin' === $meta_key && isset( $post->meta_input['origin'] ) ) { - return $single ? $post->meta_input['origin'] : array( $post->meta_input['origin'] ); + $meta_filter = function( $value, $post_id, $meta_key, $single ) use ( $changes ) { + if ( 'origin' === $meta_key && isset( $changes->meta_input['origin'] ) ) { + return $single ? $changes->meta_input['origin'] : array( $changes->meta_input['origin'] ); } - if ( 'is_wp_suggestion' === $meta_key && isset( $post->meta_input['is_wp_suggestion'] ) ) { - return $single ? $post->meta_input['is_wp_suggestion'] : array( $post->meta_input['is_wp_suggestion'] ); + if ( 'is_wp_suggestion' === $meta_key && isset( $changes->meta_input['is_wp_suggestion'] ) ) { + return $single ? $changes->meta_input['is_wp_suggestion'] : array( $changes->meta_input['is_wp_suggestion'] ); } return $value; @@ -1496,7 +1496,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { add_filter( 'get_the_terms', $terms_filter, 10, 3 ); add_filter( 'get_post_metadata', $meta_filter, 10, 4 ); - $template = _build_block_template_result_from_post( $post ); + $template = _build_block_template_result_from_post( $changes ); remove_filter( 'get_post_metadata', $meta_filter ); remove_filter( 'get_the_terms', $terms_filter ); @@ -1505,9 +1505,9 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - $blocks = parse_blocks( $post->post_content ); + $blocks = parse_blocks( $changes->post_content ); $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - $post->post_content = $content; - return $post; + $changes->post_content = $content; + return $changes; } From 3318e8695c0fa25db7e5107086a8c05533cc48ca Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 15 Mar 2024 10:25:57 +0100 Subject: [PATCH 031/111] Remove temp var --- src/wp-includes/block-template-utils.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 065e3552ee0bc..54f2be42dc01d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1505,9 +1505,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); - $blocks = parse_blocks( $changes->post_content ); - $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + $blocks = parse_blocks( $changes->post_content ); + $changes->post_content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - $changes->post_content = $content; return $changes; } From 817be3989307e24cd9f4fe758c63de7d124895d5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 15 Mar 2024 10:45:37 +0100 Subject: [PATCH 032/111] Polyfill missing information in changes from existing template post --- src/wp-includes/block-template-utils.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 54f2be42dc01d..b5b437536e4b1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1465,6 +1465,15 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) // To that end, we need to suppress hooked blocks from getting inserted into the template. add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); + if ( isset( $changes->ID ) ) { + $post = get_post( $changes->ID ); + } else { + // This means that there's not post for this template in the DB yet. + $post = new stdClass; // Is this correct? Might instead want to init a WP_Post with some fields set. + } + + $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); + // We also need to mimic terms and meta for the post based on the corresponding // `terms_input` and `meta_input` properties in the changes object. @@ -1496,7 +1505,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) add_filter( 'get_the_terms', $terms_filter, 10, 3 ); add_filter( 'get_post_metadata', $meta_filter, 10, 4 ); - $template = _build_block_template_result_from_post( $changes ); + $template = _build_block_template_result_from_post( $post_with_changes_applied ); remove_filter( 'get_post_metadata', $meta_filter ); remove_filter( 'get_the_terms', $terms_filter ); From 494de753d60ddf039f142b5a45bbda5044bd7fc8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 15 Mar 2024 10:56:29 +0100 Subject: [PATCH 033/111] Revert now-obsolete change to the controller --- .../rest-api/endpoints/class-wp-rest-templates-controller.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index ac4ea293536bf..cec8a77e04ef9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -555,7 +555,6 @@ protected function prepare_item_for_database( $request ) { ); } else { $changes->post_name = $template->slug; - $changes->post_type = $this->post_type; $changes->ID = $template->wp_id; $changes->post_status = 'publish'; } From 8720bc9777122e09457f93689a58f129bd1b9ab5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 15 Mar 2024 11:03:36 +0100 Subject: [PATCH 034/111] Remove now-obsolete test fixtures --- .../phpunit/tests/rest-api/wpRestTemplatesController.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 4d6c7f1d4649a..45bc45172b4a0 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -974,12 +974,9 @@ public function test_prepare_item_for_database_injects_hooked_block() { $id = get_stylesheet() . '//' . 'my_template_part'; $body_params = array( - 'id' => $id, - 'title' => 'Untitled Template Part', - 'slug' => 'my_template_part', - 'description' => 'Description of my template part.', - 'author' => 1, - 'content' => 'Hello', + 'id' => $id, + 'slug' => 'my_template_part', + 'content' => 'Hello', ); $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); From 1cae0ca2e16e5487f9fb31407025eb7749a1e27e Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 14:22:16 +0000 Subject: [PATCH 035/111] Add helper function to create a WP_Block_Template object from --- src/wp-includes/block-template-utils.php | 183 ++++++++++++----------- 1 file changed, 99 insertions(+), 84 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b5b437536e4b1..cc5c15e73a9e2 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -724,6 +724,68 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, return true; } +/** + * Build a block template object for based on a WP_Post object. + * + * @since 6.5.0 + * @access private + * + * @param WP_Post $post Template post. + * @param array $additional_fields Additional fields to add to the template object. + * @param bool $hook_blocks Whether to insert hooked blocks into the content or not. + * @return WP_Block_Template|WP_Error Template or error object. + */ +function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array(), $hook_blocks = true ) { + $default_template_types = get_default_block_template_types(); + $template_file = _get_block_template_file( $post->post_type, $post->post_name ); + + $template = new WP_Block_Template(); + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + + // Set default values for properties expected to be set. + $template->wp_id = null; + $template->modified = null; + $template->is_custom = null; + $template->has_theme_file = null; + $template->author = null; + $template->area = null; + $template->post_types = null; + + if ( + 'wp_template' === $post->post_type && + isset( $template_file['postTypes'] ) && + isset( $additional_fields['has_theme_file'] ) && + $additional_fields['has_theme_file'] + ) { + $template->post_types = $template_file['postTypes']; + } + + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { + $template->is_custom = false; + } + + if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { + foreach ( $additional_fields as $key => $value ) { + $template->{$key} = $value; + } + } + + if ( $hook_blocks ) { + $hooked_blocks = get_hooked_blocks(); + if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); + $blocks = parse_blocks( $template->content ); + $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + } + } +} + /** * Builds a unified template object based a post Object. * @@ -736,8 +798,6 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @return WP_Block_Template|WP_Error Template or error object. */ function _build_block_template_result_from_post( $post ) { - $default_template_types = get_default_block_template_types(); - $post_id = wp_is_post_revision( $post ); if ( ! $post_id ) { $post_id = $post; @@ -761,38 +821,25 @@ function _build_block_template_result_from_post( $post ) { $origin = get_post_meta( $parent_post->ID, 'origin', true ); $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); - $template = new WP_Block_Template(); - $template->wp_id = $post->ID; - $template->id = $theme . '//' . $parent_post->post_name; - $template->theme = $theme; - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->source = 'custom'; - $template->origin = ! empty( $origin ) ? $origin : null; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; - $template->has_theme_file = $has_theme_file; - $template->is_custom = empty( $is_wp_suggestion ); - $template->author = $post->post_author; - $template->modified = $post->post_modified; - - if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { - $template->post_types = $template_file['postTypes']; - } - - if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) { - $template->is_custom = false; - } + $additional_fields = array( + 'wp_id' => $post->ID, + 'has_theme_file' => $has_theme_file, + 'is_custom' => empty( $is_wp_suggestion ), + 'author' => $post->post_author, + 'modified' => $post->post_modified, + 'origin' => ! empty( $origin ) ? $origin : null, + 'source' => 'custom', + ); if ( 'wp_template_part' === $parent_post->post_type ) { $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $template->area = $type_terms[0]->name; + $additional_fields['area'] = $type_terms[0]->name; } } + $template = _build_block_template_object_from_wp_post_object( $post, $additional_fields ); + // Check for a block template without a description and title or with a title equal to the slug. if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) { $matches = array(); @@ -901,14 +948,6 @@ function _build_block_template_result_from_post( $post ) { } } - $hooked_blocks = get_hooked_blocks(); - if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); - $blocks = parse_blocks( $template->content ); - $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - } - return $template; } @@ -1450,66 +1489,42 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @return stdClass The updated object representing a template or template part. */ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) { - $filter_name = current_filter(); - if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) { - return $changes; - } - $post_type = str_replace( 'rest_pre_insert_', '', $filter_name ); - $hooked_blocks = get_hooked_blocks(); if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { return $changes; } - // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. - // To that end, we need to suppress hooked blocks from getting inserted into the template. - add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); - - if ( isset( $changes->ID ) ) { - $post = get_post( $changes->ID ); - } else { - // This means that there's not post for this template in the DB yet. - $post = new stdClass; // Is this correct? Might instead want to init a WP_Post with some fields set. + // Custom made templates don't have a post_name set. + if ( ! isset( $changes->post_name ) ) { + $changes->post_name = $request['slug']; } - $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); - - // We also need to mimic terms and meta for the post based on the corresponding - // `terms_input` and `meta_input` properties in the changes object. - - $terms_filter = function( $terms, $post_id, $taxonomy ) use ( $changes ) { - if ( 'wp_theme' !== $taxonomy || ! isset( $changes->tax_input['wp_theme'] ) ) { - return $terms; - } - - // TODO: Verify it's not an error object. - // TODO: Verify that $post_id matches (or isn't set). - $term = new stdClass; - $term->name = $changes->tax_input['wp_theme']; - - $term = new WP_Term( $term ); - return array( $term ); - }; + $theme = $changes->tax_input['wp_theme']; + $template_file = _get_block_template_file( $changes->post_type, $changes->post_name ); - $meta_filter = function( $value, $post_id, $meta_key, $single ) use ( $changes ) { - if ( 'origin' === $meta_key && isset( $changes->meta_input['origin'] ) ) { - return $single ? $changes->meta_input['origin'] : array( $changes->meta_input['origin'] ); - } - - if ( 'is_wp_suggestion' === $meta_key && isset( $changes->meta_input['is_wp_suggestion'] ) ) { - return $single ? $changes->meta_input['is_wp_suggestion'] : array( $changes->meta_input['is_wp_suggestion'] ); - } + $additional_fields = array( + 'theme' => $changes->tax_input['wp_theme'], + 'id' => $theme . '//' . $changes->post_name, + 'has_theme_file' => get_stylesheet() === $theme && null !== $template_file, + 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, + 'source' => 'custom', + 'author' => isset( $changes->post_author ) ? $changes->post_author : null, + 'is_custom' => isset( $changes->meta_input['is_wp_suggestion'] ) ? $changes->meta_input['is_wp_suggestion'] : false, + ); - return $value; - }; + if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { + $additional_fields['area'] = $changes->tax_input['wp_template_part_area']; + } - add_filter( 'get_the_terms', $terms_filter, 10, 3 ); - add_filter( 'get_post_metadata', $meta_filter, 10, 4 ); - $template = _build_block_template_result_from_post( $post_with_changes_applied ); - remove_filter( 'get_post_metadata', $meta_filter ); - remove_filter( 'get_the_terms', $terms_filter ); + if ( ! empty( $changes->ID ) ) { + $post = get_post( $changes->ID ); + } else { + $post = $changes; + } - remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); + $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); + // Last parameter is set to false to avoid hooking blocks into the content. + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields, false ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From 1cdb1cd1f6b3837c8a78dc89a9fc5b6a1fe9c7f4 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 14:32:05 +0000 Subject: [PATCH 036/111] Question above post_name --- src/wp-includes/block-template-utils.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index cc5c15e73a9e2..ed4a1113e0b11 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1495,6 +1495,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } // Custom made templates don't have a post_name set. + // TODO: Shall we update the controller to always set the post_name? if ( ! isset( $changes->post_name ) ) { $changes->post_name = $request['slug']; } @@ -1523,6 +1524,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); + // Last parameter is set to false to avoid hooking blocks into the content. $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields, false ); From 48c7fec9a82f6c9aec5fb1e12c8028dac70fe1b7 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 19:26:04 +0000 Subject: [PATCH 037/111] Remove hooked blocks algorithm from new helper function --- src/wp-includes/block-template-utils.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index ed4a1113e0b11..2ae7acda9eaa8 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -735,7 +735,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @param bool $hook_blocks Whether to insert hooked blocks into the content or not. * @return WP_Block_Template|WP_Error Template or error object. */ -function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array(), $hook_blocks = true ) { +function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { $default_template_types = get_default_block_template_types(); $template_file = _get_block_template_file( $post->post_type, $post->post_name ); @@ -775,15 +775,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } - if ( $hook_blocks ) { - $hooked_blocks = get_hooked_blocks(); - if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); - $blocks = parse_blocks( $template->content ); - $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - } - } + return $template; } /** @@ -948,6 +940,14 @@ function _build_block_template_result_from_post( $post ) { } } + $hooked_blocks = get_hooked_blocks(); + if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); + $blocks = parse_blocks( $template->content ); + $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + } + return $template; } @@ -1526,7 +1526,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); // Last parameter is set to false to avoid hooking blocks into the content. - $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields, false ); + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From ec5c8fbb7a136985a2f409c8045a38902f2e9ee2 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 19:26:26 +0000 Subject: [PATCH 038/111] Remove default values as theyre set in the class declaration --- src/wp-includes/block-template-utils.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2ae7acda9eaa8..e44f9d0891e40 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -747,15 +747,6 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template->title = $post->post_title; $template->status = $post->post_status; - // Set default values for properties expected to be set. - $template->wp_id = null; - $template->modified = null; - $template->is_custom = null; - $template->has_theme_file = null; - $template->author = null; - $template->area = null; - $template->post_types = null; - if ( 'wp_template' === $post->post_type && isset( $template_file['postTypes'] ) && From 7ee78fabe0bc31108668c3d93c39496e68aae921 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 20:03:26 +0000 Subject: [PATCH 039/111] Add post to template mapping --- src/wp-includes/block-template-utils.php | 53 ++++++++++++++---------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index e44f9d0891e40..b4acf8c048705 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -739,13 +739,28 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $default_template_types = get_default_block_template_types(); $template_file = _get_block_template_file( $post->post_type, $post->post_name ); + /* + * These are fields that are directly mapped from the post object to the template object. + */ + $post_to_template_mapping = array( + 'ID' => 'wp_id', + 'post_content' => 'content', + 'post_name' => 'slug', + 'post_type' => 'type', + 'post_excerpt' => 'description', + 'post_title' => 'title', + 'post_status' => 'status', + 'post_author' => 'author', + 'post_modified' => 'modified', + ); + $template = new WP_Block_Template(); - $template->content = $post->post_content; - $template->slug = $post->post_name; - $template->type = $post->post_type; - $template->description = $post->post_excerpt; - $template->title = $post->post_title; - $template->status = $post->post_status; + + foreach ( $post_to_template_mapping as $post_key => $template_key ) { + if ( isset( $post->{$post_key} ) ) { + $template->{$template_key} = $post->{$post_key}; + } + } if ( 'wp_template' === $post->post_type && @@ -808,8 +823,6 @@ function _build_block_template_result_from_post( $post ) { 'wp_id' => $post->ID, 'has_theme_file' => $has_theme_file, 'is_custom' => empty( $is_wp_suggestion ), - 'author' => $post->post_author, - 'modified' => $post->post_modified, 'origin' => ! empty( $origin ) ? $origin : null, 'source' => 'custom', ); @@ -1485,14 +1498,18 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - // Custom made templates don't have a post_name set. - // TODO: Shall we update the controller to always set the post_name? - if ( ! isset( $changes->post_name ) ) { - $changes->post_name = $request['slug']; - } - $theme = $changes->tax_input['wp_theme']; $template_file = _get_block_template_file( $changes->post_type, $changes->post_name ); + $additional_fields = array(); + + if ( ! empty( $changes->ID ) ) { + $post = get_post( $changes->ID ); + } else { + if ( ! isset( $changes->post_name ) ) { + $changes->post_name = $request['slug']; + } + $post = $changes; + } $additional_fields = array( 'theme' => $changes->tax_input['wp_theme'], @@ -1500,7 +1517,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) 'has_theme_file' => get_stylesheet() === $theme && null !== $template_file, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, 'source' => 'custom', - 'author' => isset( $changes->post_author ) ? $changes->post_author : null, 'is_custom' => isset( $changes->meta_input['is_wp_suggestion'] ) ? $changes->meta_input['is_wp_suggestion'] : false, ); @@ -1508,15 +1524,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $additional_fields['area'] = $changes->tax_input['wp_template_part_area']; } - if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); - } else { - $post = $changes; - } - $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); - // Last parameter is set to false to avoid hooking blocks into the content. $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From 377da4c16cc1c119a21f515b2151a7e84bc4dd5c Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 20:28:01 +0000 Subject: [PATCH 040/111] Updated to missing default fields and is_custom --- src/wp-includes/block-template-utils.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b4acf8c048705..86fa2298cca6a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -821,6 +821,8 @@ function _build_block_template_result_from_post( $post ) { $additional_fields = array( 'wp_id' => $post->ID, + 'id' => $theme . '//' . $parent_post->post_name, + 'theme' => $theme, 'has_theme_file' => $has_theme_file, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, @@ -1517,7 +1519,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) 'has_theme_file' => get_stylesheet() === $theme && null !== $template_file, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, 'source' => 'custom', - 'is_custom' => isset( $changes->meta_input['is_wp_suggestion'] ) ? $changes->meta_input['is_wp_suggestion'] : false, + 'is_custom' => empty( $changes->meta_input['is_wp_suggestion'] ), ); if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { From 6462922493723e11840d7868ff5c781beae563ed Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 10:06:53 +0000 Subject: [PATCH 041/111] Update safety checks --- src/wp-includes/block-template-utils.php | 37 ++++++++++++++---------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 86fa2298cca6a..2aa10cef1cc32 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -726,7 +726,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, /** * Build a block template object for based on a WP_Post object. - * + * * @since 6.5.0 * @access private * @@ -762,11 +762,17 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } + if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { + foreach ( $additional_fields as $key => $value ) { + $template->{$key} = $value; + } + } + if ( 'wp_template' === $post->post_type && - isset( $template_file['postTypes'] ) && isset( $additional_fields['has_theme_file'] ) && - $additional_fields['has_theme_file'] + $additional_fields['has_theme_file'] && + isset( $template_file['postTypes'] ) ) { $template->post_types = $template_file['postTypes']; } @@ -775,12 +781,6 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template->is_custom = false; } - if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { - foreach ( $additional_fields as $key => $value ) { - $template->{$key} = $value; - } - } - return $template; } @@ -820,7 +820,6 @@ function _build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); $additional_fields = array( - 'wp_id' => $post->ID, 'id' => $theme . '//' . $parent_post->post_name, 'theme' => $theme, 'has_theme_file' => $has_theme_file, @@ -1500,22 +1499,30 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - $theme = $changes->tax_input['wp_theme']; - $template_file = _get_block_template_file( $changes->post_type, $changes->post_name ); + $template_file = isset( $changes->post_type ) ? _get_block_template_file( $changes->post_type, $changes->post_name ) : null; $additional_fields = array(); if ( ! empty( $changes->ID ) ) { $post = get_post( $changes->ID ); + $terms = get_the_terms( $post, 'wp_theme' ); + if ( $terms ) { + $theme = $terms[0]->name; + } else { + $theme = null; + } } else { - if ( ! isset( $changes->post_name ) ) { + if ( empty( $changes->post_name ) ) { $changes->post_name = $request['slug']; } $post = $changes; + $theme = isset( $post->tax_input['wp_theme'] ) ? $post->tax_input['wp_theme'] : null; } + $template_id = ! empty( $theme ) && ! empty( $post->post_name ) ? $theme . '//' . $post->post_name : null; + $additional_fields = array( - 'theme' => $changes->tax_input['wp_theme'], - 'id' => $theme . '//' . $changes->post_name, + 'theme' => $theme, + 'id' => $template_id, 'has_theme_file' => get_stylesheet() === $theme && null !== $template_file, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, 'source' => 'custom', From 7812ce779292483619ad68e8a1fff02289094f76 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 10:48:34 +0000 Subject: [PATCH 042/111] Update condition --- src/wp-includes/block-template-utils.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2aa10cef1cc32..f9a37f1143c70 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1505,11 +1505,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) if ( ! empty( $changes->ID ) ) { $post = get_post( $changes->ID ); $terms = get_the_terms( $post, 'wp_theme' ); - if ( $terms ) { - $theme = $terms[0]->name; - } else { - $theme = null; - } + $theme = ! is_wp_error( $terms ) && ! empty( $terms ) ? $terms[0]->name : null; } else { if ( empty( $changes->post_name ) ) { $changes->post_name = $request['slug']; From 1cda30aa32fe1ff16969b5236a9b4388f2f38054 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 12:55:58 +0100 Subject: [PATCH 043/111] Compute has_theme_file inside helper --- src/wp-includes/block-template-utils.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f9a37f1143c70..1593565fda5d9 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -737,7 +737,14 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, */ function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { $default_template_types = get_default_block_template_types(); + + if ( empty ( $additional_fields['theme'] ) ) { + return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); + } + $theme = $additional_fields['theme']; + $template_file = _get_block_template_file( $post->post_type, $post->post_name ); + $has_theme_file = get_stylesheet() === $theme && null !== $template_file; /* * These are fields that are directly mapped from the post object to the template object. @@ -768,12 +775,9 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } - if ( - 'wp_template' === $post->post_type && - isset( $additional_fields['has_theme_file'] ) && - $additional_fields['has_theme_file'] && - isset( $template_file['postTypes'] ) - ) { + $template->has_theme_file = $has_theme_file; + + if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { $template->post_types = $template_file['postTypes']; } @@ -813,8 +817,6 @@ function _build_block_template_result_from_post( $post ) { } $theme = $terms[0]->name; - $template_file = _get_block_template_file( $post->post_type, $post->post_name ); - $has_theme_file = get_stylesheet() === $theme && null !== $template_file; $origin = get_post_meta( $parent_post->ID, 'origin', true ); $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); @@ -822,7 +824,6 @@ function _build_block_template_result_from_post( $post ) { $additional_fields = array( 'id' => $theme . '//' . $parent_post->post_name, 'theme' => $theme, - 'has_theme_file' => $has_theme_file, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, 'source' => 'custom', @@ -1499,7 +1500,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - $template_file = isset( $changes->post_type ) ? _get_block_template_file( $changes->post_type, $changes->post_name ) : null; $additional_fields = array(); if ( ! empty( $changes->ID ) ) { @@ -1519,7 +1519,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $additional_fields = array( 'theme' => $theme, 'id' => $template_id, - 'has_theme_file' => get_stylesheet() === $theme && null !== $template_file, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, 'source' => 'custom', 'is_custom' => empty( $changes->meta_input['is_wp_suggestion'] ), From 36c3597b7a85cb77cbb758d70237fb62d7ef003e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 12:56:19 +0100 Subject: [PATCH 044/111] Alignment --- src/wp-includes/block-template-utils.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 1593565fda5d9..4b35e6855eee1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -750,14 +750,14 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi * These are fields that are directly mapped from the post object to the template object. */ $post_to_template_mapping = array( - 'ID' => 'wp_id', - 'post_content' => 'content', - 'post_name' => 'slug', - 'post_type' => 'type', - 'post_excerpt' => 'description', - 'post_title' => 'title', - 'post_status' => 'status', - 'post_author' => 'author', + 'ID' => 'wp_id', + 'post_content' => 'content', + 'post_name' => 'slug', + 'post_type' => 'type', + 'post_excerpt' => 'description', + 'post_title' => 'title', + 'post_status' => 'status', + 'post_author' => 'author', 'post_modified' => 'modified', ); From 7fc18004102074d4f52da8cf542407ff1014890a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 13:00:22 +0100 Subject: [PATCH 045/111] source is hard-wired to 'custom' --- src/wp-includes/block-template-utils.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 4b35e6855eee1..cf155cc97f787 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -775,6 +775,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } + $template->source = 'custom'; $template->has_theme_file = $has_theme_file; if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { @@ -826,7 +827,6 @@ function _build_block_template_result_from_post( $post ) { 'theme' => $theme, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, - 'source' => 'custom', ); if ( 'wp_template_part' === $parent_post->post_type ) { @@ -1520,7 +1520,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) 'theme' => $theme, 'id' => $template_id, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, - 'source' => 'custom', 'is_custom' => empty( $changes->meta_input['is_wp_suggestion'] ), ); From 631ff10a9410643328c940b1c46836606586ccdb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 13:02:46 +0100 Subject: [PATCH 046/111] Whitespace --- 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 cf155cc97f787..d0cb26e88e055 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -817,7 +817,7 @@ function _build_block_template_result_from_post( $post ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } - $theme = $terms[0]->name; + $theme = $terms[0]->name; $origin = get_post_meta( $parent_post->ID, 'origin', true ); $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); From dd74e43f42ca8477595302734646af7d5b101d39 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 13:12:03 +0100 Subject: [PATCH 047/111] Still whitespace --- 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 d0cb26e88e055..d2355aacce9c0 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -738,7 +738,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { $default_template_types = get_default_block_template_types(); - if ( empty ( $additional_fields['theme'] ) ) { + if ( empty( $additional_fields['theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } $theme = $additional_fields['theme']; From 0d35323dd25965450d8cbf195ced6d264d7ec1ac Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 12:26:01 +0000 Subject: [PATCH 048/111] Build template ID from values --- src/wp-includes/block-template-utils.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index d2355aacce9c0..d2b3e7d033852 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -775,6 +775,8 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } + $template->id = ! empty( $template->theme ) && ! empty( $template->slug ) ? $template->theme . '//' . $template->slug : null; + $template->source = 'custom'; $template->has_theme_file = $has_theme_file; @@ -823,7 +825,6 @@ function _build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); $additional_fields = array( - 'id' => $theme . '//' . $parent_post->post_name, 'theme' => $theme, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, @@ -1514,11 +1515,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $theme = isset( $post->tax_input['wp_theme'] ) ? $post->tax_input['wp_theme'] : null; } - $template_id = ! empty( $theme ) && ! empty( $post->post_name ) ? $theme . '//' . $post->post_name : null; - $additional_fields = array( 'theme' => $theme, - 'id' => $template_id, 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, 'is_custom' => empty( $changes->meta_input['is_wp_suggestion'] ), ); From 95d679b93a52ccd2aea0acb6654d25e9933790cd Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 12:31:02 +0000 Subject: [PATCH 049/111] Guard against class properties --- src/wp-includes/block-template-utils.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index d2b3e7d033852..e87676babb1f3 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -764,14 +764,16 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template = new WP_Block_Template(); foreach ( $post_to_template_mapping as $post_key => $template_key ) { - if ( isset( $post->{$post_key} ) ) { + if ( isset( $post->{$post_key} ) && property_exists( $template, $template_key ) ) { $template->{$template_key} = $post->{$post_key}; } } if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { foreach ( $additional_fields as $key => $value ) { - $template->{$key} = $value; + if ( property_exists( $template, $key ) ) { + $template->{$key} = $value; + } } } From 6490f66bae37ea584cbaacb74bae509bbfd1e3bd Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 13:10:53 +0000 Subject: [PATCH 050/111] Whitespace --- src/wp-includes/block-template-utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index e87676babb1f3..474b605590e5d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -778,7 +778,6 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } $template->id = ! empty( $template->theme ) && ! empty( $template->slug ) ? $template->theme . '//' . $template->slug : null; - $template->source = 'custom'; $template->has_theme_file = $has_theme_file; From bf05a79da19face8612034f8e4844ca692820d92 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 14:46:57 +0100 Subject: [PATCH 051/111] Whitespace --- 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 474b605590e5d..bccf4f859b80f 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -772,7 +772,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { foreach ( $additional_fields as $key => $value ) { if ( property_exists( $template, $key ) ) { - $template->{$key} = $value; + $template->{$key} = $value; } } } From c974de87e5108e4ea556c98163c1cd5184f20efd Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:04:30 +0000 Subject: [PATCH 052/111] Refactor derived template object value assignment --- src/wp-includes/block-template-utils.php | 30 ++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index bccf4f859b80f..2dbe25d47d182 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -745,6 +745,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template_file = _get_block_template_file( $post->post_type, $post->post_name ); $has_theme_file = get_stylesheet() === $theme && null !== $template_file; + $template_id = ! empty( $theme ) && ! empty( $post->post_name ) ? $theme . '//' . $post->post_name : null; /* * These are fields that are directly mapped from the post object to the template object. @@ -761,6 +762,23 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi 'post_modified' => 'modified', ); + $derived_or_hardcoded_fields = array( + 'id' => $template_id, + 'source' => 'custom', + 'has_theme_file' => $has_theme_file, + 'theme' => $theme, + ); + + if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { + $derived_or_hardcoded_fields['post_types'] = $template_file['postTypes']; + } + + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $post->post_name ] ) ) { + $derived_or_hardcoded_fields['is_custom'] = false; + } + + $additional_fields = is_array( $additional_fields ) && ! empty( $additional_fields ) ? array_merge( $additional_fields, $derived_or_hardcoded_fields ) : $derived_or_hardcoded_fields; + $template = new WP_Block_Template(); foreach ( $post_to_template_mapping as $post_key => $template_key ) { @@ -777,18 +795,6 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi } } - $template->id = ! empty( $template->theme ) && ! empty( $template->slug ) ? $template->theme . '//' . $template->slug : null; - $template->source = 'custom'; - $template->has_theme_file = $has_theme_file; - - if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { - $template->post_types = $template_file['postTypes']; - } - - if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { - $template->is_custom = false; - } - return $template; } From 9274b8dc030033d78090112b1c191a3b42911dad Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:06:13 +0000 Subject: [PATCH 053/111] Remove from derived values --- src/wp-includes/block-template-utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2dbe25d47d182..2512840bed8f0 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -766,7 +766,6 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi 'id' => $template_id, 'source' => 'custom', 'has_theme_file' => $has_theme_file, - 'theme' => $theme, ); if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { From b59b2d8bb2ca06ce3585b5298b50d7c1cd1bf958 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:13:05 +0000 Subject: [PATCH 054/111] Tabs to spaces --- 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 2512840bed8f0..261ce59d5b36a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -764,7 +764,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $derived_or_hardcoded_fields = array( 'id' => $template_id, - 'source' => 'custom', + 'source' => 'custom', 'has_theme_file' => $has_theme_file, ); From 7e60e5ae12d714837f6ea68bb9fec83052b43958 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:13:23 +0000 Subject: [PATCH 055/111] Remove space --- 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 261ce59d5b36a..92453a9af79f4 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -776,7 +776,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $derived_or_hardcoded_fields['is_custom'] = false; } - $additional_fields = is_array( $additional_fields ) && ! empty( $additional_fields ) ? array_merge( $additional_fields, $derived_or_hardcoded_fields ) : $derived_or_hardcoded_fields; + $additional_fields = is_array( $additional_fields ) && ! empty( $additional_fields ) ? array_merge( $additional_fields, $derived_or_hardcoded_fields ) : $derived_or_hardcoded_fields; $template = new WP_Block_Template(); From fb03812aa92bfb57a3688564612aa35c446f99a4 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:16:11 +0000 Subject: [PATCH 056/111] Comment --- src/wp-includes/block-template-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 92453a9af79f4..b173d7f2f5329 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -762,6 +762,10 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi 'post_modified' => 'modified', ); + /* + * These are derived or hardcoded fields that are added to the template object. + * Doing it this way means we benefit from the property_exists check on the WP_Block_Template class. + */ $derived_or_hardcoded_fields = array( 'id' => $template_id, 'source' => 'custom', From efe5dbeebe11eb581d420e8b580e865ead070d0f Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:35:39 +0000 Subject: [PATCH 057/111] Add ID to additional fields --- src/wp-includes/block-template-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b173d7f2f5329..77463ee870b40 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -780,6 +780,9 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $derived_or_hardcoded_fields['is_custom'] = false; } + /* + * Merge $additional_fields with $derived_or_hardcoded_fields as the second argument to array_merge to ensure that $derived_or_hardcoded_fields take precedence. + */ $additional_fields = is_array( $additional_fields ) && ! empty( $additional_fields ) ? array_merge( $additional_fields, $derived_or_hardcoded_fields ) : $derived_or_hardcoded_fields; $template = new WP_Block_Template(); @@ -835,6 +838,7 @@ function _build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); $additional_fields = array( + 'id' => $theme . '//' . $parent_post->post_name, 'theme' => $theme, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, From 535b1f3c07cf31c42681d5d6fcdefc0d9b34b189 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Mon, 18 Mar 2024 14:37:38 +0000 Subject: [PATCH 058/111] Tabs to space --- 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 77463ee870b40..5add15da88b49 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -838,7 +838,7 @@ function _build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); $additional_fields = array( - 'id' => $theme . '//' . $parent_post->post_name, + 'id' => $theme . '//' . $parent_post->post_name, 'theme' => $theme, 'is_custom' => empty( $is_wp_suggestion ), 'origin' => ! empty( $origin ) ? $origin : null, From db647c1bd59f2f26a2a762bf407aae56e486da2a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 16:44:36 +0100 Subject: [PATCH 059/111] Make more explicit --- src/wp-includes/block-template-utils.php | 92 +++++++++--------------- 1 file changed, 34 insertions(+), 58 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 5add15da88b49..5bf69acb521e8 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -736,69 +736,46 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @return WP_Block_Template|WP_Error Template or error object. */ function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { - $default_template_types = get_default_block_template_types(); - if ( empty( $additional_fields['theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } $theme = $additional_fields['theme']; - $template_file = _get_block_template_file( $post->post_type, $post->post_name ); - $has_theme_file = get_stylesheet() === $theme && null !== $template_file; - $template_id = ! empty( $theme ) && ! empty( $post->post_name ) ? $theme . '//' . $post->post_name : null; - - /* - * These are fields that are directly mapped from the post object to the template object. - */ - $post_to_template_mapping = array( - 'ID' => 'wp_id', - 'post_content' => 'content', - 'post_name' => 'slug', - 'post_type' => 'type', - 'post_excerpt' => 'description', - 'post_title' => 'title', - 'post_status' => 'status', - 'post_author' => 'author', - 'post_modified' => 'modified', - ); - /* - * These are derived or hardcoded fields that are added to the template object. - * Doing it this way means we benefit from the property_exists check on the WP_Block_Template class. - */ - $derived_or_hardcoded_fields = array( - 'id' => $template_id, - 'source' => 'custom', - 'has_theme_file' => $has_theme_file, - ); - - if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { - $derived_or_hardcoded_fields['post_types'] = $template_file['postTypes']; - } + $default_template_types = get_default_block_template_types(); - if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $post->post_name ] ) ) { - $derived_or_hardcoded_fields['is_custom'] = false; + $post_id = wp_is_post_revision( $post ); + if ( ! $post_id ) { + $post_id = $post; } + $parent_post = get_post( $post_id ); - /* - * Merge $additional_fields with $derived_or_hardcoded_fields as the second argument to array_merge to ensure that $derived_or_hardcoded_fields take precedence. - */ - $additional_fields = is_array( $additional_fields ) && ! empty( $additional_fields ) ? array_merge( $additional_fields, $derived_or_hardcoded_fields ) : $derived_or_hardcoded_fields; - - $template = new WP_Block_Template(); + $template_file = _get_block_template_file( $post->post_type, $post->post_name ); + $has_theme_file = get_stylesheet() === $theme && null !== $template_file; - foreach ( $post_to_template_mapping as $post_key => $template_key ) { - if ( isset( $post->{$post_key} ) && property_exists( $template, $template_key ) ) { - $template->{$template_key} = $post->{$post_key}; - } + $template = new WP_Block_Template(); + $template->wp_id = $post->ID; + $template->id = $theme . '//' . $parent_post->post_name; + $template->theme = $theme; + $template->content = $post->post_content; + $template->slug = $post->post_name; + $template->source = 'custom'; + $template->origin = ! empty( $additional_fields['origin'] ) ? $additional_fields['origin'] : null; + $template->type = $post->post_type; + $template->description = $post->post_excerpt; + $template->title = $post->post_title; + $template->status = $post->post_status; + $template->has_theme_file = $has_theme_file; + $template->is_custom = empty( $additional_fields['is_wp_suggestion'] ); + $template->author = $post->post_author; + $template->modified = $post->post_modified; + + if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { + $template->post_types = $template_file['postTypes']; } - if ( is_array( $additional_fields ) && ! empty( $additional_fields ) ) { - foreach ( $additional_fields as $key => $value ) { - if ( property_exists( $template, $key ) ) { - $template->{$key} = $value; - } - } + if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $post->post_name ] ) ) { + $template->is_custom = false; } return $template; @@ -838,10 +815,9 @@ function _build_block_template_result_from_post( $post ) { $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); $additional_fields = array( - 'id' => $theme . '//' . $parent_post->post_name, - 'theme' => $theme, - 'is_custom' => empty( $is_wp_suggestion ), - 'origin' => ! empty( $origin ) ? $origin : null, + 'theme' => $theme, + 'origin' => $origin, + 'is_wp_suggestion' => $is_wp_suggestion, ); if ( 'wp_template_part' === $parent_post->post_type ) { @@ -1530,9 +1506,9 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } $additional_fields = array( - 'theme' => $theme, - 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, - 'is_custom' => empty( $changes->meta_input['is_wp_suggestion'] ), + 'theme' => $theme, + 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, + 'is_wp_suggestion' => isset( $changes->meta_input['is_wp_suggestion'] ) ? $changes->meta_input['is_wp_suggestion'] : null, ); if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { From c0807cc6947c194f8d975de8c046874978c434a6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 16:46:01 +0100 Subject: [PATCH 060/111] Even closer to original --- 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 5bf69acb521e8..f6c6c82d3b8bd 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -774,7 +774,7 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template->post_types = $template_file['postTypes']; } - if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $post->post_name ] ) ) { + if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) { $template->is_custom = false; } From a22a6c48f4c05dd62449cc6c777699ce4373a869 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 16:50:05 +0100 Subject: [PATCH 061/111] Remove now-obsolete PHPDoc --- src/wp-includes/block-template-utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f6c6c82d3b8bd..589e6037396a2 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -732,7 +732,6 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * * @param WP_Post $post Template post. * @param array $additional_fields Additional fields to add to the template object. - * @param bool $hook_blocks Whether to insert hooked blocks into the content or not. * @return WP_Block_Template|WP_Error Template or error object. */ function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { From 2faaab7269bfe80082149865572e5a9755b2ecb6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:04:31 +0100 Subject: [PATCH 062/111] Break additional_fields into terms and meta --- src/wp-includes/block-template-utils.php | 44 ++++++++++-------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 589e6037396a2..ff8a258ee421a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -730,16 +730,16 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @since 6.5.0 * @access private * - * @param WP_Post $post Template post. - * @param array $additional_fields Additional fields to add to the template object. + * @param WP_Post $post Template post. + * @param array $terms Additional terms to inform the template object. + * @param array $meta Additional meta fields to inform the template object. * @return WP_Block_Template|WP_Error Template or error object. */ -function _build_block_template_object_from_wp_post_object( $post, $additional_fields = array() ) { - if ( empty( $additional_fields['theme'] ) ) { +function _build_block_template_object_from_wp_post_object( $post, $terms = array(), $meta = array() ) { + if ( empty( $terms['theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } - $theme = $additional_fields['theme']; - + $theme = $terms['theme']; $default_template_types = get_default_block_template_types(); @@ -759,13 +759,13 @@ function _build_block_template_object_from_wp_post_object( $post, $additional_fi $template->content = $post->post_content; $template->slug = $post->post_name; $template->source = 'custom'; - $template->origin = ! empty( $additional_fields['origin'] ) ? $additional_fields['origin'] : null; + $template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null; $template->type = $post->post_type; $template->description = $post->post_excerpt; $template->title = $post->post_title; $template->status = $post->post_status; $template->has_theme_file = $has_theme_file; - $template->is_custom = empty( $additional_fields['is_wp_suggestion'] ); + $template->is_custom = empty( $meta['is_wp_suggestion'] ); $template->author = $post->post_author; $template->modified = $post->post_modified; @@ -808,15 +808,13 @@ function _build_block_template_result_from_post( $post ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } - $theme = $terms[0]->name; - - $origin = get_post_meta( $parent_post->ID, 'origin', true ); - $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ); + $terms = array( + 'theme' => $terms[0]->name, + ); - $additional_fields = array( - 'theme' => $theme, - 'origin' => $origin, - 'is_wp_suggestion' => $is_wp_suggestion, + $meta = array( + 'origin' => get_post_meta( $parent_post->ID, 'origin', true ), + 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), ); if ( 'wp_template_part' === $parent_post->post_type ) { @@ -826,7 +824,7 @@ function _build_block_template_result_from_post( $post ) { } } - $template = _build_block_template_object_from_wp_post_object( $post, $additional_fields ); + $template = _build_block_template_object_from_wp_post_object( $post, $terms, $meta ); // Check for a block template without a description and title or with a title equal to the slug. if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) { @@ -1490,8 +1488,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - $additional_fields = array(); - if ( ! empty( $changes->ID ) ) { $post = get_post( $changes->ID ); $terms = get_the_terms( $post, 'wp_theme' ); @@ -1504,19 +1500,17 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $theme = isset( $post->tax_input['wp_theme'] ) ? $post->tax_input['wp_theme'] : null; } - $additional_fields = array( - 'theme' => $theme, - 'origin' => isset( $changes->meta_input['origin'] ) ? $changes->meta_input['origin'] : null, - 'is_wp_suggestion' => isset( $changes->meta_input['is_wp_suggestion'] ) ? $changes->meta_input['is_wp_suggestion'] : null, + $terms = array( + 'theme' => $theme, ); if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { - $additional_fields['area'] = $changes->tax_input['wp_template_part_area']; + $terms['area'] = $changes->tax_input['wp_template_part_area']; } $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); - $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $additional_fields ); + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $terms, $changes->meta_input ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From 1f4a05e97b1bf5d4e2f975432aa22bc8091884bf Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Mon, 18 Mar 2024 17:10:13 +0100 Subject: [PATCH 063/111] Remove unnecessary `get_post` call. --- src/wp-includes/block-template-utils.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index ff8a258ee421a..023548419f6c8 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1489,8 +1489,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); - $terms = get_the_terms( $post, 'wp_theme' ); + $terms = get_the_terms( $changes->ID, 'wp_theme' ); $theme = ! is_wp_error( $terms ) && ! empty( $terms ) ? $terms[0]->name : null; } else { if ( empty( $changes->post_name ) ) { From 66123309594ae07a779377a4132edbd70f396eb0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:13:22 +0100 Subject: [PATCH 064/111] Revert "Remove unnecessary `get_post` call." This reverts commit efb250a3b00ad1ffa316ff29b7d2f74cc2f3a124. --- src/wp-includes/block-template-utils.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 023548419f6c8..ff8a258ee421a 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1489,7 +1489,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } if ( ! empty( $changes->ID ) ) { - $terms = get_the_terms( $changes->ID, 'wp_theme' ); + $post = get_post( $changes->ID ); + $terms = get_the_terms( $post, 'wp_theme' ); $theme = ! is_wp_error( $terms ) && ! empty( $terms ) ? $terms[0]->name : null; } else { if ( empty( $changes->post_name ) ) { From b38be2c17a3333edf805cd29d8495f379f715b43 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:19:21 +0100 Subject: [PATCH 065/111] Whitespace --- 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 ff8a258ee421a..d4079a85f638b 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1489,7 +1489,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); + $post = get_post( $changes->ID ); $terms = get_the_terms( $post, 'wp_theme' ); $theme = ! is_wp_error( $terms ) && ! empty( $terms ) ? $terms[0]->name : null; } else { From f3d3e512d80a0443635dd146195b13ffc8649b29 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:19:38 +0100 Subject: [PATCH 066/111] Taxonomy is called wp_theme --- src/wp-includes/block-template-utils.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index d4079a85f638b..fa03acacd30be 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -736,10 +736,10 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @return WP_Block_Template|WP_Error Template or error object. */ function _build_block_template_object_from_wp_post_object( $post, $terms = array(), $meta = array() ) { - if ( empty( $terms['theme'] ) ) { + if ( empty( $terms['wp_theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } - $theme = $terms['theme']; + $theme = $terms['wp_theme']; $default_template_types = get_default_block_template_types(); @@ -809,7 +809,7 @@ function _build_block_template_result_from_post( $post ) { } $terms = array( - 'theme' => $terms[0]->name, + 'wp_theme' => $terms[0]->name, ); $meta = array( @@ -1501,7 +1501,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } $terms = array( - 'theme' => $theme, + 'wp_theme' => $theme, ); if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { From 35b3b5ec8670ac5ef23f37519b15034a38f6e6ab Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:22:49 +0100 Subject: [PATCH 067/111] Base terms on tax_input --- src/wp-includes/block-template-utils.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index fa03acacd30be..e45e2deb73603 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1500,13 +1500,8 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $theme = isset( $post->tax_input['wp_theme'] ) ? $post->tax_input['wp_theme'] : null; } - $terms = array( - 'wp_theme' => $theme, - ); - - if ( isset( $changes->tax_input['wp_template_part_area'] ) ) { - $terms['area'] = $changes->tax_input['wp_template_part_area']; - } + $terms = $changes->tax_input; + $terms['wp_theme'] = $theme; $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); From 13bf5275dc1081f46d9646187cb2dd782c40d506 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:25:09 +0100 Subject: [PATCH 068/111] Rename terms to type_terms --- src/wp-includes/block-template-utils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index e45e2deb73603..98f856d963402 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1489,9 +1489,9 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) } if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); - $terms = get_the_terms( $post, 'wp_theme' ); - $theme = ! is_wp_error( $terms ) && ! empty( $terms ) ? $terms[0]->name : null; + $post = get_post( $changes->ID ); + $type_terms = get_the_terms( $post, 'wp_theme' ); + $theme = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; } else { if ( empty( $changes->post_name ) ) { $changes->post_name = $request['slug']; From f70aa769b9d4cdcaa9a5339035bfd8da56622ca3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:26:38 +0100 Subject: [PATCH 069/111] Rearrange logic a bit --- src/wp-includes/block-template-utils.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 98f856d963402..acf6f459618ce 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1488,21 +1488,19 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } + $terms = $changes->tax_input; + if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); - $type_terms = get_the_terms( $post, 'wp_theme' ); - $theme = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; + $post = get_post( $changes->ID ); + $type_terms = get_the_terms( $post, 'wp_theme' ); + $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; } else { if ( empty( $changes->post_name ) ) { $changes->post_name = $request['slug']; } $post = $changes; - $theme = isset( $post->tax_input['wp_theme'] ) ? $post->tax_input['wp_theme'] : null; } - $terms = $changes->tax_input; - $terms['wp_theme'] = $theme; - $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $terms, $changes->meta_input ); From fac17bbc137d34e0cadb15ce2c73d96a5d5dbe84 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:26:52 +0100 Subject: [PATCH 070/111] Whitespace --- 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 acf6f459618ce..b52fb2be4462d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1488,7 +1488,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - $terms = $changes->tax_input; + $terms = $changes->tax_input; if ( ! empty( $changes->ID ) ) { $post = get_post( $changes->ID ); From 6c969e900e7fb90d36942c74f5aa9967dd53ea64 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 18 Mar 2024 17:28:55 +0100 Subject: [PATCH 071/111] Some more streamlining --- src/wp-includes/block-template-utils.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b52fb2be4462d..b51298e7f2151 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1491,9 +1491,12 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $terms = $changes->tax_input; if ( ! empty( $changes->ID ) ) { - $post = get_post( $changes->ID ); - $type_terms = get_the_terms( $post, 'wp_theme' ); + $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; + + // Apply changes to the existing post object. + $post = get_post( $changes->ID ); + $post = (object) array_merge( (array) $post, (array) $changes ); } else { if ( empty( $changes->post_name ) ) { $changes->post_name = $request['slug']; @@ -1501,9 +1504,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post = $changes; } - $post_with_changes_applied = (object) array_merge( (array) $post, (array) $changes ); - - $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post_with_changes_applied ), $terms, $changes->meta_input ); + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $changes->meta_input ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From a0be4c495d9abf35d6bb9d41b14ab583ee5c5ebe Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 09:06:12 +0100 Subject: [PATCH 072/111] Missed one additional_fields instance --- 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 b51298e7f2151..234bcc0dc7bcf 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -820,7 +820,7 @@ function _build_block_template_result_from_post( $post ) { if ( 'wp_template_part' === $parent_post->post_type ) { $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $additional_fields['area'] = $type_terms[0]->name; + $terms['area'] = $type_terms[0]->name; } } From 86e8f3c004643f6e87af884ff7f2456a72c77766 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 09:07:19 +0100 Subject: [PATCH 073/111] Move meta below terms --- src/wp-includes/block-template-utils.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 234bcc0dc7bcf..b1434e10e527b 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -812,11 +812,6 @@ function _build_block_template_result_from_post( $post ) { 'wp_theme' => $terms[0]->name, ); - $meta = array( - 'origin' => get_post_meta( $parent_post->ID, 'origin', true ), - 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), - ); - if ( 'wp_template_part' === $parent_post->post_type ) { $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { @@ -824,6 +819,11 @@ function _build_block_template_result_from_post( $post ) { } } + $meta = array( + 'origin' => get_post_meta( $parent_post->ID, 'origin', true ), + 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), + ); + $template = _build_block_template_object_from_wp_post_object( $post, $terms, $meta ); // Check for a block template without a description and title or with a title equal to the slug. From fd736a5601eeea09f0ae4b54df68eff6bf6f02f3 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 09:42:31 +0000 Subject: [PATCH 074/111] Add missing properties to --- src/wp-includes/block-template-utils.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b1434e10e527b..4a4a34e113b3c 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1504,6 +1504,14 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post = $changes; } + // Required for the WP_Block_Template. Update the post object with the current time. + $post->post_modified = current_time( 'mysql' ); + + // If the post_author is empty, set it to the current user. + if ( empty( $post->post_author ) ) { + $post->post_author = get_current_user_id(); + } + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $changes->meta_input ); $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); From 3d6b2fe08048eb1501183fd602dad1ed82ae8aeb Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 09:45:34 +0000 Subject: [PATCH 075/111] Return if error --- src/wp-includes/block-template-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 4a4a34e113b3c..e417d820939e1 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -826,6 +826,10 @@ function _build_block_template_result_from_post( $post ) { $template = _build_block_template_object_from_wp_post_object( $post, $terms, $meta ); + if ( is_wp_error( $template ) ) { + return $template; + } + // Check for a block template without a description and title or with a title equal to the slug. if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) { $matches = array(); From d0a00d2e315652098c0085055fc4e1c867b55631 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 10:01:35 +0000 Subject: [PATCH 076/111] Return error to prepare_item_for_database method if template is a wp_error --- src/wp-includes/block-template-utils.php | 4 ++++ .../rest-api/endpoints/class-wp-rest-templates-controller.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index e417d820939e1..705407de2b0d5 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1518,6 +1518,10 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $changes->meta_input ); + if ( is_wp_error( $template ) ) { + return $template; + } + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index cec8a77e04ef9..9eb64707aea07 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -532,7 +532,7 @@ public function delete_item( $request ) { * @since 5.8.0 * * @param WP_REST_Request $request Request object. - * @return stdClass Changes to pass to wp_update_post. + * @return stdClass|WP_Error Changes to pass to wp_update_post. */ protected function prepare_item_for_database( $request ) { $template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null; From c2ebc06e0d826401797432fcbd10059554e73150 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 11:19:32 +0100 Subject: [PATCH 077/111] Provide terms fallback --- 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 705407de2b0d5..3a08f8f2cd4ef 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1492,7 +1492,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } - $terms = $changes->tax_input; + $terms = isset( $changes->tax_input ) ? $changes->tax_input : array(); if ( ! empty( $changes->ID ) ) { $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); From 4f9759d449fb5a9d47f429e7e0c22d8c07f69b7d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 11:20:37 +0100 Subject: [PATCH 078/111] Provide meta fallback --- src/wp-includes/block-template-utils.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 3a08f8f2cd4ef..62a762ebf28ae 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1492,6 +1492,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) return $changes; } + $meta = isset( $changes->meta_input ) ? $changes->meta_input : array(); $terms = isset( $changes->tax_input ) ? $changes->tax_input : array(); if ( ! empty( $changes->ID ) ) { @@ -1516,7 +1517,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post->post_author = get_current_user_id(); } - $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $changes->meta_input ); + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $meta ); if ( is_wp_error( $template ) ) { return $template; From a12bca6e52bd1cb562519046972b0c9b88fafbbc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 11:42:07 +0100 Subject: [PATCH 079/111] Correctly set area --- src/wp-includes/block-template-utils.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 62a762ebf28ae..032976e9ac9a4 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -777,6 +777,10 @@ function _build_block_template_object_from_wp_post_object( $post, $terms = array $template->is_custom = false; } + if ( 'wp_template_part' === $parent_post->post_type && isset( $terms['wp_template_part_area'] ) ) { + $template->area = $terms['wp_template_part_area']; + } + return $template; } @@ -815,7 +819,7 @@ function _build_block_template_result_from_post( $post ) { if ( 'wp_template_part' === $parent_post->post_type ) { $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { - $terms['area'] = $type_terms[0]->name; + $terms['wp_template_part_area'] = $type_terms[0]->name; } } @@ -1517,6 +1521,11 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post->post_author = get_current_user_id(); } + if ( 'wp_template_part' === $post->post_type && ! isset( $terms['wp_template_part_area'] ) ) { + $area_terms = get_the_terms( $changes->ID, 'wp_template_part_area' ); + $terms['wp_template_part_area'] = ! is_wp_error( $area_terms ) && ! empty( $area_terms ) ? $area_terms[0]->name : null; + } + $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $meta ); if ( is_wp_error( $template ) ) { From 605f1c8d65161c9d6b72338b07e5214f62352fa0 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 10:45:39 +0000 Subject: [PATCH 080/111] Update function return value doc --- 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 032976e9ac9a4..0a7559ee55468 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1488,7 +1488,7 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @param stdClass $changes An object representing a template or template part * prepared for inserting or updating the database. * @param WP_REST_Request $request Request object. - * @return stdClass The updated object representing a template or template part. + * @return stdClass|WP_Error The updated object representing a template or template part. */ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) { $hooked_blocks = get_hooked_blocks(); From 19f553b185c2972a68cf610800b696e1b23ba78b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 12:23:38 +0100 Subject: [PATCH 081/111] Inidividual assertions --- tests/phpunit/tests/block-template-utils.php | 68 +++++++------------- 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 990cb837ef51c..daf3cc5b0cda3 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -492,23 +492,10 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); $request->set_param( 'id', $id ); - $changes = new stdClass(); - $changes->ID = self::$template_post->ID; - $changes->post_type = 'wp_template'; - $changes->post_author = 123; - $changes->post_name = 'my-updated-template'; - $changes->post_title = 'My updated Template'; - $changes->post_content = 'Hello'; - $changes->post_excerpt = 'Displays a single post on your website unless a custom template...'; - $changes->post_status = 'publish'; - $changes->post_modified = '2021-07-01 12:00:00'; // FIXME - $changes->meta_input = array( - 'origin' => 'custom', - 'is_wp_suggestion' => true, - ); - $changes->tax_input = array( - 'wp_theme' => self::TEST_THEME, - ); + $changes = new stdClass(); + $changes->post_name = 'my-updated-template'; + $changes->ID = self::$template_post->ID; + $changes->post_content = 'Hello'; inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); @@ -517,35 +504,28 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $context = end( $args )[3]; $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + $this->assertInstanceOf( 'WP_Block_Template', $context ); - $post_to_template_key_map = array( - 'ID' => 'wp_id', - 'post_author' => 'author', - 'post_name' => 'slug', - 'post_content' => 'content', - 'post_title' => 'title', - 'post_excerpt' => 'description', - 'post_type' => 'type', - 'post_status' => 'status', - 'post_modified' => 'modified', + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', + $context->status, + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - - $expected = array(); - foreach ( $post_to_template_key_map as $post_key => $template_key ) { - if ( isset( $changes->$post_key ) ) { - $expected[ $template_key ] = $changes->$post_key; - } - } - $expected['id'] = self::TEST_THEME . '//' . 'my-updated-template'; - $expected['origin'] = 'custom'; - $expected['source'] = 'custom'; - $expected['theme'] = self::TEST_THEME; - $expected['is_custom'] = false; - $expected['has_theme_file'] = false; - $expected['post_types'] = null; - $expected['area'] = null; - - $this->assertEquals( $expected, (array) $context ); } } From 6f5148d7bed825ab614d9ea818776498e53e75be Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 11:46:54 +0000 Subject: [PATCH 082/111] Set ID on in test --- tests/phpunit/tests/block-template-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index daf3cc5b0cda3..6a2b9ed4fae89 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -427,8 +427,10 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $id = self::TEST_THEME . '//' . 'my_template'; $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); $request->set_param( 'id', $id ); + $template = get_block_template( $id, 'wp_template' ); $changes = new stdClass(); + $changes->ID = $template->wp_id; $changes->post_content = 'Hello'; $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); @@ -462,8 +464,10 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $id = self::TEST_THEME . '//' . 'my_template_part'; $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); $request->set_param( 'id', $id ); + $template = get_block_template( $id, 'wp_template_part' ); $changes = new stdClass(); + $changes->ID = $template->wp_id; $changes->post_content = 'Hello'; $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); From 092538662598f9c141b20359a3ab324d8d3c27ae Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 19 Mar 2024 13:13:40 +0000 Subject: [PATCH 083/111] Coding standards --- tests/phpunit/tests/rest-api/wpRestTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 45bc45172b4a0..b2336e5498fec 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -53,7 +53,7 @@ public static function wpSetupBeforeClass( $factory ) { 'post_content' => 'Content', 'post_excerpt' => 'Description of my template part.', 'tax_input' => array( - 'wp_theme' => array( + 'wp_theme' => array( get_stylesheet(), ), 'wp_template_part_area' => array( From 619e3d3e90fdd2f26c8577378413f8c43e903ef1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 14:54:45 +0100 Subject: [PATCH 084/111] Remove now-obsolete filter mocks --- tests/phpunit/tests/block-template-utils.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 6a2b9ed4fae89..431e8a2a37946 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -410,11 +410,6 @@ public function test_wp_generate_block_templates_export_file() { * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { - global $wp_current_filter; - // Mock currently set filter. The $wp_current_filter global is reset during teardown by - // the unit test base class. - $wp_current_filter[] = 'rest_pre_insert_wp_template'; - register_block_type( 'tests/hooked-block', array( @@ -447,11 +442,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { - global $wp_current_filter; - // Mock currently set filter. The $wp_current_filter global is reset during teardown by - // the unit test base class. - $wp_current_filter[] = 'rest_pre_insert_wp_template_part'; - register_block_type( 'tests/hooked-block', array( @@ -484,11 +474,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { - global $wp_current_filter; - // Mock currently set filter. - // The $wp_current_filter global is reset during teardown by the unit test base class. - $wp_current_filter[] = 'rest_pre_insert_wp_template'; - $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); From 4329b14fceb4d8b297ec54c19d52b6196bf238c1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 14:56:34 +0100 Subject: [PATCH 085/111] Remove now-obsolete post name stubbing --- src/wp-includes/block-template-utils.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 0a7559ee55468..fdcd879b1e635 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1487,10 +1487,9 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * * @param stdClass $changes An object representing a template or template part * prepared for inserting or updating the database. - * @param WP_REST_Request $request Request object. * @return stdClass|WP_Error The updated object representing a template or template part. */ -function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) { +function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { $hooked_blocks = get_hooked_blocks(); if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { return $changes; @@ -1507,9 +1506,6 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ) $post = get_post( $changes->ID ); $post = (object) array_merge( (array) $post, (array) $changes ); } else { - if ( empty( $changes->post_name ) ) { - $changes->post_name = $request['slug']; - } $post = $changes; } From c28a0fdd65f9b2aeded96d91c23b0b52568a0d38 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 14:59:26 +0100 Subject: [PATCH 086/111] Change filter args --- src/wp-includes/default-filters.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 884357f41350a..04c644174dbad 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -753,7 +753,7 @@ add_action( 'init', '_wp_register_default_font_collections' ); // Add ignoredHookedBlocks metadata attribute to the template and template part post types. -add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); -add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); +add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' ); +add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ); unset( $filter, $action ); From f06316714ad7eb78dac7df6f5435d8afae5e49e1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:00:18 +0100 Subject: [PATCH 087/111] Rewrite a bit to make more legible --- src/wp-includes/block-template-utils.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index fdcd879b1e635..a83a9e2df5e77 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1498,15 +1498,16 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { $meta = isset( $changes->meta_input ) ? $changes->meta_input : array(); $terms = isset( $changes->tax_input ) ? $changes->tax_input : array(); - if ( ! empty( $changes->ID ) ) { - $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); - $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; - + if ( empty( $changes->ID ) ) { + // There's no post object for this template in the database for this template yet. + $post = $changes; + } else { // Apply changes to the existing post object. $post = get_post( $changes->ID ); $post = (object) array_merge( (array) $post, (array) $changes ); - } else { - $post = $changes; + + $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); + $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; } // Required for the WP_Block_Template. Update the post object with the current time. From 827dc59b72f9874fac90861275d66b0b0271e12d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:09:07 +0100 Subject: [PATCH 088/111] Add assertions to cover fallback --- tests/phpunit/tests/block-template-utils.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 431e8a2a37946..fcc5ce61d645a 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -516,5 +516,16 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi $context->content, 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); + + $this->assertSame( + self::$template_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); } } From 6e3829dfca4f52dfe98b1b71cf8c0394ec0e1ac7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:17:19 +0100 Subject: [PATCH 089/111] Add test coverage for template parts --- tests/phpunit/tests/block-template-utils.php | 70 +++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index fcc5ce61d645a..dc2ab01a32f58 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -473,7 +473,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ * * @covers inject_ignored_hooked_blocks_metadata_attributes */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_filter_correctly() { + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_applies_filter_correctly() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); @@ -528,4 +528,72 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_applies_fi 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' ); } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $id = self::TEST_THEME . '//' . 'my_template_part'; + $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); + $request->set_param( 'id', $id ); + + $changes = new stdClass(); + $changes->post_name = 'my-updated-template-part'; + $changes->ID = self::$template_part_post->ID; + $changes->post_content = 'Hello'; + + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; + + inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', + $context->status, + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->tax_input['wp_template_part_area'], + $context->area, + 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + + $this->assertSame( + self::$template_part_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_part_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + } } From fe003257449facaf50c6558970c05a3469db5d86 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:22:24 +0100 Subject: [PATCH 090/111] Remove now-obsolete request arg --- tests/phpunit/tests/block-template-utils.php | 24 +++++--------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index dc2ab01a32f58..291027a8a36ab 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -419,16 +419,14 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ ) ); - $id = self::TEST_THEME . '//' . 'my_template'; - $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); - $request->set_param( 'id', $id ); + $id = self::TEST_THEME . '//' . 'my_template'; $template = get_block_template( $id, 'wp_template' ); $changes = new stdClass(); $changes->ID = $template->wp_id; $changes->post_content = 'Hello'; - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); $this->assertSame( 'Hello', $post->post_content, @@ -451,16 +449,14 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ ) ); - $id = self::TEST_THEME . '//' . 'my_template_part'; - $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); - $request->set_param( 'id', $id ); + $id = self::TEST_THEME . '//' . 'my_template_part'; $template = get_block_template( $id, 'wp_template_part' ); $changes = new stdClass(); $changes->ID = $template->wp_id; $changes->post_content = 'Hello'; - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); $this->assertSame( 'Hello', $post->post_content, @@ -477,16 +473,12 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - $id = self::TEST_THEME . '//' . 'my_template'; - $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); - $request->set_param( 'id', $id ); - $changes = new stdClass(); $changes->post_name = 'my-updated-template'; $changes->ID = self::$template_post->ID; $changes->post_content = 'Hello'; - inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + inject_ignored_hooked_blocks_metadata_attributes( $changes ); $args = $action->get_args(); $anchor_block_type = end( $args )[2]; @@ -538,10 +530,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - $id = self::TEST_THEME . '//' . 'my_template_part'; - $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); - $request->set_param( 'id', $id ); - $changes = new stdClass(); $changes->post_name = 'my-updated-template-part'; $changes->ID = self::$template_part_post->ID; @@ -549,7 +537,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; - inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + inject_ignored_hooked_blocks_metadata_attributes( $changes ); $args = $action->get_args(); $anchor_block_type = end( $args )[2]; From 0e4dae00ccb29a31669a6ac695218355721e3748 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:24:52 +0100 Subject: [PATCH 091/111] Whitespace --- src/wp-includes/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index a83a9e2df5e77..c50da3ad31ae9 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1485,8 +1485,8 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @since 6.5.0 * @access private * - * @param stdClass $changes An object representing a template or template part - * prepared for inserting or updating the database. + * @param stdClass $changes An object representing a template or template part + * prepared for inserting or updating the database. * @return stdClass|WP_Error The updated object representing a template or template part. */ function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { From eef7df826785671e2c28e663795f83cd852d7163 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:27:12 +0100 Subject: [PATCH 092/111] Remove wp_ infix --- src/wp-includes/block-template-utils.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c50da3ad31ae9..f861c55ac0ca7 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -735,7 +735,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * @param array $meta Additional meta fields to inform the template object. * @return WP_Block_Template|WP_Error Template or error object. */ -function _build_block_template_object_from_wp_post_object( $post, $terms = array(), $meta = array() ) { +function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) { if ( empty( $terms['wp_theme'] ) ) { return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); } @@ -828,7 +828,7 @@ function _build_block_template_result_from_post( $post ) { 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), ); - $template = _build_block_template_object_from_wp_post_object( $post, $terms, $meta ); + $template = _build_block_template_object_from_post_object( $post, $terms, $meta ); if ( is_wp_error( $template ) ) { return $template; @@ -1523,7 +1523,7 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { $terms['wp_template_part_area'] = ! is_wp_error( $area_terms ) && ! empty( $area_terms ) ? $area_terms[0]->name : null; } - $template = _build_block_template_object_from_wp_post_object( new WP_Post( $post ), $terms, $meta ); + $template = _build_block_template_object_from_post_object( new WP_Post( $post ), $terms, $meta ); if ( is_wp_error( $template ) ) { return $template; From 4459f05136fc0ed5833fc30baaea702a27e5348c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 19 Mar 2024 15:38:08 +0100 Subject: [PATCH 093/111] Fix PHPDoc --- 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 f861c55ac0ca7..476861e63f22d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -725,7 +725,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, } /** - * Build a block template object for based on a WP_Post object. + * Build a block template object from a post object. * * @since 6.5.0 * @access private From 383a9032d3a880b5584fc036b8583f93214eb549 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Mar 2024 16:18:06 +0100 Subject: [PATCH 094/111] Add unit test to cover the file-based template case --- tests/phpunit/tests/block-template-utils.php | 66 ++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 291027a8a36ab..29343146c0467 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -584,4 +584,70 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' ); } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'index'; + $changes->post_type = 'wp_template'; + $changes->post_status = 'publish'; + $changes->post_title = 'Index'; + $changes->post_excerpt = 'Description of index template.'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertTrue( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' + ); + } } From 9c07f47547b1b3dd0ee77735a6f558d14a13ff7a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Mar 2024 16:35:35 +0100 Subject: [PATCH 095/111] Move revision handling out of _build_block_template_object_from_post_object --- src/wp-includes/block-template-utils.php | 30 ++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 476861e63f22d..3d0323080407f 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -743,18 +743,12 @@ function _build_block_template_object_from_post_object( $post, $terms = array(), $default_template_types = get_default_block_template_types(); - $post_id = wp_is_post_revision( $post ); - if ( ! $post_id ) { - $post_id = $post; - } - $parent_post = get_post( $post_id ); - $template_file = _get_block_template_file( $post->post_type, $post->post_name ); $has_theme_file = get_stylesheet() === $theme && null !== $template_file; $template = new WP_Block_Template(); $template->wp_id = $post->ID; - $template->id = $theme . '//' . $parent_post->post_name; + $template->id = $theme . '//' . $post->post_name; $template->theme = $theme; $template->content = $post->post_content; $template->slug = $post->post_name; @@ -769,15 +763,15 @@ function _build_block_template_object_from_post_object( $post, $terms = array(), $template->author = $post->post_author; $template->modified = $post->post_modified; - if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { + if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { $template->post_types = $template_file['postTypes']; } - if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) { + if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { $template->is_custom = false; } - if ( 'wp_template_part' === $parent_post->post_type && isset( $terms['wp_template_part_area'] ) ) { + if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) { $template->area = $terms['wp_template_part_area']; } @@ -800,7 +794,9 @@ function _build_block_template_result_from_post( $post ) { if ( ! $post_id ) { $post_id = $post; } - $parent_post = get_post( $post_id ); + $parent_post = get_post( $post_id ); + $post->post_name = $parent_post->post_name; + $post->post_type = $parent_post->post_type; $terms = get_the_terms( $parent_post, 'wp_theme' ); @@ -1502,8 +1498,18 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { // There's no post object for this template in the database for this template yet. $post = $changes; } else { - // Apply changes to the existing post object. + // Find the existing post object. $post = get_post( $changes->ID ); + + // If the post is a revision, use the parent post's post_name and post_type. + $post_id = wp_is_post_revision( $post ); + if ( $post_id ) { + $parent_post = get_post( $post_id ); + $post->post_name = $parent_post->post_name; + $post->post_type = $parent_post->post_type; + } + + // Apply the changes to the existing post object. $post = (object) array_merge( (array) $post, (array) $changes ); $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); From 52ea662742d01279f889c8ea1d7a42e3cb070d0a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Mar 2024 18:04:05 +0100 Subject: [PATCH 096/111] Remove some extraneous assertions --- tests/phpunit/tests/block-template-utils.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 29343146c0467..95786eb5c77f5 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -598,8 +598,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_ $changes->post_name = 'index'; $changes->post_type = 'wp_template'; $changes->post_status = 'publish'; - $changes->post_title = 'Index'; - $changes->post_excerpt = 'Description of index template.'; $changes->post_content = 'Hello'; $changes->tax_input = array( 'wp_theme' => get_stylesheet(), @@ -630,16 +628,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_ $context->status, 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - $this->assertSame( - $changes->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); $this->assertSame( $changes->post_content, $context->content, From 8270b57a691604cec268319cf1c2a01ce7a7acd7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Mar 2024 19:45:25 +0100 Subject: [PATCH 097/111] Add coverage for template part --- tests/phpunit/tests/block-template-utils.php | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 95786eb5c77f5..0f0cd15f0fda0 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -638,4 +638,65 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_ 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' ); } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_part_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'small-header'; + $changes->post_type = 'wp_template_part'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertTrue( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' + ); + $this->assertSame( + $changes->tax_input['wp_template_part_area'], + $context->area, + 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + } } From bc2c5ef06189aa7c89991c776eca10b8aac64024 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 11:01:43 +0100 Subject: [PATCH 098/111] Move tests to separate file --- tests/phpunit/tests/block-template-utils.php | 296 ---------------- ...tIgnoredHookedBlocksMetadataAttributes.php | 319 ++++++++++++++++++ 2 files changed, 319 insertions(+), 296 deletions(-) create mode 100644 tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 0f0cd15f0fda0..c56d3a01bc7cd 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -403,300 +403,4 @@ public function test_wp_generate_block_templates_export_file() { } $this->assertTrue( $has_html_files, 'contains at least one html file' ); } - - /** - * @ticket 60671 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { - register_block_type( - 'tests/hooked-block', - array( - 'block_hooks' => array( - 'tests/anchor-block' => 'after', - ), - ) - ); - - $id = self::TEST_THEME . '//' . 'my_template'; - $template = get_block_template( $id, 'wp_template' ); - - $changes = new stdClass(); - $changes->ID = $template->wp_id; - $changes->post_content = 'Hello'; - - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); - $this->assertSame( - 'Hello', - $post->post_content, - 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' - ); - } - - /** - * @ticket 60671 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { - register_block_type( - 'tests/hooked-block', - array( - 'block_hooks' => array( - 'tests/anchor-block' => 'after', - ), - ) - ); - - $id = self::TEST_THEME . '//' . 'my_template_part'; - $template = get_block_template( $id, 'wp_template_part' ); - - $changes = new stdClass(); - $changes->ID = $template->wp_id; - $changes->post_content = 'Hello'; - - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); - $this->assertSame( - 'Hello', - $post->post_content, - 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' - ); - } - - /** - * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_applies_filter_correctly() { - $action = new MockAction(); - add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - - $changes = new stdClass(); - $changes->post_name = 'my-updated-template'; - $changes->ID = self::$template_post->ID; - $changes->post_content = 'Hello'; - - inject_ignored_hooked_blocks_metadata_attributes( $changes ); - - $args = $action->get_args(); - $anchor_block_type = end( $args )[2]; - $context = end( $args )[3]; - - $this->assertSame( 'tests/anchor-block', $anchor_block_type ); - - $this->assertInstanceOf( 'WP_Block_Template', $context ); - - $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->ID, - $context->wp_id, - 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - 'publish', - $context->status, - 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' - ); - $this->assertSame( - $changes->post_content, - $context->content, - 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - - $this->assertSame( - self::$template_post->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - $this->assertSame( - self::$template_post->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - } - - /** - * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_applies_filter_correctly() { - $action = new MockAction(); - add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - - $changes = new stdClass(); - $changes->post_name = 'my-updated-template-part'; - $changes->ID = self::$template_part_post->ID; - $changes->post_content = 'Hello'; - - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; - - inject_ignored_hooked_blocks_metadata_attributes( $changes ); - - $args = $action->get_args(); - $anchor_block_type = end( $args )[2]; - $context = end( $args )[3]; - - $this->assertSame( 'tests/anchor-block', $anchor_block_type ); - - $this->assertInstanceOf( 'WP_Block_Template', $context ); - - $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->ID, - $context->wp_id, - 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - 'publish', - $context->status, - 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' - ); - $this->assertSame( - $changes->post_content, - $context->content, - 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->tax_input['wp_template_part_area'], - $context->area, - 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - - $this->assertSame( - self::$template_part_post->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - $this->assertSame( - self::$template_part_post->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - } - - /** - * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_applies_filter_correctly() { - $action = new MockAction(); - add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - - $changes = new stdClass(); - $changes->post_name = 'index'; - $changes->post_type = 'wp_template'; - $changes->post_status = 'publish'; - $changes->post_content = 'Hello'; - $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), - ); - - inject_ignored_hooked_blocks_metadata_attributes( $changes ); - - $args = $action->get_args(); - $anchor_block_type = end( $args )[2]; - $context = end( $args )[3]; - - $this->assertSame( 'tests/anchor-block', $anchor_block_type ); - - $this->assertInstanceOf( 'WP_Block_Template', $context ); - - $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_type, - $context->type, - 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_status, - $context->status, - 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_content, - $context->content, - 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertTrue( - $context->has_theme_file, - 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' - ); - } - - /** - * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_part_applies_filter_correctly() { - $action = new MockAction(); - add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); - - $changes = new stdClass(); - $changes->post_name = 'small-header'; - $changes->post_type = 'wp_template_part'; - $changes->post_status = 'publish'; - $changes->post_content = 'Hello'; - $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), - ); - - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; - - inject_ignored_hooked_blocks_metadata_attributes( $changes ); - - $args = $action->get_args(); - $anchor_block_type = end( $args )[2]; - $context = end( $args )[3]; - - $this->assertSame( 'tests/anchor-block', $anchor_block_type ); - - $this->assertInstanceOf( 'WP_Block_Template', $context ); - - $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_type, - $context->type, - 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_status, - $context->status, - 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->post_content, - $context->content, - 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertTrue( - $context->has_theme_file, - 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' - ); - $this->assertSame( - $changes->tax_input['wp_template_part_area'], - $context->area, - 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - } } diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php new file mode 100644 index 0000000000000..55678004ce533 --- /dev/null +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -0,0 +1,319 @@ +is_registered( 'tests/hooked-block' ) ) { + unregister_block_type( 'tests/hooked-block' ); + } + + parent::tear_down(); + } + + /** + * @ticket 60671 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { + register_block_type( + 'tests/hooked-block', + array( + 'block_hooks' => array( + 'tests/anchor-block' => 'after', + ), + ) + ); + + $id = self::TEST_THEME . '//' . 'my_template'; + $template = get_block_template( $id, 'wp_template' ); + + $changes = new stdClass(); + $changes->ID = $template->wp_id; + $changes->post_content = 'Hello'; + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); + $this->assertSame( + 'Hello', + $post->post_content, + 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' + ); + } + + /** + * @ticket 60671 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { + register_block_type( + 'tests/hooked-block', + array( + 'block_hooks' => array( + 'tests/anchor-block' => 'after', + ), + ) + ); + + $id = self::TEST_THEME . '//' . 'my_template_part'; + $template = get_block_template( $id, 'wp_template_part' ); + + $changes = new stdClass(); + $changes->ID = $template->wp_id; + $changes->post_content = 'Hello'; + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); + $this->assertSame( + 'Hello', + $post->post_content, + 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' + ); + } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'my-updated-template'; + $changes->ID = self::$template_post->ID; + $changes->post_content = 'Hello'; + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', + $context->status, + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + + $this->assertSame( + self::$template_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'my-updated-template-part'; + $changes->ID = self::$template_part_post->ID; + $changes->post_content = 'Hello'; + + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', + $context->status, + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->tax_input['wp_template_part_area'], + $context->area, + 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + + $this->assertSame( + self::$template_part_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_part_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'index'; + $changes->post_type = 'wp_template'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertTrue( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' + ); + } + + /** + * @ticket 60754 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_part_applies_filter_correctly() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_name = 'small-header'; + $changes->post_type = 'wp_template_part'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertTrue( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' + ); + $this->assertSame( + $changes->tax_input['wp_template_part_area'], + $context->area, + 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + } +} From 86fbd4595ebae46ca5ea3f1cb4643465df1c3eb4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 11:13:32 +0100 Subject: [PATCH 099/111] Remove now-obsolete covers PHPDoc --- .../injectIgnoredHookedBlocksMetadataAttributes.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index 55678004ce533..b83c26cc4ee5d 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -23,8 +23,6 @@ public function tear_down() { /** * @ticket 60671 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { register_block_type( @@ -53,8 +51,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60671 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { register_block_type( @@ -83,8 +79,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_applies_filter_correctly() { $action = new MockAction(); @@ -140,8 +134,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_applies_filter_correctly() { $action = new MockAction(); @@ -204,8 +196,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_applies_filter_correctly() { $action = new MockAction(); @@ -258,8 +248,6 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_ /** * @ticket 60754 - * - * @covers inject_ignored_hooked_blocks_metadata_attributes */ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_part_applies_filter_correctly() { $action = new MockAction(); From afaed487cdf0d28ec8bb361dee116a3a5f67a8c7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:00:21 +0100 Subject: [PATCH 100/111] Rename some tests --- .../injectIgnoredHookedBlocksMetadataAttributes.php | 8 ++++---- .../phpunit/tests/rest-api/wpRestTemplatesController.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index b83c26cc4ee5d..8e23a6c26f6d1 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -80,7 +80,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_applies_filter_correctly() { + public function test_hooked_block_types_filter_with_existing_template_post() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); @@ -135,7 +135,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part_applies_filter_correctly() { + public function test_hooked_block_types_filter_with_existing_template_part_post() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); @@ -197,7 +197,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_templ /** * @ticket 60754 */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_applies_filter_correctly() { + public function test_hooked_block_types_filter_with_existing_template_file() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); @@ -249,7 +249,7 @@ public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_ /** * @ticket 60754 */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_file_based_template_part_applies_filter_correctly() { + public function test_hooked_block_types_filter_with_existing_template_part_file() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index b2336e5498fec..75787d7ff872e 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -979,7 +979,7 @@ public function test_prepare_item_for_database_injects_hooked_block() { 'content' => 'Hello', ); - $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' . $id ); + $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' ); $request->set_body_params( $body_params ); $prepared = $prepare_item_for_database->invoke( $endpoint, $request ); From 15e9a5921ae78d15b89201ef9db5cd982a26748b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:14:05 +0100 Subject: [PATCH 101/111] Add check for origin field --- ...jectIgnoredHookedBlocksMetadataAttributes.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index 8e23a6c26f6d1..127a353abd82c 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -206,6 +206,9 @@ public function test_hooked_block_types_filter_with_existing_template_file() { $changes->post_type = 'wp_template'; $changes->post_status = 'publish'; $changes->post_content = 'Hello'; + $changes->meta_input = array( + 'origin' => 'theme', + ); $changes->tax_input = array( 'wp_theme' => get_stylesheet(), ); @@ -244,6 +247,11 @@ public function test_hooked_block_types_filter_with_existing_template_file() { $context->has_theme_file, 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' ); + $this->assertSame( + $changes->meta_input['origin'], + $context->origin, + 'The origin field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); } /** @@ -258,6 +266,9 @@ public function test_hooked_block_types_filter_with_existing_template_part_file( $changes->post_type = 'wp_template_part'; $changes->post_status = 'publish'; $changes->post_content = 'Hello'; + $changes->meta_input = array( + 'origin' => 'theme', + ); $changes->tax_input = array( 'wp_theme' => get_stylesheet(), ); @@ -298,6 +309,11 @@ public function test_hooked_block_types_filter_with_existing_template_part_file( $context->has_theme_file, 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to true.' ); + $this->assertSame( + $changes->meta_input['origin'], + $context->origin, + 'The origin field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); $this->assertSame( $changes->tax_input['wp_template_part_area'], $context->area, From ad8946453ad95087670c3e14d1b8621d75fc2bfd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:23:55 +0100 Subject: [PATCH 102/111] Add some more explanatory PHPDoc --- src/wp-includes/block-template-utils.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 3d0323080407f..c5ab0e9753167 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -727,6 +727,10 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, /** * Build a block template object from a post object. * + * This is a helper function that creates a block template object from a given post object. + * It is self-sufficient in that it only uses information passed as arguments; it does not + * query the database for additional information. + * * @since 6.5.0 * @access private * From 1affcf60d1027a03e9f30e0c0673a65a0c9e5441 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:32:44 +0100 Subject: [PATCH 103/111] Add test coverage for newly created template and template part --- ...tIgnoredHookedBlocksMetadataAttributes.php | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index 127a353abd82c..fd1c0b737630a 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -320,4 +320,103 @@ public function test_hooked_block_types_filter_with_existing_template_part_file( 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); } + + /** + * @ticket 60754 + */ + public function test_hooked_block_types_filter_with_newly_created_template() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_type = 'wp_template'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertFalse( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' + ); + } + + /** + * @ticket 60754 + */ + public function test_hooked_block_types_filter_with_newly_created_template_part() { + $action = new MockAction(); + add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); + + $changes = new stdClass(); + $changes->post_type = 'wp_template_part'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); + + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; + + inject_ignored_hooked_blocks_metadata_attributes( $changes ); + + $args = $action->get_args(); + $anchor_block_type = end( $args )[2]; + $context = end( $args )[3]; + + $this->assertSame( 'tests/anchor-block', $anchor_block_type ); + + $this->assertInstanceOf( 'WP_Block_Template', $context ); + + $this->assertSame( + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_status, + $context->status, + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + $changes->post_content, + $context->content, + 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertFalse( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' + ); + $this->assertSame( + $changes->tax_input['wp_template_part_area'], + $context->area, + 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + } } From 5b005913772599248e149650653c7ac2e403c0ee Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:43:24 +0100 Subject: [PATCH 104/111] Change order of tests --- ...tIgnoredHookedBlocksMetadataAttributes.php | 268 +++++++++--------- 1 file changed, 134 insertions(+), 134 deletions(-) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index fd1c0b737630a..e1f56059aa53e 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -21,73 +21,20 @@ public function tear_down() { parent::tear_down(); } - /** - * @ticket 60671 - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { - register_block_type( - 'tests/hooked-block', - array( - 'block_hooks' => array( - 'tests/anchor-block' => 'after', - ), - ) - ); - - $id = self::TEST_THEME . '//' . 'my_template'; - $template = get_block_template( $id, 'wp_template' ); - - $changes = new stdClass(); - $changes->ID = $template->wp_id; - $changes->post_content = 'Hello'; - - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); - $this->assertSame( - 'Hello', - $post->post_content, - 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' - ); - } - - /** - * @ticket 60671 - */ - public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { - register_block_type( - 'tests/hooked-block', - array( - 'block_hooks' => array( - 'tests/anchor-block' => 'after', - ), - ) - ); - - $id = self::TEST_THEME . '//' . 'my_template_part'; - $template = get_block_template( $id, 'wp_template_part' ); - - $changes = new stdClass(); - $changes->ID = $template->wp_id; - $changes->post_content = 'Hello'; - - $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); - $this->assertSame( - 'Hello', - $post->post_content, - 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' - ); - } - /** * @ticket 60754 */ - public function test_hooked_block_types_filter_with_existing_template_post() { + public function test_hooked_block_types_filter_with_newly_created_template() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); $changes = new stdClass(); - $changes->post_name = 'my-updated-template'; - $changes->ID = self::$template_post->ID; + $changes->post_type = 'wp_template'; + $changes->post_status = 'publish'; $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); inject_ignored_hooked_blocks_metadata_attributes( $changes ); @@ -100,51 +47,42 @@ public function test_hooked_block_types_filter_with_existing_template_post() { $this->assertInstanceOf( 'WP_Block_Template', $context ); $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->ID, - $context->wp_id, - 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( - 'publish', + $changes->post_status, $context->status, - 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( $changes->post_content, $context->content, 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - - $this->assertSame( - self::$template_post->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - $this->assertSame( - self::$template_post->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + $this->assertFalse( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' ); } /** * @ticket 60754 */ - public function test_hooked_block_types_filter_with_existing_template_part_post() { + public function test_hooked_block_types_filter_with_newly_created_template_part() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); $changes = new stdClass(); - $changes->post_name = 'my-updated-template-part'; - $changes->ID = self::$template_part_post->ID; + $changes->post_type = 'wp_template_part'; + $changes->post_status = 'publish'; $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => get_stylesheet(), + ); - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; inject_ignored_hooked_blocks_metadata_attributes( $changes ); @@ -157,41 +95,29 @@ public function test_hooked_block_types_filter_with_existing_template_part_post( $this->assertInstanceOf( 'WP_Block_Template', $context ); $this->assertSame( - $changes->post_name, - $context->slug, - 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' - ); - $this->assertSame( - $changes->ID, - $context->wp_id, - 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + $changes->post_type, + $context->type, + 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( - 'publish', + $changes->post_status, $context->status, - 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' + 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( $changes->post_content, $context->content, 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); + $this->assertFalse( + $context->has_theme_file, + 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' + ); $this->assertSame( $changes->tax_input['wp_template_part_area'], $context->area, 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - - $this->assertSame( - self::$template_part_post->post_title, - $context->title, - 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); - $this->assertSame( - self::$template_part_post->post_excerpt, - $context->description, - 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' - ); } /** @@ -324,17 +250,14 @@ public function test_hooked_block_types_filter_with_existing_template_part_file( /** * @ticket 60754 */ - public function test_hooked_block_types_filter_with_newly_created_template() { + public function test_hooked_block_types_filter_with_existing_template_post() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); $changes = new stdClass(); - $changes->post_type = 'wp_template'; - $changes->post_status = 'publish'; + $changes->post_name = 'my-updated-template'; + $changes->ID = self::$template_post->ID; $changes->post_content = 'Hello'; - $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), - ); inject_ignored_hooked_blocks_metadata_attributes( $changes ); @@ -347,42 +270,51 @@ public function test_hooked_block_types_filter_with_newly_created_template() { $this->assertInstanceOf( 'WP_Block_Template', $context ); $this->assertSame( - $changes->post_type, - $context->type, - 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( - $changes->post_status, + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', $context->status, - 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' ); $this->assertSame( $changes->post_content, $context->content, 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - $this->assertFalse( - $context->has_theme_file, - 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' + + $this->assertSame( + self::$template_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' ); } /** * @ticket 60754 */ - public function test_hooked_block_types_filter_with_newly_created_template_part() { + public function test_hooked_block_types_filter_with_existing_template_part_post() { $action = new MockAction(); add_filter( 'hooked_block_types', array( $action, 'filter' ), 10, 4 ); $changes = new stdClass(); - $changes->post_type = 'wp_template_part'; - $changes->post_status = 'publish'; + $changes->post_name = 'my-updated-template-part'; + $changes->ID = self::$template_part_post->ID; $changes->post_content = 'Hello'; - $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), - ); - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; + $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; inject_ignored_hooked_blocks_metadata_attributes( $changes ); @@ -395,28 +327,96 @@ public function test_hooked_block_types_filter_with_newly_created_template_part( $this->assertInstanceOf( 'WP_Block_Template', $context ); $this->assertSame( - $changes->post_type, - $context->type, - 'The type field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + $changes->post_name, + $context->slug, + 'The slug field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); $this->assertSame( - $changes->post_status, + $changes->ID, + $context->wp_id, + 'The wp_id field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + ); + $this->assertSame( + 'publish', $context->status, - 'The status field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' + 'The status field of the context passed to the hooked_block_types filter isn\'t set to publish.' ); $this->assertSame( $changes->post_content, $context->content, 'The content field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); - $this->assertFalse( - $context->has_theme_file, - 'The has_theme_file field of the context passed to the hooked_block_types filter isn\'t set to false.' - ); $this->assertSame( $changes->tax_input['wp_template_part_area'], $context->area, 'The area field of the context passed to the hooked_block_types filter doesn\'t match the template changes.' ); + + $this->assertSame( + self::$template_part_post->post_title, + $context->title, + 'The title field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + $this->assertSame( + self::$template_part_post->post_excerpt, + $context->description, + 'The description field of the context passed to the hooked_block_types filter doesn\'t match the template post object.' + ); + } + + /** + * @ticket 60671 + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { + register_block_type( + 'tests/hooked-block', + array( + 'block_hooks' => array( + 'tests/anchor-block' => 'after', + ), + ) + ); + + $id = self::TEST_THEME . '//' . 'my_template'; + $template = get_block_template( $id, 'wp_template' ); + + $changes = new stdClass(); + $changes->ID = $template->wp_id; + $changes->post_content = 'Hello'; + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); + $this->assertSame( + 'Hello', + $post->post_content, + 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' + ); + } + + /** + * @ticket 60671 + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template_part() { + register_block_type( + 'tests/hooked-block', + array( + 'block_hooks' => array( + 'tests/anchor-block' => 'after', + ), + ) + ); + + $id = self::TEST_THEME . '//' . 'my_template_part'; + $template = get_block_template( $id, 'wp_template_part' ); + + $changes = new stdClass(); + $changes->ID = $template->wp_id; + $changes->post_content = 'Hello'; + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes ); + $this->assertSame( + 'Hello', + $post->post_content, + 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' + ); } } From ef3f0b0eebd73287241430c56fb373d4851f531b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:50:18 +0100 Subject: [PATCH 105/111] Remove unnecessary trailing slash --- tests/phpunit/tests/rest-api/wpRestTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 75787d7ff872e..204a84633ccea 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -979,7 +979,7 @@ public function test_prepare_item_for_database_injects_hooked_block() { 'content' => 'Hello', ); - $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts/' ); + $request = new WP_REST_Request( 'POST', '/wp/v2/template-parts' ); $request->set_body_params( $body_params ); $prepared = $prepare_item_for_database->invoke( $endpoint, $request ); From df3483d36a88776cd0b1122ff78d01539b34f244 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Mar 2024 12:51:45 +0100 Subject: [PATCH 106/111] Bump since to 6.5.1 --- src/wp-includes/block-template-utils.php | 2 +- .../injectIgnoredHookedBlocksMetadataAttributes.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c5ab0e9753167..bbedf15b61c28 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -731,7 +731,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, * It is self-sufficient in that it only uses information passed as arguments; it does not * query the database for additional information. * - * @since 6.5.0 + * @since 6.5.1 * @access private * * @param WP_Post $post Template post. diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index e1f56059aa53e..3d58c8318da2b 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -11,7 +11,7 @@ class Tests_Block_Templates_InjectIgnoredHookedBlocksMetadataAttributes extends /** * Tear down after each test. * - * @since 6.5.0 + * @since 6.5.1 */ public function tear_down() { if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/hooked-block' ) ) { From 02e7d7c2d2631674708dc5aec90f9011ab098d5b Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 2 Apr 2024 14:40:20 +0100 Subject: [PATCH 107/111] Update src/wp-includes/block-template-utils.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- 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 bbedf15b61c28..ae95fde525fae 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -725,7 +725,7 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, } /** - * Build a block template object from a post object. + * Builds a block template object from a post object. * * This is a helper function that creates a block template object from a given post object. * It is self-sufficient in that it only uses information passed as arguments; it does not From 11b38f6f129541c76887f1aa3595f42501c1cd61 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Tue, 2 Apr 2024 14:39:59 +0100 Subject: [PATCH 108/111] PHP Tests update array assignments --- ...injectIgnoredHookedBlocksMetadataAttributes.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php index 3d58c8318da2b..a7acc8df99b8e 100644 --- a/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php +++ b/tests/phpunit/tests/block-templates/injectIgnoredHookedBlocksMetadataAttributes.php @@ -79,11 +79,10 @@ public function test_hooked_block_types_filter_with_newly_created_template_part( $changes->post_status = 'publish'; $changes->post_content = 'Hello'; $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), + 'wp_theme' => get_stylesheet(), + 'wp_template_part_area' => WP_TEMPLATE_PART_AREA_HEADER, ); - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; - inject_ignored_hooked_blocks_metadata_attributes( $changes ); $args = $action->get_args(); @@ -196,11 +195,10 @@ public function test_hooked_block_types_filter_with_existing_template_part_file( 'origin' => 'theme', ); $changes->tax_input = array( - 'wp_theme' => get_stylesheet(), + 'wp_theme' => get_stylesheet(), + 'wp_template_part_area' => WP_TEMPLATE_PART_AREA_HEADER, ); - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_HEADER; - inject_ignored_hooked_blocks_metadata_attributes( $changes ); $args = $action->get_args(); @@ -314,7 +312,9 @@ public function test_hooked_block_types_filter_with_existing_template_part_post( $changes->ID = self::$template_part_post->ID; $changes->post_content = 'Hello'; - $changes->tax_input['wp_template_part_area'] = WP_TEMPLATE_PART_AREA_FOOTER; + $changes->tax_input = array( + 'wp_template_part_area' => WP_TEMPLATE_PART_AREA_FOOTER, + ); inject_ignored_hooked_blocks_metadata_attributes( $changes ); From e8fe56dbf397583316981c203a0fec4065c7ba24 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Apr 2024 12:27:31 +0200 Subject: [PATCH 109/111] Add back second filter argument as deprecated --- src/wp-includes/block-template-utils.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index ae95fde525fae..61ae17cc1c0e4 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -1485,11 +1485,16 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = ' * @since 6.5.0 * @access private * - * @param stdClass $changes An object representing a template or template part - * prepared for inserting or updating the database. + * @param stdClass $changes An object representing a template or template part + * prepared for inserting or updating the database. + * @param WP_REST_Request $deprecated Deprecated. Not used. * @return stdClass|WP_Error The updated object representing a template or template part. */ -function inject_ignored_hooked_blocks_metadata_attributes( $changes ) { +function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) { + if ( null !== $deprecated ) { + _deprecated_argument( __FUNCTION__, '6.5.1' ); + } + $hooked_blocks = get_hooked_blocks(); if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { return $changes; From afd10e7b262c9758ba0355e7aa9175a2ea23a6a3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Apr 2024 12:37:30 +0200 Subject: [PATCH 110/111] Only pass one argument during test --- tests/phpunit/tests/rest-api/wpRestTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 204a84633ccea..3e6e97728ce5d 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -965,7 +965,7 @@ public function test_prepare_item_for_database_injects_hooked_block() { ) ); - add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 ); + add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ); $endpoint = new WP_REST_Templates_Controller( 'wp_template_part' ); From f0d1818140fea695048107ee2096f92b71255e12 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Apr 2024 12:37:56 +0200 Subject: [PATCH 111/111] Remove default priority argument --- tests/phpunit/tests/rest-api/wpRestTemplatesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php index 3e6e97728ce5d..50678a686d561 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplatesController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplatesController.php @@ -77,7 +77,7 @@ public static function wpTearDownAfterClass() { */ public function tear_down() { if ( has_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ) ) { - remove_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10 ); + remove_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ); } if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/block' ) ) { unregister_block_type( 'tests/hooked-block' );