Complex classes like Kirki_Field 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Kirki_Field { |
||
18 | |||
19 | /** |
||
20 | * The ID of the kirki_config we're using. |
||
21 | * |
||
22 | * @see Kirki_Config |
||
23 | * @access protected |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $kirki_config = 'global'; |
||
27 | |||
28 | /** |
||
29 | * Thje capability required so that users can edit this field. |
||
30 | * |
||
31 | * @access protected |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $capability = 'edit_theme_options'; |
||
35 | |||
36 | /** |
||
37 | * If we're using options instead of theme_mods |
||
38 | * and we want them serialized, this is the option that |
||
39 | * will saved in the db. |
||
40 | * |
||
41 | * @access protected |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $option_name = ''; |
||
45 | |||
46 | /** |
||
47 | * Custom input attributes (defined as an array). |
||
48 | * |
||
49 | * @access protected |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $input_attrs = array(); |
||
53 | |||
54 | /** |
||
55 | * Use "theme_mod" or "option". |
||
56 | * |
||
57 | * @access protected |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $option_type = 'theme_mod'; |
||
61 | |||
62 | /** |
||
63 | * The name of this setting (id for the db). |
||
64 | * |
||
65 | * @access protected |
||
66 | * @var string|array |
||
67 | */ |
||
68 | protected $settings = ''; |
||
69 | |||
70 | /** |
||
71 | * Set to true if you want to disable all CSS output for this field. |
||
72 | * |
||
73 | * @access protected |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $disable_output = false; |
||
77 | |||
78 | /** |
||
79 | * The field type. |
||
80 | * |
||
81 | * @access protected |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $type = 'kirki-generic'; |
||
85 | |||
86 | /** |
||
87 | * Some fields require options to be set. |
||
88 | * We're whitelisting the property here |
||
89 | * and suggest you validate this in a child class. |
||
90 | * |
||
91 | * @access protected |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $choices = array(); |
||
95 | |||
96 | /** |
||
97 | * Assign this field to a section. |
||
98 | * Fields not assigned to a section will not be displayed in the customizer. |
||
99 | * |
||
100 | * @access protected |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $section = ''; |
||
104 | |||
105 | /** |
||
106 | * The default value for this field. |
||
107 | * |
||
108 | * @access protected |
||
109 | * @var string|array |
||
110 | */ |
||
111 | protected $default = ''; |
||
112 | |||
113 | /** |
||
114 | * Priority determines the position of a control inside a section. |
||
115 | * Lower priority numbers move the control to the top. |
||
116 | * |
||
117 | * @access protected |
||
118 | * @var int |
||
119 | */ |
||
120 | protected $priority = 10; |
||
121 | |||
122 | /** |
||
123 | * Unique ID for this field. |
||
124 | * This is auto-calculated from the $settings argument. |
||
125 | * |
||
126 | * @access protected |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $id = ''; |
||
130 | |||
131 | /** |
||
132 | * Use if you want to automatically generate CSS from this field's value. |
||
133 | * |
||
134 | * @see https://kirki.org/docs/arguments/output |
||
135 | * @access protected |
||
136 | * @var array |
||
137 | */ |
||
138 | protected $output = array(); |
||
139 | |||
140 | /** |
||
141 | * Use to automatically generate postMessage scripts. |
||
142 | * Not necessary to use if you use 'transport' => 'auto' |
||
143 | * and have already set an array for the 'output' argument. |
||
144 | * |
||
145 | * @see https://kirki.org/docs/arguments/js_vars |
||
146 | * @access protected |
||
147 | * @var array |
||
148 | */ |
||
149 | protected $js_vars = array(); |
||
150 | |||
151 | /** |
||
152 | * If you want to use a CSS compiler, then use this to set the variable names. |
||
153 | * |
||
154 | * @see https://kirki.org/docs/arguments/variables |
||
155 | * @access protected |
||
156 | * @var array |
||
157 | */ |
||
158 | protected $variables = array(); |
||
159 | |||
160 | /** |
||
161 | * Text that will be used in a tooltip to provide extra info for this field. |
||
162 | * |
||
163 | * @access protected |
||
164 | * @var string |
||
165 | */ |
||
166 | protected $tooltip = ''; |
||
167 | |||
168 | /** |
||
169 | * Whitelisting for backwards-compatibility. |
||
170 | * |
||
171 | * @access protected |
||
172 | * @var string |
||
173 | */ |
||
174 | protected $help = ''; |
||
175 | |||
176 | /** |
||
177 | * A custom callback to determine if the field should be visible or not. |
||
178 | * |
||
179 | * @access protected |
||
180 | * @var string|array |
||
181 | */ |
||
182 | protected $active_callback = '__return_true'; |
||
183 | |||
184 | /** |
||
185 | * A custom sanitize callback that will be used to properly save the values. |
||
186 | * |
||
187 | * @access protected |
||
188 | * @var string|array |
||
189 | */ |
||
190 | protected $sanitize_callback = ''; |
||
191 | |||
192 | /** |
||
193 | * Use 'refresh', 'postMessage' or 'auto'. |
||
194 | * 'auto' will automatically geberate any 'js_vars' from the 'output' argument. |
||
195 | * |
||
196 | * @access protected |
||
197 | * @var string |
||
198 | */ |
||
199 | protected $transport = 'refresh'; |
||
200 | |||
201 | /** |
||
202 | * Define dependencies to show/hide this field based on the values of other fields. |
||
203 | * |
||
204 | * @access protected |
||
205 | * @var array |
||
206 | */ |
||
207 | protected $required = array(); |
||
208 | |||
209 | /** |
||
210 | * Suggested width for cropped image. |
||
211 | * |
||
212 | * @access protected |
||
213 | * @var int |
||
214 | */ |
||
215 | protected $width = 150; |
||
216 | |||
217 | /** |
||
218 | * Suggested height for cropped image. |
||
219 | * |
||
220 | * @access protected |
||
221 | * @var int |
||
222 | */ |
||
223 | protected $height = 150; |
||
224 | |||
225 | /** |
||
226 | * Whether the width is flexible for cropped image. |
||
227 | * |
||
228 | * @access protected |
||
229 | * @var bool |
||
230 | */ |
||
231 | protected $flex_width = false; |
||
232 | |||
233 | /** |
||
234 | * Whether the height is flexible for cropped image. |
||
235 | * |
||
236 | * @access protected |
||
237 | * @var bool |
||
238 | */ |
||
239 | protected $flex_height = false; |
||
240 | |||
241 | /** |
||
242 | * Contain the settings for the repeater rows labels |
||
243 | * |
||
244 | * @access protected |
||
245 | * @var array |
||
246 | */ |
||
247 | protected $row_label = array(); |
||
248 | |||
249 | /** |
||
250 | * Partial Refreshes array. |
||
251 | * |
||
252 | * @access protected |
||
253 | * @var array |
||
254 | */ |
||
255 | protected $partial_refresh = array(); |
||
256 | |||
257 | /** |
||
258 | * Use only on image, cropped_image, upload controls. |
||
259 | * Limit the Media library to a specific mime type |
||
260 | * |
||
261 | * @access protected |
||
262 | * @var array |
||
263 | */ |
||
264 | protected $mime_type = ''; |
||
265 | |||
266 | /** |
||
267 | * Used by image fields. |
||
268 | * |
||
269 | * @access protected |
||
270 | * @var array |
||
271 | * @since 3.0.0 |
||
272 | */ |
||
273 | protected $button_labels = array(); |
||
274 | |||
275 | /** |
||
276 | * Use only on select controls. |
||
277 | * Defines if this is a multi-select or not. |
||
278 | * If value is > 1, then the maximum number of selectable options |
||
279 | * is the number defined here. |
||
280 | * |
||
281 | * @access protected |
||
282 | * @var integer |
||
283 | */ |
||
284 | protected $multiple = 1; |
||
285 | |||
286 | /** |
||
287 | * Allows fields to be collapsible. |
||
288 | * |
||
289 | * @access protected |
||
290 | * @since 3.0.0 |
||
291 | * @var bool |
||
292 | */ |
||
293 | protected $collapsible = false; |
||
294 | |||
295 | /** |
||
296 | * The class constructor. |
||
297 | * Parses and sanitizes all field arguments. |
||
298 | * Then it adds the field to Kirki::$fields. |
||
299 | * |
||
300 | * @access public |
||
301 | * @param string $config_id The ID of the config we want to use. |
||
302 | * Defaults to "global". |
||
303 | * Configs are handled by the Kirki_Config class. |
||
304 | * @param array $args The arguments of the field. |
||
305 | */ |
||
306 | public function __construct( $config_id = 'global', $args = array() ) { |
||
307 | |||
308 | if ( isset( $args['setting'] ) && ! empty( $args['setting'] ) && ( ! isset( $args['settings'] ) || empty( $args['settings'] ) ) ) { |
||
309 | $args['settings'] = $args['setting']; |
||
310 | unset( $args['setting'] ); |
||
311 | error_log( 'Kirki: Typo found in field ' . $args['settings'] . ' ("setting" instead of "settings").' ); |
||
312 | } |
||
313 | |||
314 | // In case the user only provides 1 argument, |
||
315 | // assume that the provided argument is $args and set $config_id = 'global'. |
||
316 | if ( is_array( $config_id ) && empty( $args ) ) { |
||
317 | $args = $config_id; |
||
318 | $config_id = 'global'; |
||
319 | } |
||
320 | |||
321 | $args['kirki_config'] = $config_id; |
||
322 | |||
323 | $this->kirki_config = ( '' === $config_id ) ? 'global' : trim( esc_attr( $config_id ) ); |
||
324 | |||
325 | // Get defaults from the class. |
||
326 | $defaults = get_class_vars( __CLASS__ ); |
||
327 | |||
328 | // Get the config arguments, and merge them with the defaults. |
||
329 | $config_defaults = ( isset( Kirki::$config['global'] ) ) ? Kirki::$config['global'] : array(); |
||
330 | if ( 'global' !== $this->kirki_config && isset( Kirki::$config[ $this->kirki_config ] ) ) { |
||
331 | $config_defaults = Kirki::$config[ $this->kirki_config ]; |
||
332 | } |
||
333 | $config_defaults = ( is_array( $config_defaults ) ) ? $config_defaults : array(); |
||
334 | foreach ( $config_defaults as $key => $value ) { |
||
335 | if ( isset( $defaults[ $key ] ) && ! empty( $value ) && $value != $defaults[ $key ] ) { |
||
336 | $defaults[ $key ] = $value; |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // Merge our args with the defaults. |
||
341 | $args = wp_parse_args( $args, $defaults ); |
||
342 | |||
343 | // Set the class properties using the parsed args. |
||
344 | foreach ( $args as $key => $value ) { |
||
345 | $this->$key = $value; |
||
346 | } |
||
347 | |||
348 | // An array of whitelisted properties that don't need to be sanitized here. |
||
349 | // Format: $key => $default_value. |
||
350 | $whitelisted = apply_filters( "kirki/{$this->kirki_config}/fields/properties_whitelist", array( |
||
351 | 'label' => '', // This is sanitized later in the controls themselves. |
||
352 | 'description' => '', // This is sanitized later in the controls themselves. |
||
353 | 'mode' => '', // Only used for backwards-compatibility reasons. |
||
354 | 'fields' => array(), // Used in repeater fields. |
||
355 | 'row_label' => array(), // Used in repeater fields. |
||
356 | ) ); |
||
357 | |||
358 | $this->set_field( $whitelisted ); |
||
359 | |||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Processes the field arguments |
||
364 | * |
||
365 | * @param array $whitelisted_properties Defines an array of arguments that will skip validation at this point. |
||
366 | */ |
||
367 | protected function set_field( $whitelisted_properties = array() ) { |
||
368 | |||
369 | $properties = get_class_vars( __CLASS__ ); |
||
370 | // Remove any whitelisted properties from above. |
||
371 | // These will get a free pass, completely unfiltered. |
||
372 | foreach ( $whitelisted_properties as $key => $default_value ) { |
||
373 | if ( isset( $properties[ $key ] ) ) { |
||
374 | unset( $properties[ $key ] ); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | // Some things must run before the others. |
||
379 | $priorities = array( |
||
380 | 'option_name', |
||
381 | 'option_type', |
||
382 | 'settings', |
||
383 | ); |
||
384 | |||
385 | foreach ( $priorities as $priority ) { |
||
386 | if ( method_exists( $this, 'set_' . $priority ) ) { |
||
387 | $method_name = 'set_' . $priority; |
||
388 | $this->$method_name(); |
||
389 | } |
||
390 | } |
||
391 | |||
392 | // Sanitize the properties, skipping the ones run from the $priorities. |
||
393 | foreach ( $properties as $property => $value ) { |
||
394 | if ( in_array( $property, $priorities, true ) ) { |
||
395 | continue; |
||
396 | } |
||
397 | if ( method_exists( $this, 'set_' . $property ) ) { |
||
398 | $method_name = 'set_' . $property; |
||
399 | $this->$method_name(); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | // Get all arguments with their values. |
||
404 | $args = get_object_vars( $this ); |
||
405 | foreach ( $args as $key => $default_value ) { |
||
406 | $args[ $key ] = $this->$key; |
||
407 | } |
||
408 | |||
409 | // Add the whitelisted properties through the back door. |
||
410 | foreach ( $whitelisted_properties as $key => $default_value ) { |
||
411 | if ( ! isset( $this->$key ) ) { |
||
412 | $this->$key = $default_value; |
||
413 | } |
||
414 | $args[ $key ] = $this->$key; |
||
415 | } |
||
416 | |||
417 | // Add the field to the static $fields variable properly indexed. |
||
418 | Kirki::$fields[ $this->settings ] = $args; |
||
419 | |||
420 | } |
||
421 | |||
422 | /** |
||
423 | * This allows us to process this on a field-basis |
||
424 | * by using sub-classes which can override this method. |
||
425 | * |
||
426 | * @access protected |
||
427 | */ |
||
428 | protected function set_default() {} |
||
429 | |||
430 | /** |
||
431 | * Escape $kirki_config. |
||
432 | * |
||
433 | * @access protected |
||
434 | */ |
||
435 | protected function set_kirki_config() { |
||
440 | |||
441 | /** |
||
442 | * Escape $option_name. |
||
443 | * |
||
444 | * @access protected |
||
445 | */ |
||
446 | protected function set_option_name() { |
||
451 | |||
452 | /** |
||
453 | * Escape the $section. |
||
454 | * |
||
455 | * @access protected |
||
456 | */ |
||
457 | protected function set_section() { |
||
462 | |||
463 | /** |
||
464 | * Escape the $section. |
||
465 | * |
||
466 | * @access protected |
||
467 | */ |
||
468 | protected function set_input_attrs() { |
||
469 | |||
470 | if ( ! is_array( $this->input_attrs ) ) { |
||
471 | $this->input_attrs = array(); |
||
472 | } |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Checks the capability chosen is valid. |
||
477 | * If not, then falls back to 'edit_theme_options' |
||
478 | * |
||
479 | * @access protected |
||
480 | */ |
||
481 | protected function set_capability() { |
||
482 | |||
483 | // Early exit if we're using 'edit_theme_options'. |
||
484 | if ( 'edit_theme_options' === $this->capability ) { |
||
485 | return; |
||
486 | } |
||
487 | // Escape & trim the capability. |
||
488 | $this->capability = trim( esc_attr( $this->capability ) ); |
||
489 | |||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Make sure we're using the correct option_type |
||
494 | * |
||
495 | * @access protected |
||
496 | */ |
||
497 | protected function set_option_type() { |
||
498 | |||
499 | // Take care of common typos. |
||
500 | if ( 'options' === $this->option_type ) { |
||
501 | $this->option_type = 'option'; |
||
502 | } |
||
503 | // Take care of common typos. |
||
504 | if ( 'theme_mods' === $this->option_type ) { |
||
505 | $this->option_type = 'theme_mod'; |
||
506 | } |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Modifications for partial refreshes. |
||
511 | * |
||
512 | * @access protected |
||
513 | */ |
||
514 | protected function set_partial_refresh() { |
||
515 | |||
516 | if ( ! is_array( $this->partial_refresh ) ) { |
||
517 | $this->partial_refresh = array(); |
||
518 | } |
||
519 | foreach ( $this->partial_refresh as $id => $args ) { |
||
520 | if ( ! is_array( $args ) || ! isset( $args['selector'] ) || ! isset( $args['render_callback'] ) || ! is_callable( $args['render_callback'] ) ) { |
||
521 | unset( $this->partial_refresh[ $id ] ); |
||
522 | continue; |
||
523 | } |
||
524 | } |
||
525 | if ( ! empty( $this->partial_refresh ) ) { |
||
526 | $this->transport = 'postMessage'; |
||
527 | } |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Sets the settings. |
||
532 | * If we're using serialized options it makes sure that settings are properly formatted. |
||
533 | * We'll also be escaping all setting names here for consistency. |
||
534 | * |
||
535 | * @access protected |
||
536 | */ |
||
537 | protected function set_settings() { |
||
538 | |||
539 | // If settings is not an array, temporarily convert it to an array. |
||
540 | // This is just to allow us to process everything the same way and avoid code duplication. |
||
541 | // if settings is not an array then it will not be set as an array in the end. |
||
542 | if ( ! is_array( $this->settings ) ) { |
||
543 | $this->settings = array( |
||
544 | 'kirki_placeholder_setting' => $this->settings, |
||
545 | ); |
||
546 | } |
||
547 | $settings = array(); |
||
548 | foreach ( $this->settings as $setting_key => $setting_value ) { |
||
549 | $settings[ sanitize_key( $setting_key ) ] = esc_attr( $setting_value ); |
||
550 | // If we're using serialized options then we need to spice this up. |
||
551 | if ( 'option' === $this->option_type && '' !== $this->option_name && ( false === strpos( $setting_key, '[' ) ) ) { |
||
552 | $settings[ sanitize_key( $setting_key ) ] = esc_attr( $this->option_name ) . '[' . esc_attr( $setting_value ) . ']'; |
||
553 | } |
||
554 | } |
||
555 | $this->settings = $settings; |
||
556 | if ( isset( $this->settings['kirki_placeholder_setting'] ) ) { |
||
557 | $this->settings = $this->settings['kirki_placeholder_setting']; |
||
558 | } |
||
559 | |||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Escapes the tooltip messages. |
||
564 | * |
||
565 | * @access protected |
||
566 | */ |
||
567 | protected function set_tooltip() { |
||
568 | |||
569 | if ( '' !== $this->tooltip ) { |
||
570 | $this->tooltip = wp_strip_all_tags( $this->tooltip ); |
||
571 | return; |
||
572 | } |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Sets the active_callback |
||
577 | * If we're using the $required argument, |
||
578 | * Then this is where the switch is made to our evaluation method. |
||
579 | * |
||
580 | * @access protected |
||
581 | */ |
||
582 | protected function set_active_callback() { |
||
583 | |||
584 | if ( is_array( $this->active_callback ) && ! is_callable( $this->active_callback ) ) { |
||
585 | if ( isset( $this->active_callback[0] ) ) { |
||
586 | $this->required = $this->active_callback; |
||
587 | } |
||
588 | } |
||
589 | |||
590 | if ( ! empty( $this->required ) ) { |
||
591 | $this->active_callback = array( 'Kirki_Active_Callback', 'evaluate' ); |
||
592 | return; |
||
593 | } |
||
594 | // No need to proceed any further if we're using the default value. |
||
595 | if ( '__return_true' === $this->active_callback ) { |
||
596 | return; |
||
597 | } |
||
598 | // Make sure the function is callable, otherwise fallback to __return_true. |
||
599 | if ( ! is_callable( $this->active_callback ) ) { |
||
600 | $this->active_callback = '__return_true'; |
||
601 | } |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Sets the control type. |
||
606 | * |
||
607 | * @access protected |
||
608 | */ |
||
609 | protected function set_type() { |
||
615 | |||
616 | /** |
||
617 | * Sets the $id. |
||
618 | * Setting the ID should happen after the 'settings' sanitization. |
||
619 | * This way we can also properly handle cases where the option_type is set to 'option' |
||
620 | * and we're using an array instead of individual options. |
||
621 | * |
||
622 | * @access protected |
||
623 | */ |
||
624 | protected function set_id() { |
||
629 | |||
630 | /** |
||
631 | * Sets the $sanitize_callback |
||
632 | * |
||
633 | * @access protected |
||
634 | */ |
||
635 | protected function set_sanitize_callback() { |
||
653 | |||
654 | /** |
||
655 | * Sets the $choices. |
||
656 | * |
||
657 | * @access protected |
||
658 | */ |
||
659 | protected function set_choices() { |
||
660 | |||
661 | if ( ! is_array( $this->choices ) ) { |
||
662 | $this->choices = array(); |
||
665 | |||
666 | /** |
||
667 | * Escapes the $disable_output. |
||
668 | * |
||
669 | * @access protected |
||
670 | */ |
||
671 | protected function set_disable_output() { |
||
676 | |||
677 | /** |
||
678 | * Sets the $sanitize_callback |
||
679 | * |
||
680 | * @access protected |
||
681 | */ |
||
682 | protected function set_output() { |
||
724 | |||
725 | /** |
||
726 | * Sets the $js_vars |
||
727 | * |
||
728 | * @access protected |
||
729 | */ |
||
730 | protected function set_js_vars() { |
||
783 | |||
784 | /** |
||
785 | * Sets the $variables |
||
786 | * |
||
787 | * @access protected |
||
788 | */ |
||
789 | protected function set_variables() { |
||
799 | |||
800 | /** |
||
801 | * This is a fallback method: |
||
802 | * $help has now become $tooltip, so this just migrates the data |
||
803 | * |
||
804 | * @access protected |
||
805 | */ |
||
806 | protected function set_help() { |
||
818 | |||
819 | /** |
||
820 | * Sets the $transport |
||
821 | * |
||
822 | * @access protected |
||
823 | */ |
||
824 | protected function set_transport() { |
||
830 | |||
831 | /** |
||
832 | * Sets the $required |
||
833 | * |
||
834 | * @access protected |
||
835 | */ |
||
836 | protected function set_required() { |
||
842 | |||
843 | /** |
||
844 | * Sets the $priority |
||
845 | * |
||
846 | * @access protected |
||
847 | */ |
||
848 | protected function set_priority() { |
||
853 | |||
854 | /** |
||
855 | * Sets the $collapsible var. |
||
856 | * |
||
857 | * @access protected |
||
858 | */ |
||
859 | protected function set_collapsible() { |
||
864 | } |
||
865 |