Make WordPress Core


Ignore:
Timestamp:
09/07/2020 02:35:52 AM (6 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/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
Note: See TracChangeset for help on using the changeset viewer.