Completed
Pull Request — master (#1451)
by Aristeides
03:13
created

Kirki_Field_Typography   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 210
rs 5.5555
c 0
b 0
f 0
wmc 56
lcom 5
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A set_type() 0 5 1
B set_default() 0 17 10
A set_sanitize_callback() 0 10 2
C set_js_vars() 0 48 8
C sanitize() 0 75 33
A set_choices() 0 13 2

How to fix   Complexity   

Complex Class

Complex classes like Kirki_Field_Typography often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Kirki_Field_Typography, and based on these observations, apply Extract Interface, too.

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