Editor: Make block type aware of variations

Currently block variations are only defined on the client. In some cases, creating block variations on the server can be very useful, especially when needed data is not exposed in the REST APIs.

Related to https://github.com/WordPress/gutenberg/pull/29095.

Props: gwwar, timothyblynjacobs.
Fixes: #52688.


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


git-svn-id: http://core.svn.wordpress.org/trunk@50140 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2021-03-12 13:35:09 +00:00
parent 96377c956f
commit f27c179afc
6 changed files with 155 additions and 60 deletions

View File

@ -2257,6 +2257,7 @@ function get_block_editor_server_block_settings() {
'parent' => 'parent',
'keywords' => 'keywords',
'example' => 'example',
'variations' => 'variations',
);
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {

View File

@ -99,6 +99,13 @@ class WP_Block_Type {
*/
public $styles = array();
/**
* Block variations.
* @since 5.8.0
* @var array
*/
public $variations = array();
/**
* Supported features.
*

View File

@ -1703,6 +1703,9 @@ function _post_type_meta_capabilities( $capabilities = null ) {
* - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
* 'Page scheduled.'
* - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
* - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'.
* - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' /
* 'A link to a page.'
*
* Above, the first default value is for non-hierarchical post types (like posts)
* and the second one is for hierarchical post types (like pages).
@ -1726,7 +1729,7 @@ function _post_type_meta_capabilities( $capabilities = null ) {
* @return object Object with all the labels as member variables.
*/
function get_post_type_labels( $post_type_object ) {
$nohier_vs_hier_defaults = array(
$nohier_vs_hier_defaults = array(
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
@ -1757,6 +1760,14 @@ function get_post_type_labels( $post_type_object ) {
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
'item_link' => array(
_x( 'Post Link', 'navigation link block title' ),
_x( 'Page Link', 'navigation link block title' ),
),
'item_link_description' => array(
_x( 'A link to a post.', 'navigation link block description' ),
_x( 'A link to a page.', 'navigation link block description' ),
),
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

View File

@ -274,6 +274,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'script',
'editor_style',
'style',
'variations',
);
foreach ( $extra_fields as $extra_field ) {
if ( rest_is_field_included( $extra_field, $fields ) ) {
@ -361,6 +362,71 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
return $this->add_additional_fields_schema( $this->schema );
}
//rest_validate_value_from_schema doesn't understand $refs, pull out reused definitions for readability.
$inner_blocks_definition = array(
'description' => __( 'The list of inner blocks used in the example.' ),
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'The name of the inner block.' ),
'type' => 'string',
),
'attributes' => array(
'description' => __( 'The attributes of the inner block.' ),
'type' => 'object',
),
'innerBlocks' => array(
'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ),
'type' => 'array',
),
),
),
);
$example_definition = array(
'description' => __( 'Block example.' ),
'type' => array( 'object', 'null' ),
'default' => null,
'properties' => array(
'attributes' => array(
'description' => __( 'The attributes used in the example.' ),
'type' => 'object',
),
'innerBlocks' => $inner_blocks_definition,
),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
$keywords_definition = array(
'description' => __( 'Block keywords.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
'default' => array(),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
$icon_definition = array(
'description' => __( 'Icon of block type.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
$category_definition = array(
'description' => __( 'Block category.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
);
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-type',
@ -394,13 +460,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'icon' => array(
'description' => __( 'Icon of block type.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'icon' => $icon_definition,
'attributes' => array(
'description' => __( 'Block attributes.' ),
'type' => array( 'object', 'null' ),
@ -441,13 +501,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'category' => array(
'description' => __( 'Block category.' ),
'type' => array( 'string', 'null' ),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'category' => $category_definition,
'is_dynamic' => array(
'description' => __( 'Is the block dynamically rendered.' ),
'type' => 'boolean',
@ -512,6 +566,58 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'variations' => array(
'description' => __( 'Block variations.' ),
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'The unique and machine-readable name.' ),
'type' => 'string',
'required' => true,
),
'title' => array(
'description' => __( 'A human-readable variation title.' ),
'type' => 'string',
'required' => true,
),
'description' => array(
'description' => __( 'A detailed variation description.' ),
'type' => 'string',
'required' => false,
),
'category' => $category_definition,
'icon' => $icon_definition,
'isDefault' => array(
'description' => __( 'Indicates whether the current variation is the default one.' ),
'type' => 'boolean',
'required' => false,
'default' => false,
),
'attributes' => array(
'description' => __( 'The initial values for attributes.' ),
'type' => 'object',
),
'innerBlocks' => $inner_blocks_definition,
'example' => $example_definition,
'scope' => array(
'description' => __( 'The list of scopes where the variation is applicable. When not provided, it assumes all available scopes.' ),
'type' => array( 'array', 'null' ),
'default' => null,
'items' => array(
'type' => 'string',
'enum' => array( 'block', 'inserter', 'transform' ),
),
'readonly' => true,
),
'keywords' => $keywords_definition,
),
),
'readonly' => true,
'context' => array( 'embed', 'view', 'edit' ),
'default' => null,
),
'textdomain' => array(
'description' => __( 'Public text domain.' ),
'type' => array( 'string', 'null' ),
@ -529,50 +635,8 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'keywords' => array(
'description' => __( 'Block keywords.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
'default' => array(),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'example' => array(
'description' => __( 'Block example.' ),
'type' => array( 'object', 'null' ),
'default' => null,
'properties' => array(
'attributes' => array(
'description' => __( 'The attributes used in the example.' ),
'type' => 'object',
),
'innerBlocks' => array(
'description' => __( 'The list of inner blocks used in the example.' ),
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'description' => __( 'The name of the inner block.' ),
'type' => 'string',
),
'attributes' => array(
'description' => __( 'The attributes of the inner block.' ),
'type' => 'object',
),
'innerBlocks' => array(
'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ),
'type' => 'array',
),
),
),
),
),
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'keywords' => $keywords_definition,
'example' => $example_definition,
),
);

View File

@ -576,6 +576,10 @@ function unregister_taxonomy( $taxonomy ) {
* @type string $items_list Label for the table hidden heading.
* @type string $most_used Title for the Most Used tab. Default 'Most Used'.
* @type string $back_to_items Label displayed after a term has been updated.
* @type string $item_link Used in the block editor. Title for a navigation link block variation.
* Default 'Tag Link'/'Category Link'.
* @type string $item_link_description Used in the block editor. Description for a navigation link block
* variation. Default 'A link to a tag.'/'A link to a category'.
* }
*/
function get_taxonomy_labels( $tax ) {
@ -613,6 +617,14 @@ function get_taxonomy_labels( $tax ) {
/* translators: Tab heading when selecting from the most used terms. */
'most_used' => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
'back_to_items' => array( __( '← Go to Tags' ), __( '← Go to Categories' ) ),
'item_link' => array(
_x( 'Tag Link', 'navigation link block title' ),
_x( 'Category Link', 'navigation link block description' ),
),
'item_link_description' => array(
_x( 'A link to a tag.', 'navigation link block description' ),
_x( 'A link to a category.', 'navigation link block description' ),
),
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.8-alpha-50525';
$wp_version = '5.8-alpha-50527';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.