Conditions | 41 |
Paths | > 20000 |
Total Lines | 295 |
Code Lines | 150 |
Lines | 18 |
Ratio | 6.1 % |
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 |
||
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) { |
||
|
|||
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) { |
||
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 | |||
776 | } |
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.