Completed
Push — develop ( a7b7e8...718dc0 )
by Aristeides
03:14
created

Kirki_Control_Gradient::l10n()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
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) 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
		Kirki_Custom_Build::register_dependency( 'jquery' );
146
		Kirki_Custom_Build::register_dependency( 'customize-base' );
147
		Kirki_Custom_Build::register_dependency( 'wp-color-picker-alpha' );
148
149
		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 );
150
151
		if ( ! Kirki_Custom_Build::is_custom_build() ) {
152
			wp_enqueue_script( 'kirki-gradient', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.js', array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ), false, true );
153
			wp_enqueue_style( 'kirki-gradient-css', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.css', null );
154
		}
155
		wp_enqueue_style( 'wp-color-picker' );
156
	}
157
158
	/**
159
	 * An Underscore (JS) template for this control's content (but not its container).
160
	 *
161
	 * Class variables for this control class are available in the `data` JS object;
162
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
163
	 *
164
	 * @see WP_Customize_Control::print_template()
165
	 *
166
	 * @access protected
167
	 */
168
	protected function content_template() {
169
		?>
170
		<div class="kirki-controls-loading-spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>
171
		<label>
172
			<span class="customize-control-title">
173
				{{{ data.label }}}
174
			</span>
175
			<# if ( data.description ) { #>
176
				<span class="description customize-control-description">{{{ data.description }}}</span>
177
			<# } #>
178
		</label>
179
		<div class="gradient-preview"></div>
180
		<div class="angle">
181
			<h4><?php esc_attr_e( 'Angle', 'kirki' ); ?></h4>
182
			<input type="range" class="angle gradient-{{ data.id }}" value="{{ data.value.angle }}" min="-90" max="90">
183
		</div>
184
		<div class="colors">
185
			<div class="color-start">
186
				<h4><?php esc_attr_e( 'Start Color', 'kirki' ); ?></h4>
187
				<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" />
188
				<h4><?php esc_attr_e( 'Color Stop', 'kirki' ); ?></h4>
189
				<input type="range" class="position gradient-{{ data.id }}-start" value="{{ data.value.start.position }}" min="0" max="100">
190
			</div>
191
			<div class="color-end">
192
				<h4><?php esc_attr_e( 'End Color', 'kirki' ); ?></h4>
193
				<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" />
194
				<h4><?php esc_attr_e( 'Color Stop', 'kirki' ); ?></h4>
195
				<input type="range" class="position gradient-{{ data.id }}-end" value="{{ data.value.end.position }}" min="0" max="100">
196
			</div>
197
		</div>
198
		<?php
199
	}
200
}
201