From de9e06a4c021295af3ac11fdd08ea29a57529668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Mon, 25 Sep 2023 10:00:33 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@56676 602fd350-edb4-49c9-b593-d223f7449a82 --- .../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 52f013a9d6..852143d96e 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 @@ 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' ), ),