Users: The 'who' parameter should not interfere with 'meta_key' + 'meta_value' in `WP_User_Query`.

Props adrianosilvaferreira.
Fixes #36724.

git-svn-id: https://develop.svn.wordpress.org/trunk@37360 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-05-04 18:56:58 +00:00
parent e415270afb
commit f3ccef62be
2 changed files with 54 additions and 7 deletions

View File

@ -255,13 +255,6 @@ class WP_User_Query {
$blog_id = absint( $qv['blog_id'] );
}
if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
$qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
$qv['meta_value'] = 0;
$qv['meta_compare'] = '!=';
$qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
}
if ( $qv['has_published_posts'] && $blog_id ) {
if ( true === $qv['has_published_posts'] ) {
$post_types = get_post_types( array( 'public' => true ) );
@ -281,6 +274,29 @@ class WP_User_Query {
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $qv );
if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
$who_query = array(
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
'value' => 0,
'compare' => '!=',
);
// Prevent extra meta query.
$qv['blog_id'] = $blog_id = 0;
if ( empty( $this->meta_query->queries ) ) {
$this->meta_query->queries = array( $who_query );
} else {
// Append the cap query to the original queries and reparse the query.
$this->meta_query->queries = array(
'relation' => 'AND',
array( $this->meta_query->queries, $who_query ),
);
}
$this->meta_query->parse_query_vars( $this->meta_query->queries );
}
$roles = array();
if ( isset( $qv['role'] ) ) {
if ( is_array( $qv['role'] ) ) {

View File

@ -702,6 +702,37 @@ class Tests_User_Query extends WP_UnitTestCase {
$this->assertNotContains( self::$author_ids[2], $found );
}
/**
* @ticket 36724
*/
public function test_who_authors_should_work_alongside_meta_params() {
if ( ! is_multisite() ) {
$this->markTestSkipped( __METHOD__ . ' requires multisite.' );
}
$b = self::factory()->blog->create();
add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
add_user_to_blog( $b, self::$author_ids[1], 'author' );
add_user_to_blog( $b, self::$author_ids[2], 'editor' );
add_user_meta( self::$author_ids[1], 'foo', 'bar' );
add_user_meta( self::$author_ids[2], 'foo', 'baz' );
$q = new WP_User_Query( array(
'who' => 'authors',
'blog_id' => $b,
'meta_key' => 'foo',
'meta_value' => 'bar',
) );
$found = wp_list_pluck( $q->get_results(), 'ID' );
$this->assertNotContains( self::$author_ids[0], $found );
$this->assertContains( self::$author_ids[1], $found );
$this->assertNotContains( self::$author_ids[2], $found );
}
/**
* @ticket 32250
*/