From 56f55ea507a9740888ea52c7546dfbb24e79f275 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 29 Oct 2014 21:50:22 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@30096 git-svn-id: http://core.svn.wordpress.org/trunk@30096 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/comment.php | 45 +++++++++++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 76715f5f1f..0906040cba 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -304,6 +304,8 @@ class WP_Comment_Query { 'post_type' => '', 'status' => 'all', 'type' => '', + 'type__in' => '', + 'type__not_in' => '', 'user_id' => '', 'search' => '', 'count' => false, @@ -520,12 +522,43 @@ class WP_Comment_Query { $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); } - if ( 'comment' == $this->query_vars['type'] ) { - $where[] = "comment_type = ''"; - } elseif( 'pings' == $this->query_vars['type'] ) { - $where[] = 'comment_type IN ("pingback", "trackback")'; - } elseif ( ! empty( $this->query_vars['type'] ) ) { - $where[] = $wpdb->prepare( 'comment_type = %s', $this->query_vars['type'] ); + // 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'] ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index c3d594cd5c..240123a23e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.1-alpha-30095'; +$wp_version = '4.1-alpha-30096'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.