Completed
Pull Request — develop (#1554)
by Aristeides
02:07
created

Kirki_Control_Typography::enqueue_scripts()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 14
nop 0
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: typography.
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       2.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Typography control.
19
 */
20
class Kirki_Control_Typography extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-typography';
29
30
	/**
31
	 * Used to automatically generate all CSS output.
32
	 *
33
	 * @access public
34
	 * @var array
35
	 */
36
	public $output = array();
37
38
	/**
39
	 * Data type
40
	 *
41
	 * @access public
42
	 * @var string
43
	 */
44
	public $option_type = 'theme_mod';
45
46
	/**
47
	 * The kirki_config we're using for this control
48
	 *
49
	 * @access public
50
	 * @var string
51
	 */
52
	public $kirki_config = 'global';
53
54
	/**
55
	 * Constructor.
56
	 *
57
	 * Supplied `$args` override class property defaults.
58
	 *
59
	 * If `$args['settings']` is not defined, use the $id as the setting ID.
60
	 *
61
	 * @since 3.0.0
62
	 *
63
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
64
	 * @param string               $id      Control ID.
65
	 * @param array                $args    {
66
	 *     Optional. Arguments to override class property defaults.
67
	 *
68
	 *     @type int                  $instance_number Order in which this instance was created in relation
69
	 *                                                 to other instances.
70
	 *     @type WP_Customize_Manager $manager         Customizer bootstrap instance.
71
	 *     @type string               $id              Control ID.
72
	 *     @type array                $settings        All settings tied to the control. If undefined, `$id` will
73
	 *                                                 be used.
74
	 *     @type string               $setting         The primary setting for the control (if there is one).
75
	 *                                                 Default 'default'.
76
	 *     @type int                  $priority        Order priority to load the control. Default 10.
77
	 *     @type string               $section         Section the control belongs to. Default empty.
78
	 *     @type string               $label           Label for the control. Default empty.
79
	 *     @type string               $description     Description for the control. Default empty.
80
	 *     @type array                $choices         List of choices for 'radio' or 'select' type controls, where
81
	 *                                                 values are the keys, and labels are the values.
82
	 *                                                 Default empty array.
83
	 *     @type array                $input_attrs     List of custom input attributes for control output, where
84
	 *                                                 attribute names are the keys and values are the values. Not
85
	 *                                                 used for 'checkbox', 'radio', 'select', 'textarea', or
86
	 *                                                 'dropdown-pages' control types. Default empty array.
87
	 *     @type array                $json            Deprecated. Use WP_Customize_Control::json() instead.
88
	 *     @type string               $type            Control type. Core controls include 'text', 'checkbox',
89
	 *                                                 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional
90
	 *                                                 input types such as 'email', 'url', 'number', 'hidden', and
91
	 *                                                 'date' are supported implicitly. Default 'text'.
92
	 * }
93
	 */
94
	public function __construct( $manager, $id, $args = array() ) {
95
96
		parent::__construct( $manager, $id, $args );
97
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
98
99
	}
100
101
	/**
102
	 * Enqueue control related scripts/styles.
103
	 *
104
	 * @access public
105
	 */
106
	public function enqueue_scripts() {
107
108
		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 );
109
		wp_enqueue_style( 'wp-color-picker' );
110
111
		wp_enqueue_script( 'kirki-typography', trailingslashit( Kirki::$url ) . 'controls/js/typography.js', array( 'jquery', 'customize-base', 'selectWoo', 'wp-color-picker-alpha' ), false, true );
112
		wp_enqueue_style( 'kirki-controls-css', trailingslashit( Kirki::$url ) . 'controls/css/controls.css', null );
113
114
		wp_enqueue_script( 'selectWoo', trailingslashit( Kirki::$url ) . 'assets/vendor/selectWoo/js/selectWoo.full.js', array( 'jquery' ), '4.0.3', true );
115
		wp_enqueue_style( 'selectWoo', trailingslashit( Kirki::$url ) . 'assets/vendor/selectWoo/css/selectWoo.css', array(), '4.0.3' );
116
		wp_enqueue_style( 'kirki-selectWoo', trailingslashit( Kirki::$url ) . 'assets/vendor/selectWoo/kirki.css', null );
117
118
		$custom_fonts_array  = ( isset( $this->choices['fonts'] ) && ( isset( $this->choices['fonts']['google'] ) || isset( $this->choices['fonts']['standard'] ) ) && ( ! empty( $this->choices['fonts']['google'] ) || ! empty( $this->choices['fonts']['standard'] ) ) );
119
		$localize_script_var = ( $custom_fonts_array ) ? 'kirkiFonts' . $this->id : 'kirkiAllFonts';
120
		wp_localize_script( 'kirki-typography', $localize_script_var, array(
121
			'standard' => $this->get_standard_fonts(),
122
			'google'   => $this->get_google_fonts(),
123
		) );
124
	}
125
126
	/**
127
	 * Refresh the parameters passed to the JavaScript via JSON.
128
	 *
129
	 * @see WP_Customize_Control::to_json()
130
	 */
131
	public function to_json() {
132
		parent::to_json();
133
134
		$this->json['default'] = $this->setting->default;
135
		if ( isset( $this->default ) ) {
136
			$this->json['default'] = $this->default;
137
		}
138
		$this->json['output']  = $this->output;
139
		$this->json['value']   = wp_parse_args(
140
			Kirki_Field_Typography::sanitize( $this->value() ),
141
			$this->json['default']
142
		);
143
		$this->json['choices'] = $this->choices;
144
		$this->json['link']    = $this->get_link();
145
		$this->json['id']      = $this->id;
146
147
		$this->json['inputAttrs'] = '';
148
		foreach ( $this->input_attrs as $attr => $value ) {
149
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
150
		}
151
152
		foreach ( array_keys( $this->json['value'] ) as $key ) {
153
			if ( ! in_array( $key, array( 'variant', 'font-weight', 'font-style' ), true ) && ! isset( $this->json['default'][ $key ] ) ) {
154
				unset( $this->json['value'][ $key ] );
155
			}
156
		}
157
158
		// Fix for https://github.com/aristath/kirki/issues/1405.
159
		foreach ( array_keys( $this->json['value'] ) as $key ) {
160
			if ( isset( $this->json['default'][ $key ] ) && false === $this->json['default'][ $key ] ) {
161
				unset( $this->json['value'][ $key ] );
162
			}
163
		}
164
		$this->json['show_variants'] = ( true === Kirki_Fonts_Google::$force_load_all_variants ) ? false : true;
165
		$this->json['show_subsets']  = ( true === Kirki_Fonts_Google::$force_load_all_subsets ) ? false : true;
166
		$this->json['languages']     = Kirki_Fonts::get_google_font_subsets();
167
	}
168
169
	/**
170
	 * An Underscore (JS) template for this control's content (but not its container).
171
	 *
172
	 * Class variables for this control class are available in the `data` JS object;
173
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
174
	 *
175
	 * @see WP_Customize_Control::print_template()
176
	 *
177
	 * @access protected
178
	 */
179
	protected function content_template() {
180
		?>
181
		<label class="customizer-text">
182
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
183
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
184
		</label>
185
186
		<div class="wrapper">
187
188
			<# if ( data.default['font-family'] ) { #>
189
				<# data.value['font-family'] = data.value['font-family'] || data['default']['font-family']; #>
190
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
191
				<div class="font-family">
192
					<h5><?php esc_attr_e( 'Font Family', 'kirki' ); ?></h5>
193
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
194
				</div>
195
				<# if ( ! _.isUndefined( data.choices['font-backup'] ) && true === data.choices['font-backup'] ) { #>
196
					<div class="font-backup hide-on-standard-fonts kirki-font-backup-wrapper">
197
						<h5><?php esc_attr_e( 'Backup Font', 'kirki' ); ?></h5>
198
						<select {{{ data.inputAttrs }}} id="kirki-typography-font-backup-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
199
					</div>
200
				<# } #>
201
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
202
					<div class="variant kirki-variant-wrapper">
203
						<h5><?php esc_attr_e( 'Variant', 'kirki' ); ?></h5>
204
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
205
					</div>
206
				<# } #>
207
				<# if ( true === data.show_subsets ) { #>
208
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
209
						<h5><?php esc_attr_e( 'Subset(s)', 'kirki' ); ?></h5>
210
						<select {{{ data.inputAttrs }}} class="subset" id="kirki-typography-subsets-{{{ data.id }}}"<# if ( _.isUndefined( data.choices['disable-multiple-variants'] ) || false === data.choices['disable-multiple-variants'] ) { #> multiple<# } #>>
211
							<# _.each( data.value.subsets, function( subset ) { #>
212
								<option value="{{ subset }}" selected="selected">{{ data.languages[ subset ] }}</option>
213
							<# } ); #>
214
						</select>
215
					</div>
216
				<# } #>
217
			<# } #>
218
219
			<# if ( data.default['font-size'] ) { #>
220
				<# data.value['font-size'] = data.value['font-size'] || data['default']['font-size']; #>
221
				<div class="font-size">
222
					<h5><?php esc_attr_e( 'Font Size', 'kirki' ); ?></h5>
223
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
224
				</div>
225
			<# } #>
226
227
			<# if ( data.default['line-height'] ) { #>
228
				<# data.value['line-height'] = data.value['line-height'] || data['default']['line-height']; #>
229
				<div class="line-height">
230
					<h5><?php esc_attr_e( 'Line Height', 'kirki' ); ?></h5>
231
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
232
				</div>
233
			<# } #>
234
235
			<# if ( data.default['letter-spacing'] ) { #>
236
				<# data.value['letter-spacing'] = data.value['letter-spacing'] || data['default']['letter-spacing']; #>
237
				<div class="letter-spacing">
238
					<h5><?php esc_attr_e( 'Letter Spacing', 'kirki' ); ?></h5>
239
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
240
				</div>
241
			<# } #>
242
243
			<# if ( data.default['word-spacing'] ) { #>
244
				<# data.value['word-spacing'] = data.value['word-spacing'] || data['default']['word-spacing']; #>
245
				<div class="word-spacing">
246
					<h5><?php esc_attr_e( 'Word Spacing', 'kirki' ); ?></h5>
247
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
248
				</div>
249
			<# } #>
250
251
			<# if ( data.default['text-align'] ) { #>
252
				<# data.value['text-align'] = data.value['text-align'] || data['default']['text-align']; #>
253
				<div class="text-align">
254
					<h5><?php esc_attr_e( 'Text Align', 'kirki' ); ?></h5>
255
					<input {{{ data.inputAttrs }}} type="radio" value="inherit" name="_customize-typography-text-align-radio-{{ data.id }}" id="{{ data.id }}-text-align-inherit" <# if ( data.value['text-align'] === 'inherit' ) { #> checked="checked"<# } #>>
256
						<label for="{{ data.id }}-text-align-inherit">
257
							<span class="dashicons dashicons-editor-removeformatting"></span>
258
							<span class="screen-reader-text"><?php esc_attr_e( 'Inherit', 'kirki' ); ?></span>
259
						</label>
260
					</input>
261
					<input {{{ data.inputAttrs }}} type="radio" value="left" name="_customize-typography-text-align-radio-{{ data.id }}" id="{{ data.id }}-text-align-left" <# if ( data.value['text-align'] === 'left' ) { #> checked="checked"<# } #>>
262
						<label for="{{ data.id }}-text-align-left">
263
							<span class="dashicons dashicons-editor-alignleft"></span>
264
							<span class="screen-reader-text"><?php esc_attr_e( 'Left', 'kirki' ); ?></span>
265
						</label>
266
					</input>
267
					<input {{{ data.inputAttrs }}} type="radio" value="center" name="_customize-typography-text-align-radio-{{ data.id }}" id="{{ data.id }}-text-align-center" <# if ( data.value['text-align'] === 'center' ) { #> checked="checked"<# } #>>
268
						<label for="{{ data.id }}-text-align-center">
269
							<span class="dashicons dashicons-editor-aligncenter"></span>
270
							<span class="screen-reader-text"><?php esc_attr_e( 'Center', 'kirki' ); ?></span>
271
						</label>
272
					</input>
273
					<input {{{ data.inputAttrs }}} type="radio" value="right" name="_customize-typography-text-align-radio-{{ data.id }}" id="{{ data.id }}-text-align-right" <# if ( data.value['text-align'] === 'right' ) { #> checked="checked"<# } #>>
274
						<label for="{{ data.id }}-text-align-right">
275
							<span class="dashicons dashicons-editor-alignright"></span>
276
							<span class="screen-reader-text"><?php esc_attr_e( 'Right', 'kirki' ); ?></span>
277
						</label>
278
					</input>
279
					<input {{{ data.inputAttrs }}} type="radio" value="justify" name="_customize-typography-text-align-radio-{{ data.id }}" id="{{ data.id }}-text-align-justify" <# if ( data.value['text-align'] === 'justify' ) { #> checked="checked"<# } #>>
280
						<label for="{{ data.id }}-text-align-justify">
281
							<span class="dashicons dashicons-editor-justify"></span>
282
							<span class="screen-reader-text"><?php esc_attr_e( 'Justify', 'kirki' ); ?></span>
283
						</label>
284
					</input>
285
				</div>
286
			<# } #>
287
288
			<# if ( data.default['text-transform'] ) { #>
289
				<# data.value['text-transform'] = data.value['text-transform'] || data['default']['text-transform']; #>
290
				<div class="text-transform">
291
					<h5><?php esc_attr_e( 'Text Transform', 'kirki' ); ?></h5>
292
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
293
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'None', 'kirki' ); ?></option>
294
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Capitalize', 'kirki' ); ?></option>
295
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Uppercase', 'kirki' ); ?></option>
296
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Lowercase', 'kirki' ); ?></option>
297
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Initial', 'kirki' ); ?></option>
298
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Inherit', 'kirki' ); ?></option>
299
					</select>
300
				</div>
301
			<# } #>
302
303
			<# if ( false !== data.default['color'] && data.default['color'] ) { #>
304
				<# data.value['color'] = data.value['color'] || data['default']['color']; #>
305
				<div class="color">
306
					<h5><?php esc_attr_e( 'Color', 'kirki' ); ?></h5>
307
					<input {{{ data.inputAttrs }}} type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="kirki-color-control"/>
308
				</div>
309
			<# } #>
310
311
			<# if ( data.default['margin-top'] ) { #>
312
				<# data.value['margin-top'] = data.value['margin-top'] || data['default']['margin-top']; #>
313
				<div class="margin-top">
314
					<h5><?php esc_attr_e( 'Margin Top', 'kirki' ); ?></h5>
315
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-top'] }}"/>
316
				</div>
317
			<# } #>
318
319
			<# if ( data.default['margin-bottom'] ) { #>
320
				<# data.value['margin-bottom'] = data.value['margin-bottom'] || data['default']['margin-bottom']; #>
321
				<div class="margin-bottom">
322
					<h5><?php esc_attr_e( 'Margin Bottom', 'kirki' ); ?></h5>
323
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-bottom'] }}"/>
324
				</div>
325
			<# } #>
326
		</div>
327
		<input class="typography-hidden-value" type="hidden" {{{ data.link }}}>
328
		<?php
329
	}
330
331
	/**
332
	 * Formats variants.
333
	 *
334
	 * @access protected
335
	 * @since 3.0.0
336
	 * @param array $variants The variants.
337
	 * @return array
338
	 */
339
	protected function format_variants_array( $variants ) {
340
341
		$all_variants = Kirki_Fonts::get_all_variants();
342
		$final_variants = array();
343
		foreach ( $variants as $variant ) {
344
			if ( is_string( $variant ) ) {
345
				$final_variants[] = array(
346
					'id'    => $variant,
347
					'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant,
348
				);
349
			} elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) {
350
				$final_variants[] = $variant;
351
			}
352
		}
353
		return $final_variants;
354
	}
355
356
	/**
357
	 * Gets standard fonts properly formatted for our control.
358
	 *
359
	 * @access protected
360
	 * @since 3.0.0
361
	 * @return array
362
	 */
363
	protected function get_standard_fonts() {
364
		// Add fonts to our JS objects.
365
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
366
367
		$std_user_keys = array();
368
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['standard'] ) ) {
369
			$std_user_keys = $this->choices['fonts']['standard'];
370
		}
371
372
		$standard_fonts_final = array();
373
		$default_variants = $this->format_variants_array( array(
374
			'regular',
375
			'italic',
376
			'700',
377
			'700italic',
378
		) );
379
		foreach ( $standard_fonts as $key => $font ) {
380
			if ( ( ! empty( $std_user_keys ) && ! in_array( $key, $std_user_keys, true ) ) || ! isset( $font['stack'] ) || ! isset( $font['label'] ) ) {
381
				continue;
382
			}
383
			$standard_fonts_final[] = array(
384
				'family'      => $font['stack'],
385
				'label'       => $font['label'],
386
				'subsets'     => array(),
387
				'is_standard' => true,
388
				'variants'    => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants,
389
			);
390
		}
391
		return $standard_fonts_final;
392
	}
393
394
	/**
395
	 * Gets google fonts properly formatted for our control.
396
	 *
397
	 * @access protected
398
	 * @since 3.0.0
399
	 * @return array
400
	 */
401
	protected function get_google_fonts() {
402
		// Add fonts to our JS objects.
403
		$google_fonts = Kirki_Fonts::get_google_fonts();
404
		$all_variants = Kirki_Fonts::get_all_variants();
405
		$all_subsets  = Kirki_Fonts::get_google_font_subsets();
406
407
		$gf_user_keys = array();
408
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['google'] ) ) {
409
			$gf_user_keys = $this->choices['fonts']['google'];
410
		}
411
412
		$google_fonts_final = array();
413
		foreach ( $google_fonts as $family => $args ) {
414
			if ( ! empty( $gf_user_keys ) && ! in_array( $family, $gf_user_keys, true ) ) {
415
				continue;
416
			}
417
418
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
419
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
420
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
421
422
			$available_variants = array();
423
			if ( is_array( $variants ) ) {
424
				foreach ( $variants as $variant ) {
425
					if ( array_key_exists( $variant, $all_variants ) ) {
426
						$available_variants[] = array(
427
							'id' => $variant,
428
							'label' => $all_variants[ $variant ],
429
						);
430
					}
431
				}
432
			}
433
434
			$available_subsets = array();
435
			if ( is_array( $subsets ) ) {
436
				foreach ( $subsets as $subset ) {
437
					if ( array_key_exists( $subset, $all_subsets ) ) {
438
						$available_subsets[] = array(
439
							'id' => $subset,
440
							'label' => $all_subsets[ $subset ],
441
						);
442
					}
443
				}
444
			}
445
446
			$google_fonts_final[] = array(
447
				'family'       => $family,
448
				'label'        => $label,
449
				'variants'     => $available_variants,
450
				'subsets'      => $available_subsets,
451
			);
452
		} // End foreach().
453
		return $google_fonts_final;
454
	}
455
456
	/**
457
	 * Render the control's content.
458
	 *
459
	 * @see WP_Customize_Control::render_content()
460
	 */
461
	protected function render_content() {}
462
}
463