Skip to content

Commit

Permalink
Editor: Add allowed_blocks field to block registration and REST API
Browse files Browse the repository at this point in the history
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.



Built from https://develop.svn.wordpress.org/trunk@57521


git-svn-id: https://core.svn.wordpress.org/trunk@57022 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
gziolo committed Feb 2, 2024
1 parent 1b635a9 commit bbc8043
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,7 @@ function get_block_editor_server_block_settings() {
'keywords' => 'keywords',
'example' => 'example',
'variations' => 'variations',
'allowed_blocks' => 'allowedBlocks',
);

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
Expand Down
3 changes: 2 additions & 1 deletion wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ function get_block_metadata_i18n_schema() {
* @since 6.1.0 Added support for `render` field.
* @since 6.3.0 Added `selectors` field.
* @since 6.4.0 Added support for `blockHooks` field.
* @since 6.5.0 Added support for `viewStyle` field.
* @since 6.5.0 Added support for `allowedBlocks` and `viewStyle` fields.
*
* @param string $file_or_folder Path to the JSON file with metadata definition for
* the block or path to the folder where the `block.json` file is located.
Expand Down Expand Up @@ -424,6 +424,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
'styles' => 'styles',
'variations' => 'variations',
'example' => 'example',
'allowedBlocks' => 'allowed_blocks',
);
$textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null;
$i18n_schema = get_block_metadata_i18n_schema();
Expand Down
9 changes: 9 additions & 0 deletions wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class WP_Block_Type {
*/
public $ancestor = null;

/**
* Limits which block types can be inserted as children of this block type.
*
* @since 6.5.0
* @var string[]|null
*/
public $allowed_blocks = null;

/**
* Block type icon.
*
Expand Down Expand Up @@ -303,6 +311,7 @@ class WP_Block_Type {
* available when nested within the specified blocks.
* @type string[]|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 $allowed_blocks Limits which block types can be inserted as children of this block type.
* @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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public function prepare_item_for_response( $item, $request ) {
'keywords',
'parent',
'ancestor',
'allowed_blocks',
'provides_context',
'uses_context',
'selectors',
Expand Down Expand Up @@ -723,6 +724,17 @@ public function get_item_schema() {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'allowed_blocks' => array(
'description' => __( 'Allowed child block types.' ),
'type' => array( 'array', 'null' ),
'items' => array(
'type' => 'string',
'pattern' => self::NAME_PATTERN,
),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'keywords' => $keywords_definition,
'example' => $example_definition,
'block_hooks' => array(
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57520';
$wp_version = '6.5-alpha-57521';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit bbc8043

Please sign in to comment.