Completed
Push — develop ( 63b96c...66d45e )
by Aristeides
03:19
created

Kirki_Control_Gradient::l10n()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 12
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
	 * 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
		if ( 'user_meta' === $this->option_type ) {
130
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
131
		}
132
133
		$this->json['inputAttrs'] = '';
134
		foreach ( $this->input_attrs as $attr => $value ) {
135
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
136
		}
137
138
		$this->json['palette']  = $this->palette;
139
		$this->choices['alpha'] = ( isset( $this->choices['alpha'] ) && $this->choices['alpha'] ) ? 'true' : 'false';
140
		$this->json['l10n']     = $this->l10n();
141
	}
142
143
	/**
144
	 * Enqueue control related scripts/styles.
145
	 *
146
	 * @access public
147
	 */
148
	public function enqueue_scripts() {
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
		wp_enqueue_script( 'kirki-gradient', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.js', array( 'jquery', 'customize-base', 'wp-color-picker-alpha' ), false, true );
151
		wp_enqueue_style( 'wp-color-picker' );
152
		wp_enqueue_style( 'kirki-gradient-css', trailingslashit( Kirki::$url ) . 'controls/gradient/gradient.css', null );
153
	}
154
155
	/**
156
	 * An Underscore (JS) template for this control's content (but not its container).
157
	 *
158
	 * Class variables for this control class are available in the `data` JS object;
159
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
160
	 *
161
	 * @see WP_Customize_Control::print_template()
162
	 *
163
	 * @access protected
164
	 */
165
	protected function content_template() {
166
		?>
167
		<label>
168
			<span class="customize-control-title">
169
				{{{ data.label }}}
170
			</span>
171
			<# if ( data.description ) { #>
172
				<span class="description customize-control-description">{{{ data.description }}}</span>
173
			<# } #>
174
		</label>
175
		<div class="gradient-preview"></div>
176
		<div class="angle">
177
			<h4>{{ data.l10n.angle }}</h4>
178
			<input type="range" class="angle gradient-{{ data.id }}" value="{{ data.value.angle }}" min="-90" max="90">
179
		</div>
180
		<div class="colors">
181
			<# _.each( ['start', 'end'], function( index ) { #>
182
				<div class="color-{{ index }}">
183
					<h4>{{ data.l10n[ index + 'color' ] }}</h4>
184
					<input type="text" {{{ data.inputAttrs }}} data-palette="{{ data.palette }}" data-default-color="{{ data.default[ index ].color }}" data-alpha="{{ data.choices['alpha'] }}" value="{{ data.value[ index ].color }}" class="kirki-gradient-control-{{ index }} color-picker" />
185
					<h4>{{ data.l10n['color_stop'] }}</h4>
186
					<input type="range" class="position gradient-{{ data.id }}-{{ index }}" value="{{ data.value[ index ].position }}" min="0" max="100">
187
				</div>
188
			<# }) #>
189
		</div>
190
		<?php
191
	}
192
	/**
193
	 * Returns an array of translation strings.
194
	 *
195
	 * @access protected
196
	 * @since 3.0.0
197
	 * @param string|false $id The string-ID.
198
	 * @return string
199
	 */
200
	protected function l10n( $id = 'global' ) {
201
		$translation_strings = array(
202
			'angle'      => esc_attr__( 'Angle', 'kirki' ),
203
			'startcolor' => esc_attr__( 'Start Color', 'kirki' ),
204
			'endcolor'   => esc_attr__( 'End Color', 'kirki' ),
205
			'color_stop' => esc_attr__( 'Color Stop', 'kirki' ),
206
			'linear'     => esc_attr__( 'Linear', 'kirki' ),
207
			'radia'      => esc_attr__( 'Radial', 'kirki' ),
208
		);
209
		$translation_strings = apply_filters( "kirki/{$this->kirki_config}/l10n", $translation_strings );
210
		return $translation_strings;
211
	}
212
}
213