Kirki_Control_Typography::content_template()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 159
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

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