Make WordPress Core

Changeset 40378


Ignore:
Timestamp:
04/05/2017 10:24:24 PM (10 years ago)
Author:
jnylen0
Message:

REST API: Allow fetching multiple users at once via the slug parameter.

This matches similar changes previously made for posts (#38579) and terms (#40027).

Props curdin, MatheusGimenez.
Fixes #40213.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r40106 r40378  
    222222                        'search'   => 'search',
    223223                        'roles'    => 'role__in',
     224                        'slug'     => 'nicename__in',
    224225                );
    225226
    … …  
    262263                        $prepared_args['search'] = '*' . $prepared_args['search'] . '*';
    263264                }
    264 
    265                 if ( isset( $registered['slug'] ) && ! empty( $request['slug'] ) ) {
    266                         $prepared_args['search'] = $request['slug'];
    267                         $prepared_args['search_columns'] = array( 'user_nicename' );
    268                 }
    269 
    270265                /**
    271266                 * Filters WP_User_Query arguments when querying users via the REST API.
    … …  
    13641359
    13651360                $query_params['slug']    = array(
    1366                         'description'        => __( 'Limit result set to users with a specific slug.' ),
    1367                         'type'               => 'string',
     1361                        'description'        => __( 'Limit result set to users with one or more specific slugs.' ),
     1362                        'type'               => 'array',
     1363                        'items'              => array(
     1364                                'type'               => 'string',
     1365                        ),
    13681366                );
    13691367
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r40106 r40378  
    573573                $this->assertEquals( 1, count( $data ) );
    574574                $this->assertEquals( $id2, $data[0]['id'] );
     575        }
     576
     577        public function test_get_items_slug_array_query() {
     578                wp_set_current_user( self::$user );
     579                $id1 = $this->factory->user->create( array(
     580                        'display_name' => 'Taco',
     581                        'user_login'   => 'taco'
     582                ) );
     583                $id2 = $this->factory->user->create( array(
     584                        'display_name' => 'Enchilada',
     585                        'user_login'   => 'enchilada'
     586                ) );
     587                $id3 = $this->factory->user->create( array(
     588                        'display_name' => 'Burrito',
     589                        'user_login'   => 'burrito'
     590                ) );
     591                $this->factory->user->create( array(
     592                        'display_name' => 'Hon Pizza',
     593                        'user_login'   => 'pizza'
     594                ) );
     595                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     596                $request->set_param( 'slug', array(
     597                        'taco',
     598                        'burrito',
     599                        'enchilada',
     600                ) );
     601                $request->set_param( 'orderby', 'slug' );
     602                $request->set_param( 'order', 'asc' );
     603                $response = $this->server->dispatch( $request );
     604                $this->assertEquals( 200, $response->get_status() );
     605                $data = $response->get_data();
     606                $slugs = wp_list_pluck( $data, 'slug' );
     607                $this->assertEquals( array( 'burrito', 'enchilada', 'taco' ), $slugs );
     608        }
     609
     610        public function test_get_items_slug_csv_query() {
     611                wp_set_current_user( self::$user );
     612                $id1 = $this->factory->user->create( array(
     613                        'display_name' => 'Taco',
     614                        'user_login'   => 'taco'
     615                ) );
     616                $id2 = $this->factory->user->create( array(
     617                        'display_name' => 'Enchilada',
     618                        'user_login'   => 'enchilada'
     619                ) );
     620                $id3 = $this->factory->user->create( array(
     621                        'display_name' => 'Burrito',
     622                        'user_login'   => 'burrito'
     623                ) );
     624                $this->factory->user->create( array(
     625                        'display_name' => 'Hon Pizza',
     626                        'user_login'   => 'pizza'
     627                ) );
     628                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     629                $request->set_param( 'slug', 'taco,burrito , enchilada');
     630                $request->set_param( 'orderby', 'slug' );
     631                $request->set_param( 'order', 'desc' );
     632                $response = $this->server->dispatch( $request );
     633                $this->assertEquals( 200, $response->get_status() );
     634                $data = $response->get_data();
     635                $slugs = wp_list_pluck( $data, 'slug' );
     636                $this->assertEquals( array( 'taco', 'enchilada', 'burrito' ), $slugs );
    575637        }
    576638
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r40376 r40378  
    19901990                        "slug": {
    19911991                            "required": false,
    1992                             "description": "Limit result set to terms with a specific slug.",
     1992                            "description": "Limit result set to terms with one or more specific slugs.",
    19931993                            "type": "array",
    19941994                            "items": {
    … …  
    22282228                        "slug": {
    22292229                            "required": false,
    2230                             "description": "Limit result set to terms with a specific slug.",
     2230                            "description": "Limit result set to terms with one or more specific slugs.",
    22312231                            "type": "array",
    22322232                            "items": {
    … …  
    24452445                        "slug": {
    24462446                            "required": false,
    2447                             "description": "Limit result set to users with a specific slug.",
    2448                             "type": "string"
     2447                            "description": "Limit result set to users with one or more specific slugs.",
     2448                            "type": "array",
     2449                            "items": {
     2450                                "type": "string"
     2451                            }
    24492452                        },
    24502453                        "roles": {
Note: See TracChangeset for help on using the changeset viewer.