Completed
Push — develop ( 414778...140499 )
by Aristeides
04:03
created

Kirki_Control_Typography::l10n()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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