Files
yeuchaybo/lib/classes/class-rgba-customizer-control.php
Thuan Bui fd25f13a4f Initial
2018-06-26 11:09:22 +07:00

142 lines
3.2 KiB
PHP

<?php
/**
* Child Theme Library
*
* WARNING: This file is a part of the core Child Theme Library.
* DO NOT edit this file under any circumstances. Please use
* the functions.php file to make any theme modifications.
*
* @package SEOThemes\ChildThemeLibrary\Classes
* @link https://github.com/seothemes/child-theme-library
* @author Thuan Bui
* @copyright Copyright © 2018 Thuan Bui
* @license GPL-2.0+
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die;
}
// Bail if not in the Customizer.
if ( ! is_customize_preview() ) {
return;
}
/**
* RGBA Color Picker Customizer Control
*
* This control adds a second slider for opacity to the stock
* WordPress color picker, and it includes logic to seamlessly
* convert between RGBa and Hex color values as opacity is
* added to or removed from a color.
*/
class RGBA_Customizer_Control extends WP_Customize_Control {
/**
* Official control name.
*
* @var string $type Control name.
*/
public $type = 'alpha-color';
/**
* Add support for palettes to be passed in.
*
* Supported values are true, false, or an array of RGBa and Hex colors.
*
* @var array $palette Color palettes.
*/
public $palette;
/**
* Add support for showing the opacity value on the slider handle.
*
* @var bool $show_opacity Show opacity.
*/
public $show_opacity;
/**
* Enqueue scripts and styles.
*
* Ideally these would get registered and given proper paths before this
* object is initialized, then we could simply enqueue them here, but
* for completeness as a stand alone class we'll enqueue them here.
*
* @since 1.0.0
*
* @return void
*/
public function enqueue() {
wp_enqueue_script(
'rgba-color-picker',
CHILD_THEME_URI . '/lib/js/customizer.js',
array( 'jquery', 'wp-color-picker' ),
'1.0.0',
true
);
wp_enqueue_style(
'rgba-color-picker',
CHILD_THEME_URI . '/lib/css/customizer.css',
array( 'wp-color-picker' ),
'1.0.0'
);
}
/**
* Render the control.
*
* @since 1.0.0
*
* @return void
*/
public function render_content() {
// Process the palette.
if ( is_array( $this->palette ) ) {
$palette = implode( '|', $this->palette );
} else {
// Default to true.
$palette = ( false === $this->palette || 'false' === $this->palette ) ? 'false' : 'true';
}
// Support passing show_opacity as string or boolean. Default to true.
$show_opacity = ( false === $this->show_opacity || 'false' === $this->show_opacity ) ? 'false' : 'true';
// Begin the output.
if ( isset( $this->label ) && '' !== $this->label ) {
echo '<span class="customize-control-title">' . esc_html( $this->label ) . '</span>';
}
?>
<label>
<?php
if ( isset( $this->description ) && '' !== $this->description ) {
echo '<span class="description customize-control-description">' . esc_html( $this->description ) . '</span>';
}
?>
<input class="alpha-color-control" type="text" data-show-opacity="<?php echo esc_html( $show_opacity ); ?>"
data-palette="<?php echo esc_attr( $palette ); ?>"
data-default-color="<?php echo esc_attr( $this->settings['default']->default ); ?>" <?php $this->link(); ?> />
</label>
<?php
}
}