X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/03f2fa83c13c1b532284205fa7efcab9b8b2c41f..16e7b37c7914d753890c1a05a9335f3b43751eb8:/wp-includes/class-wp-user-query.php diff --git a/wp-includes/class-wp-user-query.php b/wp-includes/class-wp-user-query.php index a86c9c37..0c3e1052 100644 --- a/wp-includes/class-wp-user-query.php +++ b/wp-includes/class-wp-user-query.php @@ -75,7 +75,7 @@ class WP_User_Query { * * @since 3.1.0 * - * @param null|string|array $args Optional. The query variables. + * @param null|string|array $query Optional. The query variables. */ public function __construct( $query = null ) { if ( ! empty( $query ) ) { @@ -95,7 +95,7 @@ class WP_User_Query { */ public static function fill_query_vars( $args ) { $defaults = array( - 'blog_id' => $GLOBALS['blog_id'], + 'blog_id' => get_current_blog_id(), 'role' => '', 'role__in' => array(), 'role__not_in' => array(), @@ -115,6 +115,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 ); @@ -131,6 +137,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 * @@ -140,7 +148,7 @@ class WP_User_Query { * @param string|array $query { * Optional. Array or string of Query parameters. * - * @type int $blog_id The site ID. Default is the global blog id. + * @type int $blog_id The site ID. Default is the current site. * @type string|array $role An array or a comma-separated list of role names that users must match * to be included in results. Note that this is an inclusive list: users * must match *each* role. Default empty. @@ -165,12 +173,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'. @@ -195,6 +203,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() ) { @@ -255,13 +273,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 ) ); @@ -277,10 +288,67 @@ class WP_User_Query { $this->query_where .= " AND $wpdb->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 .= $wpdb->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 .= $wpdb->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 ); + 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'] ) ) { @@ -412,7 +480,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. @@ -464,7 +536,7 @@ class WP_User_Query { } /** - * Filter the columns to search in a WP_User_Query search. + * Filters the columns to search in a WP_User_Query search. * * The default columns depend on the search term, and include 'user_email', * 'user_login', 'ID', 'user_url', 'display_name', and 'user_nicename'. @@ -531,7 +603,7 @@ class WP_User_Query { } /** - * Filter SELECT FOUND_ROWS() query for the current WP_User_Query instance. + * Filters SELECT FOUND_ROWS() query for the current WP_User_Query instance. * * @since 3.2.0 * @@ -690,6 +762,14 @@ class WP_User_Query { $include = wp_parse_id_list( $this->query_vars['include'] ); $include_sql = implode( ',', $include ); $_orderby = "FIELD( $wpdb->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'] ) ); @@ -720,7 +800,7 @@ class WP_User_Query { } /** - * Make private properties readable for backwards compatibility. + * Make private properties readable for backward compatibility. * * @since 4.0.0 * @access public @@ -735,7 +815,7 @@ class WP_User_Query { } /** - * Make private properties settable for backwards compatibility. + * Make private properties settable for backward compatibility. * * @since 4.0.0 * @access public @@ -751,7 +831,7 @@ class WP_User_Query { } /** - * Make private properties checkable for backwards compatibility. + * Make private properties checkable for backward compatibility. * * @since 4.0.0 * @access public @@ -766,7 +846,7 @@ class WP_User_Query { } /** - * Make private properties un-settable for backwards compatibility. + * Make private properties un-settable for backward compatibility. * * @since 4.0.0 * @access public @@ -780,7 +860,7 @@ class WP_User_Query { } /** - * Make private/protected methods readable for backwards compatibility. + * Make private/protected methods readable for backward compatibility. * * @since 4.0.0 * @access public