Skip to content

Commit 5d5235f

Browse files
committed
Posts, Post Types: Correct the schema for the id property of the global styles REST API endpoint.
This property is an integer as it corresponds to a post ID. Props narenin, TimothyBlynJacobs, audrasjb, johnbillion, mikinc860, abcd95 Fixes #61911 git-svn-id: https://develop.svn.wordpress.org/trunk@60359 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 93a91d9 commit 5d5235f

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,16 @@ public function register_routes() {
8888
// Lists/updates a single global style variation based on the given id.
8989
register_rest_route(
9090
$this->namespace,
91-
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
91+
'/' . $this->rest_base . '/(?P<id>[\/\d+]+)',
9292
array(
9393
array(
9494
'methods' => WP_REST_Server::READABLE,
9595
'callback' => array( $this, 'get_item' ),
9696
'permission_callback' => array( $this, 'get_item_permissions_check' ),
9797
'args' => array(
9898
'id' => array(
99-
'description' => __( 'The id of a template' ),
100-
'type' => 'string',
101-
'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ),
99+
'description' => __( 'ID of global styles config.' ),
100+
'type' => 'integer',
102101
),
103102
),
104103
),
@@ -115,17 +114,17 @@ public function register_routes() {
115114
}
116115

117116
/**
118-
* Sanitize the global styles ID or stylesheet to decode endpoint.
117+
* Sanitize the global styles stylesheet to decode endpoint.
119118
* For example, `wp/v2/global-styles/twentytwentytwo%200.4.0`
120119
* would be decoded to `twentytwentytwo 0.4.0`.
121120
*
122121
* @since 5.9.0
123122
*
124-
* @param string $id_or_stylesheet Global styles ID or stylesheet.
125-
* @return string Sanitized global styles ID or stylesheet.
123+
* @param string $stylesheet Global styles stylesheet.
124+
* @return string Sanitized global styles stylesheet.
126125
*/
127-
public function _sanitize_global_styles_callback( $id_or_stylesheet ) {
128-
return urldecode( $id_or_stylesheet );
126+
public function _sanitize_global_styles_callback( $stylesheet ) {
127+
return urldecode( $stylesheet );
129128
}
130129

131130
/**
@@ -139,7 +138,7 @@ public function _sanitize_global_styles_callback( $id_or_stylesheet ) {
139138
protected function get_post( $id ) {
140139
$error = new WP_Error(
141140
'rest_global_styles_not_found',
142-
__( 'No global styles config exist with that id.' ),
141+
__( 'No global styles config exists with that ID.' ),
143142
array( 'status' => 404 )
144143
);
145144

@@ -464,7 +463,7 @@ public function get_item_schema() {
464463
'properties' => array(
465464
'id' => array(
466465
'description' => __( 'ID of global styles config.' ),
467-
'type' => 'string',
466+
'type' => 'integer',
468467
'context' => array( 'embed', 'view', 'edit' ),
469468
'readonly' => true,
470469
),

tests/phpunit/tests/rest-api/rest-global-styles-controller.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ public function filter_theme_file_uri( $file ) {
133133
public function test_register_routes() {
134134
$routes = rest_get_server()->get_routes();
135135
$this->assertArrayHasKey(
136-
'/wp/v2/global-styles/(?P<id>[\/\w-]+)',
136+
'/wp/v2/global-styles/(?P<id>[\/\d+]+)',
137137
$routes,
138138
'Single global style based on the given ID route does not exist'
139139
);
140140
$this->assertCount(
141141
2,
142-
$routes['/wp/v2/global-styles/(?P<id>[\/\w-]+)'],
142+
$routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'],
143143
'Single global style based on the given ID route does not have exactly two elements'
144144
);
145145
$this->assertArrayHasKey(
@@ -408,7 +408,7 @@ public function data_get_theme_item_invalid_theme_dirname() {
408408
// Themes deep in subdirectories.
409409
'2 subdirectories deep' => array(
410410
'theme_dirname' => 'subdir/subsubdir/mytheme',
411-
'expected' => 'rest_global_styles_not_found',
411+
'expected' => 'rest_no_route',
412412
),
413413
);
414414
}
@@ -776,4 +776,54 @@ public function test_assign_edit_css_action_admin() {
776776
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
777777
}
778778
}
779+
780+
/**
781+
* Test that the route accepts integer IDs.
782+
*
783+
* @ticket 61911
784+
*/
785+
public function test_global_styles_route_accepts_integer_id() {
786+
wp_set_current_user( self::$admin_id );
787+
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
788+
$response = rest_get_server()->dispatch( $request );
789+
790+
$this->assertEquals( 200, $response->get_status() );
791+
792+
$data = $response->get_data();
793+
$this->assertIsInt( $data['id'] );
794+
$this->assertSame( self::$global_styles_id, $data['id'] );
795+
}
796+
797+
/**
798+
* Test that the schema defines ID as an integer.
799+
*
800+
* @ticket 61911
801+
*/
802+
public function test_global_styles_schema_id_type() {
803+
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
804+
$response = rest_get_server()->dispatch( $request );
805+
806+
$data = $response->get_data();
807+
$schema = $data['schema'];
808+
809+
$this->assertArrayHasKey( 'properties', $schema );
810+
$this->assertArrayHasKey( 'id', $schema['properties'] );
811+
$this->assertArrayHasKey( 'type', $schema['properties']['id'] );
812+
$this->assertSame( 'integer', $schema['properties']['id']['type'] );
813+
}
814+
815+
/**
816+
* Test that the route argument schema defines ID as an integer.
817+
*
818+
* @ticket 61911
819+
*/
820+
public function test_global_styles_route_args_schema() {
821+
$routes = rest_get_server()->get_routes();
822+
$route_data = $routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'];
823+
824+
$this->assertArrayHasKey( 'args', $route_data[0] );
825+
$this->assertArrayHasKey( 'id', $route_data[0]['args'] );
826+
$this->assertArrayHasKey( 'type', $route_data[0]['args']['id'] );
827+
$this->assertSame( 'integer', $route_data[0]['args']['id']['type'] );
828+
}
779829
}

tests/phpunit/tests/rest-api/rest-schema-setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function test_expected_routes_in_schema() {
133133
'/wp/v2/users/(?P<user_id>(?:[\\d]+|me))/application-passwords/(?P<uuid>[\\w\\-]+)',
134134
'/wp/v2/comments',
135135
'/wp/v2/comments/(?P<id>[\\d]+)',
136-
'/wp/v2/global-styles/(?P<id>[\/\w-]+)',
136+
'/wp/v2/global-styles/(?P<id>[\/\d+]+)',
137137
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
138138
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
139139
'/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',

tests/qunit/fixtures/wp-api-generated.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6896,7 +6896,7 @@ mockedApiResponse.Schema = {
68966896
}
68976897
]
68986898
},
6899-
"/wp/v2/global-styles/(?P<id>[\\/\\w-]+)": {
6899+
"/wp/v2/global-styles/(?P<id>[\\/\\d+]+)": {
69006900
"namespace": "wp/v2",
69016901
"methods": [
69026902
"GET",
@@ -6914,8 +6914,8 @@ mockedApiResponse.Schema = {
69146914
},
69156915
"args": {
69166916
"id": {
6917-
"description": "The id of a template",
6918-
"type": "string",
6917+
"description": "ID of global styles config.",
6918+
"type": "integer",
69196919
"required": false
69206920
}
69216921
}

0 commit comments

Comments
 (0)