Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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'] ) ) {
Expand All @@ -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 ) {
Expand Down
Loading