Completed
Push — develop ( 5399cf...608850 )
by Aristeides
02:17
created

Kirki_Control_Typography::enqueue_scripts()   C

Complexity

Conditions 7
Paths 28

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 28
nop 0
dl 0
loc 25
rs 6.7272
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
		$colorpicker_script_url = trailingslashit( Kirki::$url ) . 'assets/vendor/wp-color-picker-alpha/wp-color-picker-alpha.js';
109
		if ( Kirki_Util::is_colorpicker_script_new() ) {
110
			$colorpicker_script_url = str_replace( '.js', '-new.js', $colorpicker_script_url );
111
			wp_enqueue_style( 'wp-color-picker-alpha', trailingslashit( Kirki::$url ) . 'assets/vendor/wp-color-picker-alpha/wp-color-picker-alpha.css', null );
112
		}
113
114
		wp_enqueue_script( 'wp-color-picker-alpha', $colorpicker_script_url, array( 'wp-color-picker' ), false, true );
115
		wp_enqueue_style( 'wp-color-picker' );
116
117
		wp_enqueue_script( 'kirki-typography', trailingslashit( Kirki::$url ) . 'controls/typography/typography.js', array( 'jquery', 'customize-base', 'select2', 'wp-color-picker-alpha' ), false, true );
118
		wp_enqueue_style( 'kirki-typography-css', trailingslashit( Kirki::$url ) . 'controls/typography/typography.css', null );
119
120
		wp_enqueue_script( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/js/select2.full.js', array( 'jquery' ), '4.0.3', true );
121
		wp_enqueue_style( 'select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/css/select2.css', array(), '4.0.3' );
122
		wp_enqueue_style( 'kirki-select2', trailingslashit( Kirki::$url ) . 'assets/vendor/select2/kirki.css', null );
123
124
		$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'] ) ) );
125
		$localize_script_var = ( $custom_fonts_array ) ? 'kirkiFonts' . $this->id : 'kirkiAllFonts';
126
		wp_localize_script( 'kirki-typography', $localize_script_var, array(
127
			'standard' => $this->get_standard_fonts(),
128
			'google'   => $this->get_google_fonts(),
129
		) );
130
	}
131
132
	/**
133
	 * Refresh the parameters passed to the JavaScript via JSON.
134
	 *
135
	 * @see WP_Customize_Control::to_json()
136
	 */
137
	public function to_json() {
138
		parent::to_json();
139
140
		$this->json['default'] = $this->setting->default;
141
		if ( isset( $this->default ) ) {
142
			$this->json['default'] = $this->default;
143
		}
144
		$this->json['output']  = $this->output;
145
		$this->json['value']   = wp_parse_args(
146
			Kirki_Field_Typography::sanitize( $this->value() ),
147
			$this->json['default']
148
		);
149
		$this->json['choices'] = $this->choices;
150
		$this->json['link']    = $this->get_link();
151
		$this->json['id']      = $this->id;
152
153
		$this->json['inputAttrs'] = '';
154
		foreach ( $this->input_attrs as $attr => $value ) {
155
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
156
		}
157
158
		foreach ( array_keys( $this->json['value'] ) as $key ) {
159
			if ( ! in_array( $key, array( 'variant', 'font-weight', 'font-style' ) ) && ! isset( $this->json['default'][ $key ] ) ) {
160
				unset( $this->json['value'][ $key ] );
161
			}
162
		}
163
164
		// Fix for https://github.com/aristath/kirki/issues/1405.
165
		foreach ( array_keys( $this->json['value'] ) as $key ) {
166
			if ( isset( $this->json['default'][ $key ] ) && false === $this->json['default'][ $key ] ) {
167
				unset( $this->json['value'][ $key ] );
168
			}
169
		}
170
		$this->json['show_variants'] = ( true === Kirki_Fonts_Google::$force_load_all_variants ) ? false : true;
171
		$this->json['show_subsets']  = ( true === Kirki_Fonts_Google::$force_load_all_subsets ) ? false : true;
172
		$this->json['languages']     = Kirki_Fonts::get_google_font_subsets();
173
	}
174
175
	/**
176
	 * An Underscore (JS) template for this control's content (but not its container).
177
	 *
178
	 * Class variables for this control class are available in the `data` JS object;
179
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
180
	 *
181
	 * @see WP_Customize_Control::print_template()
182
	 *
183
	 * @access protected
184
	 */
185
	protected function content_template() {
186
		?>
187
		<label class="customizer-text">
188
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
189
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
190
		</label>
191
192
		<div class="wrapper">
193
194
			<# if ( data.default['font-family'] ) { #>
195
				<# data.value['font-family'] = data.value['font-family'] || data['default']['font-family']; #>
196
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
197
				<div class="font-family">
198
					<h5><?php esc_attr_e( 'Font Family', 'kirki' ); ?></h5>
199
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
200
				</div>
201
				<# if ( ! _.isUndefined( data.choices['font-backup'] ) && true === data.choices['font-backup'] ) { #>
202
					<div class="font-backup hide-on-standard-fonts kirki-font-backup-wrapper">
203
						<h5><?php esc_attr_e( 'Backup Font', 'kirki' ); ?></h5>
204
						<select {{{ data.inputAttrs }}} id="kirki-typography-font-backup-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
205
					</div>
206
				<# } #>
207
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
208
					<div class="variant kirki-variant-wrapper">
209
						<h5><?php esc_attr_e( 'Variant', 'kirki' ); ?></h5>
210
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
211
					</div>
212
				<# } #>
213
				<# if ( true === data.show_subsets ) { #>
214
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
215
						<h5><?php esc_attr_e( 'Subset(s)', 'kirki' ); ?></h5>
216
						<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<# } #>>
217
							<# _.each( data.value.subsets, function( subset ) { #>
218
								<option value="{{ subset }}" selected="selected">{{ data.languages[ subset ] }}</option>
219
							<# } ); #>
220
						</select>
221
					</div>
222
				<# } #>
223
			<# } #>
224
225
			<# if ( data.default['font-size'] ) { #>
226
				<# data.value['font-size'] = data.value['font-size'] || data['default']['font-size']; #>
227
				<div class="font-size">
228
					<h5><?php esc_attr_e( 'Font Size', 'kirki' ); ?></h5>
229
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
230
				</div>
231
			<# } #>
232
233
			<# if ( data.default['line-height'] ) { #>
234
				<# data.value['line-height'] = data.value['line-height'] || data['default']['line-height']; #>
235
				<div class="line-height">
236
					<h5><?php esc_attr_e( 'Line Height', 'kirki' ); ?></h5>
237
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
238
				</div>
239
			<# } #>
240
241
			<# if ( data.default['letter-spacing'] ) { #>
242
				<# data.value['letter-spacing'] = data.value['letter-spacing'] || data['default']['letter-spacing']; #>
243
				<div class="letter-spacing">
244
					<h5><?php esc_attr_e( 'Letter Spacing', 'kirki' ); ?></h5>
245
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
246
				</div>
247
			<# } #>
248
249
			<# if ( data.default['word-spacing'] ) { #>
250
				<# data.value['word-spacing'] = data.value['word-spacing'] || data['default']['word-spacing']; #>
251
				<div class="word-spacing">
252
					<h5><?php esc_attr_e( 'Word Spacing', 'kirki' ); ?></h5>
253
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
254
				</div>
255
			<# } #>
256
257
			<# if ( data.default['text-align'] ) { #>
258
				<# data.value['text-align'] = data.value['text-align'] || data['default']['text-align']; #>
259
				<div class="text-align">
260
					<h5><?php esc_attr_e( 'Text Align', 'kirki' ); ?></h5>
261
					<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"<# } #>>
262
						<label for="{{ data.id }}-text-align-inherit">
263
							<span class="dashicons dashicons-editor-removeformatting"></span>
264
							<span class="screen-reader-text"><?php esc_attr_e( 'Inherit', 'kirki' ); ?></span>
265
						</label>
266
					</input>
267
					<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"<# } #>>
268
						<label for="{{ data.id }}-text-align-left">
269
							<span class="dashicons dashicons-editor-alignleft"></span>
270
							<span class="screen-reader-text"><?php esc_attr_e( 'Left', 'kirki' ); ?></span>
271
						</label>
272
					</input>
273
					<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"<# } #>>
274
						<label for="{{ data.id }}-text-align-center">
275
							<span class="dashicons dashicons-editor-aligncenter"></span>
276
							<span class="screen-reader-text"><?php esc_attr_e( 'Center', 'kirki' ); ?></span>
277
						</label>
278
					</input>
279
					<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"<# } #>>
280
						<label for="{{ data.id }}-text-align-right">
281
							<span class="dashicons dashicons-editor-alignright"></span>
282
							<span class="screen-reader-text"><?php esc_attr_e( 'Right', 'kirki' ); ?></span>
283
						</label>
284
					</input>
285
					<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"<# } #>>
286
						<label for="{{ data.id }}-text-align-justify">
287
							<span class="dashicons dashicons-editor-justify"></span>
288
							<span class="screen-reader-text"><?php esc_attr_e( 'Justify', 'kirki' ); ?></span>
289
						</label>
290
					</input>
291
				</div>
292
			<# } #>
293
294
			<# if ( data.default['text-transform'] ) { #>
295
				<# data.value['text-transform'] = data.value['text-transform'] || data['default']['text-transform']; #>
296
				<div class="text-transform">
297
					<h5><?php esc_attr_e( 'Text Transform', 'kirki' ); ?></h5>
298
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
299
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'None', 'kirki' ); ?></option>
300
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Capitalize', 'kirki' ); ?></option>
301
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Uppercase', 'kirki' ); ?></option>
302
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Lowercase', 'kirki' ); ?></option>
303
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Initial', 'kirki' ); ?></option>
304
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Inherit', 'kirki' ); ?></option>
305
					</select>
306
				</div>
307
			<# } #>
308
309
			<# if ( false !== data.default['color'] && data.default['color'] ) { #>
310
				<# data.value['color'] = data.value['color'] || data['default']['color']; #>
311
				<div class="color">
312
					<h5><?php esc_attr_e( 'Color', 'kirki' ); ?></h5>
313
					<input {{{ data.inputAttrs }}} type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="kirki-color-control"/>
314
				</div>
315
			<# } #>
316
317
			<# if ( data.default['margin-top'] ) { #>
318
				<# data.value['margin-top'] = data.value['margin-top'] || data['default']['margin-top']; #>
319
				<div class="margin-top">
320
					<h5><?php esc_attr_e( 'Margin Top', 'kirki' ); ?></h5>
321
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-top'] }}"/>
322
				</div>
323
			<# } #>
324
325
			<# if ( data.default['margin-bottom'] ) { #>
326
				<# data.value['margin-bottom'] = data.value['margin-bottom'] || data['default']['margin-bottom']; #>
327
				<div class="margin-bottom">
328
					<h5><?php esc_attr_e( 'Margin Bottom', 'kirki' ); ?></h5>
329
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-bottom'] }}"/>
330
				</div>
331
			<# } #>
332
		</div>
333
		<#
334
		if ( ! _.isUndefined( data.value['font-family'] ) ) {
335
			data.value['font-family'] = data.value['font-family'].replace( /&quot;/g, '&#39' );
336
		}
337
		valueJSON = JSON.stringify( data.value ).replace( /'/g, '&#39' );
338
		#>
339
		<input class="typography-hidden-value" type="hidden" value='{{{ valueJSON }}}' {{{ data.link }}}>
340
		<?php
341
	}
342
343
	/**
344
	 * Formats variants.
345
	 *
346
	 * @access protected
347
	 * @since 3.0.0
348
	 * @param array $variants The variants.
349
	 * @return array
350
	 */
351
	protected function format_variants_array( $variants ) {
352
353
		$all_variants = Kirki_Fonts::get_all_variants();
354
		$final_variants = array();
355
		foreach ( $variants as $variant ) {
356
			if ( is_string( $variant ) ) {
357
				$final_variants[] = array(
358
					'id'    => $variant,
359
					'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant,
360
				);
361
			} elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) {
362
				$final_variants[] = $variant;
363
			}
364
		}
365
		return $final_variants;
366
	}
367
368
	/**
369
	 * Gets standard fonts properly formatted for our control.
370
	 *
371
	 * @access protected
372
	 * @since 3.0.0
373
	 * @return array
374
	 */
375
	protected function get_standard_fonts() {
376
		// Add fonts to our JS objects.
377
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
378
379
		$std_user_keys = array();
380
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['standard'] ) ) {
381
			$std_user_keys = $this->choices['fonts']['standard'];
382
		}
383
384
		$standard_fonts_final = array();
385
		$default_variants = $this->format_variants_array( array(
386
			'regular',
387
			'italic',
388
			'700',
389
			'700italic',
390
		) );
391
		foreach ( $standard_fonts as $key => $font ) {
392
			if ( ( ! empty( $std_user_keys ) && ! in_array( $key, $std_user_keys, true ) ) || ! isset( $font['stack'] ) || ! isset( $font['label'] ) ) {
393
				continue;
394
			}
395
			$standard_fonts_final[] = array(
396
				'family'      => $font['stack'],
397
				'label'       => $font['label'],
398
				'subsets'     => array(),
399
				'is_standard' => true,
400
				'variants'    => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants,
401
			);
402
		}
403
		return $standard_fonts_final;
404
	}
405
406
	/**
407
	 * Gets google fonts properly formatted for our control.
408
	 *
409
	 * @access protected
410
	 * @since 3.0.0
411
	 * @return array
412
	 */
413
	protected function get_google_fonts() {
414
		// Add fonts to our JS objects.
415
		$google_fonts = Kirki_Fonts::get_google_fonts();
416
		$all_variants = Kirki_Fonts::get_all_variants();
417
		$all_subsets  = Kirki_Fonts::get_google_font_subsets();
418
419
		$gf_user_keys = array();
420
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['google'] ) ) {
421
			$gf_user_keys = $this->choices['fonts']['google'];
422
		}
423
424
		$google_fonts_final = array();
425
		foreach ( $google_fonts as $family => $args ) {
426
			if ( ! empty( $gf_user_keys ) && ! in_array( $family, $gf_user_keys, true ) ) {
427
				continue;
428
			}
429
430
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
431
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
432
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
433
434
			$available_variants = array();
435
			if ( is_array( $variants ) ) {
436
				foreach ( $variants as $variant ) {
437
					if ( array_key_exists( $variant, $all_variants ) ) {
438
						$available_variants[] = array(
439
							'id' => $variant,
440
							'label' => $all_variants[ $variant ],
441
						);
442
					}
443
				}
444
			}
445
446
			$available_subsets = array();
447
			if ( is_array( $subsets ) ) {
448
				foreach ( $subsets as $subset ) {
449
					if ( array_key_exists( $subset, $all_subsets ) ) {
450
						$available_subsets[] = array(
451
							'id' => $subset,
452
							'label' => $all_subsets[ $subset ],
453
						);
454
					}
455
				}
456
			}
457
458
			$google_fonts_final[] = array(
459
				'family'       => $family,
460
				'label'        => $label,
461
				'variants'     => $available_variants,
462
				'subsets'      => $available_subsets,
463
			);
464
		} // End foreach().
465
		return $google_fonts_final;
466
	}
467
468
	/**
469
	 * Render the control's content.
470
	 *
471
	 * @see WP_Customize_Control::render_content()
472
	 */
473
	protected function render_content() {}
474
}
475