Make WordPress Core

Changeset 47450


Ignore:
Timestamp:
03/12/2020 02:40:29 AM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Introduce "hex-color" JSON Schema format.

Props spacedmonkey, chrisvanpatten.
Fixes #49270.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r47362 r47450  
    972972
    973973/**
     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 */
     981function 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/**
    974991 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
    975992 *
     
    13281345        if ( isset( $args['format'] ) ) {
    13291346                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
    13301353                        case 'date-time':
    13311354                                if ( ! rest_parse_date( $value ) ) {
     
    14861509        if ( isset( $args['format'] ) ) {
    14871510                switch ( $args['format'] ) {
     1511                        case 'hex-color':
     1512                                return (string) sanitize_hex_color( $value );
     1513
    14881514                        case 'date-time':
    14891515                                return sanitize_text_field( $value );
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r47328 r47450  
    2828                                                'type' => 'string',
    2929                                        ),
     30                                        'somehex'     => array(
     31                                                'type'   => 'string',
     32                                                'format' => 'hex-color',
     33                                        ),
    3034                                        'someenum'    => array(
    3135                                                'type' => 'string',
     
    164168                        'rest_invalid_email',
    165169                        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' )
    166185                );
    167186        }
     
    219238                                'somedate',
    220239                                'someemail',
     240                                'somehex',
    221241                                'someenum',
    222242                                'someargoptions',
     
    248268                                        'somedate',
    249269                                        'someemail',
     270                                        'somehex',
    250271                                        'someenum',
    251272                                        'someargoptions',
  • trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php

    r47362 r47450  
    7878        }
    7979
     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
    8093        public function test_type_array() {
    8194                $schema = array(
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r47362 r47450  
    7171                $this->assertTrue( rest_validate_value_from_schema( 'a@b.co', $schema ) );
    7272                $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 ) );
    7386        }
    7487
  • trunk/tests/phpunit/tests/rest-api/rest-test-controller.php

    r46696 r47450  
    6565                                        'context' => array( 'view' ),
    6666                                ),
     67                                'somehex'        => array(
     68                                        'type'    => 'string',
     69                                        'format'  => 'hex-color',
     70                                        'context' => array( 'view' ),
     71                                ),
    6772                                'someenum'       => array(
    6873                                        'type'    => 'string',
Note: See TracChangeset for help on using the changeset viewer.