Completed
Push — develop ( 2cfce0...51a12f )
by Aristeides
07:40
created

Kirki_Control_Typography::render_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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