diff options
author | Timothy Jacobs <timothyblynjacobs@git.wordpress.org> | 2020-09-07 02:35:52 +0000 |
---|---|---|
committer | Timothy Jacobs <timothyblynjacobs@git.wordpress.org> | 2020-09-07 02:35:52 +0000 |
commit | 84d524e18964e928ba69d02b248b06fab524a8bf (patch) | |
tree | ece8e011fa3ec8e2c181894049b4e75543bb5b48 | |
parent | 83f23c4c4f392b0ee1739bbb33f15c71fadfe071 (diff) | |
download | wordpress-84d524e18964e928ba69d02b248b06fab524a8bf.tar.gz wordpress-84d524e18964e928ba69d02b248b06fab524a8bf.zip |
REST API: Extract `WP_REST_Controller::get_endpoint_args_for_item_schema()` to a standalone function.
This method is useful whenever a JSON Schema needs to be converted to a format suitable for argument validation with `WP_REST_Request`. Moving the logic into a standalone function allows developers to use it outside of the `WP_REST_Controller` context.
Props pentatonicfunk.
Fixes #50876.
git-svn-id: https://develop.svn.wordpress.org/trunk@48951 602fd350-edb4-49c9-b593-d223f7449a82
-rw-r--r-- | src/wp-includes/rest-api.php | 85 | ||||
-rw-r--r-- | src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php | 73 | ||||
-rw-r--r-- | tests/phpunit/tests/rest-api/rest-controller.php | 34 |
3 files changed, 114 insertions, 78 deletions
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 83b724511a..cbad102bbe 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -2258,3 +2258,88 @@ function rest_get_queried_resource_route() { */ return apply_filters( 'rest_queried_resource_route', $route ); } + +/** + * Retrieves an array of endpoint arguments from the item schema and endpoint method. + * + * @since 5.6.0 + * + * @param array $schema The full JSON schema for the endpoint. + * @param string $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are + * checked for required values and may fall-back to a given default, this is not done + * on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE. + * @return array The endpoint arguments. + */ +function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) { + + $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); + $endpoint_args = array(); + $valid_schema_properties = array( + 'type', + 'format', + 'enum', + 'items', + 'properties', + 'additionalProperties', + 'minimum', + 'maximum', + 'exclusiveMinimum', + 'exclusiveMaximum', + 'minLength', + 'maxLength', + 'pattern', + 'minItems', + 'maxItems', + 'uniqueItems', + ); + + foreach ( $schema_properties as $field_id => $params ) { + + // Arguments specified as `readonly` are not allowed to be set. + if ( ! empty( $params['readonly'] ) ) { + continue; + } + + $endpoint_args[ $field_id ] = array( + 'validate_callback' => 'rest_validate_request_arg', + 'sanitize_callback' => 'rest_sanitize_request_arg', + ); + + if ( isset( $params['description'] ) ) { + $endpoint_args[ $field_id ]['description'] = $params['description']; + } + + if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { + $endpoint_args[ $field_id ]['default'] = $params['default']; + } + + if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) { + $endpoint_args[ $field_id ]['required'] = true; + } + + foreach ( $valid_schema_properties as $schema_prop ) { + if ( isset( $params[ $schema_prop ] ) ) { + $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; + } + } + + // Merge in any options provided by the schema property. + if ( isset( $params['arg_options'] ) ) { + + // Only use required / default from arg_options on CREATABLE endpoints. + if ( WP_REST_Server::CREATABLE !== $method ) { + $params['arg_options'] = array_diff_key( + $params['arg_options'], + array( + 'required' => '', + 'default' => '', + ) + ); + } + + $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); + } + } + + return $endpoint_args; +} diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index 44880aaec2..d0d77d7ec1 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -625,78 +625,7 @@ abstract class WP_REST_Controller { * @return array Endpoint arguments. */ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { - - $schema = $this->get_item_schema(); - $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); - $endpoint_args = array(); - $valid_schema_properties = array( - 'type', - 'format', - 'enum', - 'items', - 'properties', - 'additionalProperties', - 'minimum', - 'maximum', - 'exclusiveMinimum', - 'exclusiveMaximum', - 'minLength', - 'maxLength', - 'pattern', - 'minItems', - 'maxItems', - 'uniqueItems', - ); - - foreach ( $schema_properties as $field_id => $params ) { - - // Arguments specified as `readonly` are not allowed to be set. - if ( ! empty( $params['readonly'] ) ) { - continue; - } - - $endpoint_args[ $field_id ] = array( - 'validate_callback' => 'rest_validate_request_arg', - 'sanitize_callback' => 'rest_sanitize_request_arg', - ); - - if ( isset( $params['description'] ) ) { - $endpoint_args[ $field_id ]['description'] = $params['description']; - } - - if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { - $endpoint_args[ $field_id ]['default'] = $params['default']; - } - - if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) { - $endpoint_args[ $field_id ]['required'] = true; - } - - foreach ( $valid_schema_properties as $schema_prop ) { - if ( isset( $params[ $schema_prop ] ) ) { - $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; - } - } - - // Merge in any options provided by the schema property. - if ( isset( $params['arg_options'] ) ) { - - // Only use required / default from arg_options on CREATABLE endpoints. - if ( WP_REST_Server::CREATABLE !== $method ) { - $params['arg_options'] = array_diff_key( - $params['arg_options'], - array( - 'required' => '', - 'default' => '', - ) - ); - } - - $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); - } - } - - return $endpoint_args; + return rest_get_endpoint_args_for_schema( $this->get_item_schema(), $method ); } /** diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php index a1a1d322ec..c98e8045e4 100644 --- a/tests/phpunit/tests/rest-api/rest-controller.php +++ b/tests/phpunit/tests/rest-api/rest-controller.php @@ -222,17 +222,40 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { ); } - public function test_get_endpoint_args_for_item_schema_description() { + /** + * @ticket 50876 + */ + public function test_get_endpoint_args_for_item_schema() { $controller = new WP_REST_Test_Controller(); $args = $controller->get_endpoint_args_for_item_schema(); - $this->assertSame( 'A pretty string.', $args['somestring']['description'] ); + + $this->assertArrayHasKey( 'somestring', $args ); + $this->assertArrayHasKey( 'someinteger', $args ); + $this->assertArrayHasKey( 'someboolean', $args ); + $this->assertArrayHasKey( 'someurl', $args ); + $this->assertArrayHasKey( 'somedate', $args ); + $this->assertArrayHasKey( 'someemail', $args ); + $this->assertArrayHasKey( 'somehex', $args ); + $this->assertArrayHasKey( 'someuuid', $args ); + $this->assertArrayHasKey( 'someenum', $args ); + $this->assertArrayHasKey( 'someargoptions', $args ); + $this->assertArrayHasKey( 'somedefault', $args ); + $this->assertArrayHasKey( 'somearray', $args ); + $this->assertArrayHasKey( 'someobject', $args ); + } + + public function test_get_endpoint_args_for_item_schema_description() { + $controller = new WP_REST_Test_Controller(); + $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); + + $this->assertEquals( 'A pretty string.', $args['somestring']['description'] ); $this->assertFalse( isset( $args['someinteger']['description'] ) ); } public function test_get_endpoint_args_for_item_schema_arg_options() { $controller = new WP_REST_Test_Controller(); - $args = $controller->get_endpoint_args_for_item_schema(); + $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); $this->assertFalse( $args['someargoptions']['required'] ); $this->assertSame( '__return_true', $args['someargoptions']['sanitize_callback'] ); @@ -241,8 +264,7 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { public function test_get_endpoint_args_for_item_schema_default_value() { $controller = new WP_REST_Test_Controller(); - - $args = $controller->get_endpoint_args_for_item_schema(); + $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); $this->assertSame( 'a', $args['somedefault']['default'] ); } @@ -253,7 +275,7 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { public function test_get_endpoint_args_for_item_schema_arg_properties() { $controller = new WP_REST_Test_Controller(); - $args = $controller->get_endpoint_args_for_item_schema(); + $args = rest_get_endpoint_args_for_schema( $controller->get_item_schema() ); foreach ( array( 'minLength', 'maxLength', 'pattern' ) as $property ) { $this->assertArrayHasKey( $property, $args['somestring'] ); |