Completed
Pull Request — master (#1653)
by Aristeides
04:23 queued 02:04
created

Kirki_Control_Typography::enqueue()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 14
nop 0
dl 0
loc 13
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 Kirki_Control_Base {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-typography';
29
30
	/**
31
	 * Enqueue control related scripts/styles.
32
	 *
33
	 * @access public
34
	 */
35
	public function enqueue() {
36
37
		parent::enqueue();
38
39
		$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'] ) ) );
40
		$localize_script_var = ( $custom_fonts_array ) ? 'kirkiFonts' . $this->id : 'kirkiAllFonts';
41
		wp_localize_script(
42
			'kirki-script', $localize_script_var, array(
43
				'standard' => $this->get_standard_fonts(),
44
				'google'   => $this->get_google_fonts(),
45
			)
46
		);
47
	}
48
49
	/**
50
	 * Refresh the parameters passed to the JavaScript via JSON.
51
	 *
52
	 * @see WP_Customize_Control::to_json()
53
	 */
54
	public function to_json() {
55
		parent::to_json();
56
57
		if ( is_array( $this->json['value'] ) ) {
58
			foreach ( array_keys( $this->json['value'] ) as $key ) {
59
				if ( ! in_array( $key, array( 'variant', 'font-weight', 'font-style' ), true ) && ! isset( $this->json['default'][ $key ] ) ) {
60
					unset( $this->json['value'][ $key ] );
61
				}
62
				// Fix for https://wordpress.org/support/topic/white-font-after-updateing-to-3-0-16.
63
				if ( ! isset( $this->json['default'][ $key ] ) ) {
64
					unset( $this->json['value'][ $key ] );
65
				}
66
				// Fix for https://github.com/aristath/kirki/issues/1405.
67
				if ( isset( $this->json['default'][ $key ] ) && false === $this->json['default'][ $key ] ) {
68
					unset( $this->json['value'][ $key ] );
69
				}
70
			}
71
		}
72
73
		$this->json['show_variants'] = ( true === Kirki_Fonts_Google::$force_load_all_variants ) ? false : true;
74
		$this->json['show_subsets']  = ( true === Kirki_Fonts_Google::$force_load_all_subsets ) ? false : true;
75
		$this->json['languages']     = Kirki_Fonts::get_google_font_subsets();
76
	}
77
78
	/**
79
	 * An Underscore (JS) template for this control's content (but not its container).
80
	 *
81
	 * Class variables for this control class are available in the `data` JS object;
82
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
83
	 *
84
	 * @see WP_Customize_Control::print_template()
85
	 *
86
	 * @access protected
87
	 */
88
	protected function content_template() {
89
		?>
90
		<label class="customizer-text">
91
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
92
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
93
		</label>
94
95
		<div class="wrapper">
96
97
			<# if ( data.default['font-family'] ) { #>
98
				<# data.value['font-family'] = data.value['font-family'] || data['default']['font-family']; #>
99
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
100
				<div class="font-family">
101
					<h5><?php esc_attr_e( 'Font Family', 'kirki' ); ?></h5>
102
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
103
				</div>
104
				<# if ( ! _.isUndefined( data.choices['font-backup'] ) && true === data.choices['font-backup'] ) { #>
105
					<div class="font-backup hide-on-standard-fonts kirki-font-backup-wrapper">
106
						<h5><?php esc_attr_e( 'Backup Font', 'kirki' ); ?></h5>
107
						<select {{{ data.inputAttrs }}} id="kirki-typography-font-backup-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
108
					</div>
109
				<# } #>
110
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
111
					<div class="variant kirki-variant-wrapper">
112
						<h5><?php esc_attr_e( 'Variant', 'kirki' ); ?></h5>
113
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
114
					</div>
115
				<# } #>
116
				<# if ( true === data.show_subsets ) { #>
117
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
118
						<h5><?php esc_attr_e( 'Subset(s)', 'kirki' ); ?></h5>
119
						<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<# } #>>
120
							<# _.each( data.value.subsets, function( subset ) { #>
121
								<option value="{{ subset }}" selected="selected">{{ data.languages[ subset ] }}</option>
122
							<# } ); #>
123
						</select>
124
					</div>
125
				<# } #>
126
			<# } #>
127
128
			<# if ( data.default['font-size'] ) { #>
129
				<# data.value['font-size'] = data.value['font-size'] || data['default']['font-size']; #>
130
				<div class="font-size">
131
					<h5><?php esc_attr_e( 'Font Size', 'kirki' ); ?></h5>
132
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
133
				</div>
134
			<# } #>
135
136
			<# if ( data.default['line-height'] ) { #>
137
				<# data.value['line-height'] = data.value['line-height'] || data['default']['line-height']; #>
138
				<div class="line-height">
139
					<h5><?php esc_attr_e( 'Line Height', 'kirki' ); ?></h5>
140
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
141
				</div>
142
			<# } #>
143
144
			<# if ( data.default['letter-spacing'] ) { #>
145
				<# data.value['letter-spacing'] = data.value['letter-spacing'] || data['default']['letter-spacing']; #>
146
				<div class="letter-spacing">
147
					<h5><?php esc_attr_e( 'Letter Spacing', 'kirki' ); ?></h5>
148
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
149
				</div>
150
			<# } #>
151
152
			<# if ( data.default['word-spacing'] ) { #>
153
				<# data.value['word-spacing'] = data.value['word-spacing'] || data['default']['word-spacing']; #>
154
				<div class="word-spacing">
155
					<h5><?php esc_attr_e( 'Word Spacing', 'kirki' ); ?></h5>
156
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
157
				</div>
158
			<# } #>
159
160
			<# if ( data.default['text-align'] ) { #>
161
				<# data.value['text-align'] = data.value['text-align'] || data['default']['text-align']; #>
162
				<div class="text-align">
163
					<h5><?php esc_attr_e( 'Text Align', 'kirki' ); ?></h5>
164
					<div class="text-align-choices">
165
						<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"<# } #>>
166
							<label for="{{ data.id }}-text-align-inherit">
167
								<span class="dashicons dashicons-editor-removeformatting"></span>
168
								<span class="screen-reader-text"><?php esc_attr_e( 'Inherit', 'kirki' ); ?></span>
169
							</label>
170
						</input>
171
						<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"<# } #>>
172
							<label for="{{ data.id }}-text-align-left">
173
								<span class="dashicons dashicons-editor-alignleft"></span>
174
								<span class="screen-reader-text"><?php esc_attr_e( 'Left', 'kirki' ); ?></span>
175
							</label>
176
						</input>
177
						<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"<# } #>>
178
							<label for="{{ data.id }}-text-align-center">
179
								<span class="dashicons dashicons-editor-aligncenter"></span>
180
								<span class="screen-reader-text"><?php esc_attr_e( 'Center', 'kirki' ); ?></span>
181
							</label>
182
						</input>
183
						<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"<# } #>>
184
							<label for="{{ data.id }}-text-align-right">
185
								<span class="dashicons dashicons-editor-alignright"></span>
186
								<span class="screen-reader-text"><?php esc_attr_e( 'Right', 'kirki' ); ?></span>
187
							</label>
188
						</input>
189
						<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"<# } #>>
190
							<label for="{{ data.id }}-text-align-justify">
191
								<span class="dashicons dashicons-editor-justify"></span>
192
								<span class="screen-reader-text"><?php esc_attr_e( 'Justify', 'kirki' ); ?></span>
193
							</label>
194
						</input>
195
					</div>
196
				</div>
197
			<# } #>
198
199
			<# if ( data.default['text-transform'] ) { #>
200
				<# data.value['text-transform'] = data.value['text-transform'] || data['default']['text-transform']; #>
201
				<div class="text-transform">
202
					<h5><?php esc_attr_e( 'Text Transform', 'kirki' ); ?></h5>
203
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
204
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'None', 'kirki' ); ?></option>
205
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Capitalize', 'kirki' ); ?></option>
206
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Uppercase', 'kirki' ); ?></option>
207
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Lowercase', 'kirki' ); ?></option>
208
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Initial', 'kirki' ); ?></option>
209
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Inherit', 'kirki' ); ?></option>
210
					</select>
211
				</div>
212
			<# } #>
213
214
			<# if ( data.default['text-decoration'] ) { #>
215
				<# data.value['text-decoration'] = data.value['text-decoration'] || data['default']['text-decoration']; #>
216
				<div class="text-decoration">
217
					<h5><?php esc_attr_e( 'Text Decoration', 'kirki' ); ?></h5>
218
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-decoration-{{{ data.id }}}">
219
						<option value="none"<# if ( 'none' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'None', 'kirki' ); ?></option>
220
						<option value="underline"<# if ( 'underline' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'Underline', 'kirki' ); ?></option>
221
						<option value="overline"<# if ( 'overline' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'Overline', 'kirki' ); ?></option>
222
						<option value="line-through"<# if ( 'line-through' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'Line-Through', 'kirki' ); ?></option>
223
						<option value="initial"<# if ( 'initial' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'Initial', 'kirki' ); ?></option>
224
						<option value="inherit"<# if ( 'inherit' === data.value['text-decoration'] ) { #>selected<# } #>><?php esc_attr_e( 'Inherit', 'kirki' ); ?></option>
225
					</select>
226
				</div>
227
			<# } #>
228
229
			<# if ( data.default['margin-top'] ) { #>
230
				<# data.value['margin-top'] = data.value['margin-top'] || data['default']['margin-top']; #>
231
				<div class="margin-top">
232
					<h5><?php esc_attr_e( 'Margin Top', 'kirki' ); ?></h5>
233
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-top'] }}"/>
234
				</div>
235
			<# } #>
236
237
			<# if ( data.default['margin-bottom'] ) { #>
238
				<# data.value['margin-bottom'] = data.value['margin-bottom'] || data['default']['margin-bottom']; #>
239
				<div class="margin-bottom">
240
					<h5><?php esc_attr_e( 'Margin Bottom', 'kirki' ); ?></h5>
241
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-bottom'] }}"/>
242
				</div>
243
			<# } #>
244
245
			<# if ( false !== data.default['color'] && data.default['color'] ) { #>
246
				<# data.value['color'] = data.value['color'] || data['default']['color']; #>
247
				<div class="color">
248
					<h5><?php esc_attr_e( 'Color', 'kirki' ); ?></h5>
249
					<input {{{ data.inputAttrs }}} type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="kirki-color-control"/>
250
				</div>
251
			<# } #>
252
253
		</div>
254
		<?php if ( Kirki_Util::get_wp_version() >= 4.9 ) : ?>
255
			<input class="typography-hidden-value" type="hidden" {{{ data.link }}}>
256
			<?php return; ?>
257
		<?php endif; ?>
1 ignored issue
show
Unused Code introduced by
?> <?php does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
258
		<#
259
		if ( ! _.isUndefined( data.value['font-family'] ) ) {
260
			data.value['font-family'] = data.value['font-family'].replace( /&quot;/g, '&#39' );
261
		}
262
		valueJSON = JSON.stringify( data.value ).replace( /'/g, '&#39' );
263
		#>
264
		<input class="typography-hidden-value" type="hidden" value='{{{ valueJSON }}}' {{{ data.link }}}>
265
		<?php
266
	}
267
268
	/**
269
	 * Formats variants.
270
	 *
271
	 * @access protected
272
	 * @since 3.0.0
273
	 * @param array $variants The variants.
274
	 * @return array
275
	 */
276
	protected function format_variants_array( $variants ) {
277
278
		$all_variants = Kirki_Fonts::get_all_variants();
279
		$final_variants = array();
280
		foreach ( $variants as $variant ) {
281
			if ( is_string( $variant ) ) {
282
				$final_variants[] = array(
283
					'id'    => $variant,
284
					'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant,
285
				);
286
			} elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) {
287
				$final_variants[] = $variant;
288
			}
289
		}
290
		return $final_variants;
291
	}
292
293
	/**
294
	 * Gets standard fonts properly formatted for our control.
295
	 *
296
	 * @access protected
297
	 * @since 3.0.0
298
	 * @return array
299
	 */
300
	protected function get_standard_fonts() {
301
		// Add fonts to our JS objects.
302
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
303
304
		$std_user_keys = array();
305
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['standard'] ) ) {
306
			$std_user_keys = $this->choices['fonts']['standard'];
307
		}
308
309
		$standard_fonts_final = array();
310
		$default_variants = $this->format_variants_array(
311
			array(
312
				'regular',
313
				'italic',
314
				'700',
315
				'700italic',
316
			)
317
		);
318
		foreach ( $standard_fonts as $key => $font ) {
319
			if ( ( ! empty( $std_user_keys ) && ! in_array( $key, $std_user_keys, true ) ) || ! isset( $font['stack'] ) || ! isset( $font['label'] ) ) {
320
				continue;
321
			}
322
			$standard_fonts_final[] = array(
323
				'family'      => $font['stack'],
324
				'label'       => $font['label'],
325
				'subsets'     => array(),
326
				'is_standard' => true,
327
				'variants'    => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants,
328
			);
329
		}
330
		return $standard_fonts_final;
331
	}
332
333
	/**
334
	 * Gets google fonts properly formatted for our control.
335
	 *
336
	 * @access protected
337
	 * @since 3.0.0
338
	 * @return array
339
	 */
340
	protected function get_google_fonts() {
341
		// Add fonts to our JS objects.
342
		$google_fonts = Kirki_Fonts::get_google_fonts();
343
		$all_variants = Kirki_Fonts::get_all_variants();
344
		$all_subsets  = Kirki_Fonts::get_google_font_subsets();
345
346
		$gf_user_keys = array();
347
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['google'] ) ) {
348
			$gf_user_keys = $this->choices['fonts']['google'];
349
		}
350
351
		$google_fonts_final = array();
352
		foreach ( $google_fonts as $family => $args ) {
353
			if ( ! empty( $gf_user_keys ) && ! in_array( $family, $gf_user_keys, true ) ) {
354
				continue;
355
			}
356
357
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
358
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
359
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
360
361
			$available_variants = array();
362
			if ( is_array( $variants ) ) {
363
				foreach ( $variants as $variant ) {
364
					if ( array_key_exists( $variant, $all_variants ) ) {
365
						$available_variants[] = array(
366
							'id' => $variant,
367
							'label' => $all_variants[ $variant ],
368
						);
369
					}
370
				}
371
			}
372
373
			$available_subsets = array();
374
			if ( is_array( $subsets ) ) {
375
				foreach ( $subsets as $subset ) {
376
					if ( array_key_exists( $subset, $all_subsets ) ) {
377
						$available_subsets[] = array(
378
							'id' => $subset,
379
							'label' => $all_subsets[ $subset ],
380
						);
381
					}
382
				}
383
			}
384
385
			$google_fonts_final[] = array(
386
				'family'       => $family,
387
				'label'        => $label,
388
				'variants'     => $available_variants,
389
				'subsets'      => $available_subsets,
390
			);
391
		} // End foreach().
392
		return $google_fonts_final;
393
	}
394
}
395