Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes PHP warnings on repeatable ColorPicker with an array as default #1340

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/CMB2_Utils.php
Expand Up @@ -589,6 +589,7 @@ public static function concat_attrs( $attrs, $attr_exclude = array() ) {
foreach ( $attrs as $attr => $val ) {
$excluded = in_array( $attr, (array) $attr_exclude, true );
$empty = false === $val && 'value' !== $attr;
$val = is_array( $val ) ? implode( ',', $val ) : $val;
if ( ! $excluded && ! $empty ) {
// if data attribute, use single quote wraps, else double.
rubengc marked this conversation as resolved.
Show resolved Hide resolved
$quotes = self::is_data_attribute( $attr, 'data-' ) ? "'" : '"';
Expand Down
52 changes: 39 additions & 13 deletions includes/types/CMB2_Type_Colorpicker.php
Expand Up @@ -35,19 +35,7 @@ public function __construct( CMB2_Types $types, $args = array(), $value = '' ) {
public function render( $args = array() ) {
$meta_value = $this->value ? $this->value : $this->field->escaped_value();

$hex_color = '(([a-fA-F0-9]){3}){1,2}$';
if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) {
// Value is just 123abc, so prepend #
$meta_value = '#' . $meta_value;
} elseif (
// If value doesn't match #123abc...
! preg_match( '/^#' . $hex_color . '/i', $meta_value )
// And value doesn't match rgba()...
&& 0 !== strpos( trim( $meta_value ), 'rgba' )
) {
// Then sanitize to just #.
$meta_value = '#';
}
$meta_value = self::sanitize_color( $meta_value );

wp_enqueue_style( 'wp-color-picker' );

Expand All @@ -69,6 +57,44 @@ public function render( $args = array() ) {
return parent::render( $args );
}

/**
* Sanitizes the given color, or array of colors.
*
* @since 2.7.1
*
* @param string|array $color The color or array of colors to sanitize.
*
* @return string|array The color or array of colors, sanitized.
**/
rubengc marked this conversation as resolved.
Show resolved Hide resolved
public static function sanitize_color( $color ) {

if ( is_array( $color ) ) {

$color = array_map( 'CMB2_Type_Colorpicker::sanitize_color', $color );
rubengc marked this conversation as resolved.
Show resolved Hide resolved

} else {

// Regexp for hexadecimal colors
$hex_color = '(([a-fA-F0-9]){3}){1,2}$';

if ( preg_match( '/^' . $hex_color . '/i', $color ) ) {
// Value is just 123abc, so prepend #
$color = '#' . $color;
} elseif (
// If value doesn't match #123abc...
! preg_match( '/^#' . $hex_color . '/i', $color )
// And value doesn't match rgba()...
&& 0 !== strpos( trim( $color ), 'rgba' )
) {
// Then sanitize to just #.
$color = '#';
}

}

return $color;
}

public static function dequeue_rgba_colorpicker_script() {
if ( wp_script_is( 'jw-cmb2-rgba-picker-js', 'enqueued' ) ) {
wp_dequeue_script( 'jw-cmb2-rgba-picker-js' );
Expand Down