Make WordPress Core


Ignore:
Timestamp:
11/14/2016 04:35:35 PM (8 years ago)
Author:
joehoyle
Message:

REST API: Validate and Sanitize registered meta based off the schema.

With the addition of Array support in our schema validation functions, it's now possible to use these in the meta validation and sanitization steps. Also, this increases the test coverage of using registered via meta the API significantly.

Fixes #38531.
Props rachelbaker, tharsheblows.

File:
1 edited

Legend:

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

    r39179 r39222  
    8585        }
    8686
    87         return (object) $response;
     87        return $response;
    8888    }
    8989
     
    134134            if ( is_null( $request[ $name ] ) ) {
    135135                $result = $this->delete_meta_value( $object_id, $name );
    136             } elseif ( $args['single'] ) {
    137                 $result = $this->update_meta_value( $object_id, $name, $request[ $name ] );
     136                if ( is_wp_error( $result ) ) {
     137                    return $result;
     138                }
     139                continue;
     140            }
     141
     142            $is_valid = rest_validate_value_from_schema( $request[ $name ], $args['schema'], 'meta.' . $name );
     143            if ( is_wp_error( $is_valid ) ) {
     144                $is_valid->add_data( array( 'status' => 400 ) );
     145                return $is_valid;
     146            }
     147
     148            $value = rest_sanitize_value_from_schema( $request[ $name ], $args['schema'] );
     149
     150            if ( $args['single'] ) {
     151                $result = $this->update_meta_value( $object_id, $name, $value );
    138152            } else {
    139                 $result = $this->update_multi_meta_value( $object_id, $name, $request[ $name ] );
     153                $result = $this->update_multi_meta_value( $object_id, $name, $value );
    140154            }
    141155
     
    320334                'name'             => $name,
    321335                'single'           => $args['single'],
     336                'type'             => ! empty( $args['type'] ) ? $args['type'] : null,
    322337                'schema'           => array(),
    323338                'prepare_callback' => array( $this, 'prepare_value' ),
     
    325340
    326341            $default_schema = array(
    327                 'type'        => null,
     342                'type'        => $default_args['type'],
    328343                'description' => empty( $args['description'] ) ? '' : $args['description'],
    329344                'default'     => isset( $args['default'] ) ? $args['default'] : null,
     
    333348            $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );
    334349
    335             if ( empty( $rest_args['schema']['type'] ) ) {
    336                 // Skip over meta fields that don't have a defined type.
    337                 if ( empty( $args['type'] ) ) {
    338                     continue;
    339                 }
    340 
    341                 if ( $rest_args['single'] ) {
    342                     $rest_args['schema']['type'] = $args['type'];
    343                 } else {
    344                     $rest_args['schema']['type'] = 'array';
    345                     $rest_args['schema']['items'] = array(
    346                         'type' => $args['type'],
    347                     );
    348                 }
     350            $type = ! empty( $rest_args['type'] ) ? $rest_args['type'] : null;
     351            $type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type;
     352
     353            if ( ! in_array( $type, array( 'string', 'boolean', 'integer', 'number' ) ) ) {
     354                continue;
     355            }
     356
     357            if ( empty( $rest_args['single'] ) ) {
     358                $rest_args['schema']['items'] = array(
     359                    'type' => $rest_args['type'],
     360                );
     361                $rest_args['schema']['type'] = 'array';
    349362            }
    350363
Note: See TracChangeset for help on using the changeset viewer.