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|bool |
||
| 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() ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Processes the field arguments |
||
| 361 | * |
||
| 362 | * @access protected |
||
| 363 | */ |
||
| 364 | protected function set_field() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Gets the classname from a property. |
||
| 402 | * |
||
| 403 | * @access private |
||
| 404 | * @since 3.0.10 |
||
| 405 | * @param string $property The property. |
||
| 406 | * @return string A classname derived from the property. |
||
| 407 | */ |
||
| 408 | private function get_property_classname( $property ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * No need to do anything, these are escaped on the fields themselves. |
||
| 424 | * |
||
| 425 | * @access protected |
||
| 426 | */ |
||
| 427 | protected function set_label() {} |
||
| 428 | |||
| 429 | /** |
||
| 430 | * No need to do anything, these are escaped on the fields themselves. |
||
| 431 | * |
||
| 432 | * @access protected |
||
| 433 | */ |
||
| 434 | protected function set_description() {} |
||
| 435 | |||
| 436 | /** |
||
| 437 | * No need to do anything, these are escaped on the fields themselves. |
||
| 438 | * |
||
| 439 | * @access protected |
||
| 440 | */ |
||
| 441 | protected function set_mode() {} |
||
| 442 | |||
| 443 | /** |
||
| 444 | * No need to do anything, these are escaped on the fields themselves. |
||
| 445 | * Only used in repeaters. |
||
| 446 | * |
||
| 447 | * @access protected |
||
| 448 | */ |
||
| 449 | protected function set_fields() {} |
||
| 450 | |||
| 451 | /** |
||
| 452 | * No need to do anything, these are escaped on the fields themselves. |
||
| 453 | * Only used in repeaters. |
||
| 454 | * |
||
| 455 | * @access protected |
||
| 456 | */ |
||
| 457 | protected function set_row_label() {} |
||
| 458 | |||
| 459 | /** |
||
| 460 | * This allows us to process this on a field-basis |
||
| 461 | * by using sub-classes which can override this method. |
||
| 462 | * |
||
| 463 | * @access protected |
||
| 464 | */ |
||
| 465 | protected function set_default() {} |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Escape $kirki_config. |
||
| 469 | * |
||
| 470 | * @access protected |
||
| 471 | */ |
||
| 472 | protected function set_kirki_config() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Escape $option_name. |
||
| 480 | * |
||
| 481 | * @access protected |
||
| 482 | */ |
||
| 483 | protected function set_option_name() { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Escape the $section. |
||
| 491 | * |
||
| 492 | * @access protected |
||
| 493 | */ |
||
| 494 | protected function set_section() { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Escape the $section. |
||
| 502 | * |
||
| 503 | * @access protected |
||
| 504 | */ |
||
| 505 | protected function set_input_attrs() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Checks the capability chosen is valid. |
||
| 514 | * If not, then falls back to 'edit_theme_options' |
||
| 515 | * |
||
| 516 | * @access protected |
||
| 517 | */ |
||
| 518 | protected function set_capability() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Make sure we're using the correct option_type |
||
| 531 | * |
||
| 532 | * @access protected |
||
| 533 | */ |
||
| 534 | protected function set_option_type() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Modifications for partial refreshes. |
||
| 550 | * |
||
| 551 | * @access protected |
||
| 552 | */ |
||
| 553 | protected function set_partial_refresh() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Sets the settings. |
||
| 573 | * If we're using serialized options it makes sure that settings are properly formatted. |
||
| 574 | * We'll also be escaping all setting names here for consistency. |
||
| 575 | * |
||
| 576 | * @access protected |
||
| 577 | */ |
||
| 578 | protected function set_settings() { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Escapes the tooltip messages. |
||
| 605 | * |
||
| 606 | * @access protected |
||
| 607 | */ |
||
| 608 | protected function set_tooltip() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Sets the active_callback |
||
| 618 | * If we're using the $required argument, |
||
| 619 | * Then this is where the switch is made to our evaluation method. |
||
| 620 | * |
||
| 621 | * @access protected |
||
| 622 | */ |
||
| 623 | protected function set_active_callback() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Sets the control type. |
||
| 647 | * |
||
| 648 | * @access protected |
||
| 649 | */ |
||
| 650 | protected function set_type() { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Sets the $id. |
||
| 659 | * Setting the ID should happen after the 'settings' sanitization. |
||
| 660 | * This way we can also properly handle cases where the option_type is set to 'option' |
||
| 661 | * and we're using an array instead of individual options. |
||
| 662 | * |
||
| 663 | * @access protected |
||
| 664 | */ |
||
| 665 | protected function set_id() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Sets the $sanitize_callback |
||
| 673 | * |
||
| 674 | * @access protected |
||
| 675 | */ |
||
| 676 | protected function set_sanitize_callback() {} |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Sets the $choices. |
||
| 680 | * |
||
| 681 | * @access protected |
||
| 682 | */ |
||
| 683 | protected function set_choices() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Escapes the $disable_output. |
||
| 692 | * |
||
| 693 | * @access protected |
||
| 694 | */ |
||
| 695 | protected function set_disable_output() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Sets the $sanitize_callback |
||
| 703 | * |
||
| 704 | * @access protected |
||
| 705 | */ |
||
| 706 | protected function set_output() { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Sets the $js_vars |
||
| 755 | * |
||
| 756 | * @access protected |
||
| 757 | */ |
||
| 758 | protected function set_js_vars() { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Sets the $variables |
||
| 814 | * |
||
| 815 | * @access protected |
||
| 816 | */ |
||
| 817 | protected function set_variables() { |
||
| 827 | |||
| 828 | /** |
||
| 829 | * This is a fallback method: |
||
| 830 | * $help has now become $tooltip, so this just migrates the data |
||
| 831 | * |
||
| 832 | * @access protected |
||
| 833 | */ |
||
| 834 | protected function set_help() { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Sets the $transport |
||
| 850 | * |
||
| 851 | * @access protected |
||
| 852 | */ |
||
| 853 | protected function set_transport() { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Sets the $required |
||
| 862 | * |
||
| 863 | * @access protected |
||
| 864 | */ |
||
| 865 | protected function set_required() { |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Sets the $priority |
||
| 874 | * |
||
| 875 | * @access protected |
||
| 876 | */ |
||
| 877 | protected function set_priority() { |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Sets the $collapsible var. |
||
| 885 | * |
||
| 886 | * @access protected |
||
| 887 | */ |
||
| 888 | protected function set_collapsible() { |
||
| 893 | } |
||
| 894 |
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: