Completed
Push — develop ( 8a2f67...52f1e8 )
by Aristeides
02:16
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) 2016, 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
	 * Enqueue control related scripts/styles.
56
	 *
57
	 * @access public
58
	 */
59
	public function enqueue() {
60
61
		wp_enqueue_script( 'selectize', trailingslashit( Kirki::$url ) . 'controls/typography/selectize.js', array( 'jquery' ), false, true );
62
		wp_enqueue_script( 'wp-color-picker-alpha', trailingslashit( Kirki::$url ) . 'controls/typography/wp-color-picker-alpha.js', array( 'wp-color-picker' ), '1.2', true );
63
		wp_enqueue_script( 'kirki-typography', trailingslashit( Kirki::$url ) . 'controls/typography/typography.js', array( 'jquery', 'customize-base', 'selectize', 'wp-color-picker-alpha' ), false, true );
64
65
		// Add fonts to our JS objects.
66
		$google_fonts   = Kirki_Fonts::get_google_fonts();
67
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
68
		$all_variants   = Kirki_Fonts::get_all_variants();
69
		$all_subsets    = Kirki_Fonts::get_google_font_subsets();
70
71
		$standard_fonts_final = array();
72
		foreach ( $standard_fonts as $font ) {
73
			$standard_fonts_final[] = array(
74
				'family'      => $font['stack'],
75
				'label'       => $font['label'],
76
				'subsets'     => array(),
77
				'is_standard' => true,
78
				'variants'    => array(
79
					array(
80
						'id'    => 'regular',
81
						'label' => $all_variants['regular'],
82
					),
83
					array(
84
						'id'    => 'italic',
85
						'label' => $all_variants['italic'],
86
					),
87
					array(
88
						'id'    => '700',
89
						'label' => $all_variants['700'],
90
					),
91
					array(
92
						'id'    => '700italic',
93
						'label' => $all_variants['700italic'],
94
					),
95
				),
96
			);
97
		}
98
99
		$google_fonts_final = array();
100
		foreach ( $google_fonts as $family => $args ) {
101
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
102
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
103
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
104
105
			$available_variants = array();
106
			foreach ( $variants as $variant ) {
107
				if ( array_key_exists( $variant, $all_variants ) ) {
108
					$available_variants[] = array( 'id' => $variant, 'label' => $all_variants[ $variant ] );
109
				}
110
			}
111
112
			$available_subsets = array();
113
			foreach ( $subsets as $subset ) {
114
				if ( array_key_exists( $subset, $all_subsets ) ) {
115
					$available_subsets[] = array( 'id' => $subset, 'label' => $all_subsets[ $subset ] );
116
				}
117
			}
118
119
			$google_fonts_final[] = array(
120
				'family'       => $family,
121
				'label'        => $label,
122
				'variants'     => $available_variants,
123
				'subsets'      => $available_subsets,
124
			);
125
		}
126
		$final = array_merge( $standard_fonts_final, $google_fonts_final );
127
		wp_localize_script( 'kirki-typography', 'kirkiAllFonts', $final );
128
129
		wp_enqueue_style( 'kirki-typography-css', trailingslashit( Kirki::$url ) . 'controls/typography/typography.css', null );
130
131
	}
132
133
	/**
134
	 * Refresh the parameters passed to the JavaScript via JSON.
135
	 *
136
	 * @see WP_Customize_Control::to_json()
137
	 */
138
	public function to_json() {
139
		parent::to_json();
140
141
		$this->json['default'] = $this->setting->default;
142
		if ( isset( $this->default ) ) {
143
			$this->json['default'] = $this->default;
144
		}
145
		$this->json['output']      = $this->output;
146
		$this->json['value']       = $this->value();
147
		$this->json['choices']     = $this->choices;
148
		$this->json['link']        = $this->get_link();
149
		$this->json['id']          = $this->id;
150
		$this->json['l10n']        = $this->l10n();
151
		$this->json['kirkiConfig'] = $this->kirki_config;
152
153
		if ( 'user_meta' === $this->option_type ) {
154
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
155
		}
156
157
		$this->json['inputAttrs'] = '';
158
		foreach ( $this->input_attrs as $attr => $value ) {
159
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
160
		}
161
162
		$this->add_values_backwards_compatibility();
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
	}
177
178
	/**
179
	 * An Underscore (JS) template for this control's content (but not its container).
180
	 *
181
	 * Class variables for this control class are available in the `data` JS object;
182
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
183
	 *
184
	 * @see WP_Customize_Control::print_template()
185
	 *
186
	 * @access protected
187
	 */
188
	protected function content_template() {
189
		?>
190
		<label class="customizer-text">
191
			<# if ( data.label ) { #>
192
				<span class="customize-control-title">{{{ data.label }}}</span>
193
			<# } #>
194
			<# if ( data.description ) { #>
195
				<span class="description customize-control-description">{{{ data.description }}}</span>
196
			<# } #>
197
		</label>
198
199
		<div class="wrapper">
200
201
			<# if ( data.default['font-family'] ) { #>
202
				<# if ( '' == data.value['font-family'] ) { data.value['font-family'] = data.default['font-family']; } #>
203
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
204
				<div class="font-family">
205
					<h5>{{ data.l10n['font-family'] }}</h5>
206
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="{{ data.l10n['select-font-family'] }}"></select>
207
				</div>
208
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
209
					<div class="variant hide-on-standard-fonts kirki-variant-wrapper">
210
						<h5>{{ data.l10n['variant'] }}</h5>
211
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
212
					</div>
213
				<# } #>
214
				<# if ( true === data.show_subsets ) { #>
215
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
216
						<h5>{{ data.l10n['subsets'] }}</h5>
217
						<select {{{ data.inputAttrs }}} class="subset" id="kirki-typography-subsets-{{{ data.id }}}"></select>
218
					</div>
219
				<# } #>
220
			<# } #>
221
222
			<# if ( data.default['font-size'] ) { #>
223
				<div class="font-size">
224
					<h5>{{ data.l10n['font-size'] }}</h5>
225
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
226
				</div>
227
			<# } #>
228
229
			<# if ( data.default['line-height'] ) { #>
230
				<div class="line-height">
231
					<h5>{{ data.l10n['line-height'] }}</h5>
232
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
233
				</div>
234
			<# } #>
235
236
			<# if ( data.default['letter-spacing'] ) { #>
237
				<div class="letter-spacing">
238
					<h5>{{ data.l10n['letter-spacing'] }}</h5>
239
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
240
				</div>
241
			<# } #>
242
243
			<# if ( data.default['word-spacing'] ) { #>
244
				<div class="word-spacing">
245
					<h5>{{ data.l10n['word-spacing'] }}</h5>
246
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
247
				</div>
248
			<# } #>
249
250
			<# if ( data.default['text-align'] ) { #>
251
				<div class="text-align">
252
					<h5>{{ data.l10n['text-align'] }}</h5>
253
					<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"<# } #>>
254
						<label for="{{ data.id }}-text-align-inherit">
255
							<span class="dashicons dashicons-editor-removeformatting"></span>
256
							<span class="screen-reader-text">{{ data.l10n['inherit'] }}</span>
257
						</label>
258
					</input>
259
					<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"<# } #>>
260
						<label for="{{ data.id }}-text-align-left">
261
							<span class="dashicons dashicons-editor-alignleft"></span>
262
							<span class="screen-reader-text">{{ data.l10n['left'] }}</span>
263
						</label>
264
					</input>
265
					<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"<# } #>>
266
						<label for="{{ data.id }}-text-align-center">
267
							<span class="dashicons dashicons-editor-aligncenter"></span>
268
							<span class="screen-reader-text">{{ data.l10n['center'] }}</span>
269
						</label>
270
					</input>
271
					<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"<# } #>>
272
						<label for="{{ data.id }}-text-align-right">
273
							<span class="dashicons dashicons-editor-alignright"></span>
274
							<span class="screen-reader-text">{{ data.l10n['right'] }}</span>
275
						</label>
276
					</input>
277
					<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"<# } #>>
278
						<label for="{{ data.id }}-text-align-justify">
279
							<span class="dashicons dashicons-editor-justify"></span>
280
							<span class="screen-reader-text">{{ data.l10n['justify'] }}</span>
281
						</label>
282
					</input>
283
				</div>
284
			<# } #>
285
286
			<# if ( data.default['text-transform'] ) { #>
287
				<div class="text-transform">
288
					<h5>{{ data.l10n['text-transform'] }}</h5>
289
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
290
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['none'] }}</option>
291
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['capitalize'] }}</option>
292
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['uppercase'] }}</option>
293
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['lowercase'] }}</option>
294
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['initial'] }}</option>
295
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>>{{ data.l10n['inherit'] }}</option>
296
					</select>
297
				</div>
298
			<# } #>
299
300
			<# if ( data.default['color'] ) { #>
301
				<div class="color">
302
					<h5>{{ data.l10n['color'] }}</h5>
303
					<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 }}} />
304
				</div>
305
			<# } #>
306
		</div>
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
		// Letter spacing was in px, now it requires units.
335
		if ( isset( $value['letter-spacing'] ) && is_numeric( $value['letter-spacing'] ) && $value['letter-spacing'] ) {
336
			$value['letter-spacing'] .= 'px';
337
		}
338
339
		$this->json['value'] = wp_parse_args( $value, $old_values );
340
341
		// Cleanup.
342
		if ( isset( $this->json['value']['font-weight'] ) ) {
343
			unset( $this->json['value']['font-weight'] );
344
		}
345
346
		// Make sure we use "subsets" instead of "subset".
347
		if ( isset( $this->json['value']['subset'] ) ) {
348
			if ( ! empty( $this->json['value']['subset'] ) ) {
349
				if ( ! isset( $this->json['value']['subsets'] ) || empty( $this->json['value']['subsets'] ) ) {
350
					$this->json['value']['subsets'] = $this->json['value']['subset'];
351
				}
352
			}
353
			unset( $this->json['value']['subset'] );
354
		}
355
	}
356
357
	/**
358
	 * Returns an array of translation strings.
359
	 *
360
	 * @access protected
361
	 * @since 2.4.0
362
	 * @param string|false $id The string-ID.
363
	 * @return string
364
	 */
365
	protected function l10n( $id = false ) {
366
		$translation_strings = array(
367
			'inherit'        => esc_attr__( 'Inherit', 'kirki' ),
368
			'font-family'    => esc_attr__( 'Font Family', 'kirki' ),
369
			'font-size'      => esc_attr__( 'Font Size', 'kirki' ),
370
			'line-height'    => esc_attr__( 'Line Height', 'kirki' ),
371
			'letter-spacing' => esc_attr__( 'Letter Spacing', 'kirki' ),
372
			'word-spacing'   => esc_attr__( 'Word Spacing', 'kirki' ),
373
			'left'           => esc_attr__( 'Left', 'kirki' ),
374
			'right'          => esc_attr__( 'Right', 'kirki' ),
375
			'center'         => esc_attr__( 'Center', 'kirki' ),
376
			'justify'        => esc_attr__( 'Justify', 'kirki' ),
377
			'color'          => esc_attr__( 'Color', 'kirki' ),
378
			'variant'        => esc_attr__( 'Variant', 'kirki' ),
379
			'subsets'        => esc_attr__( 'Subset', 'kirki' ),
380
			'text-align'     => esc_attr__( 'Text Align', 'kirki' ),
381
			'text-transform' => esc_attr__( 'Text Transform', 'kirki' ),
382
			'none'           => esc_attr__( 'None', 'kirki' ),
383
			'capitalize'     => esc_attr__( 'Capitalize', 'kirki' ),
384
			'uppercase'      => esc_attr__( 'Uppercase', 'kirki' ),
385
			'lowercase'      => esc_attr__( 'Lowercase', 'kirki' ),
386
			'initial'        => esc_attr__( 'Initial', 'kirki' ),
387
		);
388
		$translation_strings = apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
389
		if ( false === $id ) {
390
			return $translation_strings;
391
		}
392
		return $translation_strings[ $id ];
393
	}
394
395
	/**
396
	 * Render the control's content.
397
	 *
398
	 * @see WP_Customize_Control::render_content()
399
	 */
400
	protected function render_content() {}
401
}
402