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 | * An array of the field arguments. |
||
21 | * |
||
22 | * @access protected |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $args = array(); |
||
26 | |||
27 | /** |
||
28 | * The ID of the kirki_config we're using. |
||
29 | * |
||
30 | * @see Kirki_Config |
||
31 | * @access protected |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $kirki_config = 'global'; |
||
35 | |||
36 | /** |
||
37 | * Thje capability required so that users can edit this field. |
||
38 | * |
||
39 | * @access protected |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $capability = 'edit_theme_options'; |
||
43 | |||
44 | /** |
||
45 | * If we're using options instead of theme_mods |
||
46 | * and we want them serialized, this is the option that |
||
47 | * will saved in the db. |
||
48 | * |
||
49 | * @access protected |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $option_name = ''; |
||
53 | |||
54 | /** |
||
55 | * Custom input attributes (defined as an array). |
||
56 | * |
||
57 | * @access protected |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $input_attrs = array(); |
||
61 | |||
62 | /** |
||
63 | * Use "theme_mod" or "option". |
||
64 | * |
||
65 | * @access protected |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $option_type = 'theme_mod'; |
||
69 | |||
70 | /** |
||
71 | * The name of this setting (id for the db). |
||
72 | * |
||
73 | * @access protected |
||
74 | * @var string|array |
||
75 | */ |
||
76 | protected $settings = ''; |
||
77 | |||
78 | /** |
||
79 | * Set to true if you want to disable all CSS output for this field. |
||
80 | * |
||
81 | * @access protected |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $disable_output = false; |
||
85 | |||
86 | /** |
||
87 | * The field type. |
||
88 | * |
||
89 | * @access protected |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $type = 'kirki-generic'; |
||
93 | |||
94 | /** |
||
95 | * Some fields require options to be set. |
||
96 | * We're whitelisting the property here |
||
97 | * and suggest you validate this in a child class. |
||
98 | * |
||
99 | * @access protected |
||
100 | * @var array |
||
101 | */ |
||
102 | protected $choices = array(); |
||
103 | |||
104 | /** |
||
105 | * Assign this field to a section. |
||
106 | * Fields not assigned to a section will not be displayed in the customizer. |
||
107 | * |
||
108 | * @access protected |
||
109 | * @var string |
||
110 | */ |
||
111 | protected $section = ''; |
||
112 | |||
113 | /** |
||
114 | * The default value for this field. |
||
115 | * |
||
116 | * @access protected |
||
117 | * @var string|array|bool |
||
118 | */ |
||
119 | protected $default = ''; |
||
120 | |||
121 | /** |
||
122 | * Priority determines the position of a control inside a section. |
||
123 | * Lower priority numbers move the control to the top. |
||
124 | * |
||
125 | * @access protected |
||
126 | * @var int |
||
127 | */ |
||
128 | protected $priority = 10; |
||
129 | |||
130 | /** |
||
131 | * Unique ID for this field. |
||
132 | * This is auto-calculated from the $settings argument. |
||
133 | * |
||
134 | * @access protected |
||
135 | * @var string |
||
136 | */ |
||
137 | protected $id = ''; |
||
138 | |||
139 | /** |
||
140 | * Use if you want to automatically generate CSS from this field's value. |
||
141 | * |
||
142 | * @see https://kirki.org/docs/arguments/output |
||
143 | * @access protected |
||
144 | * @var array |
||
145 | */ |
||
146 | protected $output = array(); |
||
147 | |||
148 | /** |
||
149 | * Use to automatically generate postMessage scripts. |
||
150 | * Not necessary to use if you use 'transport' => 'auto' |
||
151 | * and have already set an array for the 'output' argument. |
||
152 | * |
||
153 | * @see https://kirki.org/docs/arguments/js_vars |
||
154 | * @access protected |
||
155 | * @var array |
||
156 | */ |
||
157 | protected $js_vars = array(); |
||
158 | |||
159 | /** |
||
160 | * If you want to use a CSS compiler, then use this to set the variable names. |
||
161 | * |
||
162 | * @see https://kirki.org/docs/arguments/variables |
||
163 | * @access protected |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $variables = array(); |
||
167 | |||
168 | /** |
||
169 | * Text that will be used in a tooltip to provide extra info for this field. |
||
170 | * |
||
171 | * @access protected |
||
172 | * @var string |
||
173 | */ |
||
174 | protected $tooltip = ''; |
||
175 | |||
176 | /** |
||
177 | * Whitelisting for backwards-compatibility. |
||
178 | * |
||
179 | * @access protected |
||
180 | * @var string |
||
181 | */ |
||
182 | protected $help = ''; |
||
183 | |||
184 | /** |
||
185 | * A custom callback to determine if the field should be visible or not. |
||
186 | * |
||
187 | * @access protected |
||
188 | * @var string|array |
||
189 | */ |
||
190 | protected $active_callback = '__return_true'; |
||
191 | |||
192 | /** |
||
193 | * A custom sanitize callback that will be used to properly save the values. |
||
194 | * |
||
195 | * @access protected |
||
196 | * @var string|array |
||
197 | */ |
||
198 | protected $sanitize_callback = ''; |
||
199 | |||
200 | /** |
||
201 | * Use 'refresh', 'postMessage' or 'auto'. |
||
202 | * 'auto' will automatically geberate any 'js_vars' from the 'output' argument. |
||
203 | * |
||
204 | * @access protected |
||
205 | * @var string |
||
206 | */ |
||
207 | protected $transport = 'refresh'; |
||
208 | |||
209 | /** |
||
210 | * Define dependencies to show/hide this field based on the values of other fields. |
||
211 | * |
||
212 | * @access protected |
||
213 | * @var array |
||
214 | */ |
||
215 | protected $required = array(); |
||
216 | |||
217 | /** |
||
218 | * Suggested width for cropped image. |
||
219 | * |
||
220 | * @access protected |
||
221 | * @var int |
||
222 | */ |
||
223 | protected $width = 150; |
||
224 | |||
225 | /** |
||
226 | * Suggested height for cropped image. |
||
227 | * |
||
228 | * @access protected |
||
229 | * @var int |
||
230 | */ |
||
231 | protected $height = 150; |
||
232 | |||
233 | /** |
||
234 | * Whether the width is flexible for cropped image. |
||
235 | * |
||
236 | * @access protected |
||
237 | * @var bool |
||
238 | */ |
||
239 | protected $flex_width = false; |
||
240 | |||
241 | /** |
||
242 | * Whether the height is flexible for cropped image. |
||
243 | * |
||
244 | * @access protected |
||
245 | * @var bool |
||
246 | */ |
||
247 | protected $flex_height = false; |
||
248 | |||
249 | /** |
||
250 | * Contain the settings for the repeater rows labels |
||
251 | * |
||
252 | * @access protected |
||
253 | * @var array |
||
254 | */ |
||
255 | protected $row_label = array(); |
||
256 | |||
257 | /** |
||
258 | * Partial Refreshes array. |
||
259 | * |
||
260 | * @access protected |
||
261 | * @var array |
||
262 | */ |
||
263 | protected $partial_refresh = array(); |
||
264 | |||
265 | /** |
||
266 | * Use only on image, cropped_image, upload controls. |
||
267 | * Limit the Media library to a specific mime type |
||
268 | * |
||
269 | * @access protected |
||
270 | * @var array |
||
271 | */ |
||
272 | protected $mime_type = ''; |
||
273 | |||
274 | /** |
||
275 | * Used by image fields. |
||
276 | * |
||
277 | * @access protected |
||
278 | * @var array |
||
279 | * @since 3.0.0 |
||
280 | */ |
||
281 | protected $button_labels = array(); |
||
282 | |||
283 | /** |
||
284 | * Use only on select controls. |
||
285 | * Defines if this is a multi-select or not. |
||
286 | * If value is > 1, then the maximum number of selectable options |
||
287 | * is the number defined here. |
||
288 | * |
||
289 | * @access protected |
||
290 | * @var integer |
||
291 | */ |
||
292 | protected $multiple = 1; |
||
293 | |||
294 | /** |
||
295 | * Allows fields to be collapsible. |
||
296 | * |
||
297 | * @access protected |
||
298 | * @since 3.0.0 |
||
299 | * @var bool |
||
300 | */ |
||
301 | protected $collapsible = false; |
||
302 | |||
303 | /** |
||
304 | * The class constructor. |
||
305 | * Parses and sanitizes all field arguments. |
||
306 | * Then it adds the field to Kirki::$fields. |
||
307 | * |
||
308 | * @access public |
||
309 | * @param string $config_id The ID of the config we want to use. |
||
310 | * Defaults to "global". |
||
311 | * Configs are handled by the Kirki_Config class. |
||
312 | * @param array $args The arguments of the field. |
||
313 | */ |
||
314 | public function __construct( $config_id = 'global', $args = array() ) { |
||
368 | |||
369 | /** |
||
370 | * Processes the field arguments |
||
371 | * |
||
372 | * @access protected |
||
373 | */ |
||
374 | protected function set_field() { |
||
409 | |||
410 | /** |
||
411 | * Gets the classname from a property. |
||
412 | * |
||
413 | * @access private |
||
414 | * @since 3.0.10 |
||
415 | * @param string $property The property. |
||
416 | * @return string A classname derived from the property. |
||
417 | */ |
||
418 | private function get_property_classname( $property ) { |
||
431 | |||
432 | /** |
||
433 | * No need to do anything, these are escaped on the fields themselves. |
||
434 | * |
||
435 | * @access protected |
||
436 | */ |
||
437 | protected function set_label() {} |
||
438 | |||
439 | /** |
||
440 | * No need to do anything, these are escaped on the fields themselves. |
||
441 | * |
||
442 | * @access protected |
||
443 | */ |
||
444 | protected function set_description() {} |
||
445 | |||
446 | /** |
||
447 | * No need to do anything, these are escaped on the fields themselves. |
||
448 | * |
||
449 | * @access protected |
||
450 | */ |
||
451 | protected function set_mode() {} |
||
452 | |||
453 | /** |
||
454 | * No need to do anything, these are escaped on the fields themselves. |
||
455 | * Only used in repeaters. |
||
456 | * |
||
457 | * @access protected |
||
458 | */ |
||
459 | protected function set_fields() {} |
||
460 | |||
461 | /** |
||
462 | * No need to do anything, these are escaped on the fields themselves. |
||
463 | * Only used in repeaters. |
||
464 | * |
||
465 | * @access protected |
||
466 | */ |
||
467 | protected function set_row_label() {} |
||
468 | |||
469 | /** |
||
470 | * This allows us to process this on a field-basis |
||
471 | * by using sub-classes which can override this method. |
||
472 | * |
||
473 | * @access protected |
||
474 | */ |
||
475 | protected function set_default() {} |
||
476 | |||
477 | /** |
||
478 | * Escape $kirki_config. |
||
479 | * |
||
480 | * @access protected |
||
481 | */ |
||
482 | protected function set_kirki_config() { |
||
486 | |||
487 | /** |
||
488 | * Escape $option_name. |
||
489 | * |
||
490 | * @access protected |
||
491 | */ |
||
492 | protected function set_option_name() { |
||
496 | |||
497 | /** |
||
498 | * Escape the $section. |
||
499 | * |
||
500 | * @access protected |
||
501 | */ |
||
502 | protected function set_section() { |
||
506 | |||
507 | /** |
||
508 | * Escape the $section. |
||
509 | * |
||
510 | * @access protected |
||
511 | */ |
||
512 | protected function set_input_attrs() { |
||
518 | |||
519 | /** |
||
520 | * Checks the capability chosen is valid. |
||
521 | * If not, then falls back to 'edit_theme_options' |
||
522 | * |
||
523 | * @access protected |
||
524 | */ |
||
525 | protected function set_capability() { |
||
534 | |||
535 | /** |
||
536 | * Make sure we're using the correct option_type |
||
537 | * |
||
538 | * @access protected |
||
539 | */ |
||
540 | protected function set_option_type() { |
||
553 | |||
554 | /** |
||
555 | * Modifications for partial refreshes. |
||
556 | * |
||
557 | * @access protected |
||
558 | */ |
||
559 | protected function set_partial_refresh() { |
||
576 | |||
577 | /** |
||
578 | * Sets the settings. |
||
579 | * If we're using serialized options it makes sure that settings are properly formatted. |
||
580 | * We'll also be escaping all setting names here for consistency. |
||
581 | * |
||
582 | * @access protected |
||
583 | */ |
||
584 | protected function set_settings() { |
||
607 | |||
608 | /** |
||
609 | * Escapes the tooltip messages. |
||
610 | * |
||
611 | * @access protected |
||
612 | */ |
||
613 | protected function set_tooltip() { |
||
620 | |||
621 | /** |
||
622 | * Sets the active_callback |
||
623 | * If we're using the $required argument, |
||
624 | * Then this is where the switch is made to our evaluation method. |
||
625 | * |
||
626 | * @access protected |
||
627 | */ |
||
628 | protected function set_active_callback() { |
||
649 | |||
650 | /** |
||
651 | * Sets the control type. |
||
652 | * |
||
653 | * @access protected |
||
654 | */ |
||
655 | protected function set_type() { |
||
660 | |||
661 | /** |
||
662 | * Sets the $id. |
||
663 | * Setting the ID should happen after the 'settings' sanitization. |
||
664 | * This way we can also properly handle cases where the option_type is set to 'option' |
||
665 | * and we're using an array instead of individual options. |
||
666 | * |
||
667 | * @access protected |
||
668 | */ |
||
669 | protected function set_id() { |
||
673 | |||
674 | /** |
||
675 | * Sets the $sanitize_callback |
||
676 | * |
||
677 | * @access protected |
||
678 | */ |
||
679 | protected function set_sanitize_callback() {} |
||
680 | |||
681 | /** |
||
682 | * Sets the $choices. |
||
683 | * |
||
684 | * @access protected |
||
685 | */ |
||
686 | protected function set_choices() { |
||
692 | |||
693 | /** |
||
694 | * Escapes the $disable_output. |
||
695 | * |
||
696 | * @access protected |
||
697 | */ |
||
698 | protected function set_disable_output() { |
||
702 | |||
703 | /** |
||
704 | * Sets the $js_vars |
||
705 | * |
||
706 | * @access protected |
||
707 | */ |
||
708 | protected function set_js_vars() { |
||
761 | |||
762 | /** |
||
763 | * Sets the $variables |
||
764 | * |
||
765 | * @access protected |
||
766 | */ |
||
767 | protected function set_variables() { |
||
777 | |||
778 | /** |
||
779 | * This is a fallback method: |
||
780 | * $help has now become $tooltip, so this just migrates the data |
||
781 | * |
||
782 | * @access protected |
||
783 | */ |
||
784 | protected function set_help() { |
||
797 | |||
798 | /** |
||
799 | * Sets the $transport |
||
800 | * |
||
801 | * @access protected |
||
802 | */ |
||
803 | protected function set_transport() { |
||
809 | |||
810 | /** |
||
811 | * Sets the $required |
||
812 | * |
||
813 | * @access protected |
||
814 | */ |
||
815 | protected function set_required() { |
||
821 | |||
822 | /** |
||
823 | * Sets the $priority |
||
824 | * |
||
825 | * @access protected |
||
826 | */ |
||
827 | protected function set_priority() { |
||
832 | |||
833 | /** |
||
834 | * Sets the $collapsible var. |
||
835 | * |
||
836 | * @access protected |
||
837 | */ |
||
838 | protected function set_collapsible() { |
||
843 | } |
||
844 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: