From cfec48cc31c35475e081483d99ee51f9ed98bf66 Mon Sep 17 00:00:00 2001 From: "K. Adam White" Date: Mon, 21 Oct 2019 19:10:06 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@46563 git-svn-id: http://core.svn.wordpress.org/trunk@46360 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../rest-api/fields/class-wp-rest-meta-fields.php | 10 +++++++--- wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index 411fc5460c..692542612f 100644 --- a/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php +++ b/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php @@ -430,7 +430,7 @@ abstract class WP_REST_Meta_Fields { $type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type; if ( null === $rest_args['schema']['default'] ) { - $rest_args['schema']['default'] = $this->get_default_for_type( $type ); + $rest_args['schema']['default'] = static::get_empty_value_for_type( $type ); } $rest_args['schema'] = $this->default_additional_properties_to_false( $rest_args['schema'] ); @@ -501,6 +501,10 @@ abstract class WP_REST_Meta_Fields { $schema = $args['schema']['items']; } + if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) { + $value = static::get_empty_value_for_type( $schema['type'] ); + } + if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) { return null; } @@ -559,14 +563,14 @@ abstract class WP_REST_Meta_Fields { } /** - * Gets the default value for a schema type. + * Gets the empty value for a schema type. * * @since 5.3.0 * * @param string $type The schema type. * @return mixed */ - protected function get_default_for_type( $type ) { + protected static function get_empty_value_for_type( $type ) { switch ( $type ) { case 'string': return ''; diff --git a/wp-includes/version.php b/wp-includes/version.php index 0ffce94eb8..28ff42bcd6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-RC1-46562'; +$wp_version = '5.3-RC1-46563'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.