Completed
Push — develop ( 7b02dc...95198f )
by Aristeides
17:08
created

Kirki_Field_Typography   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 229
rs 5.1724
c 0
b 0
f 0
wmc 57
lcom 5
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A set_type() 0 5 1
A __construct() 0 4 1
B set_default() 0 17 10
A set_sanitize_callback() 0 10 2
C set_js_vars() 0 48 8
C sanitize() 0 73 32
A set_choices() 0 18 3

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