Completed
Push — develop ( 687a85...8cb4fe )
by Aristeides
05:00 queued 02:33
created

Kirki_Control_Gradient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: gradient.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Adds a gradients control.
19
 *
20
 * @uses https://github.com/23r9i0/wp-color-picker-alpha
21
 */
22
class Kirki_Control_Gradient extends WP_Customize_Control {
23
24
	/**
25
	 * The control type.
26
	 *
27
	 * @access public
28
	 * @var string
29
	 */
30
	public $type = 'kirki-gradient';
31
32
	/**
33
	 * Colorpicker palette
34
	 *
35
	 * @access public
36
	 * @var bool
37
	 */
38
	public $palette = true;
39
40
	/**
41
	 * Used to automatically generate all CSS output.
42
	 *
43
	 * @access public
44
	 * @var array
45
	 */
46
	public $output = array();
47
48
	/**
49
	 * Data type
50
	 *
51
	 * @access public
52
	 * @var string
53
	 */
54
	public $option_type = 'theme_mod';
55
56
	/**
57
	 * Constructor.
58
	 *
59
	 * Supplied `$args` override class property defaults.
60
	 *
61
	 * If `$args['settings']` is not defined, use the $id as the setting ID.
62
	 *
63
	 * @since 3.0.0
64
	 *
65
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
66
	 * @param string               $id      Control ID.
67
	 * @param array                $args    {
68
	 *     Optional. Arguments to override class property defaults.
69
	 *
70
	 *     @type int                  $instance_number Order in which this instance was created in relation
71
	 *                                                 to other instances.
72
	 *     @type WP_Customize_Manager $manager         Customizer bootstrap instance.
73
	 *     @type string               $id              Control ID.
74
	 *     @type array                $settings        All settings tied to the control. If undefined, `$id` will
75
	 *                                                 be used.
76
	 *     @type string               $setting         The primary setting for the control (if there is one).
77
	 *                                                 Default 'default'.
78
	 *     @type int                  $priority        Order priority to load the control. Default 10.
79
	 *     @type string               $section         Section the control belongs to. Default empty.
80
	 *     @type string               $label           Label for the control. Default empty.
81
	 *     @type string               $description     Description for the control. Default empty.
82
	 *     @type array                $choices         List of choices for 'radio' or 'select' type controls, where
83
	 *                                                 values are the keys, and labels are the values.
84
	 *                                                 Default empty array.
85
	 *     @type array                $input_attrs     List of custom input attributes for control output, where
86
	 *                                                 attribute names are the keys and values are the values. Not
87
	 *                                                 used for 'checkbox', 'radio', 'select', 'textarea', or
88
	 *                                                 'dropdown-pages' control types. Default empty array.
89
	 *     @type array                $json            Deprecated. Use WP_Customize_Control::json() instead.
90
	 *     @type string               $type            Control type. Core controls include 'text', 'checkbox',
91
	 *                                                 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional
92
	 *                                                 input types such as 'email', 'url', 'number', 'hidden', and
93
	 *                                                 'date' are supported implicitly. Default 'text'.
94
	 * }
95
	 */
96
	public function __construct( $manager, $id, $args = array() ) {
97
98
		parent::__construct( $manager, $id, $args );
99
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
100
101
	}
102
103
	/**
104
	 * Refresh the parameters passed to the JavaScript via JSON.
105
	 *
106
	 * @access public
107
	 */
108
	public function to_json() {
109
		parent::to_json();
110
111
		$this->json['default'] = $this->setting->default;
112
		if ( isset( $this->default ) ) {
113
			$this->json['default'] = $this->default;
114
		}
115
		$this->json['output']  = $this->output;
116
		$this->json['value']   = $this->value();
117
		$this->json['choices'] = $this->choices;
118
		$this->json['link']    = $this->get_link();
119
		$this->json['id']      = $this->id;
120
121
		if ( 'user_meta' === $this->option_type ) {
122
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
123
		}
124
125
		$this->json['inputAttrs'] = '';
126
		foreach ( $this->input_attrs as $attr => $value ) {
127
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
128
		}
129
130
		$this->json['palette'] = $this->palette;
131
		$this->choices['alpha'] = ( isset( $this->choices['alpha'] ) && $this->choices['alpha'] ) ? 'true' : 'false';
132
	}
133
134
	/**
135
	 * Enqueue control related scripts/styles.
136
	 *
137
	 * @access public
138
	 */
139
	public function enqueue_scripts() {
140
		wp_enqueue_script( 'wp-color-picker-alpha', trailingslashit( Kirki::$url ) . 'assets/vendor/wp-color-picker-alpha/wp-color-picker-alpha.js', array( 'wp-color-picker' ), '1.2', true );
141
		wp_enqueue_script( 'kirki-gradient', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.js', array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ), false, true );
142
		wp_enqueue_style( 'wp-color-picker' );
143
		wp_enqueue_style( 'kirki-gradient-css', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.css', null );
144
	}
145
146
	/**
147
	 * An Underscore (JS) template for this control's content (but not its container).
148
	 *
149
	 * Class variables for this control class are available in the `data` JS object;
150
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
151
	 *
152
	 * @see WP_Customize_Control::print_template()
153
	 *
154
	 * @access protected
155
	 */
156
	protected function content_template() {
157
		?>
158
		<label>
159
			<span class="customize-control-title">
160
				{{{ data.label }}}
161
			</span>
162
			<# if ( data.description ) { #>
163
				<span class="description customize-control-description">{{{ data.description }}}</span>
164
			<# } #>
165
			<div class="gradient-preview"></div>
166
			<input type="text" {{{ data.inputAttrs }}} data-palette="{{ data.palette }}" data-default-color="{{ data.default.colors[0] }}" data-alpha="{{ data.choices['alpha'] }}" value="{{ data.value.colors[0] }}" class="kirki-gradient-control-0 color-picker" {{{ data.link }}}/>
167
			<input type="text" {{{ data.inputAttrs }}} data-palette="{{ data.palette }}" data-default-color="{{ data.default.colors[1] }}" data-alpha="{{ data.choices['alpha'] }}" value="{{ data.value.colors[1] }}" class="kirki-gradient-control-1 color-picker" />
168
			<input type="hidden" class="hidden-gradient-input" {{{ data.link }}} />
169
		</label>
170
		<?php
171
	}
172
}
173