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
This commit is contained in:
Boone Gorges 2014-10-29 21:50:22 +00:00
parent ee6d4968da
commit 56f55ea507
2 changed files with 40 additions and 7 deletions

View File

@ -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'] ) {

View File

@ -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.