diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 988dd9c778..8642460dd8 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1097,6 +1097,9 @@ function rest_sanitize_value_from_schema( $value, $args ) { 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'] ); } diff --git a/tests/phpunit/tests/rest-api/rest-schema-sanitization.php b/tests/phpunit/tests/rest-api/rest-schema-sanitization.php new file mode 100644 index 0000000000..875a2aafc5 --- /dev/null +++ b/tests/phpunit/tests/rest-api/rest-schema-sanitization.php @@ -0,0 +1,89 @@ + 'number', + ); + $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) ); + $this->assertEquals( 1.10, rest_sanitize_value_from_schema( '1.10', $schema ) ); + $this->assertEquals( 1, rest_sanitize_value_from_schema( '1abc', $schema ) ); + $this->assertEquals( 0, rest_sanitize_value_from_schema( 'abc', $schema ) ); + $this->assertEquals( 0, rest_sanitize_value_from_schema( array(), $schema ) ); + } + + public function test_type_integer() { + $schema = array( + 'type' => 'integer', + ); + $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) ); + $this->assertEquals( 1, rest_sanitize_value_from_schema( '1.10', $schema ) ); + $this->assertEquals( 1, rest_sanitize_value_from_schema( '1abc', $schema ) ); + $this->assertEquals( 0, rest_sanitize_value_from_schema( 'abc', $schema ) ); + $this->assertEquals( 0, rest_sanitize_value_from_schema( array(), $schema ) ); + } + + public function test_type_string() { + $schema = array( + 'type' => 'string', + ); + $this->assertEquals( 'Hello', rest_sanitize_value_from_schema( 'Hello', $schema ) ); + $this->assertEquals( '1.10', rest_sanitize_value_from_schema( 1.10, $schema ) ); + $this->assertEquals( '1', rest_sanitize_value_from_schema( 1, $schema ) ); + } + + public function test_type_boolean() { + $schema = array( + 'type' => 'boolean', + ); + $this->assertEquals( true, rest_sanitize_value_from_schema( '1', $schema ) ); + $this->assertEquals( true, rest_sanitize_value_from_schema( 'true', $schema ) ); + $this->assertEquals( true, rest_sanitize_value_from_schema( '100', $schema ) ); + $this->assertEquals( true, rest_sanitize_value_from_schema( 1, $schema ) ); + $this->assertEquals( false, rest_sanitize_value_from_schema( '0', $schema ) ); + $this->assertEquals( false, rest_sanitize_value_from_schema( 'false', $schema ) ); + $this->assertEquals( false, rest_sanitize_value_from_schema( 0, $schema ) ); + } + + public function test_format_email() { + $schema = array( + 'type' => 'string', + 'format' => 'email', + ); + $this->assertEquals( 'email@example.com', rest_sanitize_value_from_schema( 'email@example.com', $schema ) ); + $this->assertEquals( 'a@b.c', rest_sanitize_value_from_schema( 'a@b.c', $schema ) ); + $this->assertEquals( 'invalid', rest_sanitize_value_from_schema( 'invalid', $schema ) ); + } + + public function test_type_array() { + $schema = array( + 'type' => 'array', + 'items' => array( + 'type' => 'number', + ), + ); + $this->assertEquals( array( 1 ), rest_sanitize_value_from_schema( array( 1 ), $schema ) ); + $this->assertEquals( array( 1 ), rest_sanitize_value_from_schema( array( '1' ), $schema ) ); + } + + public function test_type_array_as_csv() { + $schema = array( + 'type' => 'array', + 'items' => array( + 'type' => 'number', + ), + ); + $this->assertEquals( array( 1, 2 ), rest_sanitize_value_from_schema( '1,2', $schema ) ); + $this->assertEquals( array( 1, 2, 0 ), rest_sanitize_value_from_schema( '1,2,a', $schema ) ); + } +}