Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Extract get_endpoint_args_for_item_schema to standalone function #492

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/wp-includes/rest-api.php
Expand Up @@ -2258,3 +2258,87 @@ 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.
*
* @param array $schema
* @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 rest 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;
}
73 changes: 1 addition & 72 deletions src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
Expand Up @@ -625,78 +625,7 @@ function( $response_fields, $field ) use ( $fields ) {
* @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 );
}

/**
Expand Down
31 changes: 25 additions & 6 deletions tests/phpunit/tests/rest-api/rest-controller.php
Expand Up @@ -222,17 +222,37 @@ public function test_validate_schema_format_uuid() {
);
}

public function test_get_endpoint_args_for_item_schema() {
$controller = new WP_REST_Test_Controller();
$args = $controller->get_endpoint_args_for_item_schema(); // making sure its automatically using correct schema from controller it self

$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 = $controller->get_endpoint_args_for_item_schema();
$this->assertSame( 'A pretty string.', $args['somestring']['description'] );
$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'] );
Expand All @@ -241,8 +261,7 @@ public function test_get_endpoint_args_for_item_schema_arg_options() {
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'] );
}
Expand All @@ -253,7 +272,7 @@ public function test_get_endpoint_args_for_item_schema_default_value() {
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'] );
Expand Down