Completed
Push — develop ( 4115c0...019cf2 )
by Aristeides
03:44
created

Kirki_Control_Typography::enqueue_scripts()   F

Complexity

Conditions 15
Paths 296

Size

Total Lines 83
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 56
nc 296
nop 0
dl 0
loc 83
rs 3.7313
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/js/select2.full.js', array( 'jquery' ), false, true );
109
		wp_enqueue_style( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/kirki.css', null );
110
		wp_enqueue_style( 'wp-color-picker' );
111
		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 );
112
		wp_enqueue_script( 'kirki-typography', trailingslashit( Kirki::$url ) . 'controls/typography/typography.js', array( 'jquery', 'customize-base', 'select2', 'wp-color-picker-alpha' ), false, true );
113
114
		// Add fonts to our JS objects.
115
		$google_fonts   = Kirki_Fonts::get_google_fonts();
116
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
117
		$all_variants   = Kirki_Fonts::get_all_variants();
118
		$all_subsets    = Kirki_Fonts::get_google_font_subsets();
119
120
		$standardfonts_user_keys = $this->choices['fonts']['standard'];
121
		$googlefonts_user_keys   = $this->choices['fonts']['google'];
122
123
		$standard_fonts_final = array();
124
		$default_variants = $this->format_variants_array( array(
125
			'regular',
126
			'italic',
127
			'700',
128
			'700italic',
129
		) );
130
		foreach ( $standard_fonts as $key => $font ) {
131
			if ( ! empty( $standardfonts_user_keys ) && ! in_array( $key, $standardfonts_user_keys ) ) {
132
				continue;
133
			}
134
			$standard_fonts_final[] = array(
135
				'family'      => $font['stack'],
136
				'label'       => $font['label'],
137
				'subsets'     => array(),
138
				'is_standard' => true,
139
				'variants'    => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants,
140
			);
141
		}
142
143
		$google_fonts_final = array();
144
		foreach ( $google_fonts as $family => $args ) {
145
			if ( ! empty( $googlefonts_user_keys ) && ! in_array( $family, $googlefonts_user_keys ) ) {
146
				continue;
147
			}
148
149
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
150
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
151
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
152
153
			$available_variants = array();
154
			foreach ( $variants as $variant ) {
155
				if ( array_key_exists( $variant, $all_variants ) ) {
156
					$available_variants[] = array(
157
						'id' => $variant,
158
						'label' => $all_variants[ $variant ],
159
					);
160
				}
161
			}
162
163
			$available_subsets = array();
164
			foreach ( $subsets as $subset ) {
165
				if ( array_key_exists( $subset, $all_subsets ) ) {
166
					$available_subsets[] = array(
167
						'id' => $subset,
168
						'label' => $all_subsets[ $subset ],
169
					);
170
				}
171
			}
172
173
			$google_fonts_final[] = array(
174
				'family'       => $family,
175
				'label'        => $label,
176
				'variants'     => $available_variants,
177
				'subsets'      => $available_subsets,
178
			);
179
		}
180
		$final = array(
181
			'standard' => $standard_fonts_final,
182
			'google'   => $google_fonts_final,
183
		);
184
		wp_localize_script( 'kirki-typography', 'kirkiFonts' . $this->id, $final );
185
186
		wp_enqueue_style( 'kirki-typography-css', trailingslashit( Kirki::$url ) . 'controls/typography/typography.css', null );
187
188
	}
189
190
	/**
191
	 * Refresh the parameters passed to the JavaScript via JSON.
192
	 *
193
	 * @see WP_Customize_Control::to_json()
194
	 */
195
	public function to_json() {
196
		parent::to_json();
197
198
		$this->json['default'] = $this->setting->default;
199
		if ( isset( $this->default ) ) {
200
			$this->json['default'] = $this->default;
201
		}
202
		$this->json['output']  = $this->output;
203
		$this->json['value']   = $this->value();
204
		$this->json['choices'] = $this->choices;
205
		$this->json['link']    = $this->get_link();
206
		$this->json['id']      = $this->id;
207
		$this->json['l10n']    = $this->l10n();
208
209
		if ( 'user_meta' === $this->option_type ) {
210
			// @codingStandardsIgnoreLine
211
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
212
		}
213
214
		$this->json['inputAttrs'] = '';
215
		foreach ( $this->input_attrs as $attr => $value ) {
216
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
217
		}
218
219
		$this->add_values_backwards_compatibility();
220
		$defaults = array(
221
			'font-family'    => false,
222
			'font-size'      => false,
223
			'variant'        => false,
224
			'line-height'    => false,
225
			'letter-spacing' => false,
226
			'word-spacing'   => false,
227
			'color'          => false,
228
			'text-align'     => false,
229
		);
230
		$this->json['default'] = wp_parse_args( $this->json['default'], $defaults );
231
		$this->json['show_variants'] = ( true === Kirki_Fonts_Google::$force_load_all_variants ) ? false : true;
232
		$this->json['show_subsets']  = ( true === Kirki_Fonts_Google::$force_load_all_subsets ) ? false : true;
233
		$this->json['languages'] = array(
234
			'cyrillic'     => 'Cyrillic',
235
			'cyrillic-ext' => 'Cyrillic Extended',
236
			'devanagari'   => 'Devanagari',
237
			'greek'        => 'Greek',
238
			'greek-ext'    => 'Greek Extended',
239
			'khmer'        => 'Khmer',
240
			'latin'        => 'Latin',
241
			'latin-ext'    => 'Latin Extended',
242
			'vietnamese'   => 'Vietnamese',
243
			'hebrew'       => 'Hebrew',
244
			'arabic'       => 'Arabic',
245
			'bengali'      => 'Bengali',
246
			'gujarati'     => 'Gujarati',
247
			'tamil'        => 'Tamil',
248
			'telugu'       => 'Telugu',
249
			'thai'         => 'Thai',
250
		);
251
	}
252
253
	/**
254
	 * An Underscore (JS) template for this control's content (but not its container).
255
	 *
256
	 * Class variables for this control class are available in the `data` JS object;
257
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
258
	 *
259
	 * @see WP_Customize_Control::print_template()
260
	 *
261
	 * @access protected
262
	 */
263
	protected function content_template() {
264
		?>
265
		<label class="customizer-text">
266
			<# if ( data.label ) { #>
267
				<span class="customize-control-title">{{{ data.label }}}</span>
268
			<# } #>
269
			<# if ( data.description ) { #>
270
				<span class="description customize-control-description">{{{ data.description }}}</span>
271
			<# } #>
272
		</label>
273
274
		<div class="wrapper">
275
276
			<# if ( data.default['font-family'] ) { #>
277
				<# if ( '' == data.value['font-family'] ) { data.value['font-family'] = data.default['font-family']; } #>
278
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
279
				<div class="font-family">
280
					<h5>{{ data.l10n['font-family'] }}</h5>
281
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="{{ data.l10n['select-font-family'] }}"></select>
282
				</div>
283
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
284
					<div class="variant kirki-variant-wrapper">
285
						<h5>{{ data.l10n['variant'] }}</h5>
286
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
287
					</div>
288
				<# } #>
289
				<# if ( true === data.show_subsets ) { #>
290
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
291
						<h5>{{ data.l10n['subsets'] }}</h5>
292
						<select {{{ data.inputAttrs }}} class="subset" id="kirki-typography-subsets-{{{ data.id }}}" multiple>
293
							<# _.each( data.value.subsets, function( subset ) { #>
294
								<option value="{{ subset }}" selected="selected">{{ data.languages[ subset ] }}</option>
295
							<# } ); #>
296
						</select>
297
					</div>
298
				<# } #>
299
			<# } #>
300
301
			<# if ( data.default['font-size'] ) { #>
302
				<div class="font-size">
303
					<h5>{{ data.l10n['font-size'] }}</h5>
304
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
305
				</div>
306
			<# } #>
307
308
			<# if ( data.default['line-height'] ) { #>
309
				<div class="line-height">
310
					<h5>{{ data.l10n['line-height'] }}</h5>
311
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
312
				</div>
313
			<# } #>
314
315
			<# if ( data.default['letter-spacing'] ) { #>
316
				<div class="letter-spacing">
317
					<h5>{{ data.l10n['letter-spacing'] }}</h5>
318
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
319
				</div>
320
			<# } #>
321
322
			<# if ( data.default['word-spacing'] ) { #>
323
				<div class="word-spacing">
324
					<h5>{{ data.l10n['word-spacing'] }}</h5>
325
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
326
				</div>
327
			<# } #>
328
329
			<# if ( data.default['text-align'] ) { #>
330
				<div class="text-align">
331
					<h5>{{ data.l10n['text-align'] }}</h5>
332
					<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"<# } #>>
333
						<label for="{{ data.id }}-text-align-inherit">
334
							<span class="dashicons dashicons-editor-removeformatting"></span>
335
							<span class="screen-reader-text">{{ data.l10n['inherit'] }}</span>
336
						</label>
337
					</input>
338
					<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"<# } #>>
339
						<label for="{{ data.id }}-text-align-left">
340
							<span class="dashicons dashicons-editor-alignleft"></span>
341
							<span class="screen-reader-text">{{ data.l10n['left'] }}</span>
342
						</label>
343
					</input>
344
					<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"<# } #>>
345
						<label for="{{ data.id }}-text-align-center">
346
							<span class="dashicons dashicons-editor-aligncenter"></span>
347
							<span class="screen-reader-text">{{ data.l10n['center'] }}</span>
348
						</label>
349
					</input>
350
					<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"<# } #>>
351
						<label for="{{ data.id }}-text-align-right">
352
							<span class="dashicons dashicons-editor-alignright"></span>
353
							<span class="screen-reader-text">{{ data.l10n['right'] }}</span>
354
						</label>
355
					</input>
356
					<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"<# } #>>
357
						<label for="{{ data.id }}-text-align-justify">
358
							<span class="dashicons dashicons-editor-justify"></span>
359
							<span class="screen-reader-text">{{ data.l10n['justify'] }}</span>
360
						</label>
361
					</input>
362
				</div>
363
			<# } #>
364
365
			<# if ( data.default['text-transform'] ) { #>
366
				<div class="text-transform">
367
					<h5>{{ data.l10n['text-transform'] }}</h5>
368
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
369
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['none'] }}</option>
370
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['capitalize'] }}</option>
371
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['uppercase'] }}</option>
372
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['lowercase'] }}</option>
373
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['initial'] }}</option>
374
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['inherit'] }}</option>
375
					</select>
376
				</div>
377
			<# } #>
378
379
			<# if ( data.default['color'] ) { #>
380
				<div class="color">
381
					<h5>{{ data.l10n['color'] }}</h5>
382
					<input {{{ data.inputAttrs }}} type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="kirki-color-control color-picker" {{{ data.link }}} />
383
				</div>
384
			<# } #>
385
		</div>
386
		<#
387
		if ( ! _.isUndefined( data.value['font-family'] ) ) {
388
			data.value['font-family'] = data.value['font-family'].replace( /&quot;/g, '&#39' );
389
		}
390
		valueJSON = JSON.stringify( data.value ).replace( /'/g, '&#39' );
391
		#>
392
		<input class="typography-hidden-value" type="hidden" value='{{{ valueJSON }}}' {{{ data.link }}}>
393
		<?php
394
	}
395
396
	/**
397
	 * Adds backwards-compatibility for values.
398
	 * Converts font-weight to variant
399
	 * Adds units to letter-spacing
400
	 *
401
	 * @access protected
402
	 */
403
	protected function add_values_backwards_compatibility() {
404
		$value = $this->value();
405
		$old_values = array(
406
			'font-family'    => '',
407
			'font-size'      => '',
408
			'variant'        => ( isset( $value['font-weight'] ) ) ? $value['font-weight'] : 'regular',
409
			'line-height'    => '',
410
			'letter-spacing' => '',
411
			'color'          => '',
412
		);
413
414
		// Font-weight is now variant.
415
		// All values are the same with the exception of 400 (becomes regular).
416
		if ( '400' === $old_values['variant'] || 400 === $old_values['variant'] ) {
417
			$old_values['variant'] = 'regular';
418
		}
419
420
		if ( isset( $value['variant'] ) && in_array( $value['variant'], array( '100light', '600bold', '800bold', '900bold' ) ) ) {
421
			$value['variant'] = (string) intval( $value['variant'] );
422
		}
423
424
		// Letter spacing was in px, now it requires units.
425
		if ( isset( $value['letter-spacing'] ) && is_numeric( $value['letter-spacing'] ) && $value['letter-spacing'] ) {
426
			$value['letter-spacing'] .= 'px';
427
		}
428
429
		$this->json['value'] = wp_parse_args( $value, $old_values );
430
431
		// Cleanup.
432
		if ( isset( $this->json['value']['font-weight'] ) ) {
433
			unset( $this->json['value']['font-weight'] );
434
		}
435
436
		// Make sure we use "subsets" instead of "subset".
437
		if ( isset( $this->json['value']['subset'] ) ) {
438
			if ( ! empty( $this->json['value']['subset'] ) && ! isset( $this->json['value']['subsets'] ) || empty( $this->json['value']['subsets'] ) ) {
439
				$this->json['value']['subsets'] = $this->json['value']['subset'];
440
			}
441
			unset( $this->json['value']['subset'] );
442
		}
443
	}
444
445
	/**
446
	 * Returns an array of translation strings.
447
	 *
448
	 * @access protected
449
	 * @since 3.0.0
450
	 * @param string|false $config_id The string-ID.
451
	 * @return string
452
	 */
453
	protected function l10n( $config_id = false ) {
454
		$translation_strings = array(
455
			'inherit'        => esc_attr__( 'Inherit', 'kirki' ),
456
			'font-family'    => esc_attr__( 'Font Family', 'kirki' ),
457
			'font-size'      => esc_attr__( 'Font Size', 'kirki' ),
458
			'line-height'    => esc_attr__( 'Line Height', 'kirki' ),
459
			'letter-spacing' => esc_attr__( 'Letter Spacing', 'kirki' ),
460
			'word-spacing'   => esc_attr__( 'Word Spacing', 'kirki' ),
461
			'left'           => esc_attr__( 'Left', 'kirki' ),
462
			'right'          => esc_attr__( 'Right', 'kirki' ),
463
			'center'         => esc_attr__( 'Center', 'kirki' ),
464
			'justify'        => esc_attr__( 'Justify', 'kirki' ),
465
			'color'          => esc_attr__( 'Color', 'kirki' ),
466
			'variant'        => esc_attr__( 'Variant', 'kirki' ),
467
			'subsets'        => esc_attr__( 'Subset', 'kirki' ),
468
			'text-align'     => esc_attr__( 'Text Align', 'kirki' ),
469
			'text-transform' => esc_attr__( 'Text Transform', 'kirki' ),
470
			'none'           => esc_attr__( 'None', 'kirki' ),
471
			'capitalize'     => esc_attr__( 'Capitalize', 'kirki' ),
472
			'uppercase'      => esc_attr__( 'Uppercase', 'kirki' ),
473
			'lowercase'      => esc_attr__( 'Lowercase', 'kirki' ),
474
			'initial'        => esc_attr__( 'Initial', 'kirki' ),
475
		);
476
		$translation_strings = apply_filters( "kirki/{$this->kirki_config}/l10n", $translation_strings );
477
		if ( false === $config_id ) {
478
			return $translation_strings;
479
		}
480
		return $translation_strings[ $config_id ];
481
	}
482
483
	/**
484
	 * Formats variants.
485
	 *
486
	 * @access protected
487
	 * @since 3.0.0
488
	 * @param array $variants The variants.
489
	 * @return array
490
	 */
491
	protected function format_variants_array( $variants ) {
492
493
		$all_variants = Kirki_Fonts::get_all_variants();
494
		$final_variants = array();
495
		foreach ( $variants as $variant ) {
496
			if ( is_string( $variant ) ) {
497
				$final_variants[] = array(
498
					'id'    => $variant,
499
					'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant,
500
				);
501
			} elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) {
502
				$final_variants[] = $variant;
503
			}
504
		}
505
		return $final_variants;
506
	}
507
508
	/**
509
	 * Render the control's content.
510
	 *
511
	 * @see WP_Customize_Control::render_content()
512
	 */
513
	protected function render_content() {}
514
}
515