Make WordPress Core


Ignore:
Timestamp:
09/23/2019 05:24:58 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Pass "null" as the post date property to reset post to initial "floating" date value.

Props TimothyBlynJacobs, adamsilverstein, jnylen0, mnelson4.
Fixes #44975.

File:
1 edited

Legend:

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

    r46233 r46249  
    12101210 */
    12111211function rest_validate_value_from_schema( $value, $args, $param = '' ) {
     1212        if ( is_array( $args['type'] ) ) {
     1213                foreach ( $args['type'] as $type ) {
     1214                        $type_args         = $args;
     1215                        $type_args['type'] = $type;
     1216
     1217                        if ( true === rest_validate_value_from_schema( $value, $type_args, $param ) ) {
     1218                                return true;
     1219                        }
     1220                }
     1221
     1222                /* translators: 1: Parameter, 2: List of types. */
     1223                return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s' ), $param, implode( ',', $args['type'] ) ) );
     1224        }
     1225
    12121226        if ( 'array' === $args['type'] ) {
    12131227                if ( ! is_null( $value ) ) {
    … …  
    12601274                        }
    12611275                }
     1276        }
     1277
     1278        if ( 'null' === $args['type'] ) {
     1279                if ( null !== $value ) {
     1280                        /* translators: 1: Parameter, 2: Type name. */
     1281                        return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ) );
     1282                }
     1283
     1284                return true;
    12621285        }
    12631286
    … …  
    13661389 */
    13671390function rest_sanitize_value_from_schema( $value, $args ) {
     1391        if ( is_array( $args['type'] ) ) {
     1392                // Determine which type the value was validated against, and use that type when performing sanitization
     1393                $validated_type = '';
     1394
     1395                foreach ( $args['type'] as $type ) {
     1396                        $type_args         = $args;
     1397                        $type_args['type'] = $type;
     1398
     1399                        if ( ! is_wp_error( rest_validate_value_from_schema( $value, $type_args ) ) ) {
     1400                                $validated_type = $type;
     1401                                break;
     1402                        }
     1403                }
     1404
     1405                if ( ! $validated_type ) {
     1406                        return null;
     1407                }
     1408
     1409                $args['type'] = $validated_type;
     1410        }
     1411
    13681412        if ( 'array' === $args['type'] ) {
    13691413                if ( empty( $args['items'] ) ) {
    … …  
    14081452        }
    14091453
     1454        if ( 'null' === $args['type'] ) {
     1455                return null;
     1456        }
     1457
    14101458        if ( 'integer' === $args['type'] ) {
    14111459                return (int) $value;
Note: See TracChangeset for help on using the changeset viewer.