Make WordPress Core


Ignore:
Timestamp:
03/12/2021 01:33:21 PM (4 years ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r49929 r50527  
    275275            'editor_style',
    276276            'style',
     277            'variations',
    277278        );
    278279        foreach ( $extra_fields as $extra_field ) {
     
    362363        }
    363364
     365        //rest_validate_value_from_schema doesn't understand $refs, pull out reused definitions for readability.
     366        $inner_blocks_definition = array(
     367            'description' => __( 'The list of inner blocks used in the example.' ),
     368            'type'        => 'array',
     369            'items'       => array(
     370                'type'       => 'object',
     371                'properties' => array(
     372                    'name'        => array(
     373                        'description' => __( 'The name of the inner block.' ),
     374                        'type'        => 'string',
     375                    ),
     376                    'attributes'  => array(
     377                        'description' => __( 'The attributes of the inner block.' ),
     378                        'type'        => 'object',
     379                    ),
     380                    'innerBlocks' => array(
     381                        'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ),
     382                        'type'        => 'array',
     383                    ),
     384                ),
     385            ),
     386        );
     387
     388        $example_definition      = array(
     389            'description' => __( 'Block example.' ),
     390            'type'        => array( 'object', 'null' ),
     391            'default'     => null,
     392            'properties'  => array(
     393                'attributes'  => array(
     394                    'description' => __( 'The attributes used in the example.' ),
     395                    'type'        => 'object',
     396                ),
     397                'innerBlocks' => $inner_blocks_definition,
     398            ),
     399            'context'     => array( 'embed', 'view', 'edit' ),
     400            'readonly'    => true,
     401        );
     402
     403        $keywords_definition = array(
     404            'description' => __( 'Block keywords.' ),
     405            'type'        => 'array',
     406            'items'       => array(
     407                'type' => 'string',
     408            ),
     409            'default'     => array(),
     410            'context'     => array( 'embed', 'view', 'edit' ),
     411            'readonly'    => true,
     412        );
     413
     414        $icon_definition = array(
     415            'description' => __( 'Icon of block type.' ),
     416            'type'        => array( 'string', 'null' ),
     417            'default'     => null,
     418            'context'     => array( 'embed', 'view', 'edit' ),
     419            'readonly'    => true,
     420        );
     421
     422        $category_definition = array(
     423            'description' => __( 'Block category.' ),
     424            'type'        => array( 'string', 'null' ),
     425            'default'     => null,
     426            'context'     => array( 'embed', 'view', 'edit' ),
     427            'readonly'    => true,
     428        );
     429
    364430        $schema = array(
    365431            '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    395461                    'readonly'    => true,
    396462                ),
    397                 'icon'             => array(
    398                     'description' => __( 'Icon of block type.' ),
    399                     'type'        => array( 'string', 'null' ),
    400                     'default'     => null,
    401                     'context'     => array( 'embed', 'view', 'edit' ),
    402                     'readonly'    => true,
    403                 ),
     463                'icon'             => $icon_definition,
    404464                'attributes'       => array(
    405465                    'description'          => __( 'Block attributes.' ),
     
    442502                    'readonly'    => true,
    443503                ),
    444                 'category'         => array(
    445                     'description' => __( 'Block category.' ),
    446                     'type'        => array( 'string', 'null' ),
    447                     'default'     => null,
    448                     'context'     => array( 'embed', 'view', 'edit' ),
    449                     'readonly'    => true,
    450                 ),
     504                'category'         => $category_definition,
    451505                'is_dynamic'       => array(
    452506                    'description' => __( 'Is the block dynamically rendered.' ),
     
    513567                    'readonly'    => true,
    514568                ),
     569                'variations'       => array(
     570                    'description' => __( 'Block variations.' ),
     571                    'type'        => 'array',
     572                    'items'       => array(
     573                        'type'       => 'object',
     574                        'properties' => array(
     575                            'name'        => array(
     576                                'description' => __( 'The unique and machine-readable name.' ),
     577                                'type'        => 'string',
     578                                'required'    => true,
     579                            ),
     580                            'title'       => array(
     581                                'description' => __( 'A human-readable variation title.' ),
     582                                'type'        => 'string',
     583                                'required'    => true,
     584                            ),
     585                            'description' => array(
     586                                'description' => __( 'A detailed variation description.' ),
     587                                'type'        => 'string',
     588                                'required'    => false,
     589                            ),
     590                            'category'    => $category_definition,
     591                            'icon'        => $icon_definition,
     592                            'isDefault'   => array(
     593                                'description' => __( 'Indicates whether the current variation is the default one.' ),
     594                                'type'        => 'boolean',
     595                                'required'    => false,
     596                                'default'     => false,
     597                            ),
     598                            'attributes'  => array(
     599                                'description' => __( 'The initial values for attributes.' ),
     600                                'type'        => 'object',
     601                            ),
     602                            'innerBlocks' => $inner_blocks_definition,
     603                            'example'     => $example_definition,
     604                            'scope'       => array(
     605                                'description' => __( 'The list of scopes where the variation is applicable. When not provided, it assumes all available scopes.' ),
     606                                'type'        => array( 'array', 'null' ),
     607                                'default'     => null,
     608                                'items'       => array(
     609                                    'type' => 'string',
     610                                    'enum' => array( 'block', 'inserter', 'transform' ),
     611                                ),
     612                                'readonly'    => true,
     613                            ),
     614                            'keywords'    => $keywords_definition,
     615                        ),
     616                    ),
     617                    'readonly'    => true,
     618                    'context'     => array( 'embed', 'view', 'edit' ),
     619                    'default'     => null,
     620                ),
    515621                'textdomain'       => array(
    516622                    'description' => __( 'Public text domain.' ),
     
    530636                    'readonly'    => true,
    531637                ),
    532                 'keywords'         => array(
    533                     'description' => __( 'Block keywords.' ),
    534                     'type'        => 'array',
    535                     'items'       => array(
    536                         'type' => 'string',
    537                     ),
    538                     'default'     => array(),
    539                     'context'     => array( 'embed', 'view', 'edit' ),
    540                     'readonly'    => true,
    541                 ),
    542                 'example'          => array(
    543                     'description' => __( 'Block example.' ),
    544                     'type'        => array( 'object', 'null' ),
    545                     'default'     => null,
    546                     'properties'  => array(
    547                         'attributes'  => array(
    548                             'description' => __( 'The attributes used in the example.' ),
    549                             'type'        => 'object',
    550                         ),
    551                         'innerBlocks' => array(
    552                             'description' => __( 'The list of inner blocks used in the example.' ),
    553                             'type'        => 'array',
    554                             'items'       => array(
    555                                 'type'       => 'object',
    556                                 'properties' => array(
    557                                     'name'        => array(
    558                                         'description' => __( 'The name of the inner block.' ),
    559                                         'type'        => 'string',
    560                                     ),
    561                                     'attributes'  => array(
    562                                         'description' => __( 'The attributes of the inner block.' ),
    563                                         'type'        => 'object',
    564                                     ),
    565                                     'innerBlocks' => array(
    566                                         'description' => __( "A list of the inner block's own inner blocks. This is a recursive definition following the parent innerBlocks schema." ),
    567                                         'type'        => 'array',
    568                                     ),
    569                                 ),
    570                             ),
    571                         ),
    572                     ),
    573                     'context'     => array( 'embed', 'view', 'edit' ),
    574                     'readonly'    => true,
    575                 ),
     638                'keywords'         => $keywords_definition,
     639                'example'          => $example_definition,
    576640            ),
    577641        );
Note: See TracChangeset for help on using the changeset viewer.