From 1367b1175f357a5c7299f724ee7e447ac76846fe Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Thu, 12 Mar 2020 02:42:08 +0000 Subject: [PATCH] REST API: Introduce "hex-color" JSON Schema format. Props spacedmonkey, chrisvanpatten. Fixes #49270. Built from https://develop.svn.wordpress.org/trunk@47450 git-svn-id: http://core.svn.wordpress.org/trunk@47237 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 26 ++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index bdfd8fac42..9f63b87f93 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -970,6 +970,23 @@ function rest_parse_date( $date, $force_utc = false ) { return strtotime( $date ); } +/** + * Parses a 3 or 6 digit hex color (with #). + * + * @since 5.4.0 + * + * @param string $color 3 or 6 digit hex color (with #). + * @return string|false + */ +function rest_parse_hex_color( $color ) { + $regex = '|^#([A-Fa-f0-9]{3}){1,2}$|'; + if ( ! preg_match( $regex, $color, $matches ) ) { + return false; + } + + return $color; +} + /** * Parses a date into both its local and UTC equivalent, in MySQL datetime format. * @@ -1327,6 +1344,12 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { if ( isset( $args['format'] ) ) { switch ( $args['format'] ) { + case 'hex-color': + if ( ! rest_parse_hex_color( $value ) ) { + return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) ); + } + break; + case 'date-time': if ( ! rest_parse_date( $value ) ) { return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) ); @@ -1485,6 +1508,9 @@ function rest_sanitize_value_from_schema( $value, $args ) { if ( isset( $args['format'] ) ) { switch ( $args['format'] ) { + case 'hex-color': + return (string) sanitize_hex_color( $value ); + case 'date-time': return sanitize_text_field( $value ); diff --git a/wp-includes/version.php b/wp-includes/version.php index f002f6cd90..6961414351 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-47449'; +$wp_version = '5.5-alpha-47450'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.