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

Kirki_Control_Typography   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 445
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 445
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 2
cbo 5

8 Methods

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