Make WordPress Core


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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.