Conditions | 40 |
Paths | > 20000 |
Total Lines | 294 |
Code Lines | 118 |
Lines | 15 |
Ratio | 5.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 |
||
370 | function render_field( $field ) { |
||
371 | |||
372 | // vars |
||
373 | $values = array(); |
||
374 | $atts = array( |
||
375 | 'id' => $field['id'], |
||
376 | 'class' => "acf-relationship {$field['class']}", |
||
377 | 'data-min' => $field['min'], |
||
378 | 'data-max' => $field['max'], |
||
379 | 'data-s' => '', |
||
380 | 'data-post_type' => '', |
||
381 | 'data-taxonomy' => '', |
||
382 | 'data-paged' => 1, |
||
383 | ); |
||
384 | |||
385 | |||
386 | // Lang |
||
387 | if( defined('ICL_LANGUAGE_CODE') ) { |
||
388 | |||
389 | $atts['data-lang'] = ICL_LANGUAGE_CODE; |
||
390 | |||
391 | } |
||
392 | |||
393 | |||
394 | // data types |
||
395 | $field['post_type'] = acf_get_array( $field['post_type'] ); |
||
396 | $field['taxonomy'] = acf_get_array( $field['taxonomy'] ); |
||
397 | |||
398 | |||
399 | // width for select filters |
||
400 | $width = array( |
||
401 | 'search' => 0, |
||
402 | 'post_type' => 0, |
||
403 | 'taxonomy' => 0 |
||
404 | ); |
||
405 | |||
406 | if( !empty($field['filters']) ) { |
||
407 | |||
408 | $width = array( |
||
409 | 'search' => 50, |
||
410 | 'post_type' => 25, |
||
411 | 'taxonomy' => 25 |
||
412 | ); |
||
413 | |||
414 | foreach( array_keys($width) as $k ) { |
||
415 | |||
416 | if( ! in_array($k, $field['filters']) ) { |
||
417 | |||
418 | $width[ $k ] = 0; |
||
419 | |||
420 | } |
||
421 | |||
422 | } |
||
423 | |||
424 | |||
425 | // search |
||
426 | if( $width['search'] == 0 ) { |
||
427 | |||
428 | $width['post_type'] = ( $width['post_type'] == 0 ) ? 0 : 50; |
||
429 | $width['taxonomy'] = ( $width['taxonomy'] == 0 ) ? 0 : 50; |
||
430 | |||
431 | } |
||
432 | |||
433 | // post_type |
||
434 | View Code Duplication | if( $width['post_type'] == 0 ) { |
|
435 | |||
436 | $width['taxonomy'] = ( $width['taxonomy'] == 0 ) ? 0 : 50; |
||
437 | |||
438 | } |
||
439 | |||
440 | |||
441 | // taxonomy |
||
442 | View Code Duplication | if( $width['taxonomy'] == 0 ) { |
|
443 | |||
444 | $width['post_type'] = ( $width['post_type'] == 0 ) ? 0 : 50; |
||
445 | |||
446 | } |
||
447 | |||
448 | |||
449 | // search |
||
450 | View Code Duplication | if( $width['post_type'] == 0 && $width['taxonomy'] == 0 ) { |
|
451 | |||
452 | $width['search'] = ( $width['search'] == 0 ) ? 0 : 100; |
||
453 | |||
454 | } |
||
455 | } |
||
456 | |||
457 | |||
458 | // post type filter |
||
459 | $post_types = array(); |
||
460 | |||
461 | if( $width['post_type'] ) { |
||
462 | |||
463 | if( !empty($field['post_type']) ) { |
||
464 | |||
465 | $post_types = $field['post_type']; |
||
466 | |||
467 | |||
468 | } else { |
||
469 | |||
470 | $post_types = acf_get_post_types(); |
||
471 | |||
472 | } |
||
473 | |||
474 | $post_types = acf_get_pretty_post_types($post_types); |
||
475 | |||
476 | } |
||
477 | |||
478 | |||
479 | // taxonomy filter |
||
480 | $taxonomies = array(); |
||
481 | $term_groups = array(); |
||
482 | |||
483 | if( $width['taxonomy'] ) { |
||
484 | |||
485 | // taxonomies |
||
486 | if( !empty($field['taxonomy']) ) { |
||
487 | |||
488 | // get the field's terms |
||
489 | $term_groups = acf_get_array( $field['taxonomy'] ); |
||
490 | $term_groups = acf_decode_taxonomy_terms( $term_groups ); |
||
491 | |||
492 | |||
493 | // update taxonomies |
||
494 | $taxonomies = array_keys($term_groups); |
||
495 | |||
496 | } elseif( !empty($field['post_type']) ) { |
||
497 | |||
498 | // loop over post types and find connected taxonomies |
||
499 | foreach( $field['post_type'] as $post_type ) { |
||
500 | |||
501 | $post_taxonomies = get_object_taxonomies( $post_type ); |
||
502 | |||
503 | // bail early if no taxonomies |
||
504 | if( empty($post_taxonomies) ) { |
||
505 | |||
506 | continue; |
||
507 | |||
508 | } |
||
509 | |||
510 | foreach( $post_taxonomies as $post_taxonomy ) { |
||
511 | |||
512 | if( !in_array($post_taxonomy, $taxonomies) ) { |
||
513 | |||
514 | $taxonomies[] = $post_taxonomy; |
||
515 | |||
516 | } |
||
517 | |||
518 | } |
||
519 | |||
520 | } |
||
521 | |||
522 | } else { |
||
523 | |||
524 | $taxonomies = acf_get_taxonomies(); |
||
525 | |||
526 | } |
||
527 | |||
528 | |||
529 | // terms |
||
530 | $term_groups = acf_get_taxonomy_terms( $taxonomies ); |
||
531 | |||
532 | |||
533 | // update $term_groups with specific terms |
||
534 | if( !empty($field['taxonomy']) ) { |
||
535 | |||
536 | foreach( array_keys($term_groups) as $taxonomy ) { |
||
537 | |||
538 | foreach( array_keys($term_groups[ $taxonomy ]) as $term ) { |
||
539 | |||
540 | if( ! in_array($term, $field['taxonomy']) ) { |
||
541 | |||
542 | unset($term_groups[ $taxonomy ][ $term ]); |
||
543 | |||
544 | } |
||
545 | |||
546 | } |
||
547 | |||
548 | } |
||
549 | |||
550 | } |
||
551 | |||
552 | } |
||
553 | // end taxonomy filter |
||
554 | |||
555 | ?> |
||
556 | <div <?php acf_esc_attr_e($atts); ?>> |
||
557 | |||
558 | <div class="acf-hidden"> |
||
559 | <input type="hidden" name="<?php echo $field['name']; ?>" value="" /> |
||
560 | </div> |
||
561 | |||
562 | <?php if( $width['search'] || $width['post_type'] || $width['taxonomy'] ): ?> |
||
563 | <div class="filters"> |
||
564 | |||
565 | <ul class="acf-hl"> |
||
566 | |||
567 | <?php if( $width['search'] ): ?> |
||
568 | <li style="width:<?php echo $width['search']; ?>%;"> |
||
569 | <div class="inner"> |
||
570 | <input class="filter" data-filter="s" placeholder="<?php _e("Search...",'acf'); ?>" type="text" /> |
||
571 | </div> |
||
572 | </li> |
||
573 | <?php endif; ?> |
||
574 | |||
575 | <?php if( $width['post_type'] ): ?> |
||
576 | <li style="width:<?php echo $width['post_type']; ?>%;"> |
||
577 | <div class="inner"> |
||
578 | <select class="filter" data-filter="post_type"> |
||
579 | <option value=""><?php _e('Select post type','acf'); ?></option> |
||
580 | <?php foreach( $post_types as $k => $v ): ?> |
||
581 | <option value="<?php echo $k; ?>"><?php echo $v; ?></option> |
||
582 | <?php endforeach; ?> |
||
583 | </select> |
||
584 | </div> |
||
585 | </li> |
||
586 | <?php endif; ?> |
||
587 | |||
588 | <?php if( $width['taxonomy'] ): ?> |
||
589 | <li style="width:<?php echo $width['taxonomy']; ?>%;"> |
||
590 | <div class="inner"> |
||
591 | <select class="filter" data-filter="taxonomy"> |
||
592 | <option value=""><?php _e('Select taxonomy','acf'); ?></option> |
||
593 | <?php foreach( $term_groups as $k_opt => $v_opt ): ?> |
||
594 | <optgroup label="<?php echo $k_opt; ?>"> |
||
595 | <?php foreach( $v_opt as $k => $v ): ?> |
||
596 | <option value="<?php echo $k; ?>"><?php echo $v; ?></option> |
||
597 | <?php endforeach; ?> |
||
598 | </optgroup> |
||
599 | <?php endforeach; ?> |
||
600 | </select> |
||
601 | </div> |
||
602 | </li> |
||
603 | <?php endif; ?> |
||
604 | </ul> |
||
605 | |||
606 | </div> |
||
607 | <?php endif; ?> |
||
608 | |||
609 | <div class="selection acf-cf"> |
||
610 | |||
611 | <div class="choices"> |
||
612 | |||
613 | <ul class="acf-bl list"></ul> |
||
614 | |||
615 | </div> |
||
616 | |||
617 | <div class="values"> |
||
618 | |||
619 | <ul class="acf-bl list"> |
||
620 | |||
621 | <?php if( !empty($field['value']) ): |
||
622 | |||
623 | // get posts |
||
624 | $posts = acf_get_posts(array( |
||
625 | 'post__in' => $field['value'], |
||
626 | 'post_type' => $field['post_type'] |
||
627 | )); |
||
628 | |||
629 | |||
630 | // set choices |
||
631 | if( !empty($posts) ): |
||
632 | |||
633 | foreach( array_keys($posts) as $i ): |
||
634 | |||
635 | // vars |
||
636 | $post = acf_extract_var( $posts, $i ); |
||
637 | |||
638 | |||
639 | ?><li> |
||
640 | <input type="hidden" name="<?php echo $field['name']; ?>[]" value="<?php echo $post->ID; ?>" /> |
||
641 | <span data-id="<?php echo $post->ID; ?>" class="acf-rel-item"> |
||
642 | <?php echo $this->get_post_title( $post, $field ); ?> |
||
643 | <a href="#" class="acf-icon -minus small dark" data-name="remove_item"></a> |
||
644 | </span> |
||
645 | </li><?php |
||
646 | |||
647 | endforeach; |
||
648 | |||
649 | endif; |
||
650 | |||
651 | endif; ?> |
||
652 | |||
653 | </ul> |
||
654 | |||
655 | |||
656 | |||
657 | </div> |
||
658 | |||
659 | </div> |
||
660 | |||
661 | </div> |
||
662 | <?php |
||
663 | } |
||
664 | |||
927 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.