Completed
Pull Request — master (#206)
by Kiran
05:47
created

geodir_bestof_widget::widget()   F

Complexity

Conditions 41
Paths > 20000

Size

Total Lines 295
Code Lines 150

Duplication

Lines 18
Ratio 6.1 %
Metric Value
dl 18
loc 295
rs 2
cc 41
eloc 150
nc 213909504
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * GeoDirectory Best of widget
4
 *
5
 * @since 1.3.9
6
 *
7
 * @package GeoDirectory
8
 */
9
10
/**
11
 * GeoDirectory Best of widget widget class.
12
 *
13
 * @since 1.3.9
14
 */
15
class geodir_bestof_widget extends WP_Widget
16
{
17
    /**
18
	 * Register the best of widget with WordPress.
19
	 *
20
	 * @since 1.3.9
21
     * @since 1.5.1 Changed from PHP4 style constructors to PHP5 __construct.
22
	 */
23
    public function __construct() {
24
        $widget_ops = array('classname' => 'geodir_bestof_widget', 'description' => __('GD > Best of widget', 'geodirectory'));
25
        parent::__construct(
26
            'bestof_widget', // Base ID
27
            __('GD > Best of widget', 'geodirectory'), // Name
28
            $widget_ops// Args
29
        );
30
    }
31
32
	/**
33
	 * Front-end display content for best of widget.
34
	 *
35
	 * @since 1.3.9
36
     * @since 1.5.1 Added filter to view all link.
37
     * @since 1.5.1 Declare function public.
38
	 *
39
	 * @param array $args     Widget arguments.
40
	 * @param array $instance Saved values from database.
41
	 */
42
	public function widget($args, $instance)
43
    {
44
        extract($args);
45
		/**
46
		 * Filter the best of widget tab layout.
47
		 *
48
		 * @since 1.3.9
49
		 *
50
		 * @param string $instance['tab_layout'] Best of widget tab layout name.
51
		 */
52
		$tab_layout = empty($instance['tab_layout']) ? 'bestof-tabs-on-top' : apply_filters('bestof_widget_tab_layout', $instance['tab_layout']);
53
        echo '<div class="bestof-widget-tab-layout ' . $tab_layout . '">';
54
        echo $before_widget;
55
        $loc_terms = geodir_get_current_location_terms();
56
        if ($loc_terms) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $loc_terms of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
57
            $cur_location = ' : ' . geodir_ucwords(str_replace('-', ' ', end($loc_terms)));
58
        } else {
59
            $cur_location = '';
60
        }
61
		
62
		/**
63
		 * Filter the current location name.
64
		 *
65
		 * @since 1.3.9
66
		 *
67
		 * @param string $cur_location Current location name.
68
		 */
69
		$cur_location = apply_filters('bestof_widget_cur_location', $cur_location);
70
71
        /**
72
		 * Filter the widget title.
73
		 *
74
		 * @since 1.3.9
75
		 *
76
		 * @param string $instance['title'] The widget title.
77
		 */
78
		$title = empty($instance['title']) ? wp_sprintf( __( 'Best of %s', 'geodirectory' ), get_bloginfo('name') . $cur_location ) : apply_filters('bestof_widget_title', __($instance['title'], 'geodirectory'));
79
        
80
		/**
81
		 * Filter the post type.
82
		 *
83
		 * @since 1.3.9
84
		 *
85
		 * @param string $instance['post_type'] The post type.
86
		 */
87
		$post_type = empty($instance['post_type']) ? 'gd_place' : apply_filters('bestof_widget_post_type', $instance['post_type']);
88
89
        /**
90
         * Filter the excerpt type.
91
         *
92
         * @since 1.5.4
93
         *
94
         * @param string $instance['excerpt_type'] The excerpt type.
95
         */
96
        $excerpt_type = empty($instance['excerpt_type']) ? 'show-desc' : apply_filters('bestof_widget_excerpt_type', $instance['excerpt_type']);
97
98
99
        /**
100
		 * Filter the listing limit.
101
		 *
102
		 * @since 1.3.9
103
		 *
104
		 * @param int $instance['post_limit'] No. of posts to display.
105
		 */
106
		$post_limit = empty($instance['post_limit']) ? '5' : apply_filters('bestof_widget_post_limit', $instance['post_limit']);
107
        
108
		/**
109
		 * Filter the category limit.
110
		 *
111
		 * @since 1.3.9
112
		 *
113
		 * @param int $instance['categ_limit'] No. of categories to display.
114
		 */
115
		$categ_limit = empty($instance['categ_limit']) ? '3' : apply_filters('bestof_widget_categ_limit', $instance['categ_limit']);
116
        $use_viewing_post_type = !empty($instance['use_viewing_post_type']) ? true : false;
117
        
118
		/**
119
		 * Filter the use of location filter.
120
		 *
121
		 * @since 1.3.9
122
		 *
123
		 * @param int|bool $instance['add_location_filter'] Filter listings using current location.
124
		 */
125
		$add_location_filter = empty($instance['add_location_filter']) ? '1' : apply_filters('bestof_widget_location_filter', $instance['add_location_filter']);
126
127
        // set post type to current viewing post type
128 View Code Duplication
        if ($use_viewing_post_type) {
129
            $current_post_type = geodir_get_current_posttype();
130
            if ($current_post_type != '' && $current_post_type != $post_type) {
131
                $post_type = $current_post_type;
132
            }
133
        }
134
135 View Code Duplication
        if (isset($instance['character_count'])) {
136
			/**
137
			 * Filter the widget's excerpt character count.
138
			 *
139
			 * @since 1.3.9
140
             *
141
			 * @param int $instance['character_count'] Excerpt character count.
142
			 */
143
			$character_count = apply_filters('bestof_widget_list_character_count', $instance['character_count']);
144
        } else {
145
            $character_count = '';
146
        }
147
148
        $category_taxonomy = geodir_get_taxonomies($post_type);
149
150
        $term_args = array(
151
            'hide_empty' => true,
152
            'parent' => 0
153
        );
154
155
        if (is_tax()) {
156
            $taxonomy = get_query_var('taxonomy');
157
            $cur_term = get_query_var('term');
158
            $term_data = get_term_by('name', $cur_term, $taxonomy);
159
            $term_args['parent'] = $term_data->term_id;
160
        }
161
162
        $terms = get_terms($category_taxonomy[0], $term_args);
163
164
        $term_reviews = geodir_count_reviews_by_terms();
165
        $a_terms = array();
166
        foreach ($terms as $term) {
167
168
169
            if ($term->count > 0) {
170
                if (isset($term_reviews[$term->term_id])) {
171
                    $term->review_count = $term_reviews[$term->term_id];
172
                } else {
173
                    $term->review_count = '0';
174
                }
175
176
                $a_terms[] = $term;
177
            }
178
179
        }
180
181
182
        $terms = geodir_sort_terms($a_terms, 'review_count');
183
184
        $query_args = array(
185
            'posts_per_page' => $post_limit,
186
            'is_geodir_loop' => true,
187
            'post_type' => $post_type,
188
            'gd_location' => $add_location_filter ? true : false,
189
            'order_by' => 'high_review'
190
        );
191
        if ($character_count >= 0) {
192
            $query_args['excerpt_length'] = $character_count;
193
        }
194
195
        $layout = array();
196
        if ($tab_layout == 'bestof-tabs-as-dropdown') {
197
            $layout[] = $tab_layout;
198
        } else {
199
            $layout[] = 'bestof-tabs-as-dropdown';
200
            $layout[] = $tab_layout;
201
        }
202
203
204
        echo $before_title . __($title) . $after_title;
205
206
        //term navigation - start
207
        echo '<div class="geodir-tabs gd-bestof-tabs" id="gd-bestof-tabs" style="position:relative;">';
208
209
        $final_html = '';
210
        foreach ($layout as $tab_layout) {
211
            $nav_html = '';
212
            $is_dropdown = ($tab_layout == 'bestof-tabs-as-dropdown') ? true : false;
213
214
            if ($is_dropdown) {
215
                $nav_html .= '<select id="geodir_bestof_tab_dd" class="chosen_select" name="geodir_bestof_tab_dd" data-placeholder="' . esc_attr( __( 'Select Category', 'geodirectory' ) ).'">';
216
            } else {
217
                $nav_html .= '<dl class="geodir-tab-head geodir-bestof-cat-list">';
218
                $nav_html .= '<dt></dt>';
219
            }
220
221
222
            $term_icon = geodir_get_term_icon();
223
            $cat_count = 0;
224
            foreach ($terms as $cat) {
0 ignored issues
show
Bug introduced by
The expression $terms of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
225
                $cat_count++;
226
                if ($cat_count > $categ_limit) {
227
                    break;
228
                }
229
                if ($is_dropdown) {
230
                    $selected = ($cat_count == 1) ? 'selected="selected"' : '';
231
                    $nav_html .= '<option ' . $selected . ' value="' . $cat->term_id . '">' . geodir_ucwords($cat->name) . '</option>';
232
                } else {
233
                    if ($cat_count == 1) {
234
                        $nav_html .= '<dd class="geodir-tab-active">';
235
                    } else {
236
                        $nav_html .= '<dd class="">';
237
                    }
238
                    $term_icon_url = !empty($term_icon) && isset($term_icon[$cat->term_id]) ? $term_icon[$cat->term_id] : '';
239
                    $nav_html .= '<a data-termid="' . $cat->term_id . '" href="' . get_term_link($cat, $cat->taxonomy) . '">';
240
                    $nav_html .= '<img alt="' . $cat->name . ' icon" class="bestof-cat-icon" src="' . $term_icon_url . '"/>';
241
                    $nav_html .= '<span>';
242
                    $nav_html .= geodir_ucwords($cat->name);
243
                    $nav_html .= '<small>';
244
                    if (isset($cat->review_count)) {
245
                        $num_reviews = $cat->review_count;
246
                        if ($num_reviews == 0) {
247
                            $reviews = __('No Reviews', 'geodirectory');
248
                        } elseif ($num_reviews > 1) {
249
                            $reviews = $num_reviews . __(' Reviews', 'geodirectory');
250
                        } else {
251
                            $reviews = __('1 Review', 'geodirectory');
252
                        }
253
                        $nav_html .= $reviews;
254
                    }
255
                    $nav_html .= '</small>';
256
                    $nav_html .= '</span>';
257
                    $nav_html .= '</a>';
258
                    $nav_html .= '</dd>';
259
                }
260
            }
261
            if ($is_dropdown) {
262
                $nav_html .= '</select>';
263
            } else {
264
                $nav_html .= '</dl>';
265
            }
266
            $final_html .= $nav_html;
267
        }
268
        if ($terms) {
269
            echo $final_html;
270
        }
271
        echo '</div>';
272
        //term navigation - end
273
274
        //first term listings by default - start
275
        $first_term = '';
276
        if ($terms) {
277
            $first_term = $terms[0];
278
            $tax_query = array(
279
                'taxonomy' => $category_taxonomy[0],
280
                'field' => 'id',
281
                'terms' => $first_term->term_id
282
            );
283
            $query_args['tax_query'] = array($tax_query);
284
        }
285
286
        ?>
287
        <input type="hidden" id="bestof_widget_post_type" name="bestof_widget_post_type"
288
               value="<?php echo $post_type; ?>">
289
        <input type="hidden" id="bestof_widget_excerpt_type" name="bestof_widget_excerpt_type"
290
               value="<?php echo $excerpt_type; ?>">
291
        <input type="hidden" id="bestof_widget_post_limit" name="bestof_widget_post_limit"
292
               value="<?php echo $post_limit; ?>">
293
        <input type="hidden" id="bestof_widget_taxonomy" name="bestof_widget_taxonomy"
294
               value="<?php echo $category_taxonomy[0]; ?>">
295
        <input type="hidden" id="bestof_widget_location_filter" name="bestof_widget_location_filter"
296
               value="<?php if ($add_location_filter) {
297
                   echo 1;
298
               } else {
299
                   echo 0;
300
               } ?>">
301
        <input type="hidden" id="bestof_widget_char_count" name="bestof_widget_char_count"
302
               value="<?php echo $character_count; ?>">
303
        <div class="geo-bestof-contentwrap geodir-tabs-content" style="position: relative; z-index: 0;">
304
            <p id="geodir-bestof-loading" class="geodir-bestof-loading"><i class="fa fa-cog fa-spin"></i></p>
305
            <?php
306
            echo '<div id="geodir-bestof-places">';
307
            if ($terms) {
308
                $view_all_link = add_query_arg(array('sort_by' => 'rating_count_desc'), get_term_link($first_term, $first_term->taxonomy));
309
				/**
310
				 * Filter the page link to view all lisitngs.
311
				 *
312
				 * @since 1.5.1
313
				 *
314
				 * @param array $view_all_link View all listings page link.
315
				 * @param array $post_type The Post type.
316
				 * @param array $first_term The category term object.
317
				 */
318
				$view_all_link = apply_filters('geodir_bestof_widget_view_all_link', $view_all_link, $post_type, $first_term);
319
				
320
				echo '<h3 class="bestof-cat-title">' . wp_sprintf( __( 'Best of %s', 'geodirectory' ), $first_term->name ) . '<a href="' . esc_url($view_all_link) . '">' . __("View all", 'geodirectory') . '</a></h3>';
321
            }
322
            if ($excerpt_type == 'show-reviews') {
323
                add_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
324
            }
325
            geodir_bestof_places_by_term($query_args);
326
            if ($excerpt_type == 'show-reviews') {
327
                remove_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
328
            }
329
            echo "</div>";
330
            ?>
331
        </div>
332
        <?php //first term listings by default - end
333
        ?>
334
        <?php echo $after_widget;
335
        echo "</div>";
336
    }
337
338
	/**
339
	 * Sanitize best of widget form values as they are saved.
340
	 *
341
	 * @since 1.3.9
342
     * @since 1.5.1 Declare function public.
343
	 *
344
	 * @param array $new_instance Values just sent to be saved.
345
	 * @param array $old_instance Previously saved values from database.
346
	 *
347
	 * @return array Updated safe values to be saved.
348
	 */
349
	public function update($new_instance, $old_instance)
350
    {
351
        $instance = $old_instance;
352
        $instance['title'] = strip_tags($new_instance['title']);
353
        $instance['post_type'] = strip_tags($new_instance['post_type']);
354
        $instance['post_limit'] = strip_tags($new_instance['post_limit']);
355
        $instance['categ_limit'] = strip_tags($new_instance['categ_limit']);
356
        $instance['character_count'] = $new_instance['character_count'];
357
        $instance['tab_layout'] = $new_instance['tab_layout'];
358
        $instance['excerpt_type'] = $new_instance['excerpt_type'];
359 View Code Duplication
        if (isset($new_instance['add_location_filter']) && $new_instance['add_location_filter'] != '')
360
            $instance['add_location_filter'] = strip_tags($new_instance['add_location_filter']);
361
        else
362
            $instance['add_location_filter'] = '0';
363
        $instance['use_viewing_post_type'] = isset($new_instance['use_viewing_post_type']) && $new_instance['use_viewing_post_type'] ? 1 : 0;
364
        return $instance;
365
    }
366
367
	/**
368
	 * Back-end best of widget settings form.
369
	 *
370
	 * @since 1.3.9
371
     * @since 1.5.1 Declare function public.
372
	 *
373
	 * @param array $instance Previously saved values from database.
374
	 */
375
	public function form($instance)
376
    {
377
        $instance = wp_parse_args((array)$instance,
378
            array(
379
                'title' => '',
380
                'post_type' => '',
381
                'post_limit' => '5',
382
                'categ_limit' => '3',
383
                'character_count' => '20',
384
                'add_location_filter' => '1',
385
                'tab_layout' => 'bestof-tabs-on-top',
386
                'excerpt_type' => 'show-desc',
387
                'use_viewing_post_type' => ''
388
            )
389
        );
390
        $title = strip_tags($instance['title']);
391
        $post_type = strip_tags($instance['post_type']);
392
        $post_limit = strip_tags($instance['post_limit']);
393
        $categ_limit = strip_tags($instance['categ_limit']);
394
        $character_count = strip_tags($instance['character_count']);
395
        $tab_layout = strip_tags($instance['tab_layout']);
396
        $excerpt_type = strip_tags($instance['excerpt_type']);
397
        $add_location_filter = strip_tags($instance['add_location_filter']);
398
        $use_viewing_post_type = isset($instance['use_viewing_post_type']) && $instance['use_viewing_post_type'] ? true : false;
399
400
        ?>
401
        <p>
402
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'geodirectory');?>
403
404
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
405
                       name="<?php echo $this->get_field_name('title'); ?>" type="text"
406
                       value="<?php echo esc_attr($title); ?>"/>
407
            </label>
408
        </p>
409
410
        <p>
411
            <label
412
                for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e('Post Type:', 'geodirectory');?>
413
414
                <?php $postypes = geodir_get_posttypes();
415
				/**
416
				 * Filter the post types to display in widget.
417
				 *
418
				 * @since 1.3.9
419
				 *
420
				 * @param array $postypes Post types array.
421
				 */
422
				$postypes = apply_filters('geodir_post_type_list_in_p_widget', $postypes); ?>
423
424
                <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>"
425
                        name="<?php echo $this->get_field_name('post_type'); ?>"
426
                        onchange="geodir_change_category_list(this)">
427
428 View Code Duplication
                    <?php foreach ($postypes as $postypes_obj) { ?>
429
430
                        <option <?php if ($post_type == $postypes_obj) {
431
                            echo 'selected="selected"';
432
                        } ?> value="<?php echo $postypes_obj; ?>"><?php $extvalue = explode('_', $postypes_obj);
433
                            echo ucfirst($extvalue[1]); ?></option>
434
435
                    <?php } ?>
436
437
                </select>
438
            </label>
439
        </p>
440
441
        <p>
442
443
            <label
444
                for="<?php echo $this->get_field_id('post_limit'); ?>"><?php _e('Number of posts:', 'geodirectory');?>
445
446
                <input class="widefat" id="<?php echo $this->get_field_id('post_limit'); ?>"
447
                       name="<?php echo $this->get_field_name('post_limit'); ?>" type="text"
448
                       value="<?php echo esc_attr($post_limit); ?>"/>
449
            </label>
450
        </p>
451
452
        <p>
453
454
            <label
455
                for="<?php echo $this->get_field_id('categ_limit'); ?>"><?php _e('Number of categories:', 'geodirectory');?>
456
457
                <input class="widefat" id="<?php echo $this->get_field_id('categ_limit'); ?>"
458
                       name="<?php echo $this->get_field_name('categ_limit'); ?>" type="text"
459
                       value="<?php echo esc_attr($categ_limit); ?>"/>
460
            </label>
461
        </p>
462
463
        <p>
464
            <label
465
                for="<?php echo $this->get_field_id('character_count'); ?>"><?php _e('Post Content excerpt character count :', 'geodirectory');?>
466
                <input class="widefat" id="<?php echo $this->get_field_id('character_count'); ?>"
467
                       name="<?php echo $this->get_field_name('character_count'); ?>" type="text"
468
                       value="<?php echo esc_attr($character_count); ?>"/>
469
            </label>
470
        </p>
471
        <p>
472
            <label
473
                for="<?php echo $this->get_field_id('tab_layout'); ?>"><?php _e('Tab Layout:', 'geodirectory');?>
474
475
                <select class="widefat" id="<?php echo $this->get_field_id('tab_layout'); ?>"
476
                        name="<?php echo $this->get_field_name('tab_layout'); ?>">
477
478
                    <option <?php if ($tab_layout == 'bestof-tabs-on-top') {
479
                        echo 'selected="selected"';
480
                    } ?> value="bestof-tabs-on-top"><?php _e('Tabs on Top', 'geodirectory'); ?></option>
481
                    <option <?php if ($tab_layout == 'bestof-tabs-on-left') {
482
                        echo 'selected="selected"';
483
                    } ?> value="bestof-tabs-on-left"><?php _e('Tabs on Left', 'geodirectory'); ?></option>
484
                    <option <?php if ($tab_layout == 'bestof-tabs-as-dropdown') {
485
                        echo 'selected="selected"';
486
                    } ?>
487
                        value="bestof-tabs-as-dropdown"><?php _e('Tabs as Dropdown', 'geodirectory'); ?></option>
488
                </select>
489
            </label>
490
        </p>
491
492
        <p>
493
            <label
494
                for="<?php echo $this->get_field_id('excerpt_type'); ?>"><?php _e('Excerpt Type:', 'geodirectory');?>
495
496
                <select class="widefat" id="<?php echo $this->get_field_id('excerpt_type'); ?>"
497
                        name="<?php echo $this->get_field_name('excerpt_type'); ?>">
498
499
                    <option <?php if ($excerpt_type == 'show-desc') {
500
                        echo 'selected="selected"';
501
                    } ?> value="show-desc"><?php _e('Show Description', 'geodirectory'); ?></option>
502
                    <option <?php if ($excerpt_type == 'show-reviews') {
503
                        echo 'selected="selected"';
504
                    } ?> value="show-reviews"><?php _e('Show Reviews if Available', 'geodirectory'); ?></option>
505
                </select>
506
            </label>
507
        </p>
508
509
        <p>
510
            <label for="<?php echo $this->get_field_id('add_location_filter'); ?>">
511
                <?php _e('Enable Location Filter:', 'geodirectory');?>
512
                <input type="checkbox" id="<?php echo $this->get_field_id('add_location_filter'); ?>"
513
                       name="<?php echo $this->get_field_name('add_location_filter'); ?>" <?php if ($add_location_filter) echo 'checked="checked"';?>
514
                       value="1"/>
515
            </label>
516
        </p>
517
518
        <p>
519
            <label
520
                for="<?php echo $this->get_field_id('use_viewing_post_type'); ?>"><?php _e('Use current viewing post type:', 'geodirectory'); ?>
521
                <input type="checkbox" id="<?php echo $this->get_field_id('use_viewing_post_type'); ?>"
522
                       name="<?php echo $this->get_field_name('use_viewing_post_type'); ?>" <?php if ($use_viewing_post_type) {
523
                    echo 'checked="checked"';
524
                } ?>  value="1"/>
525
            </label>
526
        </p>
527
    <?php
528
    }
529
} // class geodir_bestof_widget
530
531
register_widget('geodir_bestof_widget');
532
533
/**
534
 * Display the best of widget listings using the given query args.
535
 *
536
 * @since 1.3.9
537
 *
538
 * @global object $post The current post object.
539
 * @global array  $map_jason Map data in json format.
540
 * @global array $map_canvas_arr Map canvas array.
541
 * @global string $gridview_columns_widget The girdview style of the listings for widget.
542
 * @global object $gd_session GeoDirectory Session object.
543
 *
544
 * @param array $query_args The query array.
545
 */
546
function geodir_bestof_places_by_term($query_args) {
547
    global $gd_session;
548
	
549
	/**
550
     * This action called before querying widget listings.
551
     *
552
     * @since 1.0.0
553
     */
554
    do_action('geodir_bestof_get_widget_listings_before');
555
556
    $widget_listings = geodir_get_widget_listings($query_args);
557
558
    /**
559
     * This action called after querying widget listings.
560
     *
561
     * @since 1.0.0
562
     */
563
    do_action('geodir_bestof_get_widget_listings_after');
564
565
    $character_count = isset($query_args['excerpt_length']) ? $query_args['excerpt_length'] : '';
566
567
    if (!isset($character_count)) {
568
		/** This filter is documented in geodirectory-widgets/geodirectory_bestof_widget.php */
569
		$character_count = $character_count == '' ? 50 : apply_filters('bestof_widget_character_count', $character_count);
570
    }
571
	
572
	/** This filter is documented in geodirectory-functions/general_functions.php */
573
	$template = apply_filters("geodir_template_part-widget-listing-listview", geodir_locate_template('widget-listing-listview'));
574
575
    global $post, $map_jason, $map_canvas_arr, $gridview_columns_widget, $geodir_is_widget_listing;
576
    $current_post = $post;
577
    $current_map_jason = $map_jason;
578
    $current_map_canvas_arr = $map_canvas_arr;
579
    $current_grid_view = $gridview_columns_widget;
580
    $gridview_columns_widget = null;
581
    
582
	$gd_listing_view_set = $gd_session->get('gd_listing_view') ? true : false;
583
	$gd_listing_view_old = $gd_listing_view_set ? $gd_session->get('gd_listing_view') : '';
584
	
585
    $gd_session->set('gd_listing_view', '1');
586
    $geodir_is_widget_listing = true;
587
588
	/**
589
	 * Includes the template for the listing listview.
590
	 *
591
	 * @since 1.3.9
592
	 */
593
	include($template);
594
595
    $geodir_is_widget_listing = false;
596
597
    $GLOBALS['post'] = $current_post;
598
	if (!empty($current_post)) {
599
    	setup_postdata($current_post);
600
	}
601
	if ($gd_listing_view_set) { // Set back previous value
602
		$gd_session->set('gd_listing_view', $gd_listing_view_old);
603
	} else {
604
		$gd_session->un_set('gd_listing_view');
605
	}
606
    $map_jason = $current_map_jason;
607
    $map_canvas_arr = $current_map_canvas_arr;
608
    $gridview_columns_widget = $current_grid_view;
609
}
610
611
//Ajax functions
612
add_action('wp_ajax_geodir_bestof', 'geodir_bestof_callback');
613
add_action('wp_ajax_nopriv_geodir_bestof', 'geodir_bestof_callback');
614
615
/**
616
 * Get the best of widget content using ajax.
617
 *
618
 * @since 1.3.9
619
 * @since 1.5.1 Added filter to view all link.
620
 *
621
 * @return string Html content.
622
 */
623
function geodir_bestof_callback()
624
{
625
    check_ajax_referer('geodir-bestof-nonce', 'geodir_bestof_nonce');
626
    //set variables
627
    $post_type = strip_tags(esc_sql($_POST['post_type']));
628
    $post_limit = strip_tags(esc_sql($_POST['post_limit']));
629
    $character_count = strip_tags(esc_sql($_POST['char_count']));
630
    $taxonomy = strip_tags(esc_sql($_POST['taxonomy']));
631
    $add_location_filter = strip_tags(esc_sql($_POST['add_location_filter']));
632
    $term_id = strip_tags(esc_sql($_POST['term_id']));
633
    $excerpt_type = '';
0 ignored issues
show
Unused Code introduced by
$excerpt_type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
634
    $excerpt_type = strip_tags(esc_sql($_POST['excerpt_type']));
635
636
    $query_args = array(
637
        'posts_per_page' => $post_limit,
638
        'is_geodir_loop' => true,
639
        'post_type' => $post_type,
640
        'gd_location' => $add_location_filter ? true : false,
641
        'order_by' => 'high_review'
642
    );
643
644
    if ($character_count >= 0) {
645
        $query_args['excerpt_length'] = $character_count;
646
    }
647
648
    $tax_query = array(
649
        'taxonomy' => $taxonomy,
650
        'field' => 'id',
651
        'terms' => $term_id
652
    );
653
654
    $query_args['tax_query'] = array($tax_query);
655
    if ($term_id && $taxonomy) {
656
        $term = get_term_by('id', $term_id, $taxonomy);
657
        $view_all_link = add_query_arg(array('sort_by' => 'rating_count_desc'), get_term_link($term)) ;
658
		/** This filter is documented in geodirectory-widgets/geodirectory_bestof_widget.php */
659
		$view_all_link = apply_filters('geodir_bestof_widget_view_all_link', $view_all_link, $post_type, $term);
660
				
661
		echo '<h3 class="bestof-cat-title">' . wp_sprintf( __( 'Best of %s', 'geodirectory' ), $term->name ) . '<a href="' . esc_url( $view_all_link ) . '">' . __("View all", 'geodirectory') . '</a></h3>';
662
    }
663
    if ($excerpt_type == 'show-reviews') {
664
        add_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
665
    }
666
    geodir_bestof_places_by_term($query_args);
667
    if ($excerpt_type == 'show-reviews') {
668
        remove_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
669
    }
670
    wp_die();
671
}
672
673
//Javascript
674
add_action('wp_footer', 'geodir_bestof_js');
675
676
/**
677
 * Adds the javascript in the footer for best of widget.
678
 *
679
 * @since 1.3.9
680
 */
681
function geodir_bestof_js() {
682
	$ajax_nonce = wp_create_nonce("geodir-bestof-nonce");
683
?>
684
<script type="text/javascript">
685
jQuery(document).ready(function() {
686
	jQuery('.geodir-bestof-cat-list a, #geodir_bestof_tab_dd').on("click change", function(e) {
687
		var widgetBox = jQuery(this).closest('.geodir_bestof_widget');
688
		var loading = jQuery(widgetBox).find("#geodir-bestof-loading");
689
		var container = jQuery(widgetBox).find('#geodir-bestof-places');
690
		
691
		jQuery(document).ajaxStart(function() {
692
			//container.hide(); // Not working when more then one widget on page
693
			//loading.show();
694
		}).ajaxStop(function() {
695
			loading.hide();
696
			container.fadeIn('slow');
697
		});
698
		
699
		e.preventDefault();
700
		
701
		var activeTab = jQuery(this).closest('dl').find('dd.geodir-tab-active');
702
		activeTab.removeClass('geodir-tab-active');
703
		jQuery(this).parent().addClass('geodir-tab-active');
704
		
705
		var term_id = 0;
706
		if (e.type === "change") {
707
			term_id = jQuery(this).val();
708
		} else if (e.type === "click") {
709
			term_id = jQuery(this).attr('data-termid');
710
		}
711
		
712
		var post_type = jQuery(widgetBox).find('#bestof_widget_post_type').val();
713
        var excerpt_type = jQuery(widgetBox).find('#bestof_widget_excerpt_type').val();
714
		var post_limit = jQuery(widgetBox).find('#bestof_widget_post_limit').val();
715
		var taxonomy = jQuery(widgetBox).find('#bestof_widget_taxonomy').val();
716
		var char_count = jQuery(widgetBox).find('#bestof_widget_char_count').val();
717
		var add_location_filter = jQuery(widgetBox).find('#bestof_widget_location_filter').val();
718
		
719
		var data = {
720
			'action': 'geodir_bestof',
721
			'geodir_bestof_nonce': '<?php echo $ajax_nonce; ?>',
722
			'post_type': post_type,
723
            'excerpt_type': excerpt_type,
724
			'post_limit': post_limit,
725
			'taxonomy': taxonomy,
726
			'geodir_ajax': true,
727
			'term_id': term_id,
728
			'char_count': char_count,
729
			'add_location_filter': add_location_filter
730
		};
731
		
732
		container.hide();
733
		loading.show();
734
		
735
		jQuery.post(geodir_var.geodir_ajax_url, data, function(response) {
736
			container.html(response);
737
			jQuery(widgetBox).find('.geodir_category_list_view li .geodir-post-img .geodir_thumbnail img').css('display', 'block');
738
		});
739
	})
740
});
741
jQuery(document).ready(function() {
742
	if (jQuery(window).width() < 660) {
743
		if (jQuery('.bestof-widget-tab-layout').hasClass('bestof-tabs-on-left')) {
744
			jQuery('.bestof-widget-tab-layout').removeClass('bestof-tabs-on-left').addClass('bestof-tabs-as-dropdown');
745
		} else if (jQuery('.bestof-widget-tab-layout').hasClass('bestof-tabs-on-top')) {
746
			jQuery('.bestof-widget-tab-layout').removeClass('bestof-tabs-on-top').addClass('bestof-tabs-as-dropdown');
747
		}
748
	}
749
});
750
</script>
751
<?php
752
}
753
754
function best_of_show_review_in_excerpt($excerpt) {
755
    global $wpdb, $post;
756
//    $args = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
757
//                'status' => 'approve',
758
//                'number' => 10,
759
//                'parent' => 0,
760
//                'post_id' => $post->ID
761
//            );
762
763
    $review_table = GEODIR_REVIEW_TABLE;
764
    $request = "SELECT comment_ID FROM $review_table WHERE post_id = $post->ID ORDER BY post_date DESC, id DESC LIMIT 1";
765
    $comments = $wpdb->get_results($request);
766
767
    if ($comments) {
768
        foreach($comments as $comment) {
769
            // Set the extra comment info needed.
770
            $comment_extra = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID =$comment->comment_ID");
771
            $comment_content = $comment_extra->comment_content;
772
            $excerpt = strip_tags( $comment_content );
773
        }
774
    }
775
    return $excerpt;
776
}