Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try making server aware of block variants #1015

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,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 ) {
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
'styles' => 'styles',
'example' => 'example',
'apiVersion' => 'api_version',
'variations' => 'variations',
gwwar marked this conversation as resolved.
Show resolved Hide resolved
);

foreach ( $property_mappings as $key => $mapped_key ) {
Expand Down
6 changes: 6 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class WP_Block_Type {
*/
public $styles = array();

/**
* Block variations.
* @var array
*/
public $variations = array();

/**
* Supported features.
*
Expand Down
12 changes: 10 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ function create_initial_post_types() {
'post',
array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
'item_link' => _x( 'Post Link', 'navigation link block title' ),
'item_link_description' => _x( 'A link to a post.', 'navigation link block description' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
Expand All @@ -46,7 +48,9 @@ function create_initial_post_types() {
'page',
array(
'labels' => array(
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),
'item_link' => _x( 'Page Link', 'navigation link block title' ),
'item_link_description' => _x( 'A link to a page.', 'navigation link block description' ),
),
'public' => true,
'publicly_queryable' => false,
Expand Down Expand Up @@ -1703,6 +1707,8 @@ 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 null/null.
* - `item_link_description` - Description for a navigation link block variation. Default null/null.
*
* Above, the first default value is for non-hierarchical post types (like posts)
* and the second one is for hierarchical post types (like pages).
Expand Down Expand Up @@ -1757,6 +1763,8 @@ 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( null, null ),
'item_link_description' => array( null, null ),
gwwar marked this conversation as resolved.
Show resolved Hide resolved
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public function prepare_item_for_response( $block_type, $request ) {
'script',
'editor_style',
'style',
'variations',
);
foreach ( $extra_fields as $extra_field ) {
if ( rest_is_field_included( $extra_field, $fields ) ) {
Expand Down Expand Up @@ -361,6 +362,63 @@ public function get_item_schema() {
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,
);

$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'block-type',
Expand Down Expand Up @@ -394,13 +452,7 @@ public function get_item_schema() {
'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' ),
Expand Down Expand Up @@ -512,6 +564,62 @@ public function get_item_schema() {
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
'variations' => array(
gwwar marked this conversation as resolved.
Show resolved Hide resolved
'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' => array(
gwwar marked this conversation as resolved.
Show resolved Hide resolved
'description' => __( 'Block type category classification, used in search interfaces to arrange block types by category.' ),
'type' => 'string',
'required' => false,
),
'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 attributes of the variation' ),
gwwar marked this conversation as resolved.
Show resolved Hide resolved
'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' ),
Expand All @@ -529,50 +637,8 @@ public function get_item_schema() {
'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,
),
);

Expand Down
15 changes: 15 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function create_initial_taxonomies() {
'show_ui' => true,
'show_admin_column' => true,
'_builtin' => true,
'labels' => array(
'item_link' => _x( 'Category Link', 'navigation link block title' ),
'item_link_description' => _x( 'A link to a category.', 'navigation link block description' ),
),
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'edit_categories',
Expand All @@ -91,6 +95,10 @@ function create_initial_taxonomies() {
'show_ui' => true,
'show_admin_column' => true,
'_builtin' => true,
'labels' => array(
'item_link' => _x( 'Tag Link', 'navigation link block title' ),
'item_link_description' => _x( 'A link to a tag.', 'navigation link block description' ),
),
'capabilities' => array(
'manage_terms' => 'manage_post_tags',
'edit_terms' => 'edit_post_tags',
Expand Down Expand Up @@ -576,6 +584,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 Title for a navigation link block variation.
* Default null/null.
* @type string $item_link_description Description for a navigation link block variation.
* Default null/null.
* }
*/
function get_taxonomy_labels( $tax ) {
Expand Down Expand Up @@ -613,6 +625,9 @@ 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( null, null ),
'item_link_description' => array( null, null ),

);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ function test_get_block_editor_server_block_settings() {
'category' => 'common',
'styles' => array(),
'keywords' => array(),
'variations' => array(),
),
$blocks[ $name ]
);
Expand Down
Loading