-
Notifications
You must be signed in to change notification settings - Fork 651
Return user schema and request args in body of OPTIONs request #1262
Changes from all commits
957cb9f
bd2ece2
83cc548
67df600
9b81060
a7e3746
77f3556
2ced88d
796400b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -574,6 +574,10 @@ function rest_handle_options_request( $response, $handler, $request ) { | |
| $response = new WP_REST_Response(); | ||
|
|
||
| $accept = array(); | ||
| $body = array( | ||
| 'args' => array(), | ||
| 'schema' => null, | ||
| ); | ||
|
|
||
| foreach ( $handler->get_routes() as $route => $endpoints ) { | ||
| $match = preg_match( '@^' . $route . '$@i', $request->get_route(), $args ); | ||
|
|
@@ -582,14 +586,44 @@ function rest_handle_options_request( $response, $handler, $request ) { | |
| continue; | ||
| } | ||
|
|
||
| $opts = $handler->get_route_options( $route ); | ||
| if ( isset( $opts['schema'] ) ) { | ||
| $body['schema'] = $opts['schema']; | ||
| } | ||
|
|
||
| foreach ( $endpoints as $endpoint ) { | ||
| $accept = array_merge( $accept, $endpoint['methods'] ); | ||
|
|
||
| $methods = array_keys( $endpoint['methods'] ); | ||
| $method = array_shift( $methods ); | ||
|
|
||
| $args = array(); | ||
| if ( ! empty( $endpoint['args'] ) ) { | ||
| foreach ( $endpoint['args'] as $key => $arg_opts ) { | ||
| $args[ $key ] = (object) array(); | ||
|
|
||
| if ( isset( $arg_opts['description'] ) ) { | ||
| $args[ $key ]->description = $arg_opts['description']; | ||
| } | ||
|
|
||
| if ( isset( $arg_opts['type'] ) ) { | ||
| $args[ $key ]->type = $arg_opts['type']; | ||
| } | ||
|
|
||
| if ( isset( $arg_opts['default'] ) ) { | ||
| $args[ $key ]->default = $arg_opts['default']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $body['args'][ $method ] = $args; | ||
| } | ||
| break; | ||
| } | ||
| $accept = array_keys( $accept ); | ||
|
|
||
| $response->header( 'Accept', implode( ', ', $accept ) ); | ||
| $response->set_data( (object) $body ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this cast to an object? Pretty much everything else return an array /
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I want to make sure it's always an object, even when empty. Otherwise it will be an array when empty.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. Derp. I guess it was empty at one point, and then I changed my code. |
||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -714,12 +714,17 @@ public function test_delete_user_invalid_reassign_id() { | |
| $this->assertErrorResponse( 'rest_user_invalid_reassign', $response, 400 ); | ||
| } | ||
|
|
||
| public function test_get_item_schema() { | ||
| $request = new WP_REST_Request( 'GET', '/wp/v2/users/schema' ); | ||
| public function test_options_request() { | ||
|
|
||
| $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you did
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joehoyle thoughts on ^^
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird, thought I added a comment. Yes, though not quite the same, I'd expect to get the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I think I have enough to wrap this up now. |
||
| $response = $this->server->dispatch( $request ); | ||
| $data = $response->get_data(); | ||
| $properties = $data['properties']; | ||
|
|
||
| $this->assertArrayHasKey( 'GET', $data['request_args'] ); | ||
| $this->assertArrayHasKey( 'POST', $data['request_args'] ); | ||
| $this->assertArrayNotHasKey( 'DELETE', $data['request_args'] ); | ||
|
|
||
| $properties = $data['schema']['properties']; | ||
| $this->assertEquals( 16, count( $properties ) ); | ||
| $this->assertArrayHasKey( 'avatar_url', $properties ); | ||
| $this->assertArrayHasKey( 'capabilities', $properties ); | ||
|
|
@@ -736,10 +741,14 @@ public function test_get_item_schema() { | |
| $this->assertArrayHasKey( 'slug', $properties ); | ||
| $this->assertArrayHasKey( 'url', $properties ); | ||
| $this->assertArrayHasKey( 'username', $properties ); | ||
|
|
||
| } | ||
|
|
||
| public function test_get_additional_field_registration() { | ||
| public function test_get_item_schema() { | ||
| /** Legacy while other controllers are converted over **/ | ||
| } | ||
|
|
||
| public function test_get_additional_field_registration() { | ||
| $schema = array( | ||
| 'type' => 'integer', | ||
| 'description' => 'Some integer of mine', | ||
|
|
@@ -816,10 +825,10 @@ protected function check_user_data( $user, $data, $context ) { | |
| $this->assertEquals( get_author_posts_url( $user->ID ), $data['link'] ); | ||
|
|
||
| if ( 'view' === $context || 'edit' === $context ) { | ||
| $this->assertEquals( $user->first_name, $data['first_name'] ); | ||
| $this->assertEquals( $user->last_name, $data['last_name'] ); | ||
| $this->assertEquals( $user->nickname, $data['nickname'] ); | ||
| $this->assertEquals( $user->user_nicename, $data['slug'] ); | ||
| $this->assertEquals( $user->first_name, $data['first_name'] ); | ||
| $this->assertEquals( $user->last_name, $data['last_name'] ); | ||
| $this->assertEquals( $user->nickname, $data['nickname'] ); | ||
| $this->assertEquals( $user->user_nicename, $data['slug'] ); | ||
| } | ||
|
|
||
| if ( 'view' !== $context && 'edit' !== $context ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are endpoints registered in this format?
When would a method be
false, or an endpoint registered supporting two methods?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly this was so it's a constant-time lookup internally in
dispatch. Endpoints often support more than one method as well, all EDITABLE supports POST, PUT, and PATCH.