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

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



git-svn-id: https://develop.svn.wordpress.org/trunk@46249 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White
2019-09-23 17:24:58 +00:00
parent e3db9a1361
commit 27a5302e7d
7 changed files with 319 additions and 24 deletions

View File

@@ -1209,6 +1209,20 @@ function rest_get_avatar_sizes() {
* @return true|WP_Error
*/
function rest_validate_value_from_schema( $value, $args, $param = '' ) {
if ( is_array( $args['type'] ) ) {
foreach ( $args['type'] as $type ) {
$type_args = $args;
$type_args['type'] = $type;
if ( true === rest_validate_value_from_schema( $value, $type_args, $param ) ) {
return true;
}
}
/* translators: 1: Parameter, 2: List of types. */
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s' ), $param, implode( ',', $args['type'] ) ) );
}
if ( 'array' === $args['type'] ) {
if ( ! is_null( $value ) ) {
$value = wp_parse_list( $value );
@@ -1261,6 +1275,15 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
}
}
if ( 'null' === $args['type'] ) {
if ( null !== $value ) {
/* translators: 1: Parameter, 2: Type name. */
return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ) );
}
return true;
}
if ( ! empty( $args['enum'] ) ) {
if ( ! in_array( $value, $args['enum'], true ) ) {
/* translators: 1: Parameter, 2: List of valid values. */
@@ -1365,6 +1388,27 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
* @return true|WP_Error
*/
function rest_sanitize_value_from_schema( $value, $args ) {
if ( is_array( $args['type'] ) ) {
// Determine which type the value was validated against, and use that type when performing sanitization
$validated_type = '';
foreach ( $args['type'] as $type ) {
$type_args = $args;
$type_args['type'] = $type;
if ( ! is_wp_error( rest_validate_value_from_schema( $value, $type_args ) ) ) {
$validated_type = $type;
break;
}
}
if ( ! $validated_type ) {
return null;
}
$args['type'] = $validated_type;
}
if ( 'array' === $args['type'] ) {
if ( empty( $args['items'] ) ) {
return (array) $value;
@@ -1407,6 +1451,10 @@ function rest_sanitize_value_from_schema( $value, $args ) {
return $value;
}
if ( 'null' === $args['type'] ) {
return null;
}
if ( 'integer' === $args['type'] ) {
return (int) $value;
}