A WordPress-centric search engine for devs and theme authors



rest_sanitize_value_from_schema ›

Since4.7.0
Deprecatedn/a
rest_sanitize_value_from_schema ( $value, $args )
Parameters: (2)
  • (mixed) $value The value to sanitize.
    Required: Yes
  • (array) $args Schema array to use for sanitization.
    Required: Yes
Returns:
  • (true|WP_Error)
Defined at:
Codex:

Sanitize a value based on a schema.



Source

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 ( '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;
}