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
This commit is contained in:
Greg Ziółkowski
2022-04-06 09:45:31 +00:00
parent b72c1d00ae
commit 815441077c
5 changed files with 32 additions and 1 deletions

View File

@@ -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',

View File

@@ -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

View File

@@ -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,
),

View File

@@ -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(),
),

View File

@@ -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 );
}
/**