WordPress.org

Make WordPress Core


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

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

Props spacedmonkey, chrisvanpatten.
Fixes #49270.

File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.