- if ( 'hold' == $status )
- $approved = "comment_approved = '0'";
- elseif ( 'approve' == $status )
- $approved = "comment_approved = '1'";
- elseif ( 'spam' == $status )
- $approved = "comment_approved = 'spam'";
- elseif ( 'trash' == $status )
- $approved = "comment_approved = 'trash'";
- else
- $approved = "( comment_approved = '0' OR comment_approved = '1' )";
-
- $order = ( 'ASC' == strtoupper($order) ) ? 'ASC' : 'DESC';
-
- if ( ! empty( $orderby ) ) {
- $ordersby = is_array($orderby) ? $orderby : preg_split('/[,\s]/', $orderby);
- $ordersby = array_intersect(
- $ordersby,
- array(
- 'comment_agent',
- 'comment_approved',
- 'comment_author',
- 'comment_author_email',
- 'comment_author_IP',
- 'comment_author_url',
- 'comment_content',
- 'comment_date',
- 'comment_date_gmt',
- 'comment_ID',
- 'comment_karma',
- 'comment_parent',
- 'comment_post_ID',
- 'comment_type',
- 'user_id',
- )
- );
- $orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
+ // 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 );