Make WordPress Core


Ignore:
Timestamp:
10/05/2017 12:18:44 AM (7 years ago)
Author:
kadamwhite
Message:

REST API: Support objects in settings schema.

Enables register_setting to accept an object as its schema value, allowing settings to accept non-scalar values through the REST API.
This whitelists the added type in the settings controller, and passes properties from argument registration into the validation functions.

Props joehoyle.
See #38583.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    r41731 r41758  
    120120     */
    121121    protected function prepare_value( $value, $schema ) {
    122         // If the value is not a scalar, it's not possible to cast it to anything.
    123         if ( ! is_scalar( $value ) ) {
     122        // If the value is not valid by the schema, set the value to null. Null
     123        // values are specifcally non-destructive so this will not cause overwriting
     124        // the current invalid value to null.
     125        if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
    124126            return null;
    125127        }
    126 
    127         switch ( $schema['type'] ) {
    128             case 'string':
    129                 return (string) $value;
    130             case 'integer':
    131                 return (int) $value;
    132             case 'number':
    133                 return (float) $value;
    134             case 'boolean':
    135                 return (bool) $value;
    136             default:
    137                 return null;
    138         }
     128        return rest_sanitize_value_from_schema( $value, $schema );
    139129    }
    140130
     
    149139    public function update_item( $request ) {
    150140        $options = $this->get_registered_options();
     141
    151142        $params  = $request->get_params();
    152143
     
    188179                 * To protect clients from accidentally including the null
    189180                 * values from a response object in a request, we do not allow
    190                  * options with non-scalar values to be updated to null.
     181                 * options with values that don't pass validation to be updated to null.
    191182                 * Without this added protection a client could mistakenly
    192                  * delete all options that have non-scalar values from the
     183                 * delete all options that have invalid values from the
    193184                 * database.
    194185                 */
    195                 if ( ! is_scalar( get_option( $args['option_name'], false ) ) ) {
     186                if ( is_wp_error( rest_validate_value_from_schema( get_option( $args['option_name'], false ), $args['schema'] ) ) ) {
    196187                    return new WP_Error(
    197188                        'rest_invalid_stored_value', sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ), array( 'status' => 500 )
     
    254245             * to be updated with arbitrary values that we can't do decent sanitizing for.
    255246             */
    256             if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean' ), true ) ) {
     247            if ( ! in_array( $rest_args['schema']['type'], array( 'number', 'integer', 'string', 'boolean', 'array', 'object' ), true ) ) {
    257248                continue;
    258249            }
Note: See TracChangeset for help on using the changeset viewer.