Completed
Push — develop ( 4115c0...019cf2 )
by Aristeides
03:44
created

Kirki_Field_Typography   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 181
rs 8.295
c 0
b 0
f 0
wmc 42
lcom 4
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set_type() 0 5 1
A set_sanitize_callback() 0 10 2
C set_js_vars() 0 52 9
F sanitize() 0 64 25
B set_choices() 0 15 5

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 $sanitize_callback
30
	 *
31
	 * @access protected
32
	 */
33
	protected function set_sanitize_callback() {
34
35
		// If a custom sanitize_callback has been defined,
36
		// then we don't need to proceed any further.
37
		if ( ! empty( $this->sanitize_callback ) ) {
38
			return;
39
		}
40
		$this->sanitize_callback = array( $this, 'sanitize' );
41
42
	}
43
44
	/**
45
	 * Sets the $js_vars
46
	 *
47
	 * @access protected
48
	 */
49
	protected function set_js_vars() {
50
51
		if ( ! is_array( $this->js_vars ) ) {
52
			$this->js_vars = array();
53
		}
54
55
		// Check if transport is set to auto.
56
		// If not, then skip the auto-calculations and exit early.
57
		if ( 'auto' !== $this->transport ) {
58
			return;
59
		}
60
61
		// Set transport to refresh initially.
62
		// Serves as a fallback in case we failt to auto-calculate js_vars.
63
		$this->transport = 'refresh';
64
65
		$js_vars = array();
66
67
		// Try to auto-generate js_vars.
68
		// First we need to check if js_vars are empty, and that output is not empty.
69
		if ( ! empty( $this->output ) ) {
70
71
			// Start going through each item in the $output array.
72
			foreach ( $this->output as $output ) {
73
				$output['function'] = 'css';
74
75
				// If 'element' or 'property' are not defined, skip this.
76
				if ( ! isset( $output['element'] ) ) {
77
					continue;
78
				}
79
				if ( is_array( $output['element'] ) ) {
80
					$output['element'] = implode( ',', $output['element'] );
81
				}
82
				if ( false !== strpos( $output['element'], ':' ) ) {
83
					$output['function'] = 'style';
84
				}
85
86
				// If we got this far, it's safe to add this.
87
				$js_vars[] = $output;
88
			}
89
90
			// Did we manage to get all the items from 'output'?
91
			// If not, then we're missing something so don't add this.
92
			if ( count( $js_vars ) !== count( $this->output ) ) {
93
				return;
94
			}
95
			$this->js_vars   = $js_vars;
96
			$this->transport = 'postMessage';
97
98
		}
99
100
	}
101
102
	/**
103
	 * Sanitizes typography controls
104
	 *
105
	 * @since 2.2.0
106
	 * @param array $value The value.
107
	 * @return array
108
	 */
109
	public function sanitize( $value ) {
110
111
		if ( ! is_array( $value ) ) {
112
			return array();
113
		}
114
115
		// Escape the font-family.
116
		if ( isset( $value['font-family'] ) ) {
117
			$value['font-family'] = esc_attr( $value['font-family'] );
118
		}
119
120
		// Make sure we're using a valid variant.
121
		// We're adding checks for font-weight as well for backwards-compatibility
122
		// Versions 2.0 - 2.2 were using an integer font-weight.
123
		if ( isset( $value['variant'] ) || isset( $value['font-weight'] ) ) {
124
			if ( isset( $value['font-weight'] ) && ! empty( $value['font-weight'] ) ) {
125
				if ( ! isset( $value['variant'] ) || empty( $value['variant'] ) ) {
126
					$value['variant'] = $value['font-weight'];
127
				}
128
				unset( $value['font-weight'] );
129
			}
130
			$valid_variants = Kirki_Fonts::get_all_variants();
131
			if ( ! array_key_exists( $value['variant'], $valid_variants ) ) {
132
				$value['variant'] = 'regular';
133
			}
134
		}
135
136
		// Make sure the saved value is "subsets" (plural) and not "subset".
137
		// This is for compatibility with older versions.
138
		if ( isset( $value['subset'] ) ) {
139
			if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) {
140
				$value['subsets'] = $value['subset'];
141
			}
142
			unset( $value['subset'] );
143
		}
144
145
		// Make sure we're using a valid subset.
146
		if ( isset( $value['subsets'] ) ) {
147
			$valid_subsets = Kirki_Fonts::get_google_font_subsets();
148
			$subsets_ok = array();
149
			if ( is_array( $value['subsets'] ) ) {
150
				foreach ( $value['subsets'] as $subset ) {
151
					if ( array_key_exists( $subset, $valid_subsets ) ) {
152
						$subsets_ok[] = $subset;
153
					}
154
				}
155
				$value['subsets'] = $subsets_ok;
156
			}
157
		}
158
159
		foreach ( $value as $key => $subvalue ) {
160
161
			if ( in_array( $key, array( 'font-size', 'letter-spacing', 'word-spacing', 'line-height' ), true ) ) {
162
				$value[ $key ] = Kirki_Sanitize_Values::css_dimension( $value[ $key ] );
163
			} elseif ( 'text-align' === $key && ! in_array( $value['text-align'], array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
164
				$value['text-align'] = 'inherit';
165
			} elseif ( 'text-transform' === $key && ! in_array( $value['text-transform'], array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
166
				$value['text-transform'] = 'none';
167
			} elseif ( 'color' === $key ) {
168
				$value['color'] = ariColor::newColor( $value['color'] )->toCSS( 'hex' );
169
			}
170
		}
171
		return $value;
172
	}
173
174
	/**
175
	 * Sets the $choices
176
	 *
177
	 * @access protected
178
	 * @since 3.0.0
179
	 */
180
	protected function set_choices() {
181
182
		if ( ! is_array( $this->choices ) ) {
183
			$this->choices = array();
184
		}
185
		if ( ! isset( $this->choices['fonts'] ) ) {
186
			$this->choices['fonts'] = array();
187
		}
188
		if ( ! isset( $this->choices['fonts']['standard'] ) ) {
189
			$this->choices['fonts']['standard'] = array();
190
		}
191
		if ( ! isset( $this->choices['fonts']['google'] ) ) {
192
			$this->choices['fonts']['google'] = array();
193
		}
194
	}
195
}
196