Completed
Push — develop ( 54f8dd...488f1b )
by Aristeides
03:58
created

Kirki_Field_Typography::sanitize()   C

Complexity

Conditions 33
Paths 47

Size

Total Lines 75
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 53
nc 47
nop 1
dl 0
loc 75
rs 5.1609
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
		foreach ( $value as $key => $val ) {
137
			switch ( $key ) {
138
				case 'font-family':
139
					$value['font-family'] = esc_attr( $val );
140
					break;
141
				case 'font-weight':
142
					if ( ! isset( $value['variant'] ) ) {
143
						$value['variant'] = $val;
144
						if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) {
145
							$value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic';
146
						}
147
					}
148
					break;
149
				case 'variant':
150
					// Use 'regular' instead of 400 for font-variant.
151
					$value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val;
152
					// Get font-weight from variant.
153
					$value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
154
					$value['font-weight'] = absint( $value['font-weight'] );
155
					if ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) {
156
						$value['font-weight'] = 400;
157
					}
158
					// Get font-style from variant.
159
					if ( ! isset( $value['font-style'] ) ) {
160
						$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic';
161
					}
162
					break;
163
				case 'subset':
164
					// Make sure the saved value is "subsets" (plural) and not "subset".
165
					// This is for compatibility with older versions.
166
					if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) {
167
						$value['subsets'] = $value['subset'];
168
					}
169
					unset( $value['subset'] );
170
					// Make sure we're using a valid subset.
171
					$valid_subsets = Kirki_Fonts::get_google_font_subsets();
172
					$subsets_ok = array();
173
					if ( is_array( $value['subsets'] ) ) {
174
						foreach ( $value['subsets'] as $subset ) {
175
							if ( array_key_exists( $subset, $valid_subsets ) ) {
176
								$subsets_ok[] = $subset;
177
							}
178
						}
179
						$value['subsets'] = $subsets_ok;
180
					}
181
					break;
182
				case 'font-size':
183
				case 'letter-spacing':
184
				case 'word-spacing':
185
				case 'line-height':
186
					$value[ $key ] = Kirki_Sanitize_Values::css_dimension( $val );
187
					break;
188
				case 'text-align':
189
				 	if ( ! in_array( $val, array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
190
						$value['text-align'] = 'inherit';
191
					}
192
					break;
193
				case 'text-transform':
194
					if ( ! in_array( $val, array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
195
						$value['text-transform'] = 'none';
196
					}
197
					break;
198
				case 'color':
199
					$value['color'] = ariColor::newColor( $val )->toCSS( 'hex' );
200
					break;
201
			}
202
		}
203
		return $value;
204
	}
205
206
	/**
207
	 * Sets the $choices
208
	 *
209
	 * @access protected
210
	 * @since 3.0.0
211
	 */
212
	protected function set_choices() {
213
214
		if ( ! is_array( $this->choices ) ) {
215
			$this->choices = array();
216
		}
217
		$this->choices = wp_parse_args( $this->choices, array(
218
			'variant' => array(),
219
			'fonts'   => array(
220
				'standard' => array(),
221
				'google'   => array(),
222
			),
223
		) );
224
	}
225
}
226