wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_prepare_json_value_for_client › WordPress Function
Since7.2.0
Deprecatedn/a
› wp_prepare_json_value_for_client ( $value, $schema )
| Parameters: (2) |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Prepares a value for JSON encoding according to its schema.
Preserves the distinction between JSON objects and arrays by converting empty PHP arrays to objects where the schema identifies them as objects. Nested properties and items are prepared according to their schemas. Empty arrays remain arrays when a matching schema allows them. Prepares the representation of existing values without inserting defaults or performing full JSON Schema validation.Source
function wp_prepare_json_value_for_client( $value, array $schema ) {
if ( $value instanceof JsonSerializable ) {
$value = $value->jsonSerialize();
}
if ( ! is_array( $value ) && ! $value instanceof stdClass ) {
return $value;
}
foreach ( array( 'anyOf', 'oneOf' ) as $keyword ) {
if ( ! isset( $schema[ $keyword ] ) ) {
continue;
}
$matching_schema = null;
/*
* Prefer an array schema for PHP lists. An empty PHP array also matches an
* object schema, but should remain [] when an array schema accepts it.
*/
if ( is_array( $value ) && array_is_list( $value ) ) {
foreach ( $schema[ $keyword ] as $candidate_schema ) {
if ( ! isset( $candidate_schema['type'] ) && isset( $schema['type'] ) ) {
$candidate_schema['type'] = $schema['type'];
}
if ( _wp_json_schema_matches_type( $value, $candidate_schema, 'array' ) ) {
$matching_schema = $candidate_schema;
break;
}
}
}
// Like rest_filter_response_by_context(), use the first matching oneOf branch.
if ( null === $matching_schema ) {
$matching_schema = 'anyOf' === $keyword
? rest_find_any_matching_schema( $value, $schema, '' )
: rest_find_one_matching_schema( $value, $schema, '', true );
}
if ( is_wp_error( $matching_schema ) ) {
continue;
}
if ( ! isset( $schema['type'] ) && isset( $matching_schema['type'] ) ) {
$schema['type'] = $matching_schema['type'];
}
$value = wp_prepare_json_value_for_client( $value, $matching_schema );
}
$is_array_value = is_array( $value );
$types = isset( $schema['type'] ) ? (array) $schema['type'] : array();
$allows_object = in_array( 'object', $types, true );
$allows_array = in_array( 'array', $types, true );
if ( $is_array_value && empty( $value ) && $allows_object && ! $allows_array ) {
return (object) $value;
}
$is_object_value = $value instanceof stdClass || ( $is_array_value && $allows_object && ( ! $allows_array || ! array_is_list( $value ) ) );
if ( $allows_object && $is_object_value ) {
if ( $value instanceof stdClass ) {
$value = clone $value;
}
foreach ( $value as $property => $property_value ) {
$property_schema = null;
if ( isset( $schema['properties'][ $property ] ) && is_array( $schema['properties'][ $property ] ) ) {
$property_schema = $schema['properties'][ $property ];
} else {
$property_schema = rest_find_matching_pattern_property_schema( $property, $schema );
if ( null === $property_schema && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
$property_schema = $schema['additionalProperties'];
}
}
if ( null === $property_schema ) {
continue;
}
$property_value = wp_prepare_json_value_for_client( $property_value, $property_schema );
if ( is_array( $value ) ) {
$value[ $property ] = $property_value;
} else {
$value->$property = $property_value;
}
}
} elseif ( $allows_array && $is_array_value && isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
foreach ( $value as $index => $item ) {
$value[ $index ] = wp_prepare_json_value_for_client( $item, $schema['items'] );
}
}
return $value;
}