diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
index 8fbf4b1342..d517916fbe 100644
|
a
|
b
|
function rest_parse_date( $date, $force_utc = false ) { |
| 966 | 966 | return strtotime( $date ); |
| 967 | 967 | } |
| 968 | 968 | |
| | 969 | /** |
| | 970 | * Parses an 3 or 6 digit hex color (with #). |
| | 971 | * |
| | 972 | * @since 5.4.0 |
| | 973 | * |
| | 974 | * @param string $color 3 or 6 digit hex color (with #). |
| | 975 | * @return string|boolean |
| | 976 | */ |
| | 977 | function rest_parse_hex_color( $color ) { |
| | 978 | $regex = '|^#([A-Fa-f0-9]{3}){1,2}$|'; |
| | 979 | if ( ! preg_match( $regex, $color, $matches ) ) { |
| | 980 | return false; |
| | 981 | } |
| | 982 | |
| | 983 | return $color; |
| | 984 | } |
| | 985 | |
| 969 | 986 | /** |
| 970 | 987 | * Parses a date into both its local and UTC equivalent, in MySQL datetime format. |
| 971 | 988 | * |
| … |
… |
function rest_validate_value_from_schema( $value, $args, $param = '' ) { |
| 1314 | 1331 | |
| 1315 | 1332 | if ( isset( $args['format'] ) ) { |
| 1316 | 1333 | switch ( $args['format'] ) { |
| | 1334 | case 'hex-color': |
| | 1335 | if ( ! rest_parse_hex_color( $value ) ) { |
| | 1336 | return new WP_Error( 'rest_invalid_color', __( 'Invalid color.' ) ); |
| | 1337 | } |
| | 1338 | break; |
| | 1339 | |
| 1317 | 1340 | case 'date-time': |
| 1318 | 1341 | if ( ! rest_parse_date( $value ) ) { |
| 1319 | 1342 | return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) ); |
| … |
… |
function rest_sanitize_value_from_schema( $value, $args ) { |
| 1470 | 1493 | |
| 1471 | 1494 | if ( isset( $args['format'] ) ) { |
| 1472 | 1495 | switch ( $args['format'] ) { |
| | 1496 | case 'hex-color': |
| | 1497 | return sanitize_hex_color( $value ); |
| | 1498 | |
| 1473 | 1499 | case 'date-time': |
| 1474 | 1500 | return sanitize_text_field( $value ); |
| 1475 | 1501 | |
diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php
index 91e1d0c5b7..5ab9e2b56b 100644
|
a
|
b
|
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
| 27 | 27 | 'somestring' => array( |
| 28 | 28 | 'type' => 'string', |
| 29 | 29 | ), |
| | 30 | 'somehex' => array( |
| | 31 | 'type' => 'string', |
| | 32 | 'format' => 'hex-color', |
| | 33 | ), |
| 30 | 34 | 'someenum' => array( |
| 31 | 35 | 'type' => 'string', |
| 32 | 36 | 'enum' => array( 'a' ), |
| … |
… |
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
| 166 | 170 | ); |
| 167 | 171 | } |
| 168 | 172 | |
| | 173 | /** |
| | 174 | * @ticket 49270 |
| | 175 | */ |
| | 176 | public function test_validate_schema_format_hex_color() { |
| | 177 | |
| | 178 | $this->assertTrue( |
| | 179 | rest_validate_request_arg( '#000000', $this->request, 'somehex' ) |
| | 180 | ); |
| | 181 | |
| | 182 | $this->assertErrorResponse( |
| | 183 | 'rest_invalid_color', |
| | 184 | rest_validate_request_arg( 'wibble', $this->request, 'somehex' ) |
| | 185 | ); |
| | 186 | } |
| | 187 | |
| 169 | 188 | public function test_validate_schema_format_date_time() { |
| 170 | 189 | |
| 171 | 190 | $this->assertTrue( |
| … |
… |
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
| 218 | 237 | 'someurl', |
| 219 | 238 | 'somedate', |
| 220 | 239 | 'someemail', |
| | 240 | 'somehex', |
| 221 | 241 | 'someenum', |
| 222 | 242 | 'someargoptions', |
| 223 | 243 | 'somedefault', |
| … |
… |
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { |
| 247 | 267 | 'someurl', |
| 248 | 268 | 'somedate', |
| 249 | 269 | 'someemail', |
| | 270 | 'somehex', |
| 250 | 271 | 'someenum', |
| 251 | 272 | 'someargoptions', |
| 252 | 273 | 'somedefault', |
diff --git a/tests/phpunit/tests/rest-api/rest-schema-validation.php b/tests/phpunit/tests/rest-api/rest-schema-validation.php
index 9c18a846dc..f581510a07 100644
|
a
|
b
|
class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { |
| 72 | 72 | $this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) ); |
| 73 | 73 | } |
| 74 | 74 | |
| | 75 | /** |
| | 76 | * @ticket 45098 |
| | 77 | */ |
| | 78 | public function test_format_hex_color() { |
| | 79 | $schema = array( |
| | 80 | 'type' => 'string', |
| | 81 | 'format' => 'hex-color', |
| | 82 | ); |
| | 83 | $this->assertTrue( rest_validate_value_from_schema( '#000000', $schema ) ); |
| | 84 | $this->assertTrue( rest_validate_value_from_schema( '#FFF', $schema ) ); |
| | 85 | $this->assertWPError( rest_validate_value_from_schema( 'WordPress', $schema ) ); |
| | 86 | } |
| | 87 | |
| 75 | 88 | public function test_format_date_time() { |
| 76 | 89 | $schema = array( |
| 77 | 90 | 'type' => 'string', |
diff --git a/tests/phpunit/tests/rest-api/rest-test-controller.php b/tests/phpunit/tests/rest-api/rest-test-controller.php
index b79e7cf59b..a06800c3ce 100644
|
a
|
b
|
class WP_REST_Test_Controller extends WP_REST_Controller { |
| 64 | 64 | 'format' => 'email', |
| 65 | 65 | 'context' => array( 'view' ), |
| 66 | 66 | ), |
| | 67 | 'somehex' => array( |
| | 68 | 'type' => 'string', |
| | 69 | 'format' => 'hex-color', |
| | 70 | 'context' => array( 'view' ), |
| | 71 | ), |
| 67 | 72 | 'someenum' => array( |
| 68 | 73 | 'type' => 'string', |
| 69 | 74 | 'enum' => array( 'a', 'b', 'c' ), |