diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index c524f9e22a12f..4f1d04d31df41 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -2182,6 +2182,7 @@ function rest_get_allowed_schema_keywords() { * @return true|WP_Error */ function rest_validate_value_from_schema( $value, $args, $param = '' ) { + if ( isset( $args['anyOf'] ) ) { $matching_schema = rest_find_any_matching_schema( $value, $args, $param ); if ( is_wp_error( $matching_schema ) ) { @@ -2243,9 +2244,27 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { $is_valid = rest_validate_boolean_value_from_schema( $value, $param ); break; case 'object': + if ( is_string( $value ) ) { + $trimmed_value = trim( $value ); + if ( str_starts_with( $trimmed_value, '{' ) ) { + $decoded = json_decode( $value, true ); + if ( json_last_error() === JSON_ERROR_NONE ) { + $value = $decoded; + } + } + } $is_valid = rest_validate_object_value_from_schema( $value, $args, $param ); break; case 'array': + if ( is_string( $value ) ) { + $trimmed_value = trim( $value ); + if ( str_starts_with( $trimmed_value, '[' ) ) { + $decoded = json_decode( $value, true ); + if ( json_last_error() === JSON_ERROR_NONE ) { + $value = $decoded; + } + } + } $is_valid = rest_validate_array_value_from_schema( $value, $args, $param ); break; case 'number': @@ -2780,6 +2799,7 @@ function rest_validate_integer_value_from_schema( $value, $args, $param ) { * @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized. */ function rest_sanitize_value_from_schema( $value, $args, $param = '' ) { + if ( isset( $args['anyOf'] ) ) { $matching_schema = rest_find_any_matching_schema( $value, $args, $param ); if ( is_wp_error( $matching_schema ) ) { @@ -2833,6 +2853,16 @@ function rest_sanitize_value_from_schema( $value, $args, $param = '' ) { } if ( 'array' === $args['type'] ) { + if ( is_string( $value ) ) { + $trimmed_value = trim( $value ); + if ( str_starts_with( $trimmed_value, '[' ) ) { + $decoded = json_decode( $value, true ); + if ( json_last_error() === JSON_ERROR_NONE ) { + $value = $decoded; + } + } + } + $value = rest_sanitize_array( $value ); if ( ! empty( $args['items'] ) ) { @@ -2850,6 +2880,16 @@ function rest_sanitize_value_from_schema( $value, $args, $param = '' ) { } if ( 'object' === $args['type'] ) { + if ( is_string( $value ) ) { + $trimmed_value = trim( $value ); + if ( str_starts_with( $trimmed_value, '{' ) ) { + $decoded = json_decode( $value, true ); + if ( json_last_error() === JSON_ERROR_NONE ) { + $value = $decoded; + } + } + } + $value = rest_sanitize_object( $value ); foreach ( $value as $property => $v ) {