REST API: Improve the block type schema for the `name` field

Align the schema between `block.json` defined in Gutenberg and the REST API endpoint for block types. It looks like the `name` field isn't validated in all places and when it uses pattern matching in the REST API code, then it was slightly different.

Props spacedmonkey, ockham.
See #59346.


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


git-svn-id: http://core.svn.wordpress.org/trunk@56188 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2023-09-25 10:03:38 +00:00
parent 97b85f2823
commit 158f4a8fcc
2 changed files with 12 additions and 5 deletions

View File

@ -16,6 +16,8 @@
*/
class WP_REST_Block_Types_Controller extends WP_REST_Controller {
const NAME_PATTERN = '^[a-z][a-z0-9-]*/[a-z][a-z0-9-]*$';
/**
* Instance of WP_Block_Type_Registry.
*
@ -402,6 +404,8 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'name' => array(
'description' => __( 'The name of the inner block.' ),
'type' => 'string',
'pattern' => self::NAME_PATTERN,
'required' => true,
),
'attributes' => array(
'description' => __( 'The attributes of the inner block.' ),
@ -479,7 +483,8 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'name' => array(
'description' => __( 'Unique name identifying the block type.' ),
'type' => 'string',
'default' => '',
'pattern' => self::NAME_PATTERN,
'required' => true,
'context' => array( 'embed', 'view', 'edit' ),
'readonly' => true,
),
@ -689,7 +694,8 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'description' => __( 'Parent blocks.' ),
'type' => array( 'array', 'null' ),
'items' => array(
'type' => 'string',
'type' => 'string',
'pattern' => self::NAME_PATTERN,
),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
@ -699,7 +705,8 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'description' => __( 'Ancestor blocks.' ),
'type' => array( 'array', 'null' ),
'items' => array(
'type' => 'string',
'type' => 'string',
'pattern' => self::NAME_PATTERN,
),
'default' => null,
'context' => array( 'embed', 'view', 'edit' ),
@ -711,7 +718,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
'description' => __( 'This block is automatically inserted near any occurrence of the block types used as keys of this map, into a relative position given by the corresponding value.' ),
'type' => 'object',
'patternProperties' => array(
'^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$' => array(
self::NAME_PATTERN => array(
'type' => 'string',
'enum' => array( 'before', 'after', 'first_child', 'last_child' ),
),

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56675';
$wp_version = '6.4-alpha-56676';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.