Conditions | 88 |
Paths | 0 |
Total Lines | 418 |
Code Lines | 228 |
Lines | 30 |
Ratio | 7.18 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
489 | protected function get_comment_ids() { |
||
490 | // Assemble clauses related to 'comment_approved'. |
||
491 | $approved_clauses = array(); |
||
492 | |||
493 | // 'status' accepts an array or a comma-separated string. |
||
494 | $status_clauses = array(); |
||
495 | $statuses = $this->query_vars['status']; |
||
496 | if ( ! is_array( $statuses ) ) { |
||
497 | $statuses = preg_split( '/[\s,]+/', $statuses ); |
||
498 | } |
||
499 | |||
500 | // 'any' overrides other statuses. |
||
501 | if ( ! in_array( 'any', $statuses ) ) { |
||
502 | foreach ( $statuses as $status ) { |
||
503 | switch ( $status ) { |
||
504 | case 'hold' : |
||
505 | $status_clauses[] = "comment_approved = '0'"; |
||
506 | break; |
||
507 | |||
508 | case 'approve' : |
||
509 | $status_clauses[] = "comment_approved = '1'"; |
||
510 | break; |
||
511 | |||
512 | case 'all' : |
||
513 | case '' : |
||
514 | $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; |
||
515 | break; |
||
516 | |||
517 | default : |
||
518 | $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status ); |
||
519 | break; |
||
520 | } |
||
521 | } |
||
522 | |||
523 | if ( ! empty( $status_clauses ) ) { |
||
524 | $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; |
||
525 | } |
||
526 | } |
||
527 | |||
528 | // User IDs or emails whose unapproved comments are included, regardless of $status. |
||
529 | if ( ! empty( $this->query_vars['include_unapproved'] ) ) { |
||
530 | $include_unapproved = $this->query_vars['include_unapproved']; |
||
531 | |||
532 | // Accepts arrays or comma-separated strings. |
||
533 | if ( ! is_array( $include_unapproved ) ) { |
||
534 | $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved ); |
||
535 | } |
||
536 | |||
537 | $unapproved_ids = $unapproved_emails = array(); |
||
538 | foreach ( $include_unapproved as $unapproved_identifier ) { |
||
539 | // Numeric values are assumed to be user ids. |
||
540 | if ( is_numeric( $unapproved_identifier ) ) { |
||
541 | $approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); |
||
542 | |||
543 | // Otherwise we match against email addresses. |
||
544 | } else { |
||
545 | $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); |
||
546 | } |
||
547 | } |
||
548 | } |
||
549 | |||
550 | // Collapse comment_approved clauses into a single OR-separated clause. |
||
551 | if ( ! empty( $approved_clauses ) ) { |
||
552 | if ( 1 === count( $approved_clauses ) ) { |
||
553 | $this->sql_clauses['where']['approved'] = $approved_clauses[0]; |
||
554 | } else { |
||
555 | $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; |
||
556 | } |
||
557 | } |
||
558 | |||
559 | $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; |
||
560 | |||
561 | // Disable ORDER BY with 'none', an empty array, or boolean false. |
||
562 | if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { |
||
563 | $orderby = ''; |
||
564 | } elseif ( ! empty( $this->query_vars['orderby'] ) ) { |
||
565 | $ordersby = is_array( $this->query_vars['orderby'] ) ? |
||
566 | $this->query_vars['orderby'] : |
||
567 | preg_split( '/[,\s]/', $this->query_vars['orderby'] ); |
||
568 | |||
569 | $orderby_array = array(); |
||
570 | $found_orderby_comment_ID = false; |
||
571 | foreach ( $ordersby as $_key => $_value ) { |
||
572 | if ( ! $_value ) { |
||
573 | continue; |
||
574 | } |
||
575 | |||
576 | if ( is_int( $_key ) ) { |
||
577 | $_orderby = $_value; |
||
578 | $_order = $order; |
||
579 | } else { |
||
580 | $_orderby = $_key; |
||
581 | $_order = $_value; |
||
582 | } |
||
583 | |||
584 | if ( ! $found_orderby_comment_ID && in_array( $_orderby, array( 'comment_ID', 'comment__in' ) ) ) { |
||
585 | $found_orderby_comment_ID = true; |
||
586 | } |
||
587 | |||
588 | $parsed = $this->parse_orderby( $_orderby ); |
||
589 | |||
590 | if ( ! $parsed ) { |
||
591 | continue; |
||
592 | } |
||
593 | |||
594 | if ( 'comment__in' === $_orderby ) { |
||
595 | $orderby_array[] = $parsed; |
||
596 | continue; |
||
597 | } |
||
598 | |||
599 | $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
||
600 | } |
||
601 | |||
602 | // If no valid clauses were found, order by comment_date_gmt. |
||
603 | if ( empty( $orderby_array ) ) { |
||
604 | $orderby_array[] = "{$this->db->comments}.comment_date_gmt $order"; |
||
605 | } |
||
606 | |||
607 | // To ensure determinate sorting, always include a comment_ID clause. |
||
608 | if ( ! $found_orderby_comment_ID ) { |
||
609 | $comment_ID_order = ''; |
||
610 | |||
611 | // Inherit order from comment_date or comment_date_gmt, if available. |
||
612 | foreach ( $orderby_array as $orderby_clause ) { |
||
613 | if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) { |
||
614 | $comment_ID_order = $match[1]; |
||
615 | break; |
||
616 | } |
||
617 | } |
||
618 | |||
619 | // If no date-related order is available, use the date from the first available clause. |
||
620 | if ( ! $comment_ID_order ) { |
||
621 | foreach ( $orderby_array as $orderby_clause ) { |
||
622 | if ( false !== strpos( 'ASC', $orderby_clause ) ) { |
||
623 | $comment_ID_order = 'ASC'; |
||
624 | } else { |
||
625 | $comment_ID_order = 'DESC'; |
||
626 | } |
||
627 | |||
628 | break; |
||
629 | } |
||
630 | } |
||
631 | |||
632 | // Default to DESC. |
||
633 | if ( ! $comment_ID_order ) { |
||
634 | $comment_ID_order = 'DESC'; |
||
635 | } |
||
636 | |||
637 | $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order"; |
||
638 | } |
||
639 | |||
640 | $orderby = implode( ', ', $orderby_array ); |
||
641 | } else { |
||
642 | $orderby = "{$this->db->comments}.comment_date_gmt $order"; |
||
643 | } |
||
644 | |||
645 | $number = absint( $this->query_vars['number'] ); |
||
646 | $offset = absint( $this->query_vars['offset'] ); |
||
647 | |||
648 | View Code Duplication | if ( ! empty( $number ) ) { |
|
649 | if ( $offset ) { |
||
650 | $limits = 'LIMIT ' . $offset . ',' . $number; |
||
651 | } else { |
||
652 | $limits = 'LIMIT ' . $number; |
||
653 | } |
||
654 | } |
||
655 | |||
656 | if ( $this->query_vars['count'] ) { |
||
657 | $fields = 'COUNT(*)'; |
||
658 | } else { |
||
659 | $fields = "{$this->db->comments}.comment_ID"; |
||
660 | } |
||
661 | |||
662 | $post_id = absint( $this->query_vars['post_id'] ); |
||
663 | View Code Duplication | if ( ! empty( $post_id ) ) { |
|
664 | $this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id ); |
||
665 | } |
||
666 | |||
667 | // Parse comment IDs for an IN clause. |
||
668 | if ( ! empty( $this->query_vars['comment__in'] ) ) { |
||
669 | $this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; |
||
670 | } |
||
671 | |||
672 | // Parse comment IDs for a NOT IN clause. |
||
673 | if ( ! empty( $this->query_vars['comment__not_in'] ) ) { |
||
674 | $this->sql_clauses['where']['comment__not_in'] = "{$this->db->comments}.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; |
||
675 | } |
||
676 | |||
677 | // Parse comment parent IDs for an IN clause. |
||
678 | if ( ! empty( $this->query_vars['parent__in'] ) ) { |
||
679 | $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )'; |
||
680 | } |
||
681 | |||
682 | // Parse comment parent IDs for a NOT IN clause. |
||
683 | if ( ! empty( $this->query_vars['parent__not_in'] ) ) { |
||
684 | $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )'; |
||
685 | } |
||
686 | |||
687 | // Parse comment post IDs for an IN clause. |
||
688 | if ( ! empty( $this->query_vars['post__in'] ) ) { |
||
689 | $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; |
||
690 | } |
||
691 | |||
692 | // Parse comment post IDs for a NOT IN clause. |
||
693 | if ( ! empty( $this->query_vars['post__not_in'] ) ) { |
||
694 | $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; |
||
695 | } |
||
696 | |||
697 | View Code Duplication | if ( '' !== $this->query_vars['author_email'] ) { |
|
698 | $this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); |
||
699 | } |
||
700 | |||
701 | View Code Duplication | if ( '' !== $this->query_vars['author_url'] ) { |
|
702 | $this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] ); |
||
703 | } |
||
704 | |||
705 | View Code Duplication | if ( '' !== $this->query_vars['karma'] ) { |
|
706 | $this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); |
||
707 | } |
||
708 | |||
709 | // Filtering by comment_type: 'type', 'type__in', 'type__not_in'. |
||
710 | $raw_types = array( |
||
711 | 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ), |
||
712 | 'NOT IN' => (array) $this->query_vars['type__not_in'], |
||
713 | ); |
||
714 | |||
715 | $comment_types = array(); |
||
716 | foreach ( $raw_types as $operator => $_raw_types ) { |
||
717 | $_raw_types = array_unique( $_raw_types ); |
||
718 | |||
719 | foreach ( $_raw_types as $type ) { |
||
720 | switch ( $type ) { |
||
721 | // An empty translates to 'all', for backward compatibility |
||
722 | case '': |
||
723 | case 'all' : |
||
724 | break; |
||
725 | |||
726 | case 'comment': |
||
727 | case 'comments': |
||
728 | $comment_types[ $operator ][] = "''"; |
||
729 | break; |
||
730 | |||
731 | case 'pings': |
||
732 | $comment_types[ $operator ][] = "'pingback'"; |
||
733 | $comment_types[ $operator ][] = "'trackback'"; |
||
734 | break; |
||
735 | |||
736 | default: |
||
737 | $comment_types[ $operator ][] = $this->db->prepare( '%s', $type ); |
||
738 | break; |
||
739 | } |
||
740 | } |
||
741 | |||
742 | if ( ! empty( $comment_types[ $operator ] ) ) { |
||
743 | $types_sql = implode( ', ', $comment_types[ $operator ] ); |
||
744 | $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)"; |
||
745 | } |
||
746 | } |
||
747 | |||
748 | $parent = $this->query_vars['parent']; |
||
749 | if ( $this->query_vars['hierarchical'] && ! $parent ) { |
||
750 | $parent = 0; |
||
751 | } |
||
752 | |||
753 | if ( '' !== $parent ) { |
||
754 | $this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent ); |
||
755 | } |
||
756 | |||
757 | if ( is_array( $this->query_vars['user_id'] ) ) { |
||
758 | $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; |
||
759 | View Code Duplication | } elseif ( '' !== $this->query_vars['user_id'] ) { |
|
760 | $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] ); |
||
761 | } |
||
762 | |||
763 | // Falsy search strings are ignored. |
||
764 | if ( strlen( $this->query_vars['search'] ) ) { |
||
765 | $search_sql = $this->get_search_sql( |
||
766 | $this->query_vars['search'], |
||
767 | array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) |
||
768 | ); |
||
769 | |||
770 | // Strip leading 'AND'. |
||
771 | $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql ); |
||
772 | } |
||
773 | |||
774 | // If any post-related query vars are passed, join the posts table. |
||
775 | $join_posts_table = false; |
||
776 | $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) ); |
||
777 | $post_fields = array_filter( $plucked ); |
||
778 | |||
779 | if ( ! empty( $post_fields ) ) { |
||
780 | $join_posts_table = true; |
||
781 | foreach ( $post_fields as $field_name => $field_value ) { |
||
782 | // $field_value may be an array. |
||
783 | $esses = array_fill( 0, count( (array) $field_value ), '%s' ); |
||
784 | $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); |
||
785 | } |
||
786 | } |
||
787 | |||
788 | // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'. |
||
789 | foreach ( array( 'post_status', 'post_type' ) as $field_name ) { |
||
790 | $q_values = array(); |
||
791 | if ( ! empty( $this->query_vars[ $field_name ] ) ) { |
||
792 | $q_values = $this->query_vars[ $field_name ]; |
||
793 | if ( ! is_array( $q_values ) ) { |
||
794 | $q_values = explode( ',', $q_values ); |
||
795 | } |
||
796 | |||
797 | // 'any' will cause the query var to be ignored. |
||
798 | if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) { |
||
799 | continue; |
||
800 | } |
||
801 | |||
802 | $join_posts_table = true; |
||
803 | |||
804 | $esses = array_fill( 0, count( $q_values ), '%s' ); |
||
805 | $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values ); |
||
806 | } |
||
807 | } |
||
808 | |||
809 | // Comment author IDs for an IN clause. |
||
810 | if ( ! empty( $this->query_vars['author__in'] ) ) { |
||
811 | $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; |
||
812 | } |
||
813 | |||
814 | // Comment author IDs for a NOT IN clause. |
||
815 | if ( ! empty( $this->query_vars['author__not_in'] ) ) { |
||
816 | $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; |
||
817 | } |
||
818 | |||
819 | // Post author IDs for an IN clause. |
||
820 | View Code Duplication | if ( ! empty( $this->query_vars['post_author__in'] ) ) { |
|
821 | $join_posts_table = true; |
||
822 | $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; |
||
823 | } |
||
824 | |||
825 | // Post author IDs for a NOT IN clause. |
||
826 | View Code Duplication | if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { |
|
827 | $join_posts_table = true; |
||
828 | $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; |
||
829 | } |
||
830 | |||
831 | $join = ''; |
||
832 | |||
833 | if ( $join_posts_table ) { |
||
834 | $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID"; |
||
835 | } |
||
836 | |||
837 | if ( ! empty( $this->meta_query_clauses ) ) { |
||
838 | $join .= $this->meta_query_clauses['join']; |
||
839 | |||
840 | // Strip leading 'AND'. |
||
841 | $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); |
||
842 | |||
843 | if ( ! $this->query_vars['count'] ) { |
||
844 | $groupby = "{$this->db->comments}.comment_ID"; |
||
845 | } |
||
846 | } |
||
847 | |||
848 | if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { |
||
849 | $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); |
||
850 | $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); |
||
851 | } |
||
852 | |||
853 | $where = implode( ' AND ', $this->sql_clauses['where'] ); |
||
854 | |||
855 | $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); |
||
856 | /** |
||
857 | * Filters the comment query clauses. |
||
858 | * |
||
859 | * @since 3.1.0 |
||
860 | * |
||
861 | * @param array $pieces A compacted array of comment query clauses. |
||
862 | * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. |
||
863 | */ |
||
864 | $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); |
||
865 | |||
866 | $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; |
||
867 | $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; |
||
868 | $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; |
||
869 | $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; |
||
870 | $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; |
||
871 | $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; |
||
872 | |||
873 | $this->filtered_where_clause = $where; |
||
874 | |||
875 | if ( $where ) { |
||
876 | $where = 'WHERE ' . $where; |
||
877 | } |
||
878 | |||
879 | if ( $groupby ) { |
||
880 | $groupby = 'GROUP BY ' . $groupby; |
||
881 | } |
||
882 | |||
883 | if ( $orderby ) { |
||
884 | $orderby = "ORDER BY $orderby"; |
||
885 | } |
||
886 | |||
887 | $found_rows = ''; |
||
888 | if ( ! $this->query_vars['no_found_rows'] ) { |
||
889 | $found_rows = 'SQL_CALC_FOUND_ROWS'; |
||
890 | } |
||
891 | |||
892 | $this->sql_clauses['select'] = "SELECT $found_rows $fields"; |
||
893 | $this->sql_clauses['from'] = "FROM {$this->db->comments} $join"; |
||
894 | $this->sql_clauses['groupby'] = $groupby; |
||
895 | $this->sql_clauses['orderby'] = $orderby; |
||
896 | $this->sql_clauses['limits'] = $limits; |
||
897 | |||
898 | $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; |
||
899 | |||
900 | if ( $this->query_vars['count'] ) { |
||
901 | return intval( $this->db->get_var( $this->request ) ); |
||
902 | } else { |
||
903 | $comment_ids = $this->db->get_col( $this->request ); |
||
904 | return array_map( 'intval', $comment_ids ); |
||
905 | } |
||
906 | } |
||
907 | |||
1164 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.