Make WordPress Core


Ignore:
Timestamp:
10/04/2016 02:26:26 AM (8 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r38457 r38715  
    125125            'who' => '',
    126126            'has_published_posts' => null,
     127            'nicename' => '',
     128            'nicename__in' => array(),
     129            'nicename__not_in' => array(),
     130            'login' => '',
     131            'login__in' => array(),
     132            'login__not_in' => array()
    127133        );
    128134
     
    141147     *              permit an array or comma-separated list of values. The 'number' parameter was updated to support
    142148     *              querying for all users with using -1.
     149     * @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in',
     150     *              and 'login__not_in' parameters.
    143151     *
    144152     * @access public
     
    174182     *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
    175183     *                                             'ID', 'display_name' (or 'name'), 'include', 'user_login'
    176      *                                             (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
    177      *                                             (or 'email'), 'user_url' (or 'url'), 'user_registered'
    178      *                                             or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
    179      *                                             the value of `$meta_key`, or an array key of `$meta_query`. To use
    180      *                                             'meta_value' or 'meta_value_num', `$meta_key` must be also be
    181      *                                             defined. Default 'user_login'.
     184     *                                             (or 'login'), 'login__in', 'user_nicename' (or 'nicename'),
     185     *                                             'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'),
     186     *                                             'user_registered' (or 'registered'), 'post_count', 'meta_value',
     187     *                                             'meta_value_num', the value of `$meta_key`, or an array key of
     188     *                                             `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key`
     189     *                                             must be also be defined. Default 'user_login'.
    182190     *     @type string       $order               Designates ascending or descending order of users. Order values
    183191     *                                             passed as part of an `$orderby` array take precedence over this
     
    204212     *                                             published posts in those post types. `true` is an alias for all
    205213     *                                             public post types.
     214     *     @type string       $nicename            The user nicename. Default empty.
     215     *     @type array        $nicename__in        An array of nicenames to include. Users matching one of these
     216     *                                             nicenames will be included in results. Default empty array.
     217     *     @type array        $nicename__not_in    An array of nicenames to exclude. Users matching one of these
     218     *                                             nicenames will not be included in results. Default empty array.
     219     *     @type string       $login               The user login. Default empty.
     220     *     @type array        $login__in           An array of logins to include. Users matching one of these
     221     *                                             logins will be included in results. Default empty array.
     222     *     @type array        $login__not_in       An array of logins to exclude. Users matching one of these
     223     *                                             logins will not be included in results. Default empty array.
    206224     * }
    207225     */
     
    277295        }
    278296
     297        // nicename
     298        if ( '' !== $qv['nicename']) {
     299            $this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );
     300        }
     301
     302        if ( ! empty( $qv['nicename__in'] ) ) {
     303            $sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] );
     304            $nicename__in = implode( "','", $sanitized_nicename__in );
     305            $this->query_where .= " AND user_nicename IN ( '$nicename__in' )";
     306        }
     307
     308        if ( ! empty( $qv['nicename__not_in'] ) ) {
     309            $sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] );
     310            $nicename__not_in = implode( "','", $sanitized_nicename__not_in );
     311            $this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )";
     312        }
     313
     314        // login
     315        if ( '' !== $qv['login']) {
     316            $this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] );
     317        }
     318
     319        if ( ! empty( $qv['login__in'] ) ) {
     320            $sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] );
     321            $login__in = implode( "','", $sanitized_login__in );
     322            $this->query_where .= " AND user_login IN ( '$login__in' )";
     323        }
     324
     325        if ( ! empty( $qv['login__not_in'] ) ) {
     326            $sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] );
     327            $login__not_in = implode( "','", $sanitized_login__not_in );
     328            $this->query_where .= " AND user_login NOT IN ( '$login__not_in' )";
     329        }
     330
    279331        // Meta query.
    280332        $this->meta_query = new WP_Meta_Query();
     
    435487            }
    436488
    437             $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
     489            if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) {
     490                $orderby_array[] = $parsed;
     491            } else {
     492                $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
     493            }
    438494        }
    439495
     
    701757            $include_sql = implode( ',', $include );
    702758            $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
     759        } elseif ( 'nicename__in' === $orderby ) {
     760            $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
     761            $nicename__in = implode( "','", $sanitized_nicename__in );
     762            $_orderby = "FIELD( user_nicename, '$nicename__in' )";
     763        } elseif ( 'login__in' === $orderby ) {
     764            $sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] );
     765            $login__in = implode( "','", $sanitized_login__in );
     766            $_orderby = "FIELD( user_login, '$login__in' )";
    703767        } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
    704768            $meta_clause = $meta_query_clauses[ $orderby ];
Note: See TracChangeset for help on using the changeset viewer.