Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_Customize_Control 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 WP_Customize_Control, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WP_Customize_Control { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Incremented with each new class instantiation, then stored in $instance_number. |
||
| 19 | * |
||
| 20 | * Used when sorting two instances whose priorities are equal. |
||
| 21 | * |
||
| 22 | * @since 4.1.0 |
||
| 23 | * |
||
| 24 | * @static |
||
| 25 | * @access protected |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected static $instance_count = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Order in which this instance was created in relation to other instances. |
||
| 32 | * |
||
| 33 | * @since 4.1.0 |
||
| 34 | * @access public |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | public $instance_number; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Customizer manager. |
||
| 41 | * |
||
| 42 | * @since 3.4.0 |
||
| 43 | * @access public |
||
| 44 | * @var WP_Customize_Manager |
||
| 45 | */ |
||
| 46 | public $manager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Control ID. |
||
| 50 | * |
||
| 51 | * @since 3.4.0 |
||
| 52 | * @access public |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $id; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * All settings tied to the control. |
||
| 59 | * |
||
| 60 | * @since 3.4.0 |
||
| 61 | * @access public |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | public $settings; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The primary setting for the control (if there is one). |
||
| 68 | * |
||
| 69 | * @since 3.4.0 |
||
| 70 | * @access public |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $setting = 'default'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Capability required to use this control. |
||
| 77 | * |
||
| 78 | * Normally this is empty and the capability is derived from the capabilities |
||
| 79 | * of the associated `$settings`. |
||
| 80 | * |
||
| 81 | * @since 4.5.0 |
||
| 82 | * @access public |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $capability; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Order priority to load the control in Customizer. |
||
| 89 | * |
||
| 90 | * @since 3.4.0 |
||
| 91 | * @access public |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | public $priority = 10; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Section the control belongs to. |
||
| 98 | * |
||
| 99 | * @since 3.4.0 |
||
| 100 | * @access public |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | public $section = ''; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Label for the control. |
||
| 107 | * |
||
| 108 | * @since 3.4.0 |
||
| 109 | * @access public |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | public $label = ''; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Description for the control. |
||
| 116 | * |
||
| 117 | * @since 4.0.0 |
||
| 118 | * @access public |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | public $description = ''; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * List of choices for 'radio' or 'select' type controls, where values are the keys, and labels are the values. |
||
| 125 | * |
||
| 126 | * @since 3.4.0 |
||
| 127 | * @access public |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | public $choices = array(); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * List of custom input attributes for control output, where attribute names are the keys and values are the values. |
||
| 134 | * |
||
| 135 | * Not used for 'checkbox', 'radio', 'select', 'textarea', or 'dropdown-pages' control types. |
||
| 136 | * |
||
| 137 | * @since 4.0.0 |
||
| 138 | * @access public |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | public $input_attrs = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Show UI for adding new content, currently only used for the dropdown-pages control. |
||
| 145 | * |
||
| 146 | * @since 4.7.0 |
||
| 147 | * @access public |
||
| 148 | * @var bool |
||
| 149 | */ |
||
| 150 | public $allow_addition = false; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @deprecated It is better to just call the json() method |
||
| 154 | * @since 3.4.0 |
||
| 155 | * @access public |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | public $json = array(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Control's Type. |
||
| 162 | * |
||
| 163 | * @since 3.4.0 |
||
| 164 | * @access public |
||
| 165 | * @var string |
||
| 166 | */ |
||
| 167 | public $type = 'text'; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Callback. |
||
| 171 | * |
||
| 172 | * @since 4.0.0 |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @see WP_Customize_Control::active() |
||
| 176 | * |
||
| 177 | * @var callable Callback is called with one argument, the instance of |
||
| 178 | * WP_Customize_Control, and returns bool to indicate whether |
||
| 179 | * the control is active (such as it relates to the URL |
||
| 180 | * currently being previewed). |
||
| 181 | */ |
||
| 182 | public $active_callback = ''; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Constructor. |
||
| 186 | * |
||
| 187 | * Supplied `$args` override class property defaults. |
||
| 188 | * |
||
| 189 | * If `$args['settings']` is not defined, use the $id as the setting ID. |
||
| 190 | * |
||
| 191 | * @since 3.4.0 |
||
| 192 | * |
||
| 193 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
||
| 194 | * @param string $id Control ID. |
||
| 195 | * @param array $args { |
||
| 196 | * Optional. Arguments to override class property defaults. |
||
| 197 | * |
||
| 198 | * @type int $instance_number Order in which this instance was created in relation |
||
| 199 | * to other instances. |
||
| 200 | * @type WP_Customize_Manager $manager Customizer bootstrap instance. |
||
| 201 | * @type string $id Control ID. |
||
| 202 | * @type array $settings All settings tied to the control. If undefined, `$id` will |
||
| 203 | * be used. |
||
| 204 | * @type string $setting The primary setting for the control (if there is one). |
||
| 205 | * Default 'default'. |
||
| 206 | * @type int $priority Order priority to load the control. Default 10. |
||
| 207 | * @type string $section Section the control belongs to. Default empty. |
||
| 208 | * @type string $label Label for the control. Default empty. |
||
| 209 | * @type string $description Description for the control. Default empty. |
||
| 210 | * @type array $choices List of choices for 'radio' or 'select' type controls, where |
||
| 211 | * values are the keys, and labels are the values. |
||
| 212 | * Default empty array. |
||
| 213 | * @type array $input_attrs List of custom input attributes for control output, where |
||
| 214 | * attribute names are the keys and values are the values. Not |
||
| 215 | * used for 'checkbox', 'radio', 'select', 'textarea', or |
||
| 216 | * 'dropdown-pages' control types. Default empty array. |
||
| 217 | * @type array $json Deprecated. Use WP_Customize_Control::json() instead. |
||
| 218 | * @type string $type Control type. Core controls include 'text', 'checkbox', |
||
| 219 | * 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional |
||
| 220 | * input types such as 'email', 'url', 'number', 'hidden', and |
||
| 221 | * 'date' are supported implicitly. Default 'text'. |
||
| 222 | * } |
||
| 223 | */ |
||
| 224 | public function __construct( $manager, $id, $args = array() ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Enqueue control related scripts/styles. |
||
| 259 | * |
||
| 260 | * @since 3.4.0 |
||
| 261 | */ |
||
| 262 | public function enqueue() {} |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Check whether control is active to current Customizer preview. |
||
| 266 | * |
||
| 267 | * @since 4.0.0 |
||
| 268 | * |
||
| 269 | * @return bool Whether the control is active to the current preview. |
||
| 270 | */ |
||
| 271 | final public function active() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Default callback used when invoking WP_Customize_Control::active(). |
||
| 290 | * |
||
| 291 | * Subclasses can override this with their specific logic, or they may |
||
| 292 | * provide an 'active_callback' argument to the constructor. |
||
| 293 | * |
||
| 294 | * @since 4.0.0 |
||
| 295 | * |
||
| 296 | * @return true Always true. |
||
| 297 | */ |
||
| 298 | public function active_callback() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Fetch a setting's value. |
||
| 304 | * Grabs the main setting by default. |
||
| 305 | * |
||
| 306 | * @since 3.4.0 |
||
| 307 | * |
||
| 308 | * @param string $setting_key |
||
| 309 | * @return mixed The requested setting's value, if the setting exists. |
||
| 310 | */ |
||
| 311 | final public function value( $setting_key = 'default' ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Refresh the parameters passed to the JavaScript via JSON. |
||
| 319 | * |
||
| 320 | * @since 3.4.0 |
||
| 321 | */ |
||
| 322 | public function to_json() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the data to export to the client via JSON. |
||
| 344 | * |
||
| 345 | * @since 4.1.0 |
||
| 346 | * |
||
| 347 | * @return array Array of parameters passed to the JavaScript. |
||
| 348 | */ |
||
| 349 | public function json() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Checks if the user can use this control. |
||
| 356 | * |
||
| 357 | * Returns false if the user cannot manipulate one of the associated settings, |
||
| 358 | * or if one of the associated settings does not exist. Also returns false if |
||
| 359 | * the associated section does not exist or if its capability check returns |
||
| 360 | * false. |
||
| 361 | * |
||
| 362 | * @since 3.4.0 |
||
| 363 | * |
||
| 364 | * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true. |
||
| 365 | */ |
||
| 366 | final public function check_capabilities() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the control's content for insertion into the Customizer pane. |
||
| 387 | * |
||
| 388 | * @since 4.1.0 |
||
| 389 | * |
||
| 390 | * @return string Contents of the control. |
||
| 391 | */ |
||
| 392 | final public function get_content() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Check capabilities and render the control. |
||
| 400 | * |
||
| 401 | * @since 3.4.0 |
||
| 402 | * @uses WP_Customize_Control::render() |
||
| 403 | */ |
||
| 404 | final public function maybe_render() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Renders the control wrapper and calls $this->render_content() for the internals. |
||
| 434 | * |
||
| 435 | * @since 3.4.0 |
||
| 436 | */ |
||
| 437 | protected function render() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the data link attribute for a setting. |
||
| 448 | * |
||
| 449 | * @since 3.4.0 |
||
| 450 | * |
||
| 451 | * @param string $setting_key |
||
| 452 | * @return string Data link parameter, if $setting_key is a valid setting, empty string otherwise. |
||
| 453 | */ |
||
| 454 | public function get_link( $setting_key = 'default' ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Render the data link attribute for the control's input element. |
||
| 463 | * |
||
| 464 | * @since 3.4.0 |
||
| 465 | * @uses WP_Customize_Control::get_link() |
||
| 466 | * |
||
| 467 | * @param string $setting_key |
||
| 468 | */ |
||
| 469 | public function link( $setting_key = 'default' ) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Render the custom attributes for the control's input element. |
||
| 475 | * |
||
| 476 | * @since 4.0.0 |
||
| 477 | */ |
||
| 478 | public function input_attrs() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Render the control's content. |
||
| 486 | * |
||
| 487 | * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`. |
||
| 488 | * |
||
| 489 | * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`. |
||
| 490 | * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly. |
||
| 491 | * |
||
| 492 | * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template(). |
||
| 493 | * |
||
| 494 | * @since 3.4.0 |
||
| 495 | */ |
||
| 496 | protected function render_content() { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Render the control's JS template. |
||
| 647 | * |
||
| 648 | * This function is only run for control types that have been registered with |
||
| 649 | * WP_Customize_Manager::register_control_type(). |
||
| 650 | * |
||
| 651 | * In the future, this will also print the template for the control's container |
||
| 652 | * element and be override-able. |
||
| 653 | * |
||
| 654 | * @since 4.1.0 |
||
| 655 | */ |
||
| 656 | final public function print_template() { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * An Underscore (JS) template for this control's content (but not its container). |
||
| 666 | * |
||
| 667 | * Class variables for this control class are available in the `data` JS object; |
||
| 668 | * export custom variables by overriding WP_Customize_Control::to_json(). |
||
| 669 | * |
||
| 670 | * @see WP_Customize_Control::print_template() |
||
| 671 | * |
||
| 672 | * @since 4.1.0 |
||
| 673 | */ |
||
| 674 | protected function content_template() {} |
||
| 675 | |||
| 676 | } |
||
| 677 | |||
| 767 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..