Completed
Push — develop ( 4235a8...0751fb )
by Aristeides
02:33
created

Kirki_Control_Gradient::to_json()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 16
nop 0
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: gradient.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, 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
	 * The kirki_config we're using for this control
58
	 *
59
	 * @access public
60
	 * @var string
61
	 */
62
	public $kirki_config = 'global';
63
64
	/**
65
	 * Constructor.
66
	 *
67
	 * Supplied `$args` override class property defaults.
68
	 *
69
	 * If `$args['settings']` is not defined, use the $id as the setting ID.
70
	 *
71
	 * @since 3.0.0
72
	 *
73
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
74
	 * @param string               $id      Control ID.
75
	 * @param array                $args    {
76
	 *     Optional. Arguments to override class property defaults.
77
	 *
78
	 *     @type int                  $instance_number Order in which this instance was created in relation
79
	 *                                                 to other instances.
80
	 *     @type WP_Customize_Manager $manager         Customizer bootstrap instance.
81
	 *     @type string               $id              Control ID.
82
	 *     @type array                $settings        All settings tied to the control. If undefined, `$id` will
83
	 *                                                 be used.
84
	 *     @type string               $setting         The primary setting for the control (if there is one).
85
	 *                                                 Default 'default'.
86
	 *     @type int                  $priority        Order priority to load the control. Default 10.
87
	 *     @type string               $section         Section the control belongs to. Default empty.
88
	 *     @type string               $label           Label for the control. Default empty.
89
	 *     @type string               $description     Description for the control. Default empty.
90
	 *     @type array                $choices         List of choices for 'radio' or 'select' type controls, where
91
	 *                                                 values are the keys, and labels are the values.
92
	 *                                                 Default empty array.
93
	 *     @type array                $input_attrs     List of custom input attributes for control output, where
94
	 *                                                 attribute names are the keys and values are the values. Not
95
	 *                                                 used for 'checkbox', 'radio', 'select', 'textarea', or
96
	 *                                                 'dropdown-pages' control types. Default empty array.
97
	 *     @type array                $json            Deprecated. Use WP_Customize_Control::json() instead.
98
	 *     @type string               $type            Control type. Core controls include 'text', 'checkbox',
99
	 *                                                 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional
100
	 *                                                 input types such as 'email', 'url', 'number', 'hidden', and
101
	 *                                                 'date' are supported implicitly. Default 'text'.
102
	 * }
103
	 */
104
	public function __construct( $manager, $id, $args = array() ) {
105
106
		parent::__construct( $manager, $id, $args );
107
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
108
109
	}
110
111
	/**
112
	 * Refresh the parameters passed to the JavaScript via JSON.
113
	 *
114
	 * @access public
115
	 */
116
	public function to_json() {
117
		parent::to_json();
118
119
		$this->json['default'] = $this->setting->default;
120
		if ( isset( $this->default ) ) {
121
			$this->json['default'] = $this->default;
122
		}
123
		$this->json['output']  = $this->output;
124
		$this->json['value']   = $this->value();
125
		$this->json['choices'] = $this->choices;
126
		$this->json['link']    = $this->get_link();
127
		$this->json['id']      = $this->id;
128
129
		$this->json['inputAttrs'] = '';
130
		foreach ( $this->input_attrs as $attr => $value ) {
131
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
132
		}
133
134
		$this->json['palette']  = $this->palette;
135
		$this->choices['alpha'] = ( isset( $this->choices['alpha'] ) && $this->choices['alpha'] ) ? 'true' : 'false';
136
	}
137
138
	/**
139
	 * Enqueue control related scripts/styles.
140
	 *
141
	 * @access public
142
	 */
143
	public function enqueue_scripts() {
144
145
		if ( class_exists( 'Kirki_Custom_Build' ) ) {
146
			Kirki_Custom_Build::register_dependency( 'jquery' );
147
			Kirki_Custom_Build::register_dependency( 'customize-base' );
148
			Kirki_Custom_Build::register_dependency( 'wp-color-picker-alpha' );
149
		}
150
151
		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 );
152
153
		if ( ! class_exists( 'Kirki_Custom_Build' ) || ! Kirki_Custom_Build::is_custom_build() ) {
154
			wp_enqueue_script( 'kirki-gradient', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.js', array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ), false, true );
155
			wp_enqueue_style( 'kirki-gradient-css', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.css', null );
156
		}
157
		wp_enqueue_style( 'wp-color-picker' );
158
	}
159
160
	/**
161
	 * An Underscore (JS) template for this control's content (but not its container).
162
	 *
163
	 * Class variables for this control class are available in the `data` JS object;
164
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
165
	 *
166
	 * @see WP_Customize_Control::print_template()
167
	 *
168
	 * @access protected
169
	 */
170
	protected function content_template() {
171
		?>
172
		<div class="kirki-controls-loading-spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>
173
		<label>
174
			<span class="customize-control-title">
175
				{{{ data.label }}}
176
			</span>
177
			<# if ( data.description ) { #>
178
				<span class="description customize-control-description">{{{ data.description }}}</span>
179
			<# } #>
180
		</label>
181
		<div class="gradient-preview" style="width:{{ data.choices.preview.width }};height:{{ data.choices.preview.height }}"></div>
182
		<div class="global">
183
			<div class="mode">
184
				<h4><?php esc_attr_e( 'Mode', 'kirki' ); ?></h4>
185
				<input class="switch-input screen-reader-text" type="radio" value="linear" name="_customize-gradient-{{{ data.id }}}" id="{{ data.id }}linear" <# if ( ! _.isUndefined( data.value.mode ) && 'linear' === data.value.mode ) { #> checked="checked" <# } #>>
186
					<label class="switch-label switch-label-<# if ( ! _.isUndefined( data.value.mode ) && 'linear' === data.value.mode ) { #>on <# } else { #>off<# } #>" for="{{ data.id }}linear"><span class="dashicons dashicons-minus"></span><span class="screen-reader-text"><?php esc_attr_e( 'Linear', 'kirki' ); ?></span></label>
187
				</input>
188
				<input class="switch-input screen-reader-text" type="radio" value="radial" name="_customize-gradient-{{{ data.id }}}" id="{{ data.id }}radial" <# if ( ! _.isUndefined( data.value.mode ) && 'radial' === data.value.mode ) { #> checked="checked" <# } #>>
189
					<label class="switch-label switch-label-<# if ( ! _.isUndefined( data.value.mode ) && 'radial' === data.value.mode ) { #>on <# } else { #>off<# } #>" for="{{ data.id }}radial"><span class="dashicons dashicons-marker"></span><span class="screen-reader-text"><?php esc_attr_e( 'Radial', 'kirki' ); ?></span></label>
190
				</input>
191
			</div>
192
			<div class="angle">
193
				<h4><?php esc_attr_e( 'Angle', 'kirki' ); ?></h4>
194
				<input type="range" class="angle gradient-{{ data.id }}" value="{{ data.value.angle }}" min="-90" max="90">
195
			</div>
196
		</div>
197
		<div class="colors">
198
			<div class="color-start">
199
				<div class="color">
200
					<h4><?php esc_attr_e( 'Start Color', 'kirki' ); ?></h4>
201
					<input type="text" {{{ data.inputAttrs }}} data-palette="{{ data.palette }}" data-default-color="{{ data.default.start.color }}" data-alpha="{{ data.choices['alpha'] }}" value="{{ data.value.start.color }}" class="kirki-gradient-control-start color-picker" />
202
				</div>
203
				<div class="position">
204
					<h4><?php esc_attr_e( 'Color Stop', 'kirki' ); ?></h4>
205
					<input type="range" class="position gradient-{{ data.id }}-start" value="{{ data.value.start.position }}" min="0" max="100">
206
				</div>
207
			</div>
208
			<div class="color-end">
209
				<div class="color">
210
					<h4><?php esc_attr_e( 'End Color', 'kirki' ); ?></h4>
211
					<input type="text" {{{ data.inputAttrs }}} data-palette="{{ data.palette }}" data-default-color="{{ data.default.end.color }}" data-alpha="{{ data.choices['alpha'] }}" value="{{ data.value.end.color }}" class="kirki-gradient-control-end color-picker" />
212
				</div>
213
				<div class="position">
214
					<h4><?php esc_attr_e( 'Color Stop', 'kirki' ); ?></h4>
215
					<input type="range" class="position gradient-{{ data.id }}-end" value="{{ data.value.end.position }}" min="0" max="100">
216
				</div>
217
			</div>
218
		</div>
219
		<?php
220
	}
221
}
222