]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-user-query.php
WordPress 4.7
[autoinstalls/wordpress.git] / wp-includes / class-wp-user-query.php
index 84cdcf6b294ce7ca1becf225c5aee88458a61569..0c3e1052737f3c8a6ee6b93180d238ac553dc910 100644 (file)
@@ -95,7 +95,7 @@ class WP_User_Query {
         */
        public static function fill_query_vars( $args ) {
                $defaults = array(
         */
        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(),
                        'role' => '',
                        'role__in' => array(),
                        'role__not_in' => array(),
@@ -115,6 +115,12 @@ class WP_User_Query {
                        'fields' => 'all',
                        'who' => '',
                        'has_published_posts' => null,
                        '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 );
                );
 
                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.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
         *
         *
         * @access public
         *
@@ -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'
         *                                             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'.
         *     @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 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() ) {
         * }
         */
        public function prepare_query( $query = array() ) {
@@ -270,6 +288,40 @@ 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 ) . " ) )";
                }
 
                        $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 );
                // Meta query.
                $this->meta_query = new WP_Meta_Query();
                $this->meta_query->parse_query_vars( $qv );
@@ -428,7 +480,11 @@ class WP_User_Query {
                                continue;
                        }
 
                                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.
                }
 
                // If no valid clauses were found, order by user_login.
@@ -706,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 )";
                        $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'] ) );
                } 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'] ) );