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() { |
||
| 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_class( 'Question_Option', array(), FALSE, FALSE, TRUE ); |
||
| 55 | // Set up some default widget settings. |
||
| 56 | $defaults = array( |
||
| 57 | 'title' => __('Upcoming Events', 'event_espresso'), |
||
| 58 | 'category_name' => '', |
||
| 59 | 'show_expired' => FALSE, |
||
| 60 | 'show_desc' => TRUE, |
||
| 61 | 'show_dates' => TRUE, |
||
| 62 | 'show_everywhere' => FALSE, |
||
| 63 | 'date_limit' => 2, |
||
| 64 | 'limit' => 10, |
||
| 65 | 'date_range' => FALSE, |
||
| 66 | 'image_size' => 'medium' |
||
| 67 | ); |
||
| 68 | |||
| 69 | $instance = wp_parse_args( (array) $instance, $defaults ); |
||
| 70 | // don't add HTML labels for EE_Form_Fields generated inputs |
||
| 71 | add_filter( 'FHEE__EEH_Form_Fields__label_html', '__return_empty_string' ); |
||
| 72 | $yes_no_values = array( |
||
| 73 | EE_Question_Option::new_instance( array( 'QSO_value' => FALSE, 'QSO_desc' => __('No', 'event_espresso'))), |
||
| 74 | EE_Question_Option::new_instance( array( 'QSO_value' => TRUE, 'QSO_desc' => __('Yes', 'event_espresso'))) |
||
| 75 | ); |
||
| 76 | |||
| 77 | ?> |
||
| 78 | |||
| 79 | <!-- Widget Title: Text Input --> |
||
| 80 | |||
| 81 | <p> |
||
| 82 | <label for="<?php echo $this->get_field_id('title'); ?>"> |
||
| 83 | <?php _e('Title:', 'event_espresso'); ?> |
||
| 84 | </label> |
||
| 85 | <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" /> |
||
| 86 | </p> |
||
| 87 | <p> |
||
| 88 | <label for="<?php echo $this->get_field_id('category_name'); ?>"> |
||
| 89 | <?php _e('Event Category:', 'event_espresso'); ?> |
||
| 90 | </label> |
||
| 91 | <?php |
||
| 92 | $event_categories = array(); |
||
| 93 | /** @type EEM_Term $EEM_Term */ |
||
| 94 | $EEM_Term = EE_Registry::instance()->load_model( 'Term' ); |
||
| 95 | $categories = $EEM_Term->get_all_ee_categories( TRUE ); |
||
| 96 | if ( $categories ) { |
||
|
|
|||
| 97 | foreach ( $categories as $category ) { |
||
| 98 | if ( $category instanceof EE_Term ) { |
||
| 99 | $event_categories[] = EE_Question_Option::new_instance( array( 'QSO_value' => $category->get( 'slug' ), 'QSO_desc' => $category->get( 'name' ))); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | array_unshift( $event_categories, EE_Question_Option::new_instance( array( 'QSO_value' => '', 'QSO_desc' => __(' - display all - ', 'event_espresso')))); |
||
| 104 | echo EEH_Form_Fields::select( |
||
| 105 | __('Event Category:', 'event_espresso'), |
||
| 106 | $instance['category_name'], |
||
| 107 | $event_categories, |
||
| 108 | $this->get_field_name('category_name'), |
||
| 109 | $this->get_field_id('category_name') |
||
| 110 | ); |
||
| 111 | ?> |
||
| 112 | </p> |
||
| 113 | <p> |
||
| 114 | <label for="<?php echo $this->get_field_id('limit'); ?>"> |
||
| 115 | <?php _e('Number of Events to Display:', 'event_espresso'); ?> |
||
| 116 | </label> |
||
| 117 | <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" /> |
||
| 118 | </p> |
||
| 119 | <p> |
||
| 120 | <label for="<?php echo $this->get_field_id('show_expired'); ?>"> |
||
| 121 | <?php _e('Show Expired Events:', 'event_espresso'); ?> |
||
| 122 | </label> |
||
| 123 | <?php |
||
| 124 | echo EEH_Form_Fields::select( |
||
| 125 | __('Show Expired Events:', 'event_espresso'), |
||
| 126 | $instance['show_expired'], |
||
| 127 | $yes_no_values, |
||
| 128 | $this->get_field_name('show_expired'), |
||
| 129 | $this->get_field_id('show_expired') |
||
| 130 | ); |
||
| 131 | ?> |
||
| 132 | </p> |
||
| 133 | <p> |
||
| 134 | <label for="<?php echo $this->get_field_id('image_size'); ?>"> |
||
| 135 | <?php _e('Image Size:', 'event_espresso'); ?> |
||
| 136 | </label> |
||
| 137 | <?php |
||
| 138 | $image_sizes = array(); |
||
| 139 | $sizes = get_intermediate_image_sizes(); |
||
| 140 | if ( $sizes ) { |
||
| 141 | // loop thru images and create option objects out of them |
||
| 142 | foreach ( $sizes as $image_size ) { |
||
| 143 | $image_size = trim( $image_size ); |
||
| 144 | // no big images plz |
||
| 145 | if ( ! in_array( $image_size, array( 'large', 'post-thumbnail' ))) { |
||
| 146 | $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => $image_size, 'QSO_desc' => $image_size )); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | $image_sizes[] = EE_Question_Option::new_instance( array( 'QSO_value' => 'none', 'QSO_desc' => __('don\'t show images', 'event_espresso') )); |
||
| 150 | } |
||
| 151 | echo EEH_Form_Fields::select( |
||
| 152 | __('Image Size:', 'event_espresso'), |
||
| 153 | $instance['image_size'], |
||
| 154 | $image_sizes, |
||
| 155 | $this->get_field_name('image_size'), |
||
| 156 | $this->get_field_id('image_size') |
||
| 157 | ); |
||
| 158 | ?> |
||
| 159 | |||
| 160 | </p> |
||
| 161 | <p> |
||
| 162 | <label for="<?php echo $this->get_field_id('show_desc'); ?>"> |
||
| 163 | <?php _e('Show Description:', 'event_espresso'); ?> |
||
| 164 | </label> |
||
| 165 | <?php |
||
| 166 | echo EEH_Form_Fields::select( |
||
| 167 | __('Show Description:', 'event_espresso'), |
||
| 168 | $instance['show_desc'], |
||
| 169 | $yes_no_values, |
||
| 170 | $this->get_field_name('show_desc'), |
||
| 171 | $this->get_field_id('show_desc') |
||
| 172 | ); |
||
| 173 | ?> |
||
| 174 | </p> |
||
| 175 | <p> |
||
| 176 | <label for="<?php echo $this->get_field_id('show_dates'); ?>"> |
||
| 177 | <?php _e('Show Dates:', 'event_espresso'); ?> |
||
| 178 | </label> |
||
| 179 | <?php |
||
| 180 | echo EEH_Form_Fields::select( |
||
| 181 | __('Show Dates:', 'event_espresso'), |
||
| 182 | $instance['show_dates'], |
||
| 183 | $yes_no_values, |
||
| 184 | $this->get_field_name('show_dates'), |
||
| 185 | $this->get_field_id('show_dates') |
||
| 186 | ); |
||
| 187 | ?> |
||
| 188 | </p> |
||
| 189 | <p> |
||
| 190 | <label for="<?php echo $this->get_field_id('show_everywhere'); ?>"> |
||
| 191 | <?php _e('Show on all Pages:', 'event_espresso'); ?> |
||
| 192 | </label> |
||
| 193 | <?php |
||
| 194 | echo EEH_Form_Fields::select( |
||
| 195 | __('Show on all Pages:', 'event_espresso'), |
||
| 196 | $instance['show_everywhere'], |
||
| 197 | $yes_no_values, |
||
| 198 | $this->get_field_name('show_everywhere'), |
||
| 199 | $this->get_field_id('show_everywhere') |
||
| 200 | ); |
||
| 201 | ?> |
||
| 202 | </p> |
||
| 203 | <p> |
||
| 204 | <label for="<?php echo $this->get_field_id('date_limit'); ?>"> |
||
| 205 | <?php _e('Number of Dates to Display:', 'event_espresso'); ?> |
||
| 206 | </label> |
||
| 207 | <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" /> |
||
| 208 | </p> |
||
| 209 | <p> |
||
| 210 | <label for="<?php echo $this->get_field_id('date_range'); ?>"> |
||
| 211 | <?php _e('Show Date Range:', 'event_espresso'); ?> |
||
| 212 | </label> |
||
| 213 | <?php |
||
| 214 | echo EEH_Form_Fields::select( |
||
| 215 | __('Show Date Range:', 'event_espresso'), |
||
| 216 | $instance['date_range'], |
||
| 217 | $yes_no_values, |
||
| 218 | $this->get_field_name('date_range'), |
||
| 219 | $this->get_field_id('date_range') |
||
| 220 | ); |
||
| 221 | ?><span class="description"><br /><?php _e('This setting will replace the list of dates in the widget.', 'event_espresso'); ?></span> |
||
| 222 | </p> |
||
| 223 | |||
| 224 | <?php |
||
| 225 | } |
||
| 226 | |||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Sanitize widget form values as they are saved. |
||
| 231 | * |
||
| 232 | * @see WP_Widget::update() |
||
| 233 | * |
||
| 234 | * @param array $new_instance Values just sent to be saved. |
||
| 235 | * @param array $old_instance Previously saved values from database. |
||
| 236 | * |
||
| 237 | * @return array Updated safe values to be saved. |
||
| 238 | */ |
||
| 239 | public function update( $new_instance, $old_instance ) { |
||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Front-end display of widget. |
||
| 258 | * |
||
| 259 | * @see WP_Widget::widget() |
||
| 260 | * |
||
| 261 | * @param array $args Widget arguments. |
||
| 262 | * @param array $instance Saved values from database. |
||
| 263 | */ |
||
| 264 | public function widget( $args, $instance ) { |
||
| 265 | |||
| 266 | global $post; |
||
| 267 | // make sure there is some kinda post object |
||
| 268 | if ( $post instanceof WP_Post ) { |
||
| 269 | $before_widget = ''; |
||
| 270 | $before_title = ''; |
||
| 271 | $after_title = ''; |
||
| 272 | $after_widget = ''; |
||
| 273 | // but NOT an events archives page, cuz that would be like two event lists on the same page |
||
| 274 | $show_everywhere = isset( $instance['show_everywhere'] ) ? (bool) absint( $instance['show_everywhere'] ) : TRUE; |
||
| 275 | if ( $show_everywhere || ! ( $post->post_type == 'espresso_events' && is_archive() )) { |
||
| 276 | // let's use some of the event helper functions' |
||
| 277 | // make separate vars out of attributes |
||
| 278 | |||
| 279 | |||
| 280 | extract($args); |
||
| 281 | |||
| 282 | // add function to make the title a link |
||
| 283 | add_filter('widget_title', array($this, 'make_the_title_a_link'), 15); |
||
| 284 | |||
| 285 | // filter the title |
||
| 286 | $title = apply_filters('widget_title', $instance['title']); |
||
| 287 | |||
| 288 | // remove the function from the filter, so it does not affect other widgets |
||
| 289 | remove_filter('widget_title', array($this, 'make_the_title_a_link'), 15); |
||
| 290 | |||
| 291 | // Before widget (defined by themes). |
||
| 292 | echo $before_widget; |
||
| 293 | // Display the widget title if one was input (before and after defined by themes). |
||
| 294 | if ( ! empty( $title )) { |
||
| 295 | echo $before_title . $title . $after_title; |
||
| 296 | } |
||
| 297 | // grab widget settings |
||
| 298 | $category = isset( $instance['category_name'] ) && ! empty( $instance['category_name'] ) ? $instance['category_name'] : FALSE; |
||
| 299 | $show_expired = isset( $instance['show_expired'] ) ? (bool) absint( $instance['show_expired'] ) : FALSE; |
||
| 300 | $image_size = isset( $instance['image_size'] ) && ! empty( $instance['image_size'] ) ? $instance['image_size'] : 'medium'; |
||
| 301 | $show_desc = isset( $instance['show_desc'] ) ? (bool) absint( $instance['show_desc'] ) : TRUE; |
||
| 302 | $show_dates = isset( $instance['show_dates'] ) ? (bool) absint( $instance['show_dates'] ) : TRUE; |
||
| 303 | $date_limit = isset( $instance['date_limit'] ) && ! empty( $instance['date_limit'] ) ? $instance['date_limit'] : NULL; |
||
| 304 | $date_range = isset( $instance['date_range'] ) && ! empty( $instance['date_range'] ) ? $instance['date_range'] : FALSE; |
||
| 305 | // start to build our where clause |
||
| 306 | $where = array( |
||
| 307 | // 'Datetime.DTT_is_primary' => 1, |
||
| 308 | 'status' => 'publish' |
||
| 309 | ); |
||
| 310 | // add category |
||
| 311 | if ( $category ) { |
||
| 312 | $where['Term_Taxonomy.taxonomy'] = 'espresso_event_categories'; |
||
| 313 | $where['Term_Taxonomy.Term.slug'] = $category; |
||
| 314 | } |
||
| 315 | // if NOT expired then we want events that start today or in the future |
||
| 316 | if ( ! $show_expired ) { |
||
| 317 | $where['Datetime.DTT_EVT_end'] = array( '>=', EEM_Datetime::instance()->current_time_for_query( 'DTT_EVT_end' ) ); |
||
| 318 | } |
||
| 319 | // run the query |
||
| 320 | $events = EE_Registry::instance()->load_model( 'Event' )->get_all( array( |
||
| 321 | $where, |
||
| 322 | 'limit' => $instance['limit'] > 0 ? '0,' . $instance['limit'] : '0,10', |
||
| 323 | 'order_by' => 'Datetime.DTT_EVT_start', |
||
| 324 | 'order' => 'ASC', |
||
| 325 | 'group_by' => 'EVT_ID' |
||
| 326 | )); |
||
| 327 | |||
| 328 | if ( ! empty( $events )) { |
||
| 329 | echo '<ul class="ee-upcoming-events-widget-ul">'; |
||
| 330 | foreach ( $events as $event ) { |
||
| 331 | if ( $event instanceof EE_Event && ( !is_single() || $post->ID != $event->ID() ) ) { |
||
| 332 | //printr( $event, '$event <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 333 | echo '<li id="ee-upcoming-events-widget-li-' . $event->ID() . '" class="ee-upcoming-events-widget-li">'; |
||
| 334 | // how big is the event name ? |
||
| 335 | $name_length = strlen( $event->name() ); |
||
| 336 | switch( $name_length ) { |
||
| 337 | case $name_length > 70 : |
||
| 338 | $len_class = ' three-line'; |
||
| 339 | break; |
||
| 340 | case $name_length > 35 : |
||
| 341 | $len_class = ' two-line'; |
||
| 342 | break; |
||
| 343 | default : |
||
| 344 | $len_class = ' one-line'; |
||
| 345 | } |
||
| 346 | echo '<h5 class="ee-upcoming-events-widget-title-h5"><a class="ee-widget-event-name-a' . $len_class . '" href="' . get_permalink( $event->ID() ) . '">' . $event->name() . '</a></h5>'; |
||
| 347 | if ( has_post_thumbnail( $event->ID() ) && $image_size != 'none' ) { |
||
| 348 | echo '<div class="ee-upcoming-events-widget-img-dv"><a class="ee-upcoming-events-widget-img" href="' . get_permalink( $event->ID() ) . '">' . get_the_post_thumbnail( $event->ID(), $image_size ) . '</a></div>'; |
||
| 349 | } |
||
| 350 | $desc = $event->short_description( 25 ); |
||
| 351 | if ( $show_dates ) { |
||
| 352 | $date_format = apply_filters( 'FHEE__espresso_event_date_range__date_format', get_option( 'date_format' )); |
||
| 353 | $time_format = apply_filters( 'FHEE__espresso_event_date_range__time_format', get_option( 'time_format' )); |
||
| 354 | $single_date_format = apply_filters( 'FHEE__espresso_event_date_range__single_date_format', get_option( 'date_format' )); |
||
| 355 | $single_time_format = apply_filters( 'FHEE__espresso_event_date_range__single_time_format', get_option( 'time_format' )); |
||
| 356 | if ( $date_range == TRUE ) { |
||
| 357 | echo espresso_event_date_range( $date_format, $time_format, $single_date_format, $single_time_format, $event->ID() ); |
||
| 358 | }else{ |
||
| 359 | echo espresso_list_of_event_dates( $event->ID(), $date_format, $time_format, FALSE, NULL, TRUE, TRUE, $date_limit ); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | if ( $show_desc && $desc ) { |
||
| 363 | echo '<p style="margin-top: .5em">' . $desc . '</p>'; |
||
| 364 | } |
||
| 365 | echo '</li>'; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | echo '</ul>'; |
||
| 369 | } |
||
| 370 | // After widget (defined by themes). |
||
| 371 | echo $after_widget; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * make_the_title_a_link |
||
| 380 | * callback for widget_title filter |
||
| 381 | * |
||
| 382 | * @param $title |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function make_the_title_a_link($title) { |
||
| 388 | |||
| 389 | } |
||
| 390 | // End of file EEW_Upcoming_Events.widget.php |
||
| 392 |
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.