rest_sanitize_value_from_schema() - WordPress 函数参考中文文档

返回介绍

rest_sanitize_value_from_schema()

发布于 2017-09-11 字数2781 浏览 848 评论 0

rest_sanitize_value_from_schema( mixed $value,  array $args )

Sanitize a value based on a schema.


描述


参数

$value

(mixed)
(Required)
The value to sanitize.

$args

(array)
(Required)
Schema array to use for sanitization.


返回值

(true|WP_Error)


源代码

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

更新日志

Version 描述
4.7.0 Introduced.

相关函数

Uses

  • wp-includes/rest-api.php:
    rest_sanitize_value_from_schema()
  • wp-includes/rest-api.php:
    rest_sanitize_boolean()
  • wp-includes/formatting.php:
    sanitize_text_field()
  • wp-includes/formatting.php:
    esc_url_raw()

Used By

  • wp-includes/widgets/class-wp-widget-media.php:
    WP_Widget_Media::update()
  • wp-includes/rest-api.php:
    rest_sanitize_value_from_schema()
  • wp-includes/rest-api.php:
    rest_sanitize_request_arg()
  • wp-includes/rest-api/fields/class-wp-rest-meta-fields.php:
    WP_REST_Meta_Fields::update_value()

User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!