Make WordPress Core

Changeset 48951


Ignore:
Timestamp:
09/07/2020 02:35:52 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r48881 r48951  
    22592259    return apply_filters( 'rest_queried_resource_route', $route );
    22602260}
     2261
     2262/**
     2263 * Retrieves an array of endpoint arguments from the item schema and endpoint method.
     2264 *
     2265 * @since 5.6.0
     2266 *
     2267 * @param array  $schema The full JSON schema for the endpoint.
     2268 * @param string $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are
     2269 *                       checked for required values and may fall-back to a given default, this is not done
     2270 *                       on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE.
     2271 * @return array The endpoint arguments.
     2272 */
     2273function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) {
     2274
     2275    $schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
     2276    $endpoint_args           = array();
     2277    $valid_schema_properties = array(
     2278        'type',
     2279        'format',
     2280        'enum',
     2281        'items',
     2282        'properties',
     2283        'additionalProperties',
     2284        'minimum',
     2285        'maximum',
     2286        'exclusiveMinimum',
     2287        'exclusiveMaximum',
     2288        'minLength',
     2289        'maxLength',
     2290        'pattern',
     2291        'minItems',
     2292        'maxItems',
     2293        'uniqueItems',
     2294    );
     2295
     2296    foreach ( $schema_properties as $field_id => $params ) {
     2297
     2298        // Arguments specified as `readonly` are not allowed to be set.
     2299        if ( ! empty( $params['readonly'] ) ) {
     2300            continue;
     2301        }
     2302
     2303        $endpoint_args[ $field_id ] = array(
     2304            'validate_callback' => 'rest_validate_request_arg',
     2305            'sanitize_callback' => 'rest_sanitize_request_arg',
     2306        );
     2307
     2308        if ( isset( $params['description'] ) ) {
     2309            $endpoint_args[ $field_id ]['description'] = $params['description'];
     2310        }
     2311
     2312        if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
     2313            $endpoint_args[ $field_id ]['default'] = $params['default'];
     2314        }
     2315
     2316        if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
     2317            $endpoint_args[ $field_id ]['required'] = true;
     2318        }
     2319
     2320        foreach ( $valid_schema_properties as $schema_prop ) {
     2321            if ( isset( $params[ $schema_prop ] ) ) {
     2322                $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
     2323            }
     2324        }
     2325
     2326        // Merge in any options provided by the schema property.
     2327        if ( isset( $params['arg_options'] ) ) {
     2328
     2329            // Only use required / default from arg_options on CREATABLE endpoints.
     2330            if ( WP_REST_Server::CREATABLE !== $method ) {
     2331                $params['arg_options'] = array_diff_key(
     2332                    $params['arg_options'],
     2333                    array(
     2334                        'required' => '',
     2335                        'default'  => '',
     2336                    )
     2337                );
     2338            }
     2339
     2340            $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
     2341        }
     2342    }
     2343
     2344    return $endpoint_args;
     2345}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r48796 r48951  
    626626     */
    627627    public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
    628 
    629         $schema                  = $this->get_item_schema();
    630         $schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
    631         $endpoint_args           = array();
    632         $valid_schema_properties = array(
    633             'type',
    634             'format',
    635             'enum',
    636             'items',
    637             'properties',
    638             'additionalProperties',
    639             'minimum',
    640             'maximum',
    641             'exclusiveMinimum',
    642             'exclusiveMaximum',
    643             'minLength',
    644             'maxLength',
    645             'pattern',
    646             'minItems',
    647             'maxItems',
    648             'uniqueItems',
    649         );
    650 
    651         foreach ( $schema_properties as $field_id => $params ) {
    652 
    653             // Arguments specified as `readonly` are not allowed to be set.
    654             if ( ! empty( $params['readonly'] ) ) {
    655                 continue;
    656             }
    657 
    658             $endpoint_args[ $field_id ] = array(
    659                 'validate_callback' => 'rest_validate_request_arg',
    660                 'sanitize_callback' => 'rest_sanitize_request_arg',
    661             );
    662 
    663             if ( isset( $params['description'] ) ) {
    664                 $endpoint_args[ $field_id ]['description'] = $params['description'];
    665             }
    666 
    667             if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
    668                 $endpoint_args[ $field_id ]['default'] = $params['default'];
    669             }
    670 
    671             if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
    672                 $endpoint_args[ $field_id ]['required'] = true;
    673             }
    674 
    675             foreach ( $valid_schema_properties as $schema_prop ) {
    676                 if ( isset( $params[ $schema_prop ] ) ) {
    677                     $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
    678                 }
    679             }
    680 
    681             // Merge in any options provided by the schema property.
    682             if ( isset( $params['arg_options'] ) ) {
    683 
    684                 // Only use required / default from arg_options on CREATABLE endpoints.
    685                 if ( WP_REST_Server::CREATABLE !== $method ) {
    686                     $params['arg_options'] = array_diff_key(
    687                         $params['arg_options'],
    688                         array(
    689                             'required' => '',
    690                             'default'  => '',
    691                         )
    692                     );
    693                 }
    694 
    695                 $endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
    696             }
    697         }
    698 
    699         return $endpoint_args;
     628        return rest_get_endpoint_args_for_schema( $this->get_item_schema(), $method );
    700629    }
    701630
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r48939 r48951  
    223223    }
    224224
     225    /**
     226     * @ticket 50876
     227     */
     228    public function test_get_endpoint_args_for_item_schema() {
     229        $controller = new WP_REST_Test_Controller();
     230        $args       = $controller->get_endpoint_args_for_item_schema();
     231
     232        $this->assertArrayHasKey( 'somestring', $args );
     233        $this->assertArrayHasKey( 'someinteger', $args );
     234        $this->assertArrayHasKey( 'someboolean', $args );
     235        $this->assertArrayHasKey( 'someurl', $args );
     236        $this->assertArrayHasKey( 'somedate', $args );
     237        $this->assertArrayHasKey( 'someemail', $args );
     238        $this->assertArrayHasKey( 'somehex', $args );
     239        $this->assertArrayHasKey( 'someuuid', $args );
     240        $this->assertArrayHasKey( 'someenum', $args );
     241        $this->assertArrayHasKey( 'someargoptions', $args );
     242        $this->assertArrayHasKey( 'somedefault', $args );
     243        $this->assertArrayHasKey( 'somearray', $args );
     244        $this->assertArrayHasKey( 'someobject', $args );
     245    }
     246
    225247    public function test_get_endpoint_args_for_item_schema_description() {
    226248        $controller = new WP_REST_Test_Controller();
    227         $args       = $controller->get_endpoint_args_for_item_schema();
    228         $this->assertSame( 'A pretty string.', $args['somestring']['description'] );
     249        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
     250
     251        $this->assertEquals( 'A pretty string.', $args['somestring']['description'] );
    229252        $this->assertFalse( isset( $args['someinteger']['description'] ) );
    230253    }
     
    233256
    234257        $controller = new WP_REST_Test_Controller();
    235         $args       = $controller->get_endpoint_args_for_item_schema();
     258        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    236259
    237260        $this->assertFalse( $args['someargoptions']['required'] );
     
    242265
    243266        $controller = new WP_REST_Test_Controller();
    244 
    245         $args = $controller->get_endpoint_args_for_item_schema();
     267        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    246268
    247269        $this->assertSame( 'a', $args['somedefault']['default'] );
     
    254276
    255277        $controller = new WP_REST_Test_Controller();
    256         $args       = $controller->get_endpoint_args_for_item_schema();
     278        $args       = rest_get_endpoint_args_for_schema( $controller->get_item_schema() );
    257279
    258280        foreach ( array( 'minLength', 'maxLength', 'pattern' ) as $property ) {
Note: See TracChangeset for help on using the changeset viewer.