Query: Add nicename and login params to user query.
New parameters are: `login`, `login__in`, `login__not_in`, `nicename`, `nicename__in`, `nicename__not_in`. `login__in` and `nicename__in` are also now valid values for the 'orderby' parameter. Props ryanplas. Fixes #36624. git-svn-id: https://develop.svn.wordpress.org/trunk@38715 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
0a23c4d206
commit
3b6202a4d2
|
@ -124,6 +124,12 @@ class WP_User_Query {
|
|||
'fields' => 'all',
|
||||
'who' => '',
|
||||
'has_published_posts' => null,
|
||||
'nicename' => '',
|
||||
'nicename__in' => array(),
|
||||
'nicename__not_in' => array(),
|
||||
'login' => '',
|
||||
'login__in' => array(),
|
||||
'login__not_in' => array()
|
||||
);
|
||||
|
||||
return wp_parse_args( $args, $defaults );
|
||||
|
@ -140,6 +146,8 @@ class WP_User_Query {
|
|||
* @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to
|
||||
* permit an array or comma-separated list of values. The 'number' parameter was updated to support
|
||||
* querying for all users with using -1.
|
||||
* @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in',
|
||||
* and 'login__not_in' parameters.
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
|
@ -173,12 +181,12 @@ class WP_User_Query {
|
|||
* an array of values, or a multi-dimensional array with fields as
|
||||
* keys and orders ('ASC' or 'DESC') as values. Accepted values are
|
||||
* 'ID', 'display_name' (or 'name'), 'include', 'user_login'
|
||||
* (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
|
||||
* (or 'email'), 'user_url' (or 'url'), 'user_registered'
|
||||
* or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
|
||||
* the value of `$meta_key`, or an array key of `$meta_query`. To use
|
||||
* 'meta_value' or 'meta_value_num', `$meta_key` must be also be
|
||||
* defined. Default 'user_login'.
|
||||
* (or 'login'), 'login__in', 'user_nicename' (or 'nicename'),
|
||||
* 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'),
|
||||
* 'user_registered' (or 'registered'), 'post_count', 'meta_value',
|
||||
* 'meta_value_num', the value of `$meta_key`, or an array key of
|
||||
* `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key`
|
||||
* must be also be defined. Default 'user_login'.
|
||||
* @type string $order Designates ascending or descending order of users. Order values
|
||||
* passed as part of an `$orderby` array take precedence over this
|
||||
* parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
|
||||
|
@ -203,6 +211,16 @@ class WP_User_Query {
|
|||
* @type bool|array $has_published_posts Pass an array of post types to filter results to users who have
|
||||
* published posts in those post types. `true` is an alias for all
|
||||
* public post types.
|
||||
* @type string $nicename The user nicename. Default empty.
|
||||
* @type array $nicename__in An array of nicenames to include. Users matching one of these
|
||||
* nicenames will be included in results. Default empty array.
|
||||
* @type array $nicename__not_in An array of nicenames to exclude. Users matching one of these
|
||||
* nicenames will not be included in results. Default empty array.
|
||||
* @type string $login The user login. Default empty.
|
||||
* @type array $login__in An array of logins to include. Users matching one of these
|
||||
* logins will be included in results. Default empty array.
|
||||
* @type array $login__not_in An array of logins to exclude. Users matching one of these
|
||||
* logins will not be included in results. Default empty array.
|
||||
* }
|
||||
*/
|
||||
public function prepare_query( $query = array() ) {
|
||||
|
@ -276,6 +294,40 @@ class WP_User_Query {
|
|||
$this->query_where .= " AND {$this->db->users}.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
|
||||
}
|
||||
|
||||
// nicename
|
||||
if ( '' !== $qv['nicename']) {
|
||||
$this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $qv['nicename__in'] ) ) {
|
||||
$sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] );
|
||||
$nicename__in = implode( "','", $sanitized_nicename__in );
|
||||
$this->query_where .= " AND user_nicename IN ( '$nicename__in' )";
|
||||
}
|
||||
|
||||
if ( ! empty( $qv['nicename__not_in'] ) ) {
|
||||
$sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] );
|
||||
$nicename__not_in = implode( "','", $sanitized_nicename__not_in );
|
||||
$this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )";
|
||||
}
|
||||
|
||||
// login
|
||||
if ( '' !== $qv['login']) {
|
||||
$this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $qv['login__in'] ) ) {
|
||||
$sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] );
|
||||
$login__in = implode( "','", $sanitized_login__in );
|
||||
$this->query_where .= " AND user_login IN ( '$login__in' )";
|
||||
}
|
||||
|
||||
if ( ! empty( $qv['login__not_in'] ) ) {
|
||||
$sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] );
|
||||
$login__not_in = implode( "','", $sanitized_login__not_in );
|
||||
$this->query_where .= " AND user_login NOT IN ( '$login__not_in' )";
|
||||
}
|
||||
|
||||
// Meta query.
|
||||
$this->meta_query = new WP_Meta_Query();
|
||||
$this->meta_query->parse_query_vars( $qv );
|
||||
|
@ -434,7 +486,11 @@ class WP_User_Query {
|
|||
continue;
|
||||
}
|
||||
|
||||
$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
|
||||
if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) {
|
||||
$orderby_array[] = $parsed;
|
||||
} else {
|
||||
$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
|
||||
}
|
||||
}
|
||||
|
||||
// If no valid clauses were found, order by user_login.
|
||||
|
@ -700,6 +756,14 @@ class WP_User_Query {
|
|||
$include = wp_parse_id_list( $this->query_vars['include'] );
|
||||
$include_sql = implode( ',', $include );
|
||||
$_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
|
||||
} elseif ( 'nicename__in' === $orderby ) {
|
||||
$sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
|
||||
$nicename__in = implode( "','", $sanitized_nicename__in );
|
||||
$_orderby = "FIELD( user_nicename, '$nicename__in' )";
|
||||
} elseif ( 'login__in' === $orderby ) {
|
||||
$sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] );
|
||||
$login__in = implode( "','", $sanitized_login__in );
|
||||
$_orderby = "FIELD( user_login, '$login__in' )";
|
||||
} elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
|
||||
$meta_clause = $meta_query_clauses[ $orderby ];
|
||||
$_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
|
||||
|
|
|
@ -877,6 +877,193 @@ class Tests_User_Query extends WP_UnitTestCase {
|
|||
$this->assertEqualSets( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_nicename_returns_user_with_nicename() {
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[0],
|
||||
'user_nicename' => 'peter'
|
||||
) );
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'nicename' => 'peter'
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[0] );
|
||||
|
||||
$this->assertContains( "AND user_nicename = 'peter'", $q->query_where);
|
||||
$this->assertEqualSets( $expected, $found);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_nicename__in_returns_users_with_included_nicenames() {
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[0],
|
||||
'user_nicename' => 'peter'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[1],
|
||||
'user_nicename' => 'paul'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[2],
|
||||
'user_nicename' => 'mary'
|
||||
) );
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'nicename__in' => array( 'peter', 'paul', 'mary' )
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
|
||||
|
||||
$this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where);
|
||||
$this->assertEqualSets( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_nicename__not_in_returns_users_without_included_nicenames() {
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[0],
|
||||
'user_nicename' => 'peter'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[1],
|
||||
'user_nicename' => 'paul'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[2],
|
||||
'user_nicename' => 'mary'
|
||||
) );
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'nicename__not_in' => array( 'peter', 'paul', 'mary' )
|
||||
) );
|
||||
|
||||
$foundCount = count($q->get_results());
|
||||
$expectedCount = 10; // 13 total users minus 3 from query
|
||||
|
||||
$this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where);
|
||||
$this->assertEquals( $expectedCount, $foundCount );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_orderby_nicename__in() {
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[0],
|
||||
'user_nicename' => 'peter'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[1],
|
||||
'user_nicename' => 'paul'
|
||||
) );
|
||||
|
||||
wp_update_user( array(
|
||||
'ID' => self::$author_ids[2],
|
||||
'user_nicename' => 'mary'
|
||||
) );
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'nicename__in' => array( 'mary', 'peter', 'paul' ),
|
||||
'orderby' => 'nicename__in'
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
|
||||
|
||||
$this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby);
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_login_returns_user_with_login() {
|
||||
|
||||
$user_login = get_userdata( self::$author_ids[0] )->user_login;
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'login' => $user_login
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[0] );
|
||||
|
||||
$this->assertContains( "AND user_login = '$user_login'", $q->query_where);
|
||||
$this->assertEqualSets( $expected, $found);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_login__in_returns_users_with_included_logins() {
|
||||
$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
|
||||
$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
|
||||
$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'login__in' => array( $user_login1, $user_login2, $user_login3 )
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
|
||||
|
||||
$this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
|
||||
$this->assertEqualSets( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_login__not_in_returns_users_without_included_logins() {
|
||||
$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
|
||||
$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
|
||||
$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'login__not_in' => array( $user_login1, $user_login2, $user_login3 )
|
||||
) );
|
||||
|
||||
$foundCount = count($q->get_results());
|
||||
$expectedCount = 10; // 13 total users minus 3 from query
|
||||
|
||||
$this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where);
|
||||
$this->assertEquals( $expectedCount, $foundCount );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 36624
|
||||
*/
|
||||
public function test_orderby_login__in() {
|
||||
$user_login1 = get_userdata( self::$author_ids[0] )->user_login;
|
||||
$user_login2 = get_userdata( self::$author_ids[1] )->user_login;
|
||||
$user_login3 = get_userdata( self::$author_ids[2] )->user_login;
|
||||
|
||||
$q = new WP_User_Query( array (
|
||||
'login__in' => array( $user_login2, $user_login3, $user_login1 ),
|
||||
'orderby' => 'login__in'
|
||||
) );
|
||||
|
||||
$found = wp_list_pluck( $q->get_results(), 'ID' );
|
||||
$expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
|
||||
|
||||
$this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby);
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 25145
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue