From 815441077c291d0cef959612664bf0aaf3992781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Wed, 6 Apr 2022 09:45:31 +0000 Subject: [PATCH] Editor: Make block type aware of the `ancestor` field The `ancestor` field was recently added to the `block.json` schema in Gutenberg. See: https://github.com/WordPress/gutenberg/pull/39894. Props darerodz. Fixes #55531. git-svn-id: https://develop.svn.wordpress.org/trunk@53084 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/post.php | 1 + src/wp-includes/class-wp-block-type.php | 12 ++++++++++++ .../class-wp-rest-block-types-controller.php | 11 +++++++++++ tests/phpunit/tests/admin/includesPost.php | 2 ++ .../tests/rest-api/rest-block-type-controller.php | 7 ++++++- 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index f79c95428d..c13f2f7592 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', diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b3700e8d2e..47e6619fe2 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -58,6 +58,15 @@ class WP_Block_Type { */ public $parent = null; + /** + * 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; + /** * Block type icon. * @@ -207,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() * @@ -221,6 +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 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 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 c546a3082a..95b12e388a 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 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { 'category', 'keywords', 'parent', + 'ancestor', 'provides_context', 'uses_context', 'supports', @@ -645,6 +646,16 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { '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, ), diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 5f3ec7451d..bb0ed75c83 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -828,6 +828,7 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { 'icon' => 'text', 'category' => 'common', 'render_callback' => 'foo', + 'ancestor' => array( 'core/test-ancestor' ), ); register_block_type( $name, $settings ); @@ -846,6 +847,7 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { 'usesContext' => array(), 'category' => 'common', 'styles' => array(), + 'ancestor' => array( 'core/test-ancestor' ), 'keywords' => array(), 'variations' => array(), ), 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 a774b9be1f..15738397ac 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 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { '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 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $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 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { 'style' => false, 'keywords' => false, 'parent' => false, + 'ancestor' => false, 'supports' => false, 'styles' => false, 'render_callback' => false, @@ -301,6 +304,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $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'] ); @@ -378,7 +382,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $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 +405,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $this->assertArrayHasKey( 'uses_context', $properties ); $this->assertArrayHasKey( 'provides_context', $properties ); $this->assertArrayHasKey( 'variations', $properties ); + $this->assertArrayHasKey( 'ancestor', $properties ); } /**