| Conditions | 40 |
| Paths | > 20000 |
| Total Lines | 328 |
| Code Lines | 180 |
| Lines | 12 |
| Ratio | 3.66 % |
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 |
||
| 215 | public function form($instance) |
||
| 216 | {
|
||
| 217 | //widgetform in backend |
||
| 218 | $instance = wp_parse_args((array)$instance, |
||
| 219 | array('title' => '',
|
||
| 220 | 'post_type' => '', |
||
| 221 | 'category' => array(), |
||
| 222 | 'category_title' => '', |
||
| 223 | 'list_sort' => '', |
||
| 224 | 'list_order' => '', |
||
| 225 | 'post_number' => '5', |
||
| 226 | 'layout' => 'gridview_onehalf', |
||
| 227 | 'listing_width' => '', |
||
| 228 | 'add_location_filter' => '1', |
||
| 229 | 'character_count' => '20', |
||
| 230 | 'show_featured_only' => '', |
||
| 231 | 'show_special_only' => '', |
||
| 232 | 'with_pics_only' => '', |
||
| 233 | 'with_videos_only' => '', |
||
| 234 | 'use_viewing_post_type' => '' |
||
| 235 | ) |
||
| 236 | ); |
||
| 237 | |||
| 238 | $title = strip_tags($instance['title']); |
||
| 239 | |||
| 240 | $post_type = strip_tags($instance['post_type']); |
||
| 241 | |||
| 242 | $category = $instance['category']; |
||
| 243 | |||
| 244 | $category_title = strip_tags($instance['category_title']); |
||
| 245 | |||
| 246 | $list_sort = strip_tags($instance['list_sort']); |
||
| 247 | |||
| 248 | $list_order = strip_tags($instance['list_order']); |
||
| 249 | |||
| 250 | $post_number = strip_tags($instance['post_number']); |
||
| 251 | |||
| 252 | $layout = strip_tags($instance['layout']); |
||
| 253 | |||
| 254 | $listing_width = strip_tags($instance['listing_width']); |
||
| 255 | |||
| 256 | $add_location_filter = strip_tags($instance['add_location_filter']); |
||
| 257 | |||
| 258 | $character_count = $instance['character_count']; |
||
| 259 | |||
| 260 | $show_featured_only = isset($instance['show_featured_only']) && $instance['show_featured_only'] ? true : false; |
||
| 261 | $show_special_only = isset($instance['show_special_only']) && $instance['show_special_only'] ? true : false; |
||
| 262 | $with_pics_only = isset($instance['with_pics_only']) && $instance['with_pics_only'] ? true : false; |
||
| 263 | $with_videos_only = isset($instance['with_videos_only']) && $instance['with_videos_only'] ? true : false; |
||
| 264 | $use_viewing_post_type = isset($instance['use_viewing_post_type']) && $instance['use_viewing_post_type'] ? true : false; |
||
| 265 | |||
| 266 | ?> |
||
| 267 | |||
| 268 | <p> |
||
| 269 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'geodirectory');?>
|
||
| 270 | <small>(%posttype_singular_label% , |
||
| 271 | %posttype_plural_label% <?php _e('can be used', 'geodirectory');?>)
|
||
| 272 | </small> |
||
| 273 | |||
| 274 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
|
||
| 275 | name="<?php echo $this->get_field_name('title'); ?>" type="text"
|
||
| 276 | value="<?php echo esc_attr($title); ?>"/> |
||
| 277 | </label> |
||
| 278 | </p> |
||
| 279 | |||
| 280 | <p> |
||
| 281 | <label |
||
| 282 | for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e('Post Type:', 'geodirectory');?>
|
||
| 283 | |||
| 284 | <?php $postypes = geodir_get_posttypes(); |
||
| 285 | /** |
||
| 286 | * Filter the post types to display in widget. |
||
| 287 | * |
||
| 288 | * @since 1.0.0 |
||
| 289 | * |
||
| 290 | * @param array $postypes Post types array. |
||
| 291 | */ |
||
| 292 | $postypes = apply_filters('geodir_post_type_list_in_p_widget', $postypes); ?>
|
||
| 293 | |||
| 294 | <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>"
|
||
| 295 | name="<?php echo $this->get_field_name('post_type'); ?>"
|
||
| 296 | onchange="geodir_change_category_list(this)"> |
||
| 297 | |||
| 298 | View Code Duplication | <?php foreach ($postypes as $postypes_obj) { ?>
|
|
| 299 | |||
| 300 | <option <?php if ($post_type == $postypes_obj) {
|
||
| 301 | echo 'selected="selected"'; |
||
| 302 | } ?> value="<?php echo $postypes_obj; ?>"><?php $extvalue = explode('_', $postypes_obj);
|
||
| 303 | echo ucfirst($extvalue[1]); ?></option> |
||
| 304 | |||
| 305 | <?php } ?> |
||
| 306 | |||
| 307 | </select> |
||
| 308 | </label> |
||
| 309 | </p> |
||
| 310 | |||
| 311 | |||
| 312 | <p id="post_type_cats"> |
||
| 313 | <label |
||
| 314 | for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Post Category:', 'geodirectory');?>
|
||
| 315 | |||
| 316 | <?php |
||
| 317 | |||
| 318 | $post_type = ($post_type != '') ? $post_type : 'gd_place'; |
||
| 319 | |||
| 320 | $all_postypes = geodir_get_posttypes(); |
||
| 321 | |||
| 322 | if (!in_array($post_type, $all_postypes)) |
||
| 323 | $post_type = 'gd_place'; |
||
| 324 | |||
| 325 | $category_taxonomy = geodir_get_taxonomies($post_type); |
||
| 326 | $categories = get_terms($category_taxonomy, array('orderby' => 'count', 'order' => 'DESC'));
|
||
| 327 | |||
| 328 | ?> |
||
| 329 | |||
| 330 | <select multiple="multiple" class="widefat" name="<?php echo $this->get_field_name('category'); ?>[]"
|
||
| 331 | onchange="geodir_popular_widget_cat_title(this)"> |
||
| 332 | |||
| 333 | <option <?php if (!is_array($category) || (is_array($category) && in_array('0', $category))) {
|
||
| 334 | echo 'selected="selected"'; |
||
| 335 | } ?> value="0"><?php _e('All', 'geodirectory'); ?></option>
|
||
| 336 | <?php foreach ($categories as $category_obj) {
|
||
| 337 | $selected = ''; |
||
| 338 | if (is_array($category) && in_array($category_obj->term_id, $category)) |
||
| 339 | echo $selected = 'selected="selected"'; |
||
| 340 | |||
| 341 | ?> |
||
| 342 | |||
| 343 | <option <?php echo $selected; ?> |
||
| 344 | value="<?php echo $category_obj->term_id; ?>"><?php echo ucfirst($category_obj->name); ?></option> |
||
| 345 | |||
| 346 | <?php } ?> |
||
| 347 | |||
| 348 | </select> |
||
| 349 | |||
| 350 | |||
| 351 | <input type="hidden" name="<?php echo $this->get_field_name('category_title'); ?>"
|
||
| 352 | id="<?php echo $this->get_field_id('category_title'); ?>"
|
||
| 353 | value="<?php if ($category_title != '') echo $category_title; else echo __('All', 'geodirectory');?>"/>
|
||
| 354 | |||
| 355 | </label> |
||
| 356 | </p> |
||
| 357 | |||
| 358 | <p> |
||
| 359 | <label |
||
| 360 | for="<?php echo $this->get_field_id('list_sort'); ?>"><?php _e('Sort by:', 'geodirectory');?>
|
||
| 361 | |||
| 362 | <select class="widefat" id="<?php echo $this->get_field_id('list_sort'); ?>"
|
||
| 363 | name="<?php echo $this->get_field_name('list_sort'); ?>">
|
||
| 364 | |||
| 365 | <option <?php if ($list_sort == 'az') {
|
||
| 366 | echo 'selected="selected"'; |
||
| 367 | } ?> value="az"><?php _e('A-Z', 'geodirectory'); ?></option>
|
||
| 368 | |||
| 369 | <option <?php if ($list_sort == 'latest') {
|
||
| 370 | echo 'selected="selected"'; |
||
| 371 | } ?> value="latest"><?php _e('Latest', 'geodirectory'); ?></option>
|
||
| 372 | |||
| 373 | <option <?php if ($list_sort == 'featured') {
|
||
| 374 | echo 'selected="selected"'; |
||
| 375 | } ?> value="featured"><?php _e('Featured', 'geodirectory'); ?></option>
|
||
| 376 | |||
| 377 | <option <?php if ($list_sort == 'high_review') {
|
||
| 378 | echo 'selected="selected"'; |
||
| 379 | } ?> value="high_review"><?php _e('Review', 'geodirectory'); ?></option>
|
||
| 380 | |||
| 381 | <option <?php if ($list_sort == 'high_rating') {
|
||
| 382 | echo 'selected="selected"'; |
||
| 383 | } ?> value="high_rating"><?php _e('Rating', 'geodirectory'); ?></option>
|
||
| 384 | |||
| 385 | <option <?php if ($list_sort == 'random') {
|
||
| 386 | echo 'selected="selected"'; |
||
| 387 | } ?> value="random"><?php _e('Random', 'geodirectory'); ?></option>
|
||
| 388 | |||
| 389 | </select> |
||
| 390 | </label> |
||
| 391 | </p> |
||
| 392 | |||
| 393 | <p> |
||
| 394 | |||
| 395 | <label |
||
| 396 | for="<?php echo $this->get_field_id('post_number'); ?>"><?php _e('Number of posts:', 'geodirectory');?>
|
||
| 397 | |||
| 398 | <input class="widefat" id="<?php echo $this->get_field_id('post_number'); ?>"
|
||
| 399 | name="<?php echo $this->get_field_name('post_number'); ?>" type="text"
|
||
| 400 | value="<?php echo esc_attr($post_number); ?>"/> |
||
| 401 | </label> |
||
| 402 | </p> |
||
| 403 | |||
| 404 | <p> |
||
| 405 | <label for="<?php echo $this->get_field_id('layout'); ?>">
|
||
| 406 | <?php _e('Layout:', 'geodirectory');?>
|
||
| 407 | <select class="widefat" id="<?php echo $this->get_field_id('layout'); ?>"
|
||
| 408 | name="<?php echo $this->get_field_name('layout'); ?>">
|
||
| 409 | <option <?php if ($layout == 'gridview_onehalf') {
|
||
| 410 | echo 'selected="selected"'; |
||
| 411 | } ?> |
||
| 412 | value="gridview_onehalf"><?php _e('Grid View (Two Columns)', 'geodirectory'); ?></option>
|
||
| 413 | <option <?php if ($layout == 'gridview_onethird') {
|
||
| 414 | echo 'selected="selected"'; |
||
| 415 | } ?> |
||
| 416 | value="gridview_onethird"><?php _e('Grid View (Three Columns)', 'geodirectory'); ?></option>
|
||
| 417 | <option <?php if ($layout == 'gridview_onefourth') {
|
||
| 418 | echo 'selected="selected"'; |
||
| 419 | } ?> |
||
| 420 | value="gridview_onefourth"><?php _e('Grid View (Four Columns)', 'geodirectory'); ?></option>
|
||
| 421 | <option <?php if ($layout == 'gridview_onefifth') {
|
||
| 422 | echo 'selected="selected"'; |
||
| 423 | } ?> |
||
| 424 | value="gridview_onefifth"><?php _e('Grid View (Five Columns)', 'geodirectory'); ?></option>
|
||
| 425 | <option <?php if ($layout == 'list') {
|
||
| 426 | echo 'selected="selected"'; |
||
| 427 | } ?> value="list"><?php _e('List view', 'geodirectory'); ?></option>
|
||
| 428 | |||
| 429 | </select> |
||
| 430 | </label> |
||
| 431 | </p> |
||
| 432 | |||
| 433 | <p> |
||
| 434 | <label |
||
| 435 | for="<?php echo $this->get_field_id('listing_width'); ?>"><?php _e('Listing width:', 'geodirectory');?>
|
||
| 436 | |||
| 437 | <input class="widefat" id="<?php echo $this->get_field_id('listing_width'); ?>"
|
||
| 438 | name="<?php echo $this->get_field_name('listing_width'); ?>" type="text"
|
||
| 439 | value="<?php echo esc_attr($listing_width); ?>"/> |
||
| 440 | </label> |
||
| 441 | </p> |
||
| 442 | |||
| 443 | <p> |
||
| 444 | <label |
||
| 445 | for="<?php echo $this->get_field_id('character_count'); ?>"><?php _e('Post Content excerpt character count :', 'geodirectory');?>
|
||
| 446 | <input class="widefat" id="<?php echo $this->get_field_id('character_count'); ?>"
|
||
| 447 | name="<?php echo $this->get_field_name('character_count'); ?>" type="text"
|
||
| 448 | value="<?php echo esc_attr($character_count); ?>"/> |
||
| 449 | </label> |
||
| 450 | </p> |
||
| 451 | |||
| 452 | <p> |
||
| 453 | <label for="<?php echo $this->get_field_id('add_location_filter'); ?>">
|
||
| 454 | <?php _e('Enable Location Filter:', 'geodirectory');?>
|
||
| 455 | <input type="checkbox" id="<?php echo $this->get_field_id('add_location_filter'); ?>"
|
||
| 456 | name="<?php echo $this->get_field_name('add_location_filter'); ?>" <?php if ($add_location_filter) echo 'checked="checked"';?>
|
||
| 457 | value="1"/> |
||
| 458 | </label> |
||
| 459 | </p> |
||
| 460 | <p> |
||
| 461 | <label for="<?php echo $this->get_field_id('show_featured_only'); ?>">
|
||
| 462 | <?php _e('Show only featured listings:', 'geodirectory');?> <input type="checkbox"
|
||
| 463 | id="<?php echo $this->get_field_id('show_featured_only'); ?>"
|
||
| 464 | name="<?php echo $this->get_field_name('show_featured_only'); ?>" <?php if ($show_featured_only) echo 'checked="checked"';?>
|
||
| 465 | value="1"/> |
||
| 466 | </label> |
||
| 467 | </p> |
||
| 468 | <p> |
||
| 469 | <label for="<?php echo $this->get_field_id('show_special_only'); ?>">
|
||
| 470 | <?php _e('Show only listings with special offers:', 'geodirectory');?> <input type="checkbox"
|
||
| 471 | id="<?php echo $this->get_field_id('show_special_only'); ?>"
|
||
| 472 | name="<?php echo $this->get_field_name('show_special_only'); ?>" <?php if ($show_special_only) echo 'checked="checked"';?>
|
||
| 473 | value="1"/> |
||
| 474 | </label> |
||
| 475 | </p> |
||
| 476 | <p> |
||
| 477 | <label for="<?php echo $this->get_field_id('with_pics_only'); ?>">
|
||
| 478 | <?php _e('Show only listings with pics:', 'geodirectory');?> <input type="checkbox"
|
||
| 479 | id="<?php echo $this->get_field_id('with_pics_only'); ?>"
|
||
| 480 | name="<?php echo $this->get_field_name('with_pics_only'); ?>" <?php if ($with_pics_only) echo 'checked="checked"';?>
|
||
| 481 | value="1"/> |
||
| 482 | </label> |
||
| 483 | </p> |
||
| 484 | <p> |
||
| 485 | <label for="<?php echo $this->get_field_id('with_videos_only'); ?>">
|
||
| 486 | <?php _e('Show only listings with videos:', 'geodirectory');?> <input type="checkbox"
|
||
| 487 | id="<?php echo $this->get_field_id('with_videos_only'); ?>"
|
||
| 488 | name="<?php echo $this->get_field_name('with_videos_only'); ?>" <?php if ($with_videos_only) echo 'checked="checked"';?>
|
||
| 489 | value="1"/> |
||
| 490 | </label> |
||
| 491 | </p> |
||
| 492 | <p> |
||
| 493 | <label |
||
| 494 | for="<?php echo $this->get_field_id('use_viewing_post_type'); ?>"><?php _e('Use current viewing post type:', 'geodirectory'); ?>
|
||
| 495 | <input type="checkbox" id="<?php echo $this->get_field_id('use_viewing_post_type'); ?>"
|
||
| 496 | name="<?php echo $this->get_field_name('use_viewing_post_type'); ?>" <?php if ($use_viewing_post_type) {
|
||
| 497 | echo 'checked="checked"'; |
||
| 498 | } ?> value="1"/> |
||
| 499 | </label> |
||
| 500 | </p> |
||
| 501 | |||
| 502 | |||
| 503 | <script type="text/javascript"> |
||
| 504 | |||
| 505 | function geodir_popular_widget_cat_title(val) {
|
||
| 506 | |||
| 507 | jQuery(val).find("option:selected").each(function (i) {
|
||
| 508 | if (i == 0) |
||
| 509 | jQuery(val).closest('form').find('#post_type_cats input').val(jQuery(this).html());
|
||
| 510 | |||
| 511 | }); |
||
| 512 | |||
| 513 | } |
||
| 514 | |||
| 515 | function geodir_change_category_list(obj, selected) {
|
||
| 516 | var post_type = obj.value; |
||
| 517 | |||
| 518 | var ajax_url = '<?php echo geodir_get_ajax_url(); ?>' |
||
| 519 | |||
| 520 | var myurl = ajax_url + "&geodir_ajax=admin_ajax&ajax_action=get_cat_dl&post_type=" + post_type + "&selected=" + selected; |
||
| 521 | |||
| 522 | jQuery.ajax({
|
||
| 523 | type: "GET", |
||
| 524 | url: myurl, |
||
| 525 | success: function (data) {
|
||
| 526 | |||
| 527 | jQuery(obj).closest('form').find('#post_type_cats select').html(data);
|
||
| 528 | |||
| 529 | } |
||
| 530 | }); |
||
| 531 | |||
| 532 | } |
||
| 533 | |||
| 534 | View Code Duplication | <?php if(is_active_widget( false, false, $this->id_base, true )){ ?>
|
|
| 535 | var post_type = jQuery('#<?php echo $this->get_field_id('post_type'); ?>').val();
|
||
| 536 | |||
| 537 | <?php } ?> |
||
| 538 | |||
| 539 | </script> |
||
| 540 | |||
| 541 | <?php |
||
| 542 | } |
||
| 543 | } // class geodir_popular_postview |
||
| 545 | register_widget('geodir_popular_postview'); |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.