Skip to content

Extend existing color picker to support alpha channel #231

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion includes/class-options-framework-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,20 @@ function enqueue_admin_scripts( $hook ) {
if ( $this->options_screen != $hook ) {
return;
}

// Enqueue alpha color picker
wp_enqueue_script(
'wp-color-picker-alpha',
plugin_dir_url( dirname(__FILE__) ) . 'js/wp-color-picker-alpha.min.js',
array( 'wp-color-picker' ),
Options_Framework::VERSION
);

// Enqueue custom option panel JS
wp_enqueue_script(
'options-custom',
plugin_dir_url( dirname(__FILE__) ) . 'js/options-custom.js',
array( 'jquery','wp-color-picker' ),
array( 'jquery','wp-color-picker-alpha' ),
Options_Framework::VERSION
);

Expand Down
2 changes: 1 addition & 1 deletion includes/class-options-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Options_Framework {
* @since 1.7.0
* @type string
*/
const VERSION = '1.8.6';
const VERSION = '1.9.1';

/**
* Initialize the plugin.
Expand Down
4 changes: 2 additions & 2 deletions includes/class-options-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static function optionsframework_fields() {
if ( $val != $value['std'] )
$default_color = ' data-default-color="' .$value['std'] . '" ';
}
$output .= '<input name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" id="' . esc_attr( $value['id'] ) . '" class="of-color" type="text" value="' . esc_attr( $val ) . '"' . $default_color .' />';
$output .= '<input name="' . esc_attr( $option_name . '[' . $value['id'] . ']' ) . '" id="' . esc_attr( $value['id'] ) . '" class="of-color color-picker" data-alpha="true" type="text" value="' . esc_attr( $val ) . '"' . $default_color .' />';

break;

Expand Down Expand Up @@ -279,7 +279,7 @@ static function optionsframework_fields() {
if ( $val != $value['std']['color'] )
$default_color = ' data-default-color="' .$value['std']['color'] . '" ';
}
$font_color = '<input name="' . esc_attr( $option_name . '[' . $value['id'] . '][color]' ) . '" id="' . esc_attr( $value['id'] . '_color' ) . '" class="of-color of-typography-color type="text" value="' . esc_attr( $typography_stored['color'] ) . '"' . $default_color .' />';
$font_color = '<input name="' . esc_attr( $option_name . '[' . $value['id'] . '][color]' ) . '" id="' . esc_attr( $value['id'] . '_color' ) . '" class="of-color of-typography-color color-picker" data-alpha="true" type="text" value="' . esc_attr( $typography_stored['color'] ) . '"' . $default_color .' />';
}

// Allow modification/injection of typography fields
Expand Down
35 changes: 33 additions & 2 deletions includes/class-options-sanitization.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function of_sanitize_background( $input ) {
'attachment' => 'scroll'
) );

$output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
$output['color'] = apply_filters( 'of_sanitize_color', $input['color'] );
$output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
$output['repeat'] = apply_filters( 'of_background_repeat', $input['repeat'] );
$output['position'] = apply_filters( 'of_background_position', $input['position'] );
Expand Down Expand Up @@ -366,7 +366,26 @@ function of_sanitize_hex( $hex, $default = '' ) {
}
return $default;
}
add_filter( 'of_sanitize_color', 'of_sanitize_hex' );
add_filter( 'of_sanitize_hex', 'of_sanitize_hex' );

/**
* Sanitize a color
*
* @param string Color in hexadecimal, rgba, or rgba notation. "#" may or may not be prepended to the hexadecimal
* @param string The value that this function should return if it cannot be recognized as a color.
* @return string
*/

function of_sanitize_color( $color, $default = '' ) {
if ( of_validate_hex( $color ) ) {
return $color;
}
elseif ( of_validate_rgba_hsla( $color ) ) {
return $color;
}
return $default;
}
add_filter( 'of_sanitize_color', 'of_sanitize_color' );

/**
* Get recognized font sizes.
Expand Down Expand Up @@ -450,3 +469,15 @@ function of_validate_hex( $hex ) {
return true;
}
}

/**
* Is a given string a color formatted in rgb(a) or hsl(a) notation?
*
* @param string Color in rgb(a) or hsl(a) notation.
* @return bool
*/
function of_validate_rgba_hsla ( $color ) {
$color = trim( $color );

return preg_match( '/^(rgb|hsl)a?\((-?\d+%?(deg|rad|grad|turn)?[,\s]+){2,3}[\s\/]*[\d\.]+%?\)$/i', $color );
}
11 changes: 11 additions & 0 deletions js/wp-color-picker-alpha.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion options-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: Options Framework
* Plugin URI: http://wptheming.com
* Description: A framework for building theme options.
* Version: 1.8.6
* Version: 1.9.1
* Author: Devin Price
* Author URI: http://wptheming.com
* License: GPL-2.0+
Expand Down