Changeset 47450
- Timestamp:
- 03/12/2020 02:40:29 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r47362 r47450 972 972 973 973 /** 974 * Parses a 3 or 6 digit hex color (with #). 975 * 976 * @since 5.4.0 977 * 978 * @param string $color 3 or 6 digit hex color (with #). 979 * @return string|false 980 */ 981 function rest_parse_hex_color( $color ) { 982 $regex = '|^#([A-Fa-f0-9]{3}){1,2}$|'; 983 if ( ! preg_match( $regex, $color, $matches ) ) { 984 return false; 985 } 986 987 return $color; 988 } 989 990 /** 974 991 * Parses a date into both its local and UTC equivalent, in MySQL datetime format. 975 992 * … … 1328 1345 if ( isset( $args['format'] ) ) { 1329 1346 switch ( $args['format'] ) { 1347 case 'hex-color': 1348 if ( ! rest_parse_hex_color( $value ) ) { 1349 return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) ); 1350 } 1351 break; 1352 1330 1353 case 'date-time': 1331 1354 if ( ! rest_parse_date( $value ) ) { … … 1486 1509 if ( isset( $args['format'] ) ) { 1487 1510 switch ( $args['format'] ) { 1511 case 'hex-color': 1512 return (string) sanitize_hex_color( $value ); 1513 1488 1514 case 'date-time': 1489 1515 return sanitize_text_field( $value ); -
trunk/tests/phpunit/tests/rest-api/rest-controller.php
r47328 r47450 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', … … 164 168 'rest_invalid_email', 165 169 rest_validate_request_arg( 'd', $this->request, 'someemail' ) 170 ); 171 } 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_hex_color', 184 rest_validate_request_arg( 'wibble', $this->request, 'somehex' ) 166 185 ); 167 186 } … … 219 238 'somedate', 220 239 'someemail', 240 'somehex', 221 241 'someenum', 222 242 'someargoptions', … … 248 268 'somedate', 249 269 'someemail', 270 'somehex', 250 271 'someenum', 251 272 'someargoptions', -
trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php
r47362 r47450 78 78 } 79 79 80 /** 81 * @ticket 49270 82 */ 83 public function test_format_hex_color() { 84 $schema = array( 85 'type' => 'string', 86 'format' => 'hex-color', 87 ); 88 $this->assertEquals( '#000000', rest_sanitize_value_from_schema( '#000000', $schema ) ); 89 $this->assertEquals( '#FFF', rest_sanitize_value_from_schema( '#FFF', $schema ) ); 90 $this->assertEquals( '', rest_sanitize_value_from_schema( 'WordPress', $schema ) ); 91 } 92 80 93 public function test_type_array() { 81 94 $schema = array( -
trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php
r47362 r47450 71 71 $this->assertTrue( rest_validate_value_from_schema( 'a@b.co', $schema ) ); 72 72 $this->assertWPError( rest_validate_value_from_schema( 'email', $schema ) ); 73 } 74 75 /** 76 * @ticket 49270 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 ) ); 73 86 } 74 87 -
trunk/tests/phpunit/tests/rest-api/rest-test-controller.php
r46696 r47450 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',
Note: See TracChangeset
for help on using the changeset viewer.