Completed
Pull Request — develop (#1603)
by Aristeides
02:18
created

Kirki_Control_Typography::content_template()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 163
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 35
nc 2
nop 0
dl 0
loc 163
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		$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'] ) ) );
38
		$localize_script_var = ( $custom_fonts_array ) ? 'kirkiFonts' . $this->id : 'kirkiAllFonts';
39
		wp_localize_script( 'kirki-script', $localize_script_var, array(
40
			'standard' => $this->get_standard_fonts(),
41
			'google'   => $this->get_google_fonts(),
42
		) );
43
	}
44
45
	/**
46
	 * Refresh the parameters passed to the JavaScript via JSON.
47
	 *
48
	 * @see WP_Customize_Control::to_json()
49
	 */
50
	public function to_json() {
51
		parent::to_json();
52
53
		foreach ( array_keys( $this->json['value'] ) as $key ) {
54
			if ( ! in_array( $key, array( 'variant', 'font-weight', 'font-style' ) ) && ! isset( $this->json['default'][ $key ] ) ) {
55
				unset( $this->json['value'][ $key ] );
56
			}
57
		}
58
59
		// Fix for https://github.com/aristath/kirki/issues/1405.
60
		foreach ( array_keys( $this->json['value'] ) as $key ) {
61
			if ( isset( $this->json['default'][ $key ] ) && false === $this->json['default'][ $key ] ) {
62
				unset( $this->json['value'][ $key ] );
63
			}
64
		}
65
		$this->json['show_variants'] = ( true === Kirki_Fonts_Google::$force_load_all_variants ) ? false : true;
66
		$this->json['show_subsets']  = ( true === Kirki_Fonts_Google::$force_load_all_subsets ) ? false : true;
67
		$this->json['languages']     = Kirki_Fonts::get_google_font_subsets();
68
	}
69
70
	/**
71
	 * An Underscore (JS) template for this control's content (but not its container).
72
	 *
73
	 * Class variables for this control class are available in the `data` JS object;
74
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
75
	 *
76
	 * @see WP_Customize_Control::print_template()
77
	 *
78
	 * @access protected
79
	 */
80
	protected function content_template() {
81
		?>
82
		<label class="customizer-text">
83
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
84
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
85
		</label>
86
87
		<div class="wrapper">
88
89
			<# if ( data.default['font-family'] ) { #>
90
				<# data.value['font-family'] = data.value['font-family'] || data['default']['font-family']; #>
91
				<# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
92
				<div class="font-family">
93
					<h5><?php esc_attr_e( 'Font Family', 'kirki' ); ?></h5>
94
					<select {{{ data.inputAttrs }}} id="kirki-typography-font-family-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
95
				</div>
96
				<# if ( ! _.isUndefined( data.choices['font-backup'] ) && true === data.choices['font-backup'] ) { #>
97
					<div class="font-backup hide-on-standard-fonts kirki-font-backup-wrapper">
98
						<h5><?php esc_attr_e( 'Backup Font', 'kirki' ); ?></h5>
99
						<select {{{ data.inputAttrs }}} id="kirki-typography-font-backup-{{{ data.id }}}" placeholder="<?php esc_attr_e( 'Select Font Family', 'kirki' ); ?>"></select>
100
					</div>
101
				<# } #>
102
				<# if ( true === data.show_variants || false !== data.default.variant ) { #>
103
					<div class="variant kirki-variant-wrapper">
104
						<h5><?php esc_attr_e( 'Variant', 'kirki' ); ?></h5>
105
						<select {{{ data.inputAttrs }}} class="variant" id="kirki-typography-variant-{{{ data.id }}}"></select>
106
					</div>
107
				<# } #>
108
				<# if ( true === data.show_subsets ) { #>
109
					<div class="subsets hide-on-standard-fonts kirki-subsets-wrapper">
110
						<h5><?php esc_attr_e( 'Subset(s)', 'kirki' ); ?></h5>
111
						<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<# } #>>
112
							<# _.each( data.value.subsets, function( subset ) { #>
113
								<option value="{{ subset }}" selected="selected">{{ data.languages[ subset ] }}</option>
114
							<# } ); #>
115
						</select>
116
					</div>
117
				<# } #>
118
			<# } #>
119
120
			<# if ( data.default['font-size'] ) { #>
121
				<# data.value['font-size'] = data.value['font-size'] || data['default']['font-size']; #>
122
				<div class="font-size">
123
					<h5><?php esc_attr_e( 'Font Size', 'kirki' ); ?></h5>
124
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['font-size'] }}"/>
125
				</div>
126
			<# } #>
127
128
			<# if ( data.default['line-height'] ) { #>
129
				<# data.value['line-height'] = data.value['line-height'] || data['default']['line-height']; #>
130
				<div class="line-height">
131
					<h5><?php esc_attr_e( 'Line Height', 'kirki' ); ?></h5>
132
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['line-height'] }}"/>
133
				</div>
134
			<# } #>
135
136
			<# if ( data.default['letter-spacing'] ) { #>
137
				<# data.value['letter-spacing'] = data.value['letter-spacing'] || data['default']['letter-spacing']; #>
138
				<div class="letter-spacing">
139
					<h5><?php esc_attr_e( 'Letter Spacing', 'kirki' ); ?></h5>
140
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['letter-spacing'] }}"/>
141
				</div>
142
			<# } #>
143
144
			<# if ( data.default['word-spacing'] ) { #>
145
				<# data.value['word-spacing'] = data.value['word-spacing'] || data['default']['word-spacing']; #>
146
				<div class="word-spacing">
147
					<h5><?php esc_attr_e( 'Word Spacing', 'kirki' ); ?></h5>
148
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['word-spacing'] }}"/>
149
				</div>
150
			<# } #>
151
152
			<# if ( data.default['text-align'] ) { #>
153
				<# data.value['text-align'] = data.value['text-align'] || data['default']['text-align']; #>
154
				<div class="text-align">
155
					<h5><?php esc_attr_e( 'Text Align', 'kirki' ); ?></h5>
156
					<div class="text-align-choices">
157
						<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"<# } #>>
158
							<label for="{{ data.id }}-text-align-inherit">
159
								<span class="dashicons dashicons-editor-removeformatting"></span>
160
								<span class="screen-reader-text"><?php esc_attr_e( 'Inherit', 'kirki' ); ?></span>
161
							</label>
162
						</input>
163
						<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"<# } #>>
164
							<label for="{{ data.id }}-text-align-left">
165
								<span class="dashicons dashicons-editor-alignleft"></span>
166
								<span class="screen-reader-text"><?php esc_attr_e( 'Left', 'kirki' ); ?></span>
167
							</label>
168
						</input>
169
						<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"<# } #>>
170
							<label for="{{ data.id }}-text-align-center">
171
								<span class="dashicons dashicons-editor-aligncenter"></span>
172
								<span class="screen-reader-text"><?php esc_attr_e( 'Center', 'kirki' ); ?></span>
173
							</label>
174
						</input>
175
						<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"<# } #>>
176
							<label for="{{ data.id }}-text-align-right">
177
								<span class="dashicons dashicons-editor-alignright"></span>
178
								<span class="screen-reader-text"><?php esc_attr_e( 'Right', 'kirki' ); ?></span>
179
							</label>
180
						</input>
181
						<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"<# } #>>
182
							<label for="{{ data.id }}-text-align-justify">
183
								<span class="dashicons dashicons-editor-justify"></span>
184
								<span class="screen-reader-text"><?php esc_attr_e( 'Justify', 'kirki' ); ?></span>
185
							</label>
186
						</input>
187
					</div>
188
				</div>
189
			<# } #>
190
191
			<# if ( data.default['text-transform'] ) { #>
192
				<# data.value['text-transform'] = data.value['text-transform'] || data['default']['text-transform']; #>
193
				<div class="text-transform">
194
					<h5><?php esc_attr_e( 'Text Transform', 'kirki' ); ?></h5>
195
					<select {{{ data.inputAttrs }}} id="kirki-typography-text-transform-{{{ data.id }}}">
196
						<option value="none"<# if ( 'none' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'None', 'kirki' ); ?></option>
197
						<option value="capitalize"<# if ( 'capitalize' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Capitalize', 'kirki' ); ?></option>
198
						<option value="uppercase"<# if ( 'uppercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Uppercase', 'kirki' ); ?></option>
199
						<option value="lowercase"<# if ( 'lowercase' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Lowercase', 'kirki' ); ?></option>
200
						<option value="initial"<# if ( 'initial' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Initial', 'kirki' ); ?></option>
201
						<option value="inherit"<# if ( 'inherit' === data.value['text-transform'] ) { #>selected<# } #>><?php esc_attr_e( 'Inherit', 'kirki' ); ?></option>
202
					</select>
203
				</div>
204
			<# } #>
205
206
			<# if ( false !== data.default['color'] && data.default['color'] ) { #>
207
				<# data.value['color'] = data.value['color'] || data['default']['color']; #>
208
				<div class="color">
209
					<h5><?php esc_attr_e( 'Color', 'kirki' ); ?></h5>
210
					<input {{{ data.inputAttrs }}} type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="kirki-color-control"/>
211
				</div>
212
			<# } #>
213
214
			<# if ( data.default['margin-top'] ) { #>
215
				<# data.value['margin-top'] = data.value['margin-top'] || data['default']['margin-top']; #>
216
				<div class="margin-top">
217
					<h5><?php esc_attr_e( 'Margin Top', 'kirki' ); ?></h5>
218
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-top'] }}"/>
219
				</div>
220
			<# } #>
221
222
			<# if ( data.default['margin-bottom'] ) { #>
223
				<# data.value['margin-bottom'] = data.value['margin-bottom'] || data['default']['margin-bottom']; #>
224
				<div class="margin-bottom">
225
					<h5><?php esc_attr_e( 'Margin Bottom', 'kirki' ); ?></h5>
226
					<input {{{ data.inputAttrs }}} type="text" value="{{ data.value['margin-bottom'] }}"/>
227
				</div>
228
			<# } #>
229
		</div>
230
		<?php if ( Kirki_Util::get_wp_version() >= 4.9 ) : ?>
231
			<input class="typography-hidden-value" type="hidden" {{{ data.link }}}>
232
		<?php else : ?>
233
			<#
234
			if ( ! _.isUndefined( data.value['font-family'] ) ) {
235
				data.value['font-family'] = data.value['font-family'].replace( /&quot;/g, '&#39' );
236
			}
237
			valueJSON = JSON.stringify( data.value ).replace( /'/g, '&#39' );
238
			#>
239
			<input class="typography-hidden-value" type="hidden" value='{{{ valueJSON }}}' {{{ data.link }}}>
240
		<?php endif; ?>
241
		<?php
242
	}
243
244
	/**
245
	 * Formats variants.
246
	 *
247
	 * @access protected
248
	 * @since 3.0.0
249
	 * @param array $variants The variants.
250
	 * @return array
251
	 */
252
	protected function format_variants_array( $variants ) {
253
254
		$all_variants = Kirki_Fonts::get_all_variants();
255
		$final_variants = array();
256
		foreach ( $variants as $variant ) {
257
			if ( is_string( $variant ) ) {
258
				$final_variants[] = array(
259
					'id'    => $variant,
260
					'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant,
261
				);
262
			} elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) {
263
				$final_variants[] = $variant;
264
			}
265
		}
266
		return $final_variants;
267
	}
268
269
	/**
270
	 * Gets standard fonts properly formatted for our control.
271
	 *
272
	 * @access protected
273
	 * @since 3.0.0
274
	 * @return array
275
	 */
276
	protected function get_standard_fonts() {
277
		// Add fonts to our JS objects.
278
		$standard_fonts = Kirki_Fonts::get_standard_fonts();
279
280
		$std_user_keys = array();
281
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['standard'] ) ) {
282
			$std_user_keys = $this->choices['fonts']['standard'];
283
		}
284
285
		$standard_fonts_final = array();
286
		$default_variants = $this->format_variants_array( array(
287
			'regular',
288
			'italic',
289
			'700',
290
			'700italic',
291
		) );
292
		foreach ( $standard_fonts as $key => $font ) {
293
			if ( ( ! empty( $std_user_keys ) && ! in_array( $key, $std_user_keys, true ) ) || ! isset( $font['stack'] ) || ! isset( $font['label'] ) ) {
294
				continue;
295
			}
296
			$standard_fonts_final[] = array(
297
				'family'      => $font['stack'],
298
				'label'       => $font['label'],
299
				'subsets'     => array(),
300
				'is_standard' => true,
301
				'variants'    => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants,
302
			);
303
		}
304
		return $standard_fonts_final;
305
	}
306
307
	/**
308
	 * Gets google fonts properly formatted for our control.
309
	 *
310
	 * @access protected
311
	 * @since 3.0.0
312
	 * @return array
313
	 */
314
	protected function get_google_fonts() {
315
		// Add fonts to our JS objects.
316
		$google_fonts = Kirki_Fonts::get_google_fonts();
317
		$all_variants = Kirki_Fonts::get_all_variants();
318
		$all_subsets  = Kirki_Fonts::get_google_font_subsets();
319
320
		$gf_user_keys = array();
321
		if ( isset( $this->choices['fonts'] ) && isset( $this->choices['fonts']['google'] ) ) {
322
			$gf_user_keys = $this->choices['fonts']['google'];
323
		}
324
325
		$google_fonts_final = array();
326
		foreach ( $google_fonts as $family => $args ) {
327
			if ( ! empty( $gf_user_keys ) && ! in_array( $family, $gf_user_keys, true ) ) {
328
				continue;
329
			}
330
331
			$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
332
			$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
333
			$subsets  = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array();
334
335
			$available_variants = array();
336
			if ( is_array( $variants ) ) {
337
				foreach ( $variants as $variant ) {
338
					if ( array_key_exists( $variant, $all_variants ) ) {
339
						$available_variants[] = array(
340
							'id' => $variant,
341
							'label' => $all_variants[ $variant ],
342
						);
343
					}
344
				}
345
			}
346
347
			$available_subsets = array();
348
			if ( is_array( $subsets ) ) {
349
				foreach ( $subsets as $subset ) {
350
					if ( array_key_exists( $subset, $all_subsets ) ) {
351
						$available_subsets[] = array(
352
							'id' => $subset,
353
							'label' => $all_subsets[ $subset ],
354
						);
355
					}
356
				}
357
			}
358
359
			$google_fonts_final[] = array(
360
				'family'       => $family,
361
				'label'        => $label,
362
				'variants'     => $available_variants,
363
				'subsets'      => $available_subsets,
364
			);
365
		} // End foreach().
366
		return $google_fonts_final;
367
	}
368
}
369