id = $id; // Id
$this->name = $name; // Name
$this->label = isset( $atts['label'] ) && !empty( $atts['label'] ) ? $atts['label'] : ''; // Label
$this->alpha = isset( $atts['alpha'] ) && $atts['alpha'] === true ? $atts['alpha'] : false; // Alpha
$this->default = isset( $atts['default'] ) && !empty( $atts['default'] ) ? $atts['default'] : ''; // Default
$this->value = $value; // Value
}
/**
* Output admin form field
*
* @since 1.0.0
* @return string Field output
*/
public function field() {
$html = '';
if ( empty( $this->name ) ) {
return $html;
}
if ( $this->label ) {
$html .= '';
$html .= '
';
}
// If value NULL (only first time before save) set empty string to init color picker
$value = NULL === $this->value ? $this->default : $this->value;
$html .= 'id ) . '" '
. 'id="' . esc_attr( $this->id ) . '" '
. 'name="' . esc_attr( $this->name ) . '" '
. 'type="text" '
. 'value="' . esc_attr( $value ) . '" ';
if ( !empty( $this->default ) ) {
$html .= 'data-default-color="' . esc_js( $this->default ) . '" ';
}
// User selection do enable / disable alpha
if ( $this->alpha ) {
$html .= 'data-alpha="true" ';
} else {
$html .= 'data-alpha="false" ';
}
$html .= '/>';
return $html;
}
/**
* Add script
*
* @since 1.0.0
*/
public static function admin_scripts( $hook ) {
if ( 'widgets.php' !== $hook ) {
return;
}
// Default scripts
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style( 'wp-color-picker' );
/**
* Overwrite Automattic Iris for enabled Alpha Channel in wpColorPicker
* @see https://github.com/23r9i0/wp-color-picker-alpha
*/
wp_enqueue_script( 'wp-color-picker-alpha', PREDIC_WIDGET_ASSETS_URL . '/vendor/wp-color-picker-alpha/wp-color-picker-alpha.js', array( 'wp-color-picker' ), self::$version, true );
// Color picker init
wp_enqueue_script( 'predic-widget-color-field', PREDIC_WIDGET_ASSETS_URL . '/js/fields/color-field.js', array( 'wp-color-picker' ), self::$version, true );
}
}