+ 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 ) {
+ $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $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( $this->meta_query->queries ) ) {
+ $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
+ $join .= $clauses['join'];
+
+ // Strip leading 'AND'.
+ $where[] = preg_replace( '/^\s*AND\s*/', '', $clauses['where'] );
+
+ if ( ! $this->query_vars['count'] ) {
+ $groupby = "{$wpdb->comments}.comment_ID";
+ }
+ }
+
+ $date_query = $this->query_vars['date_query'];
+ if ( ! empty( $date_query ) && is_array( $date_query ) ) {
+ $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
+ $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
+ }
+
+ $where = implode( ' AND ', $where );
+
+ $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
+ /**
+ * Filter the comment query clauses.
+ *
+ * @since 3.1.0
+ *
+ * @param array $pieces A compacted array of comment query clauses.
+ * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
+ */
+ $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
+
+ $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
+ $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
+ $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
+ $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
+ $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
+ $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
+ $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
+
+ if ( $where ) {
+ $where = 'WHERE ' . $where;
+ }
+
+ if ( $groupby ) {
+ $groupby = 'GROUP BY ' . $groupby;
+ }
+
+ if ( $orderby ) {
+ $orderby = "ORDER BY $orderby $order";
+ }
+
+ $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
+
+ if ( $this->query_vars['count'] ) {
+ return $wpdb->get_var( $this->request );
+ }
+
+ if ( 'ids' == $this->query_vars['fields'] ) {
+ $this->comments = $wpdb->get_col( $this->request );
+ return array_map( 'intval', $this->comments );
+ }
+
+ $results = $wpdb->get_results( $this->request );
+ /**
+ * Filter the comment query results.
+ *
+ * @since 3.1.0
+ *
+ * @param array $results An array of comments.
+ * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
+ */
+ $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) );
+
+ wp_cache_add( $cache_key, $comments, 'comment' );
+
+ return $comments;