Skip to content

Commit 2a494ac

Browse files
committed
Editor: Add allowed_blocks field to block registration and REST API
There is a new block.json field called allowedBlocks, added in Gutenberg in WordPress/gutenberg#58262. This adds support for this new field also on the server. Props: gziolo, jsnajdr. Fixes #60403. git-svn-id: https://develop.svn.wordpress.org/trunk@57521 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 50215df commit 2a494ac

5 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/wp-admin/includes/post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,7 @@ function get_block_editor_server_block_settings() {
23032303
'keywords' => 'keywords',
23042304
'example' => 'example',
23052305
'variations' => 'variations',
2306+
'allowed_blocks' => 'allowedBlocks',
23062307
);
23072308

23082309
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {

src/wp-includes/blocks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function get_block_metadata_i18n_schema() {
327327
* @since 6.1.0 Added support for `render` field.
328328
* @since 6.3.0 Added `selectors` field.
329329
* @since 6.4.0 Added support for `blockHooks` field.
330-
* @since 6.5.0 Added support for `viewStyle` field.
330+
* @since 6.5.0 Added support for `allowedBlocks` and `viewStyle` fields.
331331
*
332332
* @param string $file_or_folder Path to the JSON file with metadata definition for
333333
* the block or path to the folder where the `block.json` file is located.
@@ -424,6 +424,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
424424
'styles' => 'styles',
425425
'variations' => 'variations',
426426
'example' => 'example',
427+
'allowedBlocks' => 'allowed_blocks',
427428
);
428429
$textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
429430
$i18n_schema = get_block_metadata_i18n_schema();

src/wp-includes/class-wp-block-type.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ class WP_Block_Type {
6868
*/
6969
public $ancestor = null;
7070

71+
/**
72+
* Limits which block types can be inserted as children of this block type.
73+
*
74+
* @since 6.5.0
75+
* @var string[]|null
76+
*/
77+
public $allowed_blocks = null;
78+
7179
/**
7280
* Block type icon.
7381
*
@@ -303,6 +311,7 @@ class WP_Block_Type {
303311
* available when nested within the specified blocks.
304312
* @type string[]|null $ancestor Setting ancestor makes a block available only inside the specified
305313
* block types at any position of the ancestor's block subtree.
314+
* @type string[]|null $allowed_blocks Limits which block types can be inserted as children of this block type.
306315
* @type string|null $icon Block type icon.
307316
* @type string $description A detailed block type description.
308317
* @type string[] $keywords Additional keywords to produce block type as

src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ public function prepare_item_for_response( $item, $request ) {
280280
'keywords',
281281
'parent',
282282
'ancestor',
283+
'allowed_blocks',
283284
'provides_context',
284285
'uses_context',
285286
'selectors',
@@ -723,6 +724,17 @@ public function get_item_schema() {
723724
'context' => array( 'embed', 'view', 'edit' ),
724725
'readonly' => true,
725726
),
727+
'allowed_blocks' => array(
728+
'description' => __( 'Allowed child block types.' ),
729+
'type' => array( 'array', 'null' ),
730+
'items' => array(
731+
'type' => 'string',
732+
'pattern' => self::NAME_PATTERN,
733+
),
734+
'default' => null,
735+
'context' => array( 'embed', 'view', 'edit' ),
736+
'readonly' => true,
737+
),
726738
'keywords' => $keywords_definition,
727739
'example' => $example_definition,
728740
'block_hooks' => array(

tests/phpunit/tests/rest-api/rest-block-type-controller.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function test_get_item_invalid() {
207207
'category' => true,
208208
'parent' => 'invalid_parent',
209209
'ancestor' => 'invalid_ancestor',
210+
'allowed_blocks' => 'invalid_allowed_blocks',
210211
'icon' => true,
211212
'description' => true,
212213
'keywords' => 'invalid_keywords',
@@ -237,6 +238,7 @@ public function test_get_item_invalid() {
237238
$this->assertNull( $data['category'] );
238239
$this->assertSameSets( array( 'invalid_parent' ), $data['parent'] );
239240
$this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] );
241+
$this->assertSameSets( array( 'invalid_allowed_blocks' ), $data['allowed_blocks'] );
240242
$this->assertNull( $data['icon'] );
241243
$this->assertSame( '1', $data['description'] );
242244
$this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] );
@@ -283,6 +285,7 @@ public function test_get_item_defaults() {
283285
'category' => false,
284286
'parent' => false,
285287
'ancestor' => false,
288+
'allowed_blocks' => false,
286289
'icon' => false,
287290
'description' => false,
288291
'keywords' => false,
@@ -313,6 +316,7 @@ public function test_get_item_defaults() {
313316
$this->assertNull( $data['category'] );
314317
$this->assertSameSets( array(), $data['parent'] );
315318
$this->assertSameSets( array(), $data['ancestor'] );
319+
$this->assertSameSets( array(), $data['allowed_blocks'] );
316320
$this->assertNull( $data['icon'] );
317321
$this->assertSame( '', $data['description'] );
318322
$this->assertSameSets( array(), $data['keywords'] );
@@ -550,20 +554,22 @@ public function test_get_variation() {
550554
* @ticket 47620
551555
* @ticket 57585
552556
* @ticket 59346
557+
* @ticket 60403
553558
*/
554559
public function test_get_item_schema() {
555560
wp_set_current_user( self::$admin_id );
556561
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/block-types' );
557562
$response = rest_get_server()->dispatch( $request );
558563
$data = $response->get_data();
559564
$properties = $data['schema']['properties'];
560-
$this->assertCount( 31, $properties );
565+
$this->assertCount( 32, $properties );
561566
$this->assertArrayHasKey( 'api_version', $properties );
562567
$this->assertArrayHasKey( 'name', $properties );
563568
$this->assertArrayHasKey( 'title', $properties );
564569
$this->assertArrayHasKey( 'category', $properties );
565570
$this->assertArrayHasKey( 'parent', $properties );
566571
$this->assertArrayHasKey( 'ancestor', $properties );
572+
$this->assertArrayHasKey( 'allowed_blocks', $properties );
567573
$this->assertArrayHasKey( 'icon', $properties );
568574
$this->assertArrayHasKey( 'description', $properties );
569575
$this->assertArrayHasKey( 'keywords', $properties );
@@ -696,6 +702,7 @@ protected function check_block_type_object( $block_type, $data, $links ) {
696702
'category',
697703
'parent',
698704
'ancestor',
705+
'allowedBlocks',
699706
'icon',
700707
'description',
701708
'keywords',

0 commit comments

Comments
 (0)