~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-includes/class-wp-user-query.php

  • Committer: Haw Loeung
  • Date: 2016-12-13 06:54:17 UTC
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: haw.loeung@canonical.com-20161213065417-pemlo49o7in8nmkn
New upstream version 4.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
95
95
         */
96
96
        public static function fill_query_vars( $args ) {
97
97
                $defaults = array(
98
 
                        'blog_id' => $GLOBALS['blog_id'],
 
98
                        'blog_id' => get_current_blog_id(),
99
99
                        'role' => '',
100
100
                        'role__in' => array(),
101
101
                        'role__not_in' => array(),
115
115
                        'fields' => 'all',
116
116
                        'who' => '',
117
117
                        'has_published_posts' => null,
 
118
                        'nicename' => '',
 
119
                        'nicename__in' => array(),
 
120
                        'nicename__not_in' => array(),
 
121
                        'login' => '',
 
122
                        'login__in' => array(),
 
123
                        'login__not_in' => array()
118
124
                );
119
125
 
120
126
                return wp_parse_args( $args, $defaults );
131
137
         * @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to
132
138
         *              permit an array or comma-separated list of values. The 'number' parameter was updated to support
133
139
         *              querying for all users with using -1.
 
140
         * @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in',
 
141
         *              and 'login__not_in' parameters.
134
142
         *
135
143
         * @access public
136
144
         *
165
173
         *                                             an array of values, or a multi-dimensional array with fields as
166
174
         *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
167
175
         *                                             'ID', 'display_name' (or 'name'), 'include', 'user_login'
168
 
         *                                             (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
169
 
         *                                             (or 'email'), 'user_url' (or 'url'), 'user_registered'
170
 
         *                                             or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
171
 
         *                                             the value of `$meta_key`, or an array key of `$meta_query`. To use
172
 
         *                                             'meta_value' or 'meta_value_num', `$meta_key` must be also be
173
 
         *                                             defined. Default 'user_login'.
 
176
         *                                             (or 'login'), 'login__in', 'user_nicename' (or 'nicename'),
 
177
         *                                             'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'),
 
178
         *                                             'user_registered' (or 'registered'), 'post_count', 'meta_value',
 
179
         *                                             'meta_value_num', the value of `$meta_key`, or an array key of
 
180
         *                                             `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key`
 
181
         *                                             must be also be defined. Default 'user_login'.
174
182
         *     @type string       $order               Designates ascending or descending order of users. Order values
175
183
         *                                             passed as part of an `$orderby` array take precedence over this
176
184
         *                                             parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
195
203
         *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
196
204
         *                                             published posts in those post types. `true` is an alias for all
197
205
         *                                             public post types.
 
206
         *     @type string       $nicename            The user nicename. Default empty.
 
207
         *     @type array        $nicename__in        An array of nicenames to include. Users matching one of these
 
208
         *                                             nicenames will be included in results. Default empty array.
 
209
         *     @type array        $nicename__not_in    An array of nicenames to exclude. Users matching one of these
 
210
         *                                             nicenames will not be included in results. Default empty array.
 
211
         *     @type string       $login               The user login. Default empty.
 
212
         *     @type array        $login__in           An array of logins to include. Users matching one of these
 
213
         *                                             logins will be included in results. Default empty array.
 
214
         *     @type array        $login__not_in       An array of logins to exclude. Users matching one of these
 
215
         *                                             logins will not be included in results. Default empty array.
198
216
         * }
199
217
         */
200
218
        public function prepare_query( $query = array() ) {
270
288
                        $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 ) . " ) )";
271
289
                }
272
290
 
 
291
                // nicename
 
292
                if ( '' !== $qv['nicename']) {
 
293
                        $this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
 
294
                }
 
295
 
 
296
                if ( ! empty( $qv['nicename__in'] ) ) {
 
297
                        $sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] );
 
298
                        $nicename__in = implode( "','", $sanitized_nicename__in );
 
299
                        $this->query_where .= " AND user_nicename IN ( '$nicename__in' )";
 
300
                }
 
301
 
 
302
                if ( ! empty( $qv['nicename__not_in'] ) ) {
 
303
                        $sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] );
 
304
                        $nicename__not_in = implode( "','", $sanitized_nicename__not_in );
 
305
                        $this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )";
 
306
                }
 
307
 
 
308
                // login
 
309
                if ( '' !== $qv['login']) {
 
310
                        $this->query_where .= $wpdb->prepare( ' AND user_login = %s', $qv['login'] );
 
311
                }
 
312
 
 
313
                if ( ! empty( $qv['login__in'] ) ) {
 
314
                        $sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] );
 
315
                        $login__in = implode( "','", $sanitized_login__in );
 
316
                        $this->query_where .= " AND user_login IN ( '$login__in' )";
 
317
                }
 
318
 
 
319
                if ( ! empty( $qv['login__not_in'] ) ) {
 
320
                        $sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] );
 
321
                        $login__not_in = implode( "','", $sanitized_login__not_in );
 
322
                        $this->query_where .= " AND user_login NOT IN ( '$login__not_in' )";
 
323
                }
 
324
 
273
325
                // Meta query.
274
326
                $this->meta_query = new WP_Meta_Query();
275
327
                $this->meta_query->parse_query_vars( $qv );
428
480
                                continue;
429
481
                        }
430
482
 
431
 
                        $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
 
483
                        if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) {
 
484
                                $orderby_array[] = $parsed;
 
485
                        } else {
 
486
                                $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
 
487
                        }
432
488
                }
433
489
 
434
490
                // If no valid clauses were found, order by user_login.
706
762
                        $include = wp_parse_id_list( $this->query_vars['include'] );
707
763
                        $include_sql = implode( ',', $include );
708
764
                        $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
 
765
                } elseif ( 'nicename__in' === $orderby ) {
 
766
                        $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
 
767
                        $nicename__in = implode( "','", $sanitized_nicename__in );
 
768
                        $_orderby = "FIELD( user_nicename, '$nicename__in' )";
 
769
                } elseif ( 'login__in' === $orderby ) {
 
770
                        $sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] );
 
771
                        $login__in = implode( "','", $sanitized_login__in );
 
772
                        $_orderby = "FIELD( user_login, '$login__in' )";
709
773
                } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
710
774
                        $meta_clause = $meta_query_clauses[ $orderby ];
711
775
                        $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );