Make WordPress Core


Ignore:
Timestamp:
08/15/2019 05:16:21 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Support 'object' and 'array' types in register_meta() schemas.

Extends meta registration to support complex schema values, mirroring the functionality in the settings controller.
Error when trying to modify a meta key containing schema-nonconformant data.

Props @TimothyBlynJacobs, @birgire, @mnelson4, @flixos90.
Fixes #43392.

File:
1 edited

Legend:

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

    r45771 r45807  
    11601160                        $value = (array) $value;
    11611161                }
     1162
     1163                if ( $value instanceof JsonSerializable ) {
     1164                        $value = $value->jsonSerialize();
     1165                }
     1166
    11621167                if ( ! is_array( $value ) ) {
    11631168                        /* translators: 1: parameter, 2: type name */
     
    11711176                                        return $is_valid;
    11721177                                }
    1173                         } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
    1174                                 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
     1178                        } elseif ( isset( $args['additionalProperties'] ) ) {
     1179                                if ( false === $args['additionalProperties'] ) {
     1180                                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
     1181                                }
     1182
     1183                                if ( is_array( $args['additionalProperties'] ) ) {
     1184                                        $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
     1185                                        if ( is_wp_error( $is_valid ) ) {
     1186                                                return $is_valid;
     1187                                        }
     1188                                }
    11751189                        }
    11761190                }
     
    12991313                        $value = (array) $value;
    13001314                }
     1315
     1316                if ( $value instanceof JsonSerializable ) {
     1317                        $value = $value->jsonSerialize();
     1318                }
     1319
    13011320                if ( ! is_array( $value ) ) {
    13021321                        return array();
     
    13061325                        if ( isset( $args['properties'][ $property ] ) ) {
    13071326                                $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ] );
    1308                         } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
    1309                                 unset( $value[ $property ] );
     1327                        } elseif ( isset( $args['additionalProperties'] ) ) {
     1328                                if ( false === $args['additionalProperties'] ) {
     1329                                        unset( $value[ $property ] );
     1330                                } elseif ( is_array( $args['additionalProperties'] ) ) {
     1331                                        $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['additionalProperties'] );
     1332                                }
    13101333                        }
    13111334                }
Note: See TracChangeset for help on using the changeset viewer.