rest_sanitize_value_from_schema( mixed $value, array $args )
Sanitize a value based on a schema.
Parameters
- $value
-
(Required) The value to sanitize.
- $args
-
(Required) Schema array to use for sanitization.
Return
(true|WP_Error)
Source
File: wp-includes/rest-api.php
function rest_sanitize_value_from_schema( $value, $args ) {
if ( 'array' === $args['type'] ) {
if ( empty( $args['items'] ) ) {
return (array) $value;
}
if ( ! is_array( $value ) ) {
$value = preg_split( '/[\s,]+/', $value );
}
foreach ( $value as $index => $v ) {
$value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'] );
}
// Normalize to numeric array so nothing unexpected
// is in the keys.
$value = array_values( $value );
return $value;
}
if ( 'object' === $args['type'] ) {
if ( $value instanceof stdClass ) {
$value = (array) $value;
}
if ( ! is_array( $value ) ) {
return array();
}
foreach ( $value as $property => $v ) {
if ( isset( $args['properties'][ $property ] ) ) {
$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ] );
} elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
unset( $value[ $property ] );
}
}
return $value;
}
if ( 'integer' === $args['type'] ) {
return (int) $value;
}
if ( 'number' === $args['type'] ) {
return (float) $value;
}
if ( 'boolean' === $args['type'] ) {
return rest_sanitize_boolean( $value );
}
if ( isset( $args['format'] ) ) {
switch ( $args['format'] ) {
case 'date-time' :
return sanitize_text_field( $value );
case 'email' :
/*
* sanitize_email() validates, which would be unexpected.
*/
return sanitize_text_field( $value );
case 'uri' :
return esc_url_raw( $value );
case 'ip' :
return sanitize_text_field( $value );
}
}
if ( 'string' === $args['type'] ) {
return strval( $value );
}
return $value;
}
Changelog
Version | Description |
---|---|
WP-4.7.0 | Introduced. |