Introduce `author__in` and `author__not_in` query vars. Fixes issue with multiple author exclusion when comma-separated string is passed for `author`. Adds a bunch of missing unit tests.

Props pollett for initial patch.
Fixes #16854.


Built from https://develop.svn.wordpress.org/trunk@25248


git-svn-id: http://core.svn.wordpress.org/trunk@25216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2013-09-04 21:33:09 +00:00
parent 365a78db7c
commit aa480a25e2
1 changed files with 22 additions and 23 deletions

View File

@ -1396,6 +1396,7 @@ class WP_Query {
, 'tag' , 'tag'
, 'cat' , 'cat'
, 'tag_id' , 'tag_id'
, 'author'
, 'author_name' , 'author_name'
, 'feed' , 'feed'
, 'tb' , 'tb'
@ -1416,7 +1417,8 @@ class WP_Query {
} }
$array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in' ); 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
'author__in', 'author__not_in' );
foreach ( $array_keys as $key ) { foreach ( $array_keys as $key ) {
if ( !isset($array[$key]) ) if ( !isset($array[$key]) )
@ -1457,6 +1459,7 @@ class WP_Query {
$qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] ); $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
$qv['paged'] = absint($qv['paged']); $qv['paged'] = absint($qv['paged']);
$qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
$qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
$qv['pagename'] = trim( $qv['pagename'] ); $qv['pagename'] = trim( $qv['pagename'] );
$qv['name'] = trim( $qv['name'] ); $qv['name'] = trim( $qv['name'] );
if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']); if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
@ -1769,9 +1772,9 @@ class WP_Query {
$q['category__in'][] = absint( reset( $q['category__and'] ) ); $q['category__in'][] = absint( reset( $q['category__and'] ) );
unset( $q['category__and'] ); unset( $q['category__and'] );
} }
if ( ! empty( $q['category__in'] ) ) { if ( ! empty( $q['category__in'] ) ) {
$q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
$tax_query[] = array( $tax_query[] = array(
'taxonomy' => 'category', 'taxonomy' => 'category',
'terms' => $q['category__in'], 'terms' => $q['category__in'],
@ -2333,26 +2336,22 @@ class WP_Query {
// Author/user stuff // Author/user stuff
if ( empty($q['author']) || ($q['author'] == '0') ) { if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
$whichauthor = ''; $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
} else { $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
$q['author'] = (string)urldecode($q['author']); foreach ( $authors as $author ) {
$q['author'] = addslashes_gpc($q['author']); $key = $author > 0 ? 'author__in' : 'author__not_in';
if ( strpos($q['author'], '-') !== false ) { $q[$key][] = abs( $author );
$eq = '!=';
$andor = 'AND';
$q['author'] = explode('-', $q['author']);
$q['author'] = (string)absint($q['author'][1]);
} else {
$eq = '=';
$andor = 'OR';
} }
$author_array = preg_split('/[,\s]+/', $q['author']); $q['author'] = implode( ',', $authors );
$_author_array = array(); }
foreach ( $author_array as $key => $_author )
$_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author); if ( ! empty( $q['author__not_in'] ) ) {
$whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')'; $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
unset($author_array, $_author_array); $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
} elseif ( ! empty( $q['author__in'] ) ) {
$author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
$where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
} }
// Author stuff for nice URLs // Author stuff for nice URLs
@ -2457,7 +2456,7 @@ class WP_Query {
$in_search_post_types = get_post_types( array('exclude_from_search' => false) ); $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
if ( empty( $in_search_post_types ) ) if ( empty( $in_search_post_types ) )
$where .= ' AND 1=0 '; $where .= ' AND 1=0 ';
else else
$where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')"; $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
} elseif ( !empty( $post_type ) && is_array( $post_type ) ) { } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
$where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')"; $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";