rest_sanitize_value_from_schema

Advertisement

Summery Summery

Sanitize a value based on a schema.

Syntax Syntax

rest_sanitize_value_from_schema( mixed $value, array $args )

Parameters Parameters

$value

(Required) The value to sanitize.

$args

(Required) Schema array to use for sanitization.

Return Return

(true|WP_Error)

Source Source

File: wp-includes/rest-api.php

		return array();
	}

	return $maybe_object;
}

/**
 * Gets the best type for a value.
 *
 * @since 5.5.0
 *
 * @param mixed $value The value to check.
 * @param array $types The list of possible types.
 * @return string The best matching type, an empty string if no types match.
 */
function rest_get_best_type_for_value( $value, $types ) {
	static $checks = array(
		'array'   => 'rest_is_array',
		'object'  => 'rest_is_object',
		'integer' => 'rest_is_integer',
		'number'  => 'is_numeric',
		'boolean' => 'rest_is_boolean',
		'string'  => 'is_string',
		'null'    => 'is_null',
	);

	// Both arrays and objects allow empty strings to be converted to their types.
	// But the best answer for this type is a string.
	if ( '' === $value && in_array( 'string', $types, true ) ) {
		return 'string';
	}

	foreach ( $types as $type ) {
		if ( isset( $checks[ $type ] ) && $checks[ $type ]( $value ) ) {
			return $type;
		}
	}

	return '';
}

/**
 * Handles getting the best type for a multi-type schema.
 *
 * This is a wrapper for {@see rest_get_best_type_for_value()} that handles
 * backward compatibility for schemas that use invalid types.
 *
 * @since 5.5.0
 *
 * @param mixed  $value The value to check.
 * @param array  $args  The schema array to use.
 * @param string $param The parameter name, used in error messages.
 * @return string
 */
function rest_handle_multi_type_schema( $value, $args, $param = '' ) {
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
	$invalid_types = array_diff( $args['type'], $allowed_types );

	if ( $invalid_types ) {
		_doing_it_wrong(
			__FUNCTION__,
			/* translators: 1. Parameter. 2. List of allowed types. */
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ),
			'5.5.0'
		);
	}

	$best_type = rest_get_best_type_for_value( $value, $args['type'] );

	if ( ! $best_type ) {
		if ( ! $invalid_types ) {
			return '';
		}

		// Backward compatibility for previous behavior which allowed the value if there was an invalid type used.
		$best_type = reset( $invalid_types );
	}

	return $best_type;
}

/**
 * Checks if an array is made up of unique items.
 *
 * @since 5.5.0
 *
 * @param array $array The array to check.
 * @return bool True if the array contains unique items, false otherwise.
 */
function rest_validate_array_contains_unique_items( $array ) {
	$seen = array();

	foreach ( $array as $item ) {
		$stabilized = rest_stabilize_value( $item );
		$key        = serialize( $stabilized );

		if ( ! isset( $seen[ $key ] ) ) {
			$seen[ $key ] = true;

			continue;
		}

		return false;
	}

Advertisement

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Advertisement

Leave a Reply