WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/29/2014 09:49:08 PM (6 years ago)
Author:
boonebgorges
Message:

Better flexibility for 'type' in WP_Comment_Query.

  • Add support for an array of values in 'type'.
  • Introduce type__in parameter. This duplicates 'type' but is added for better consistency with other query classes.
  • Introduce type__not_in.

Among other things, these changes will make it easier for plugin authors to
manage the appearance of custom comment types in various WP interfaces.

Props dancameron, mordauk.
See #12668.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r30093 r30096  
    305305            'status' => 'all',
    306306            'type' => '',
     307            'type__in' => '',
     308            'type__not_in' => '',
    307309            'user_id' => '',
    308310            'search' => '',
     
    521523        }
    522524
    523         if ( 'comment' == $this->query_vars['type'] ) {
    524             $where[] = "comment_type = ''";
    525         } elseif( 'pings' == $this->query_vars['type'] ) {
    526             $where[] = 'comment_type IN ("pingback", "trackback")';
    527         } elseif ( ! empty( $this->query_vars['type'] ) ) {
    528             $where[] = $wpdb->prepare( 'comment_type = %s', $this->query_vars['type'] );
     525        // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
     526        $raw_types = array(
     527            'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),
     528            'NOT IN' => (array) $this->query_vars['type__not_in'],
     529        );
     530
     531        $comment_types = array();
     532        foreach ( $raw_types as $operator => $_raw_types ) {
     533            $_raw_types = array_unique( $_raw_types );
     534
     535            foreach ( $_raw_types as $type ) {
     536                switch ( $type ) {
     537                    // An empty translates to 'all', for backward compatibility
     538                    case '':
     539                    case 'all' :
     540                        break;
     541
     542                    case 'comment':
     543                    case 'comments':
     544                        $comment_types[ $operator ][] = "''";
     545                        break;
     546
     547                    case 'pings':
     548                        $comment_types[ $operator ][] = "'pingback'";
     549                        $comment_types[ $operator ][] = "'trackback'";
     550                        break;
     551
     552                    default:
     553                        $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
     554                        break;
     555                }
     556            }
     557
     558            if ( ! empty( $comment_types[ $operator ] ) ) {
     559                $types_sql = implode( ', ', $comment_types[ $operator ] );
     560                $where[] = "comment_type $operator ($types_sql)";
     561            }
    529562        }
    530563
Note: See TracChangeset for help on using the changeset viewer.