Complex classes like WP_Widget 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_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class WP_Widget { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Root ID for all widgets of this type. |
||
| 24 | * |
||
| 25 | * @since 2.8.0 |
||
| 26 | * @access public |
||
| 27 | * @var mixed|string |
||
| 28 | */ |
||
| 29 | public $id_base; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Name for this widget type. |
||
| 33 | * |
||
| 34 | * @since 2.8.0 |
||
| 35 | * @access public |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | public $name; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Option name for this widget type. |
||
| 42 | * |
||
| 43 | * @since 2.8.0 |
||
| 44 | * @access public |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $option_name; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Alt option name for this widget type. |
||
| 51 | * |
||
| 52 | * @since 2.8.0 |
||
| 53 | * @access public |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | public $alt_option_name; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Option array passed to wp_register_sidebar_widget(). |
||
| 60 | * |
||
| 61 | * @since 2.8.0 |
||
| 62 | * @access public |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | public $widget_options; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Option array passed to wp_register_widget_control(). |
||
| 69 | * |
||
| 70 | * @since 2.8.0 |
||
| 71 | * @access public |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | public $control_options; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Unique ID number of the current instance. |
||
| 78 | * |
||
| 79 | * @since 2.8.0 |
||
| 80 | * @access public |
||
| 81 | * @var bool|int |
||
| 82 | */ |
||
| 83 | public $number = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Unique ID string of the current instance (id_base-number). |
||
| 87 | * |
||
| 88 | * @since 2.8.0 |
||
| 89 | * @access public |
||
| 90 | * @var bool|string |
||
| 91 | */ |
||
| 92 | public $id = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Whether the widget data has been updated. |
||
| 96 | * |
||
| 97 | * Set to true when the data is updated after a POST submit - ensures it does |
||
| 98 | * not happen twice. |
||
| 99 | * |
||
| 100 | * @since 2.8.0 |
||
| 101 | * @access public |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | public $updated = false; |
||
| 105 | |||
| 106 | // |
||
| 107 | // Member functions that must be overriden by subclasses. |
||
| 108 | // |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Echoes the widget content. |
||
| 112 | * |
||
| 113 | * Sub-classes should over-ride this function to generate their widget code. |
||
| 114 | * |
||
| 115 | * @since 2.8.0 |
||
| 116 | * @access public |
||
| 117 | * |
||
| 118 | * @param array $args Display arguments including 'before_title', 'after_title', |
||
| 119 | * 'before_widget', and 'after_widget'. |
||
| 120 | * @param array $instance The settings for the particular instance of the widget. |
||
| 121 | */ |
||
| 122 | public function widget( $args, $instance ) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Updates a particular instance of a widget. |
||
| 128 | * |
||
| 129 | * This function should check that `$new_instance` is set correctly. The newly-calculated |
||
| 130 | * value of `$instance` should be returned. If false is returned, the instance won't be |
||
| 131 | * saved/updated. |
||
| 132 | * |
||
| 133 | * @since 2.8.0 |
||
| 134 | * @access public |
||
| 135 | * |
||
| 136 | * @param array $new_instance New settings for this instance as input by the user via |
||
| 137 | * WP_Widget::form(). |
||
| 138 | * @param array $old_instance Old settings for this instance. |
||
| 139 | * @return array Settings to save or bool false to cancel saving. |
||
| 140 | */ |
||
| 141 | public function update( $new_instance, $old_instance ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Outputs the settings update form. |
||
| 147 | * |
||
| 148 | * @since 2.8.0 |
||
| 149 | * @access public |
||
| 150 | * |
||
| 151 | * @param array $instance Current settings. |
||
| 152 | * @return string Default return is 'noform'. |
||
| 153 | */ |
||
| 154 | public function form( $instance ) { |
||
| 158 | |||
| 159 | // Functions you'll need to call. |
||
| 160 | |||
| 161 | /** |
||
| 162 | * PHP5 constructor. |
||
| 163 | * |
||
| 164 | * @since 2.8.0 |
||
| 165 | * @access public |
||
| 166 | * |
||
| 167 | * @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty, |
||
| 168 | * a portion of the widget's class name will be used Has to be unique. |
||
| 169 | * @param string $name Name for the widget displayed on the configuration page. |
||
| 170 | * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for information |
||
| 171 | * on accepted arguments. Default empty array. |
||
| 172 | * @param array $control_options Optional. Widget control options. See wp_register_widget_control() for |
||
| 173 | * information on accepted arguments. Default empty array. |
||
| 174 | */ |
||
| 175 | public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * PHP4 constructor. |
||
| 185 | * |
||
| 186 | * @since 2.8.0 |
||
| 187 | * @access public |
||
| 188 | * |
||
| 189 | * @see __construct() |
||
| 190 | * |
||
| 191 | * @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty, |
||
| 192 | * a portion of the widget's class name will be used Has to be unique. |
||
| 193 | * @param string $name Name for the widget displayed on the configuration page. |
||
| 194 | * @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for information |
||
| 195 | * on accepted arguments. Default empty array. |
||
| 196 | * @param array $control_options Optional. Widget control options. See wp_register_widget_control() for |
||
| 197 | * information on accepted arguments. Default empty array. |
||
| 198 | */ |
||
| 199 | public function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Constructs name attributes for use in form() fields |
||
| 206 | * |
||
| 207 | * This function should be used in form() methods to create name attributes for fields |
||
| 208 | * to be saved by update() |
||
| 209 | * |
||
| 210 | * @since 2.8.0 |
||
| 211 | * @since 4.4.0 Array format field names are now accepted. |
||
| 212 | * @access public |
||
| 213 | * |
||
| 214 | * @param string $field_name Field name |
||
| 215 | * @return string Name attribute for $field_name |
||
| 216 | */ |
||
| 217 | public function get_field_name($field_name) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Constructs id attributes for use in WP_Widget::form() fields. |
||
| 227 | * |
||
| 228 | * This function should be used in form() methods to create id attributes |
||
| 229 | * for fields to be saved by WP_Widget::update(). |
||
| 230 | * |
||
| 231 | * @since 2.8.0 |
||
| 232 | * @since 4.4.0 Array format field IDs are now accepted. |
||
| 233 | * @access public |
||
| 234 | * |
||
| 235 | * @param string $field_name Field name. |
||
| 236 | * @return string ID attribute for `$field_name`. |
||
| 237 | */ |
||
| 238 | public function get_field_id( $field_name ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Register all widget instances of this widget class. |
||
| 244 | * |
||
| 245 | * @since 2.8.0 |
||
| 246 | * @access public |
||
| 247 | */ |
||
| 248 | public function _register() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Sets the internal order number for the widget instance. |
||
| 276 | * |
||
| 277 | * @since 2.8.0 |
||
| 278 | * @access public |
||
| 279 | * |
||
| 280 | * @param int $number The unique order number of this widget instance compared to other |
||
| 281 | * instances of the same class. |
||
| 282 | */ |
||
| 283 | public function _set($number) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Retrieves the widget display callback. |
||
| 290 | * |
||
| 291 | * @since 2.8.0 |
||
| 292 | * @access public |
||
| 293 | * |
||
| 294 | * @return callable Display callback. |
||
| 295 | */ |
||
| 296 | public function _get_display_callback() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Retrieves the widget update callback. |
||
| 302 | * |
||
| 303 | * @since 2.8.0 |
||
| 304 | * @access public |
||
| 305 | * |
||
| 306 | * @return callable Update callback. |
||
| 307 | */ |
||
| 308 | public function _get_update_callback() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieves the form callback. |
||
| 314 | * |
||
| 315 | * @since 2.8.0 |
||
| 316 | * @access public |
||
| 317 | * |
||
| 318 | * @return callable Form callback. |
||
| 319 | */ |
||
| 320 | public function _get_form_callback() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Determines whether the current request is inside the Customizer preview. |
||
| 326 | * |
||
| 327 | * If true -- the current request is inside the Customizer preview, then |
||
| 328 | * the object cache gets suspended and widgets should check this to decide |
||
| 329 | * whether they should store anything persistently to the object cache, |
||
| 330 | * to transients, or anywhere else. |
||
| 331 | * |
||
| 332 | * @since 3.9.0 |
||
| 333 | * @access public |
||
| 334 | * |
||
| 335 | * @global WP_Customize_Manager $wp_customize |
||
| 336 | * |
||
| 337 | * @return bool True if within the Customizer preview, false if not. |
||
| 338 | */ |
||
| 339 | public function is_preview() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Generates the actual widget content (Do NOT override). |
||
| 346 | * |
||
| 347 | * Finds the instance and calls WP_Widget::widget(). |
||
| 348 | * |
||
| 349 | * @since 2.8.0 |
||
| 350 | * @access public |
||
| 351 | * |
||
| 352 | * @param array $args Display arguments. See WP_Widget::widget() for information |
||
| 353 | * on accepted arguments. |
||
| 354 | * @param int|array $widget_args { |
||
| 355 | * Optional. Internal order number of the widget instance, or array of multi-widget arguments. |
||
| 356 | * Default 1. |
||
| 357 | * |
||
| 358 | * @type int $number Number increment used for multiples of the same widget. |
||
| 359 | * } |
||
| 360 | */ |
||
| 361 | public function display_callback( $args, $widget_args = 1 ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Handles changed settings (Do NOT override). |
||
| 405 | * |
||
| 406 | * @since 2.8.0 |
||
| 407 | * @access public |
||
| 408 | * |
||
| 409 | * @global array $wp_registered_widgets |
||
| 410 | * |
||
| 411 | * @param int $deprecated Not used. |
||
| 412 | */ |
||
| 413 | public function update_callback( $deprecated = 1 ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Generates the widget control form (Do NOT override). |
||
| 490 | * |
||
| 491 | * @since 2.8.0 |
||
| 492 | * @access public |
||
| 493 | * |
||
| 494 | * @param int|array $widget_args { |
||
| 495 | * Optional. Internal order number of the widget instance, or array of multi-widget arguments. |
||
| 496 | * Default 1. |
||
| 497 | * |
||
| 498 | * @type int $number Number increment used for multiples of the same widget. |
||
| 499 | * } |
||
| 500 | * @return string|null |
||
| 501 | */ |
||
| 502 | public function form_callback( $widget_args = 1 ) { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Registers an instance of the widget class. |
||
| 557 | * |
||
| 558 | * @since 2.8.0 |
||
| 559 | * @access public |
||
| 560 | * |
||
| 561 | * @param integer $number Optional. The unique order number of this widget instance |
||
| 562 | * compared to other instances of the same class. Default -1. |
||
| 563 | */ |
||
| 564 | public function _register_one( $number = -1 ) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Saves the settings for all instances of the widget class. |
||
| 572 | * |
||
| 573 | * @since 2.8.0 |
||
| 574 | * @access public |
||
| 575 | * |
||
| 576 | * @param array $settings Multi-dimensional array of widget instance settings. |
||
| 577 | */ |
||
| 578 | public function save_settings( $settings ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Retrieves the settings for all instances of the widget class. |
||
| 585 | * |
||
| 586 | * @since 2.8.0 |
||
| 587 | * @access public |
||
| 588 | * |
||
| 589 | * @return array Multi-dimensional array of widget instance settings. |
||
| 590 | */ |
||
| 591 | public function get_settings() { |
||
| 616 | } |
||
| 617 |