From e00f028a676490178b06f985cabe3d8dd8c5adc9 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 22 Sep 2023 10:38:12 +0200 Subject: [PATCH] REST API: Improve the block type schema for the `name` field --- .../class-wp-rest-block-types-controller.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index cd174e4aafecf..7101ebf304a05 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -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 @@ public function get_item_schema() { '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 @@ public function get_item_schema() { '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 @@ public function get_item_schema() { '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 @@ public function get_item_schema() { '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 @@ public function get_item_schema() { 'description' => __( 'This block is automatically inserted near any occurence 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' ), ),