WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/21/2019 07:08:34 PM (2 years ago)
Author:
kadamwhite
Message:

REST API: Cast empty meta values to correct scalar types in REST response.

Introducing complex meta value handling in [45807] unintentionally removed value casting for empty scalar meta values.

Props TimothyBlynJacobs, chrisvanpatten, rmccue, kadamwhite.
Fixes #48363.

File:
1 edited

Legend:

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

    r46454 r46563  
    431431
    432432            if ( null === $rest_args['schema']['default'] ) {
    433                 $rest_args['schema']['default'] = $this->get_default_for_type( $type );
     433                $rest_args['schema']['default'] = static::get_empty_value_for_type( $type );
    434434            }
    435435
     
    502502        }
    503503
     504        if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) {
     505            $value = static::get_empty_value_for_type( $schema['type'] );
     506        }
     507
    504508        if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
    505509            return null;
     
    560564
    561565    /**
    562      * Gets the default value for a schema type.
     566     * Gets the empty value for a schema type.
    563567     *
    564568     * @since 5.3.0
     
    567571     * @return mixed
    568572     */
    569     protected function get_default_for_type( $type ) {
     573    protected static function get_empty_value_for_type( $type ) {
    570574        switch ( $type ) {
    571575            case 'string':
Note: See TracChangeset for help on using the changeset viewer.