142 lines
4.3 KiB
PHP
142 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Popularfx Customizer Custom Controls
|
|
*
|
|
*/
|
|
|
|
if ( class_exists( 'WP_Customize_Control' ) ) {
|
|
/**
|
|
* Custom Control Base Class
|
|
*/
|
|
class Popularfx_Custom_Control extends WP_Customize_Control {
|
|
protected function get_popularfx_resource_url() {
|
|
if( strpos( wp_normalize_path( __DIR__ ), wp_normalize_path( WP_PLUGIN_DIR ) ) === 0 ) {
|
|
// We're in a plugin directory and need to determine the url accordingly.
|
|
return plugin_dir_url( __DIR__ );
|
|
}
|
|
|
|
return trailingslashit( get_template_directory_uri() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Alpha Color Picker Custom Control
|
|
*
|
|
* @author Braad Martin <http://braadmartin.com>
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html
|
|
* @link https://github.com/BraadMartin/components/tree/master/customizer/alpha-color-picker
|
|
*/
|
|
class Popularfx_Customize_Alpha_Color_Control extends Popularfx_Custom_Control {
|
|
/**
|
|
* The type of control being rendered
|
|
*/
|
|
public $type = 'alpha-color';
|
|
/**
|
|
* Add support for palettes to be passed in.
|
|
*
|
|
* Supported palette values are true, false, or an array of RGBa and Hex colors.
|
|
*/
|
|
public $palette;
|
|
/**
|
|
* Add support for showing the opacity value on the slider handle.
|
|
*/
|
|
public $show_opacity;
|
|
/**
|
|
* Enqueue our scripts and styles
|
|
*/
|
|
public function enqueue() {
|
|
wp_enqueue_script( 'wp-color-picker' );
|
|
wp_enqueue_style( 'wp-color-picker' );
|
|
wp_enqueue_script( 'popularfx-customizer-controls' );
|
|
}
|
|
/**
|
|
* Render the control in the customizer
|
|
*/
|
|
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';
|
|
|
|
// Output the label and description if they were passed in.
|
|
if ( isset( $this->label ) && '' !== $this->label ) {
|
|
echo '<span class="customize-control-title">' . sanitize_text_field( $this->label ) . '</span>';
|
|
}
|
|
if ( isset( $this->description ) && '' !== $this->description ) {
|
|
echo '<span class="description popularfx-customize-description">' . sanitize_text_field( $this->description ) . '</span>';
|
|
} ?>
|
|
|
|
<input class="alpha-color-control" type="text" data-show-opacity="<?php echo $show_opacity; ?>" data-palette="<?php echo esc_attr( $palette ); ?>" data-default-color="<?php echo esc_attr( $this->settings['default']->default ); ?>" <?php $this->link(); ?> />
|
|
<?php
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Switch sanitization
|
|
*
|
|
* @param string Switch value
|
|
* @return integer Sanitized value
|
|
*/
|
|
if ( ! function_exists( 'popularfx_switch_sanitization' ) ) {
|
|
function popularfx_switch_sanitization( $input ) {
|
|
if ( true === $input ) {
|
|
return 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Alpha Color (Hex & RGBa) sanitization
|
|
*
|
|
* @param string Input to be sanitized
|
|
* @return string Sanitized input
|
|
*/
|
|
if ( ! function_exists( 'popularfx_hex_rgba_sanitization' ) ) {
|
|
function popularfx_hex_rgba_sanitization( $input, $setting ) {
|
|
if ( empty( $input ) || is_array( $input ) ) {
|
|
return $setting->default;
|
|
}
|
|
|
|
if ( false === strpos( $input, 'rgba' ) ) {
|
|
// If string doesn't start with 'rgba' then santize as hex color
|
|
$input = sanitize_hex_color( $input );
|
|
} else {
|
|
// Sanitize as RGBa color
|
|
$input = str_replace( ' ', '', $input );
|
|
sscanf( $input, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
|
|
$input = 'rgba(' . popularfx_in_range( $red, 0, 255 ) . ',' . popularfx_in_range( $green, 0, 255 ) . ',' . popularfx_in_range( $blue, 0, 255 ) . ',' . popularfx_in_range( $alpha, 0, 1 ) . ')';
|
|
}
|
|
return $input;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Only allow values between a certain minimum & maxmium range
|
|
*
|
|
* @param number Input to be sanitized
|
|
* @return number Sanitized input
|
|
*/
|
|
if ( ! function_exists( 'popularfx_in_range' ) ) {
|
|
function popularfx_in_range( $input, $min, $max ){
|
|
if ( $input < $min ) {
|
|
$input = $min;
|
|
}
|
|
if ( $input > $max ) {
|
|
$input = $max;
|
|
}
|
|
return $input;
|
|
}
|
|
}
|
|
|
|
}
|