From 2430dfb9732bc4ec218914be4ff9cd14cdceff5b Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 5 Apr 2022 13:09:55 +0200 Subject: [PATCH 1/7] Add ancestor prop in WP_Block_Type class --- src/wp-includes/class-wp-block-type.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b3700e8d2e856..1d9861511464b 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -58,6 +58,14 @@ class WP_Block_Type { */ public $parent = null; + /** + * Setting ancestor makes a block available inside the specified + * block types at any position of the ancestor block subtree. + * + * @var array|null + */ + public $ancestor = null; + /** * Block type icon. * @@ -221,6 +229,8 @@ class WP_Block_Type { * search interfaces to arrange block types by category. * @type array|null $parent Setting parent lets a block require that it is only * available when nested within the specified blocks. + * @type array|null $ancestor Setting ancestor makes a block available inside the specified + * block types at any position of the ancestor block subtree. * @type string|null $icon Block type icon. * @type string $description A detailed block type description. * @type string[] $keywords Additional keywords to produce block type as From 066b98774b1743f58846d0241dd39864230878c6 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 5 Apr 2022 13:13:12 +0200 Subject: [PATCH 2/7] Add ancestor prop in REST block type controller --- .../class-wp-rest-block-types-controller.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index c546a3082a3d4..95b12e388a776 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -266,6 +266,7 @@ public function prepare_item_for_response( $item, $request ) { 'category', 'keywords', 'parent', + 'ancestor', 'provides_context', 'uses_context', 'supports', @@ -645,6 +646,16 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'ancestor' => array( + 'description' => __( 'Ancestor blocks.' ), + 'type' => array( 'array', 'null' ), + 'items' => array( + 'type' => 'string', + ), + 'default' => null, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'keywords' => $keywords_definition, 'example' => $example_definition, ), From 796f12099c146b9c7e359920b3b0cc19169d3aa9 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 5 Apr 2022 13:18:08 +0200 Subject: [PATCH 3/7] Return ancestor field from server block settings --- src/wp-admin/includes/post.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index f79c95428d056..c13f2f75921a0 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2232,6 +2232,7 @@ function get_block_editor_server_block_settings() { 'styles' => 'styles', 'textdomain' => 'textdomain', 'parent' => 'parent', + 'ancestor' => 'ancestor', 'keywords' => 'keywords', 'example' => 'example', 'variations' => 'variations', From 27acb0b92169614174c963a0185af2d5f90775e9 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 5 Apr 2022 18:24:37 +0200 Subject: [PATCH 4/7] Test ancestor in server_block_settings --- tests/phpunit/tests/admin/includesPost.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 5f3ec7451d947..bb0ed75c83c93 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -828,6 +828,7 @@ public function test_get_block_editor_server_block_settings() { 'icon' => 'text', 'category' => 'common', 'render_callback' => 'foo', + 'ancestor' => array( 'core/test-ancestor' ), ); register_block_type( $name, $settings ); @@ -846,6 +847,7 @@ public function test_get_block_editor_server_block_settings() { 'usesContext' => array(), 'category' => 'common', 'styles' => array(), + 'ancestor' => array( 'core/test-ancestor' ), 'keywords' => array(), 'variations' => array(), ), From b76eaa70a647ae56a57ed3c6bf618f8bfcd861e0 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 5 Apr 2022 18:38:23 +0200 Subject: [PATCH 5/7] Test ancestor in test_get_item_schema --- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index a774b9be1fc71..80c9775c3bcfa 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -378,7 +378,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 22, $properties ); + $this->assertCount( 23, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); @@ -401,6 +401,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'uses_context', $properties ); $this->assertArrayHasKey( 'provides_context', $properties ); $this->assertArrayHasKey( 'variations', $properties ); + $this->assertArrayHasKey( 'ancestor', $properties ); } /** From 24dd1103a36fe404c68b7a66e166b18274547d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Wed, 6 Apr 2022 10:21:25 +0200 Subject: [PATCH 6/7] Update class-wp-block-type.php --- src/wp-includes/class-wp-block-type.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 1d9861511464b..47e6619fe2b86 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -59,9 +59,10 @@ class WP_Block_Type { public $parent = null; /** - * Setting ancestor makes a block available inside the specified - * block types at any position of the ancestor block subtree. + * Setting ancestor makes a block available only inside the specified + * block types at any position of the ancestor's block subtree. * + * @since 6.0.0 * @var array|null */ public $ancestor = null; @@ -215,6 +216,7 @@ class WP_Block_Type { * @since 5.6.0 Added the `api_version` property. * @since 5.8.0 Added the `variations` property. * @since 5.9.0 Added the `view_script` property. + * @since 6.0.0 Added the `ancestor` property. * * @see register_block_type() * @@ -229,8 +231,8 @@ class WP_Block_Type { * search interfaces to arrange block types by category. * @type array|null $parent Setting parent lets a block require that it is only * available when nested within the specified blocks. - * @type array|null $ancestor Setting ancestor makes a block available inside the specified - * block types at any position of the ancestor block subtree. + * @type array|null $ancestor Setting ancestor makes a block available only inside the specified + * block types at any position of the ancestor's block subtree. * @type string|null $icon Block type icon. * @type string $description A detailed block type description. * @type string[] $keywords Additional keywords to produce block type as From 8e0e6cf0e5320398a1ca10cb9cc2224aa872a1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Wed, 6 Apr 2022 11:24:50 +0200 Subject: [PATCH 7/7] Update rest-block-type-controller.php --- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 80c9775c3bcfa..15738397ac263 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -221,6 +221,7 @@ public function test_get_item_invalid() { 'keywords' => 'invalid_keywords', 'example' => 'invalid_example', 'parent' => 'invalid_parent', + 'ancestor' => 'invalid_ancestor', 'supports' => 'invalid_supports', 'styles' => 'invalid_styles', 'render_callback' => 'invalid_callback', @@ -246,6 +247,7 @@ public function test_get_item_invalid() { $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] ); $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); + $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); @@ -275,6 +277,7 @@ public function test_get_item_defaults() { 'style' => false, 'keywords' => false, 'parent' => false, + 'ancestor' => false, 'supports' => false, 'styles' => false, 'render_callback' => false, @@ -301,6 +304,7 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['uses_context'] ); $this->assertSameSets( array(), $data['keywords'] ); $this->assertSameSets( array(), $data['parent'] ); + $this->assertSameSets( array(), $data['ancestor'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] );