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.
Built from https://develop.svn.wordpress.org/trunk@38715


git-svn-id: http://core.svn.wordpress.org/trunk@38658 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-10-04 02:27:29 +00:00
parent 63d73b30a8
commit e96c617c03
2 changed files with 72 additions and 8 deletions

View File

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

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38714';
$wp_version = '4.7-alpha-38715';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.