Complex classes like EEW_Upcoming_Events 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 EEW_Upcoming_Events, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 24 | class EEW_Upcoming_Events extends WP_Widget { |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Register widget with WordPress. |
||
| 29 | */ |
||
| 30 | function __construct() { |
||
| 31 | parent::__construct( |
||
| 32 | 'ee-upcoming-events-widget', |
||
| 33 | __( 'Event Espresso Upcoming Events', 'event_espresso' ), |
||
| 34 | array( 'description' => __( 'A widget to display your upcoming events.', 'event_espresso' )), |
||
| 35 | array( |
||
| 36 | 'width' => 300, |
||
| 37 | 'height' => 350, |
||
| 38 | 'id_base' => 'ee-upcoming-events-widget' |
||
| 39 | ) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Back-end widget form. |
||
| 47 | * |
||
| 48 | * @see WP_Widget::form() |
||
| 49 | * @param array $instance Previously saved values from database. |
||
| 50 | * @return string|void |
||
| 51 | */ |
||
| 52 | public function form( $instance ) { |
||
| 53 | |||
| 54 | EE_Registry::instance()->load_helper( 'Form_Fields' ); |
||
| 55 | EE_Registry::instance()->load_class( 'Question_Option', array(), FALSE, FALSE, TRUE ); |
||
| 56 | // Set up some default widget settings. |
||
| 57 | $defaults = array( |
||
| 58 | 'title' => __('Upcoming Events', 'event_espresso'), |
||
| 59 | 'category_name' => '', |
||
| 60 | 'show_expired' => FALSE, |
||
| 61 | 'show_desc' => TRUE, |
||
| 62 | 'show_dates' => TRUE, |
||
| 63 | 'show_everywhere' => FALSE, |
||
| 64 | 'date_limit' => 2, |
||
| 65 | 'limit' => 10, |
||
| 66 | 'date_range' => FALSE, |
||
| 67 | 'image_size' => 'medium' |
||
| 68 | ); |
||
| 69 | |||
| 70 | $instance = wp_parse_args( (array) $instance, $defaults ); |
||
| 71 | // don't add HTML labels for EE_Form_Fields generated inputs |
||
| 72 | add_filter( 'FHEE__EEH_Form_Fields__label_html', '__return_empty_string' ); |
||
| 73 | $yes_no_values = array( |
||
| 74 | EE_Question_Option::new_instance( array( 'QSO_value' => FALSE, 'QSO_desc' => __('No', 'event_espresso'))), |
||
| 75 | EE_Question_Option::new_instance( array( 'QSO_value' => TRUE, 'QSO_desc' => __('Yes', 'event_espresso'))) |
||
| 76 | ); |
||
| 77 | |||
| 78 | ?> |
||
| 79 | |||
| 80 | <!-- Widget Title: Text Input --> |
||
| 81 | |||
| 82 | <p> |
||
| 83 | <label for="<?php echo $this->get_field_id('title'); ?>"> |
||
| 84 | <?php _e('Title:', 'event_espresso'); ?> |
||
| 85 | </label> |
||
| 86 | <input id="<?php echo $this->get_field_id('title'); ?>" class="widefat" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" type="text" /> |
||
| 87 | </p> |
||
| 88 | <p> |
||
| 89 | <label for="<?php echo $this->get_field_id('category_name'); ?>"> |
||
| 90 | <?php _e('Event Category:', 'event_espresso'); ?> |
||
| 91 | </label> |
||
| 92 | <?php |
||
| 93 | $event_categories = array(); |
||
| 94 | /** @type EEM_Term $EEM_Term */ |
||
| 95 | $EEM_Term = EE_Registry::instance()->load_model( 'Term' ); |
||
| 96 | $categories = $EEM_Term->get_all_ee_categories( TRUE ); |
||
| 97 | if ( $categories ) { |
||
|
|
|||
| 98 | foreach ( $categories as $category ) { |
||
| 99 | if ( $category instanceof EE_Term ) { |
||
| 100 | $event_categories[] = EE_Question_Option::new_instance( array( 'QSO_value' => $category->get( 'slug' ), 'QSO_desc' => $category->get( 'name' ))); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | array_unshift( $event_categories, EE_Question_Option::new_instance( array( 'QSO_value' => '', 'QSO_desc' => __(' - display all - ', 'event_espresso')))); |
||
| 105 | echo EEH_Form_Fields::select( |
||
| 106 | __('Event Category:', 'event_espresso'), |
||
| 107 | $instance['category_name'], |
||
| 108 | $event_categories, |
||
| 109 | $this->get_field_name('category_name'), |
||
| 110 | $this->get_field_id('category_name') |
||
| 111 | ); |
||
| 112 | ?> |
||
| 113 | </p> |
||
| 114 | <p> |
||
| 115 | <label for="<?php echo $this->get_field_id('limit'); ?>"> |
||
| 116 | <?php _e('Number of Events to Display:', 'event_espresso'); ?> |
||
| 117 | </label> |
||
| 118 | <input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" value="<?php echo $instance['limit']; ?>" size="3" type="text" /> |
||
| 119 | </p> |
||
| 120 | <p> |
||
| 121 | <label for="<?php echo $this->get_field_id('show_expired'); ?>"> |
||
| 122 | <?php _e('Show Expired Events:', 'event_espresso'); ?> |
||
| 123 | </label> |
||
| 124 | <?php |
||
| 125 | echo EEH_Form_Fields::select( |
||
| 126 | __('Show Expired Events:', 'event_espresso'), |
||
| 127 | $instance['show_expired'], |
||
| 128 | $yes_no_values, |
||
| 129 | $this->get_field_name('show_expired'), |
||
| 130 | $this->get_field_id('show_expired') |
||
| 131 | ); |
||
| 132 | ?> |
||
| 133 | </p> |
||
| 134 | <p> |
||
| 135 | <label for="<?php echo $this->get_field_id('image_size'); ?>"> |
||
| 136 | <?php _e('Image Size:', 'event_espresso'); ?> |
||
| 137 | </label> |
||
| 138 | <?php |
||
| 139 | $image_sizes = array(); |
||
| 140 | $sizes = get_intermediate_image_sizes(); |
||
| 141 | if ( $sizes ) { |
||
| 142 | // loop thru images and create option objects out of them |
||
| 143 | foreach ( $sizes as $image_size ) { |
||
| 144 | $image_size = trim( $image_size ); |
||
| 145 | // no big images plz |
||
| 146 | if ( ! in_array( $image_size, array( 'large', 'post-thumbnail' ))) { |
||
| 147 | $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => $image_size, 'QSO_desc' => $image_size )); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => 'none', 'QSO_desc' => __('don\'t show images', 'event_espresso') )); |
||
| 151 | } |
||
| 152 | echo EEH_Form_Fields::select( |
||
| 153 | __('Image Size:', 'event_espresso'), |
||
| 154 | $instance['image_size'], |
||
| 155 | $image_sizes, |
||
| 156 | $this->get_field_name('image_size'), |
||
| 157 | $this->get_field_id('image_size') |
||
| 158 | ); |
||
| 159 | ?> |
||
| 160 | |||
| 161 | </p> |
||
| 162 | <p> |
||
| 163 | <label for="<?php echo $this->get_field_id('show_desc'); ?>"> |
||
| 164 | <?php _e('Show Description:', 'event_espresso'); ?> |
||
| 165 | </label> |
||
| 166 | <?php |
||
| 167 | echo EEH_Form_Fields::select( |
||
| 168 | __('Show Description:', 'event_espresso'), |
||
| 169 | $instance['show_desc'], |
||
| 170 | $yes_no_values, |
||
| 171 | $this->get_field_name('show_desc'), |
||
| 172 | $this->get_field_id('show_desc') |
||
| 173 | ); |
||
| 174 | ?> |
||
| 175 | </p> |
||
| 176 | <p> |
||
| 177 | <label for="<?php echo $this->get_field_id('show_dates'); ?>"> |
||
| 178 | <?php _e('Show Dates:', 'event_espresso'); ?> |
||
| 179 | </label> |
||
| 180 | <?php |
||
| 181 | echo EEH_Form_Fields::select( |
||
| 182 | __('Show Dates:', 'event_espresso'), |
||
| 183 | $instance['show_dates'], |
||
| 184 | $yes_no_values, |
||
| 185 | $this->get_field_name('show_dates'), |
||
| 186 | $this->get_field_id('show_dates') |
||
| 187 | ); |
||
| 188 | ?> |
||
| 189 | </p> |
||
| 190 | <p> |
||
| 191 | <label for="<?php echo $this->get_field_id('show_everywhere'); ?>"> |
||
| 192 | <?php _e('Show on all Pages:', 'event_espresso'); ?> |
||
| 193 | </label> |
||
| 194 | <?php |
||
| 195 | echo EEH_Form_Fields::select( |
||
| 196 | __('Show on all Pages:', 'event_espresso'), |
||
| 197 | $instance['show_everywhere'], |
||
| 198 | $yes_no_values, |
||
| 199 | $this->get_field_name('show_everywhere'), |
||
| 200 | $this->get_field_id('show_everywhere') |
||
| 201 | ); |
||
| 202 | ?> |
||
| 203 | </p> |
||
| 204 | <p> |
||
| 205 | <label for="<?php echo $this->get_field_id('date_limit'); ?>"> |
||
| 206 | <?php _e('Number of Dates to Display:', 'event_espresso'); ?> |
||
| 207 | </label> |
||
| 208 | <input id="<?php echo $this->get_field_id('date_limit'); ?>" name="<?php echo $this->get_field_name('date_limit'); ?>" value="<?php echo esc_attr( $instance['date_limit'] ); ?>" size="3" type="text" /> |
||
| 209 | </p> |
||
| 210 | <p> |
||
| 211 | <label for="<?php echo $this->get_field_id('date_range'); ?>"> |
||
| 212 | <?php _e('Show Date Range:', 'event_espresso'); ?> |
||
| 213 | </label> |
||
| 214 | <?php |
||
| 215 | echo EEH_Form_Fields::select( |
||
| 216 | __('Show Date Range:', 'event_espresso'), |
||
| 217 | $instance['date_range'], |
||
| 218 | $yes_no_values, |
||
| 219 | $this->get_field_name('date_range'), |
||
| 220 | $this->get_field_id('date_range') |
||
| 221 | ); |
||
| 222 | ?><span class="description"><br /><?php _e('This setting will replace the list of dates in the widget.', 'event_espresso'); ?></span> |
||
| 223 | </p> |
||
| 224 | |||
| 225 | <?php |
||
| 226 | } |
||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Sanitize widget form values as they are saved. |
||
| 232 | * |
||
| 233 | * @see WP_Widget::update() |
||
| 234 | * |
||
| 235 | * @param array $new_instance Values just sent to be saved. |
||
| 236 | * @param array $old_instance Previously saved values from database. |
||
| 237 | * |
||
| 238 | * @return array Updated safe values to be saved. |
||
| 239 | */ |
||
| 240 | public function update( $new_instance, $old_instance ) { |
||
| 241 | $instance = $old_instance; |
||
| 242 | $instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; |
||
| 243 | $instance['category_name'] = $new_instance['category_name']; |
||
| 244 | $instance['show_expired'] = $new_instance['show_expired']; |
||
| 245 | $instance['limit'] = $new_instance['limit']; |
||
| 246 | $instance['image_size'] = $new_instance['image_size']; |
||
| 247 | $instance['show_desc'] = $new_instance['show_desc']; |
||
| 248 | $instance['show_dates'] = $new_instance['show_dates']; |
||
| 249 | $instance['show_everywhere'] = $new_instance['show_everywhere']; |
||
| 250 | $instance['date_limit'] = $new_instance['date_limit']; |
||
| 251 | $instance['date_range'] = $new_instance['date_range']; |
||
| 252 | return $instance; |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Front-end display of widget. |
||
| 259 | * |
||
| 260 | * @see WP_Widget::widget() |
||
| 261 | * |
||
| 262 | * @param array $args Widget arguments. |
||
| 263 | * @param array $instance Saved values from database. |
||
| 264 | */ |
||
| 265 | public function widget( $args, $instance ) { |
||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * make_the_title_a_link |
||
| 382 | * callback for widget_title filter |
||
| 383 | * |
||
| 384 | * @param $title |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function make_the_title_a_link($title) { |
||
| 390 | |||
| 391 | } |
||
| 392 | // End of file EEW_Upcoming_Events.widget.php |
||
| 393 | // Location: /widgets/upcoming_events/EEW_Upcoming_Events.widget.php |
||
| 394 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.