| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 111 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 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 | |||
| 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.