Make WordPress Core

Ticket #12668: 12668-v2-comments-query.patch

File 12668-v2-comments-query.patch, 3.7 KB (added by dancameron, 10 years ago)

This patch addresses all points of the new scope

  • src/wp-includes/comment.php

     
    131131 * @uses $wpdb
    132132 *
    133133 * @param int $post_id The ID of the post
     134 * @param array $args WP_Comment_Query args
    134135 * @return array $comments The approved comments
    135136 */
    136 function get_approved_comments($post_id) {
    137         global $wpdb;
    138         return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
     137function get_approved_comments( $post_id = 0, $args = array() ) {
     138        $defaults = array(
     139                'status' => 1,
     140                'post_id' => $post_id
     141        );
     142        $r = wp_parse_args( $args, $defaults );
     143
     144        $query = new WP_Comment_Query;
     145        return $query->query( $r );
    139146}
    140147
    141148/**
     
    304311                        'post_type' => '',
    305312                        'status' => '',
    306313                        'type' => '',
     314                        'type__in' => '',
     315                        'type__not_in' => '',
    307316                        'user_id' => '',
    308317                        'search' => '',
    309318                        'count' => false,
     
    492501                        $where .= $wpdb->prepare( ' AND comment_karma = %d', $this->query_vars['karma'] );
    493502                }
    494503
    495                 if ( 'comment' == $this->query_vars['type'] ) {
    496                         $where .= " AND comment_type = ''";
    497                 } elseif( 'pings' == $this->query_vars['type'] ) {
    498                         $where .= ' AND comment_type IN ("pingback", "trackback")';
    499                 } elseif ( ! empty( $this->query_vars['type'] ) ) {
    500                         $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
     504                // Array of comment types
     505                $comment_types = ( ! is_array( $this->query_vars['type'] ) ) ? array( $this->query_vars['type'] ) : $this->query_vars['type'] ;
     506                // Merge in __in array of comment types
     507                $comment_types = array_merge( $comment_types, (array) $this->query_vars['type__in'] );
     508                $query_types = array();
     509                foreach ( $comment_types as $type ) {
     510                        if ( is_array( $type ) ) {
     511                                // A multi-dimensional array of comments types is unsupported.
     512                                continue;
     513                        }
     514                        switch ( $type ) {
     515                                case '': // explicit '' means all comments for backwards compatibility.
     516                                        break;
     517                                case 'comment':
     518                                case 'comments': // alias
     519                                        $query_types[] = "''";
     520                                        break;
     521                                case 'pings':
     522                                        $query_types[] = "'pingback'";
     523                                        $query_types[] = "'trackback'";
     524                                        break;
     525                                default:
     526                                        $query_types[] = $wpdb->prepare( '%s', $type );
     527                                        break;
     528                        }
    501529                }
    502530
     531                // Query based on comment type
     532                if ( ! empty( $query_types ) ) {
     533                        $where .= ' AND comment_type IN ( ' . implode( ', ', $query_types ) . ' )';
     534                }
     535
     536                // Array of comment types to exclude
     537                $excluded_comment_types = ( ! is_array( $this->query_vars['type__not_in'] ) ) ? array( $this->query_vars['type__not_in'] ) : $this->query_vars['type__not_in'] ;
     538                $excluded_query_types = array();
     539                foreach ( $excluded_comment_types as $type ) {
     540                        if ( is_array( $type ) ) {
     541                                // A multi-dimensional array of comments types is unsupported.
     542                                continue;
     543                        }
     544                        switch ( $type ) {
     545                                case '': // explicit '' means all comments for backwards compatibility.
     546                                        break;
     547                                case 'comment':
     548                                case 'comments': // alias
     549                                        $excluded_query_types[] = "''";
     550                                        break;
     551                                case 'pings':
     552                                        $excluded_query_types[] = "'pingback'";
     553                                        $excluded_query_types[] = "'trackback'";
     554                                        break;
     555                                default:
     556                                        $excluded_query_types[] = $wpdb->prepare( '%s', $type );
     557                                        break;
     558                        }
     559                }
     560
     561                // Query based on comment type
     562                if ( ! empty( $excluded_query_types ) ) {
     563                        $where .= ' AND comment_type NOT IN ( ' . implode( ', ', $excluded_query_types ) . ' )';
     564                }
     565
    503566                if ( '' !== $this->query_vars['parent'] ) {
    504567                        $where .= $wpdb->prepare( ' AND comment_parent = %d', $this->query_vars['parent'] );
    505568                }