Completed
Pull Request — master (#1451)
by Aristeides
05:07 queued 02:20
created

Kirki_Field_Typography::sanitize()   C

Complexity

Conditions 32
Paths 73

Size

Total Lines 72
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
eloc 52
nc 73
nop 1
dl 0
loc 72
rs 5.3175
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'] );
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
	}
50
51
	/**
52
	 * Sets the $sanitize_callback
53
	 *
54
	 * @access protected
55
	 */
56
	protected function set_sanitize_callback() {
57
58
		// If a custom sanitize_callback has been defined,
59
		// then we don't need to proceed any further.
60
		if ( ! empty( $this->sanitize_callback ) ) {
61
			return;
62
		}
63
		$this->sanitize_callback = array( __CLASS__, 'sanitize' );
64
65
	}
66
67
	/**
68
	 * Sets the $js_vars
69
	 *
70
	 * @access protected
71
	 */
72
	protected function set_js_vars() {
73
74
		if ( ! is_array( $this->js_vars ) ) {
75
			$this->js_vars = array();
76
		}
77
78
		// Check if transport is set to auto.
79
		// If not, then skip the auto-calculations and exit early.
80
		if ( 'auto' !== $this->transport ) {
81
			return;
82
		}
83
84
		// Set transport to refresh initially.
85
		// Serves as a fallback in case we failt to auto-calculate js_vars.
86
		$this->transport = 'refresh';
87
88
		$js_vars = array();
89
90
		// Try to auto-generate js_vars.
91
		// First we need to check if js_vars are empty, and that output is not empty.
92
		if ( ! empty( $this->output ) ) {
93
94
			// Start going through each item in the $output array.
95
			foreach ( $this->output as $output ) {
96
97
				// If 'element' or 'property' are not defined, skip this.
98
				if ( ! isset( $output['element'] ) ) {
99
					continue;
100
				}
101
				if ( is_array( $output['element'] ) ) {
102
					$output['element'] = implode( ',', $output['element'] );
103
				}
104
105
				// If we got this far, it's safe to add this.
106
				$js_vars[] = $output;
107
			}
108
109
			// Did we manage to get all the items from 'output'?
110
			// If not, then we're missing something so don't add this.
111
			if ( count( $js_vars ) !== count( $this->output ) ) {
112
				return;
113
			}
114
			$this->js_vars   = $js_vars;
115
			$this->transport = 'postMessage';
116
117
		}
118
119
	}
120
121
	/**
122
	 * Sanitizes typography controls
123
	 *
124
	 * @static
125
	 * @since 2.2.0
126
	 * @param array $value The value.
127
	 * @return array
128
	 */
129
	public static function sanitize( $value ) {
130
131
		if ( ! is_array( $value ) ) {
132
			return array();
133
		}
134
135
		foreach ( $value as $key => $val ) {
136
			switch ( $key ) {
137
				case 'font-family':
138
					$value['font-family'] = esc_attr( $val );
139
					break;
140
				case 'font-weight':
141
					if ( isset( $value['variant'] ) ) {
142
						break;
143
					}
144
					$value['variant'] = $val;
145
					if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) {
146
						$value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic';
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'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] );
155
					// Get font-style from variant.
156
					if ( ! isset( $value['font-style'] ) ) {
157
						$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic';
158
					}
159
					break;
160
				case 'subset':
161
					// Make sure the saved value is "subsets" (plural) and not "subset".
162
					// This is for compatibility with older versions.
163
					if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) {
164
						$value['subsets'] = $value['subset'];
165
					}
166
					unset( $value['subset'] );
167
					// Make sure we're using a valid subset.
168
					$valid_subsets = Kirki_Fonts::get_google_font_subsets();
169
					$subsets_ok = array();
170
					$value['subsets'] = (array) $value['subsets'];
171
					foreach ( $value['subsets'] as $subset ) {
172
						if ( array_key_exists( $subset, $valid_subsets ) ) {
173
							$subsets_ok[] = $subset;
174
						}
175
					}
176
					$value['subsets'] = $subsets_ok;
177
					break;
178
				case 'font-size':
179
				case 'letter-spacing':
180
				case 'word-spacing':
181
				case 'line-height':
182
					$value[ $key ] = Kirki_Sanitize_Values::css_dimension( $val );
183
					break;
184
				case 'text-align':
185
					if ( ! in_array( $val, array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
186
						$value['text-align'] = 'inherit';
187
					}
188
					break;
189
				case 'text-transform':
190
					if ( ! in_array( $val, array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
191
						$value['text-transform'] = 'none';
192
					}
193
					break;
194
				case 'color':
195
					$value['color'] = ariColor::newColor( $val )->toCSS( 'hex' );
196
					break;
197
			} // End switch().
198
		} // End foreach().
199
		return $value;
200
	}
201
202
	/**
203
	 * Sets the $choices
204
	 *
205
	 * @access protected
206
	 * @since 3.0.0
207
	 */
208
	protected function set_choices() {
209
210
		if ( ! is_array( $this->choices ) ) {
211
			$this->choices = array();
212
		}
213
		$this->choices = wp_parse_args( $this->choices, array(
214
			'variant' => array(),
215
			'fonts'   => array(
216
				'standard' => array(),
217
				'google'   => array(),
218
			),
219
		) );
220
	}
221
}
222