+ /**
+ * Get a list of comments matching the query vars.
+ *
+ * @since 4.2.0
+ * @access public
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @return int|array The list of comments.
+ */
+ public function get_comments() {
+ global $wpdb;
+
+ $groupby = '';
+
+ $this->parse_query();
+
+ // Parse meta query
+ $this->meta_query = new WP_Meta_Query();
+ $this->meta_query->parse_query_vars( $this->query_vars );
+
+ /**
+ * Fires before comments are retrieved.
+ *
+ * @since 3.1.0
+ *
+ * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference.
+ */
+ do_action_ref_array( 'pre_get_comments', array( &$this ) );
+
+ // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
+ $this->meta_query->parse_query_vars( $this->query_vars );
+ if ( ! empty( $this->meta_query->queries ) ) {
+ $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
+ }
+
+ // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
+ $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
+ $last_changed = wp_cache_get( 'last_changed', 'comment' );
+ if ( ! $last_changed ) {
+ $last_changed = microtime();
+ wp_cache_set( 'last_changed', $last_changed, 'comment' );
+ }
+ $cache_key = "get_comments:$key:$last_changed";
+
+ if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
+ $this->comments = $cache;
+ return $this->comments;
+ }
+
+ $where = array();
+
+ // Assemble clauses related to 'comment_approved'.
+ $approved_clauses = array();
+
+ // 'status' accepts an array or a comma-separated string.
+ $status_clauses = array();
+ $statuses = $this->query_vars['status'];
+ if ( ! is_array( $statuses ) ) {
+ $statuses = preg_split( '/[\s,]+/', $statuses );
+ }
+
+ // 'any' overrides other statuses.
+ if ( ! in_array( 'any', $statuses ) ) {
+ foreach ( $statuses as $status ) {
+ switch ( $status ) {
+ case 'hold' :
+ $status_clauses[] = "comment_approved = '0'";
+ break;
+
+ case 'approve' :
+ $status_clauses[] = "comment_approved = '1'";
+ break;
+
+ case 'all' :
+ case '' :
+ $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
+ break;
+
+ default :
+ $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
+ break;
+ }
+ }
+
+ if ( ! empty( $status_clauses ) ) {
+ $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )';
+ }
+ }
+
+ // User IDs or emails whose unapproved comments are included, regardless of $status.
+ if ( ! empty( $this->query_vars['include_unapproved'] ) ) {
+ $include_unapproved = $this->query_vars['include_unapproved'];
+
+ // Accepts arrays or comma-separated strings.
+ if ( ! is_array( $include_unapproved ) ) {
+ $include_unapproved = preg_split( '/[\s,]+/', $include_unapproved );
+ }
+
+ $unapproved_ids = $unapproved_emails = array();
+ foreach ( $include_unapproved as $unapproved_identifier ) {
+ // Numeric values are assumed to be user ids.
+ if ( is_numeric( $unapproved_identifier ) ) {
+ $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
+
+ // Otherwise we match against email addresses.
+ } else {
+ $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
+ }
+ }
+ }
+
+ // Collapse comment_approved clauses into a single OR-separated clause.
+ if ( ! empty( $approved_clauses ) ) {
+ if ( 1 === count( $approved_clauses ) ) {
+ $where[] = $approved_clauses[0];
+ } else {
+ $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
+ }
+ }
+
+ $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC';
+
+ // Disable ORDER BY with 'none', an empty array, or boolean false.
+ if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
+ $orderby = '';
+ } elseif ( ! empty( $this->query_vars['orderby'] ) ) {
+ $ordersby = is_array( $this->query_vars['orderby'] ) ?
+ $this->query_vars['orderby'] :
+ preg_split( '/[,\s]/', $this->query_vars['orderby'] );
+
+ $orderby_array = array();
+ $found_orderby_comment_ID = false;
+ foreach ( $ordersby as $_key => $_value ) {
+ if ( ! $_value ) {
+ continue;
+ }
+
+ if ( is_int( $_key ) ) {
+ $_orderby = $_value;
+ $_order = $order;
+ } else {
+ $_orderby = $_key;
+ $_order = $_value;
+ }
+
+ if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
+ $found_orderby_comment_ID = true;
+ }
+
+ $parsed = $this->parse_orderby( $_orderby );
+
+ if ( ! $parsed ) {
+ continue;
+ }
+
+ $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
+ }
+
+ // If no valid clauses were found, order by comment_date_gmt.
+ if ( empty( $orderby_array ) ) {
+ $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
+ }
+
+ // To ensure determinate sorting, always include a comment_ID clause.
+ if ( ! $found_orderby_comment_ID ) {
+ $comment_ID_order = '';
+
+ // Inherit order from comment_date or comment_date_gmt, if available.
+ foreach ( $orderby_array as $orderby_clause ) {
+ if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
+ $comment_ID_order = $match[1];
+ break;
+ }
+ }
+
+ // If no date-related order is available, use the date from the first available clause.
+ if ( ! $comment_ID_order ) {
+ foreach ( $orderby_array as $orderby_clause ) {
+ if ( false !== strpos( 'ASC', $orderby_clause ) ) {
+ $comment_ID_order = 'ASC';
+ } else {
+ $comment_ID_order = 'DESC';
+ }
+
+ break;
+ }
+ }
+
+ // Default to DESC.
+ if ( ! $comment_ID_order ) {
+ $comment_ID_order = 'DESC';
+ }
+
+ $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
+ }
+
+ $orderby = implode( ', ', $orderby_array );
+ } else {
+ $orderby = "$wpdb->comments.comment_date_gmt $order";
+ }
+
+ $number = absint( $this->query_vars['number'] );
+ $offset = absint( $this->query_vars['offset'] );
+
+ if ( ! empty( $number ) ) {
+ if ( $offset ) {
+ $limits = 'LIMIT ' . $offset . ',' . $number;
+ } else {
+ $limits = 'LIMIT ' . $number;
+ }
+ } else {
+ $limits = '';
+ }
+
+ if ( $this->query_vars['count'] ) {
+ $fields = 'COUNT(*)';
+ } else {
+ switch ( strtolower( $this->query_vars['fields'] ) ) {
+ case 'ids':
+ $fields = "$wpdb->comments.comment_ID";
+ break;
+ default:
+ $fields = "*";
+ break;
+ }
+ }
+
+ $join = '';
+
+ $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[] = "$wpdb->comments.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[] = "$wpdb->comments.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'],