Completed
Push — develop ( 441712...54f8dd )
by Aristeides
02:29
created

Kirki_Field_Typography::sanitize()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 76
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 40
nc 29161
nop 1
dl 0
loc 76
rs 2.5387
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Override field methods
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.2.7
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Typography extends Kirki_Field {
16
17
	/**
18
	 * Sets the control type.
19
	 *
20
	 * @access protected
21
	 */
22
	protected function set_type() {
23
24
		$this->type = 'kirki-typography';
25
26
	}
27
28
	/**
29
	 * Sets the default value.
30
	 *
31
	 * @access protected
32
	 */
33
	protected function set_default() {
34
35
		// Accomodate the use of font-weight and convert to variant.
36
		if ( isset( $this->default['font-weight'] ) ) {
37
			$this->default['variant'] = ( 'regular' === $this->default['font-weight'] ) ? 400 : (string) intval( $this->default['font-weight' ] );
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
38
		}
39
40
		// Make sure letter-spacing has units.
41
		if ( isset( $this->default['letter-spacing'] ) && is_numeric( $this->default['letter-spacing'] ) && $this->default['letter-spacing'] ) {
42
			$this->default['letter-spacing'] .= 'px';
43
		}
44
45
		// Make sure we use "subsets" instead of "subset".
46
		if ( isset( $this->default['subset'] ) && ! empty( $this->default['subset'] ) && ( ! isset( $this->default['subsets'] ) || empty( $this->default['subsets'] ) ) ) {
47
			$this->default['subsets'] = $this->default['subset'];
48
		}
49
		// var_dump( $this->default );
50
	}
51
52
	/**
53
	 * Sets the $sanitize_callback
54
	 *
55
	 * @access protected
56
	 */
57
	protected function set_sanitize_callback() {
58
59
		// If a custom sanitize_callback has been defined,
60
		// then we don't need to proceed any further.
61
		if ( ! empty( $this->sanitize_callback ) ) {
62
			return;
63
		}
64
		$this->sanitize_callback = array( __CLASS__, 'sanitize' );
65
66
	}
67
68
	/**
69
	 * Sets the $js_vars
70
	 *
71
	 * @access protected
72
	 */
73
	protected function set_js_vars() {
74
75
		if ( ! is_array( $this->js_vars ) ) {
76
			$this->js_vars = array();
77
		}
78
79
		// Check if transport is set to auto.
80
		// If not, then skip the auto-calculations and exit early.
81
		if ( 'auto' !== $this->transport ) {
82
			return;
83
		}
84
85
		// Set transport to refresh initially.
86
		// Serves as a fallback in case we failt to auto-calculate js_vars.
87
		$this->transport = 'refresh';
88
89
		$js_vars = array();
90
91
		// Try to auto-generate js_vars.
92
		// First we need to check if js_vars are empty, and that output is not empty.
93
		if ( ! empty( $this->output ) ) {
94
95
			// Start going through each item in the $output array.
96
			foreach ( $this->output as $output ) {
97
98
				// If 'element' or 'property' are not defined, skip this.
99
				if ( ! isset( $output['element'] ) ) {
100
					continue;
101
				}
102
				if ( is_array( $output['element'] ) ) {
103
					$output['element'] = implode( ',', $output['element'] );
104
				}
105
106
				// If we got this far, it's safe to add this.
107
				$js_vars[] = $output;
108
			}
109
110
			// Did we manage to get all the items from 'output'?
111
			// If not, then we're missing something so don't add this.
112
			if ( count( $js_vars ) !== count( $this->output ) ) {
113
				return;
114
			}
115
			$this->js_vars   = $js_vars;
116
			$this->transport = 'postMessage';
117
118
		}
119
120
	}
121
122
	/**
123
	 * Sanitizes typography controls
124
	 *
125
	 * @static
126
	 * @since 2.2.0
127
	 * @param array $value The value.
128
	 * @return array
129
	 */
130
	public static function sanitize( $value ) {
131
132
		if ( ! is_array( $value ) ) {
133
			return array();
134
		}
135
136
		// Escape the font-family.
137
		if ( isset( $value['font-family'] ) ) {
138
			$value['font-family'] = esc_attr( $value['font-family'] );
139
		}
140
141
		// Get variant from font-weight and font-style.
142
		if ( ! isset( $value['variant'] ) && isset( $value['font-weight'] ) ) {
143
			$value['variant'] = $value['font-weight'];
144
			if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) {
145
				$value['variant'] = ( '400' !== $value['font-weight'] || 400 !== $value['font-weight'] ) ? $value['variant'] . 'italic' : 'italic';
146
			}
147
		}
148
149
		// Use 'regular' instead of 400 for font-variant.
150
		if ( isset( $value['variant'] ) ) {
151
			$value['variant'] = ( 400 === $value['variant'] || '400' === $value['variant'] ) ? 'regular' : $value['variant'];
152
		}
153
154
		// Get font-weight from variant.
155
		if ( isset( $value['variant'] ) ) {
156
			$value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
157
			$value['font-weight'] = absint( $value['font-weight'] );
158
159
			if ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) {
160
				$value['font-weight'] = 400;
161
			}
162
		}
163
164
		// Get font-style from variant.
165
		if ( ! isset( $value['font-style'] ) && isset( $value['variant'] ) ) {
166
			$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic';
167
		}
168
169
		// Make sure the saved value is "subsets" (plural) and not "subset".
170
		// This is for compatibility with older versions.
171
		if ( isset( $value['subset'] ) ) {
172
			if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) {
173
				$value['subsets'] = $value['subset'];
174
			}
175
			unset( $value['subset'] );
176
		}
177
178
		// Make sure we're using a valid subset.
179
		if ( isset( $value['subsets'] ) ) {
180
			$valid_subsets = Kirki_Fonts::get_google_font_subsets();
181
			$subsets_ok = array();
182
			if ( is_array( $value['subsets'] ) ) {
183
				foreach ( $value['subsets'] as $subset ) {
184
					if ( array_key_exists( $subset, $valid_subsets ) ) {
185
						$subsets_ok[] = $subset;
186
					}
187
				}
188
				$value['subsets'] = $subsets_ok;
189
			}
190
		}
191
192
		foreach ( $value as $key => $subvalue ) {
193
194
			if ( in_array( $key, array( 'font-size', 'letter-spacing', 'word-spacing', 'line-height' ), true ) ) {
195
				$value[ $key ] = Kirki_Sanitize_Values::css_dimension( $value[ $key ] );
196
			} elseif ( 'text-align' === $key && ! in_array( $value['text-align'], array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
197
				$value['text-align'] = 'inherit';
198
			} elseif ( 'text-transform' === $key && ! in_array( $value['text-transform'], array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
199
				$value['text-transform'] = 'none';
200
			} elseif ( 'color' === $key ) {
201
				$value['color'] = ariColor::newColor( $value['color'] )->toCSS( 'hex' );
202
			}
203
		}
204
		return $value;
205
	}
206
207
	/**
208
	 * Sets the $choices
209
	 *
210
	 * @access protected
211
	 * @since 3.0.0
212
	 */
213
	protected function set_choices() {
214
215
		if ( ! is_array( $this->choices ) ) {
216
			$this->choices = array();
217
		}
218
		if ( ! isset( $this->choices['fonts'] ) ) {
219
			$this->choices['fonts'] = array();
220
		}
221
		if ( ! isset( $this->choices['fonts']['standard'] ) ) {
222
			$this->choices['fonts']['standard'] = array();
223
		}
224
		if ( ! isset( $this->choices['fonts']['google'] ) ) {
225
			$this->choices['fonts']['google'] = array();
226
		}
227
		if ( ! isset( $this->choices['variant'] ) ) {
228
			$this->choices['variant'] = array();
229
		}
230
	}
231
}
232