+
+ $post_id = absint( $this->query_vars['post_id'] );
+ if ( ! empty( $post_id ) ) {
+ $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
+ }
+
+ // Parse comment IDs for an IN clause.
+ if ( ! empty( $this->query_vars['comment__in'] ) ) {
+ $where[] = 'comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
+ }
+
+ // Parse comment IDs for a NOT IN clause.
+ if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
+ $where[] = 'comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
+ }
+
+ // Parse comment post IDs for an IN clause.
+ if ( ! empty( $this->query_vars['post__in'] ) ) {
+ $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
+ }
+
+ // Parse comment post IDs for a NOT IN clause.
+ if ( ! empty( $this->query_vars['post__not_in'] ) ) {
+ $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
+ }
+
+ if ( '' !== $this->query_vars['author_email'] ) {
+ $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
+ }
+
+ if ( '' !== $this->query_vars['karma'] ) {
+ $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
+ }
+
+ // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
+ $raw_types = array(
+ 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
+ 'NOT IN' => (array) $this->query_vars['type__not_in'],
+ );
+
+ $comment_types = array();
+ foreach ( $raw_types as $operator => $_raw_types ) {
+ $_raw_types = array_unique( $_raw_types );
+
+ foreach ( $_raw_types as $type ) {
+ switch ( $type ) {
+ // An empty translates to 'all', for backward compatibility
+ case '':
+ case 'all' :
+ break;
+
+ case 'comment':
+ case 'comments':
+ $comment_types[ $operator ][] = "''";
+ break;
+
+ case 'pings':
+ $comment_types[ $operator ][] = "'pingback'";
+ $comment_types[ $operator ][] = "'trackback'";
+ break;
+
+ default:
+ $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
+ break;
+ }
+ }
+
+ if ( ! empty( $comment_types[ $operator ] ) ) {
+ $types_sql = implode( ', ', $comment_types[ $operator ] );
+ $where[] = "comment_type $operator ($types_sql)";
+ }
+ }
+
+ if ( '' !== $this->query_vars['parent'] ) {
+ $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
+ }
+
+ if ( is_array( $this->query_vars['user_id'] ) ) {
+ $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
+ } elseif ( '' !== $this->query_vars['user_id'] ) {
+ $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
+ }
+
+ if ( '' !== $this->query_vars['search'] ) {
+ $search_sql = $this->get_search_sql(
+ $this->query_vars['search'],
+ array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
+ );
+
+ // Strip leading 'AND'.
+ $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
+ }
+
+ // If any post-related query vars are passed, join the posts table.
+ $join_posts_table = false;
+ $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
+ $post_fields = array_filter( $plucked );
+
+ if ( ! empty( $post_fields ) ) {
+ $join_posts_table = true;
+ foreach ( $post_fields as $field_name => $field_value ) {
+ // $field_value may be an array.
+ $esses = array_fill( 0, count( (array) $field_value ), '%s' );
+ $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
+ }
+ }
+
+ // Comment author IDs for an IN clause.
+ if ( ! empty( $this->query_vars['author__in'] ) ) {
+ $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
+ }
+
+ // Comment author IDs for a NOT IN clause.
+ if ( ! empty( $this->query_vars['author__not_in'] ) ) {
+ $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
+ }
+
+ // Post author IDs for an IN clause.
+ if ( ! empty( $this->query_vars['post_author__in'] ) ) {
+ $join_posts_table = true;
+ $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
+ }
+
+ // Post author IDs for a NOT IN clause.
+ if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
+ $join_posts_table = true;
+ $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
+ }
+
+ if ( $join_posts_table ) {
+ $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
+ }
+
+ if ( ! empty( $meta_query_clauses ) ) {
+ $join .= $meta_query_clauses['join'];
+
+ // Strip leading 'AND'.
+ $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
+
+ if ( ! $this->query_vars['count'] ) {
+ $groupby = "{$wpdb->comments}.comment_ID";
+ }