Total Complexity | 79 |
Total Lines | 329 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like Kirki_Output 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.
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_Output, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Kirki_Output { |
||
16 | |||
17 | /** |
||
18 | * The Kirki configuration used in the field. |
||
19 | * |
||
20 | * @access protected |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $config_id = 'global'; |
||
24 | |||
25 | /** |
||
26 | * The field's `output` argument. |
||
27 | * |
||
28 | * @access protected |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $output = array(); |
||
32 | |||
33 | /** |
||
34 | * An array of the generated styles. |
||
35 | * |
||
36 | * @access protected |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $styles = array(); |
||
40 | |||
41 | /** |
||
42 | * The field. |
||
43 | * |
||
44 | * @access protected |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $field = array(); |
||
48 | |||
49 | /** |
||
50 | * The value. |
||
51 | * |
||
52 | * @access protected |
||
53 | * @var string|array |
||
54 | */ |
||
55 | protected $value; |
||
56 | |||
57 | /** |
||
58 | * The class constructor. |
||
59 | * |
||
60 | * @access public |
||
61 | * @param string $config_id The config ID. |
||
62 | * @param array $output The output argument. |
||
63 | * @param string|array $value The value. |
||
64 | * @param array $field The field. |
||
65 | */ |
||
66 | public function __construct( $config_id, $output, $value, $field ) { |
||
67 | $this->config_id = $config_id; |
||
68 | $this->value = $value; |
||
69 | $this->output = $output; |
||
70 | $this->field = $field; |
||
71 | |||
72 | $this->parse_output(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * If we have a sanitize_callback defined, apply it to the value. |
||
77 | * |
||
78 | * @param array $output The output args. |
||
79 | * @param string|array $value The value. |
||
80 | * |
||
81 | * @return string|array |
||
82 | */ |
||
83 | protected function apply_sanitize_callback( $output, $value ) { |
||
84 | if ( isset( $output['sanitize_callback'] ) && null !== $output['sanitize_callback'] ) { |
||
85 | |||
86 | // If the sanitize_callback is invalid, return the value. |
||
87 | if ( ! is_callable( $output['sanitize_callback'] ) ) { |
||
88 | return $value; |
||
89 | } |
||
90 | return call_user_func( $output['sanitize_callback'], $this->value ); |
||
91 | } |
||
92 | return $value; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * If we have a value_pattern defined, apply it to the value. |
||
97 | * |
||
98 | * @param array $output The output args. |
||
99 | * @param string|array $value The value. |
||
100 | * @return string|array |
||
101 | */ |
||
102 | protected function apply_value_pattern( $output, $value ) { |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * If we have a value_pattern defined, apply it to the value. |
||
128 | * |
||
129 | * @param array $output The output args. |
||
130 | * @param string|array $value The value. |
||
131 | * @return string|array |
||
132 | */ |
||
133 | protected function apply_pattern_replace( $output, $value ) { |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Parses the output arguments. |
||
184 | * Calls the process_output method for each of them. |
||
185 | * |
||
186 | * @access protected |
||
187 | */ |
||
188 | protected function parse_output() { |
||
189 | foreach ( $this->output as $output ) { |
||
190 | $skip = false; |
||
191 | |||
192 | // Apply any sanitization callbacks defined. |
||
193 | $value = $this->apply_sanitize_callback( $output, $this->value ); |
||
194 | |||
195 | // Skip if value is empty. |
||
196 | if ( '' === $this->value ) { |
||
197 | $skip = true; |
||
198 | } |
||
199 | |||
200 | // No need to proceed this if the current value is the same as in the "exclude" value. |
||
201 | if ( isset( $output['exclude'] ) && is_array( $output['exclude'] ) ) { |
||
202 | foreach ( $output['exclude'] as $exclude ) { |
||
203 | if ( is_array( $value ) ) { |
||
204 | if ( is_array( $exclude ) ) { |
||
205 | $diff1 = array_diff( $value, $exclude ); |
||
206 | $diff2 = array_diff( $exclude, $value ); |
||
207 | |||
208 | if ( empty( $diff1 ) && empty( $diff2 ) ) { |
||
209 | $skip = true; |
||
210 | } |
||
211 | } |
||
212 | // If 'choice' is defined check for sub-values too. |
||
213 | // Fixes https://github.com/aristath/kirki/issues/1416. |
||
214 | if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) && $exclude == $value[ $output['choice'] ] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||
215 | $skip = true; |
||
216 | } |
||
217 | } |
||
218 | if ( $skip ) { |
||
219 | continue; |
||
220 | } |
||
221 | |||
222 | // Skip if value is defined as excluded. |
||
223 | if ( $exclude === $value || ( '' === $exclude && empty( $value ) ) ) { |
||
224 | $skip = true; |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | if ( $skip ) { |
||
229 | continue; |
||
230 | } |
||
231 | |||
232 | // Apply any value patterns defined. |
||
233 | $value = $this->apply_value_pattern( $output, $value ); |
||
234 | |||
235 | if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { |
||
236 | $output['element'] = array_unique( $output['element'] ); |
||
237 | sort( $output['element'] ); |
||
238 | $output['element'] = implode( ',', $output['element'] ); |
||
239 | } |
||
240 | |||
241 | $value = $this->process_value( $value, $output ); |
||
242 | |||
243 | if ( is_admin() && ! is_customize_preview() ) { |
||
244 | |||
245 | // Check if this is an admin style. |
||
246 | if ( ! isset( $output['context'] ) || ! in_array( 'editor', $output['context'] ) ) { |
||
247 | continue; |
||
248 | } |
||
249 | } elseif ( isset( $output['context'] ) && ! in_array( 'front', $output['context'] ) ) { |
||
250 | |||
251 | // Check if this is a frontend style. |
||
252 | continue; |
||
253 | } |
||
254 | $this->process_output( $output, $value ); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Parses an output and creates the styles array for it. |
||
260 | * |
||
261 | * @access protected |
||
262 | * @param array $output The field output. |
||
263 | * @param string|array $value The value. |
||
264 | * |
||
265 | * @return null |
||
266 | */ |
||
267 | protected function process_output( $output, $value ) { |
||
292 | } |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Some CSS properties are unique. |
||
297 | * We need to tweak the value to make everything works as expected. |
||
298 | * |
||
299 | * @access protected |
||
300 | * @param string $property The CSS property. |
||
301 | * @param string|array $value The value. |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function process_property_value( $property, $value ) { |
||
306 | $properties = apply_filters( |
||
307 | "kirki_{$this->config_id}_output_property_classnames", array( |
||
308 | 'font-family' => 'Kirki_Output_Property_Font_Family', |
||
309 | 'background-image' => 'Kirki_Output_Property_Background_Image', |
||
310 | 'background-position' => 'Kirki_Output_Property_Background_Position', |
||
311 | ) |
||
312 | ); |
||
313 | if ( array_key_exists( $property, $properties ) ) { |
||
314 | $classname = $properties[ $property ]; |
||
315 | $obj = new $classname( $property, $value ); |
||
316 | return $obj->get_value(); |
||
317 | } |
||
318 | return $value; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Returns the value. |
||
323 | * |
||
324 | * @access protected |
||
325 | * @param string|array $value The value. |
||
326 | * @param array $output The field "output". |
||
327 | * @return string|array |
||
328 | */ |
||
329 | protected function process_value( $value, $output ) { |
||
330 | if ( isset( $output['property'] ) ) { |
||
331 | return $this->process_property_value( $output['property'], $value ); |
||
332 | } |
||
333 | return $value; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Exploses the private $styles property to the world |
||
338 | * |
||
339 | * @access protected |
||
340 | * @return array |
||
341 | */ |
||
342 | public function get_styles() { |
||
344 | } |
||
345 | } |
||
346 |