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
This commit is contained in:
TimothyBlynJacobs 2020-03-12 02:42:08 +00:00
parent bcb8ee6dcf
commit 1367b1175f
2 changed files with 27 additions and 1 deletions

View File

@ -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 );

View File

@ -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.