Make WordPress Core

Changeset 40426


Ignore:
Timestamp:
04/14/2017 08:46:22 AM (10 years ago)
Author:
swissspidy
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.

Merges [40378] to the 4.7 branch.

Location:
branches/4.7
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r40111 r40426  
    221221                        'search'   => 'search',
    222222                        'roles'    => 'role__in',
     223                        'slug'     => 'nicename__in',
    223224                );
    224225
    … …  
    261262                        $prepared_args['search'] = '*' . $prepared_args['search'] . '*';
    262263                }
    263 
    264                 if ( isset( $registered['slug'] ) && ! empty( $request['slug'] ) ) {
    265                         $prepared_args['search'] = $request['slug'];
    266                         $prepared_args['search_columns'] = array( 'user_nicename' );
    267                 }
    268 
    269264                /**
    270265                 * Filters WP_User_Query arguments when querying users via the REST API.
    … …  
    13621357                $query_params['slug']    = array(
    13631358                        'description'        => __( 'Limit result set to users with a specific slug.' ),
    1364                         'type'               => 'string',
     1359                        'type'               => 'array',
     1360                        'items'              => array(
     1361                                'type'               => 'string',
     1362                        ),
    13651363                );
    13661364
  • branches/4.7/tests/phpunit/tests/rest-api/rest-users-controller.php

    r40111 r40426  
    583583                $this->assertEquals( 1, count( $data ) );
    584584                $this->assertEquals( $id2, $data[0]['id'] );
     585        }
     586
     587        public function test_get_items_slug_array_query() {
     588                wp_set_current_user( self::$user );
     589                $id1 = $this->factory->user->create( array(
     590                        'display_name' => 'Taco',
     591                        'user_login'   => 'taco'
     592                ) );
     593                $id2 = $this->factory->user->create( array(
     594                        'display_name' => 'Enchilada',
     595                        'user_login'   => 'enchilada'
     596                ) );
     597                $id3 = $this->factory->user->create( array(
     598                        'display_name' => 'Burrito',
     599                        'user_login'   => 'burrito'
     600                ) );
     601                $this->factory->user->create( array(
     602                        'display_name' => 'Hon Pizza',
     603                        'user_login'   => 'pizza'
     604                ) );
     605                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     606                $request->set_param( 'slug', array(
     607                        'taco',
     608                        'burrito',
     609                        'enchilada',
     610                ) );
     611                $request->set_param( 'orderby', 'slug' );
     612                $request->set_param( 'order', 'asc' );
     613                $response = $this->server->dispatch( $request );
     614                $this->assertEquals( 200, $response->get_status() );
     615                $data = $response->get_data();
     616                $slugs = wp_list_pluck( $data, 'slug' );
     617                $this->assertEquals( array( 'burrito', 'enchilada', 'taco' ), $slugs );
     618        }
     619
     620        public function test_get_items_slug_csv_query() {
     621                wp_set_current_user( self::$user );
     622                $id1 = $this->factory->user->create( array(
     623                        'display_name' => 'Taco',
     624                        'user_login'   => 'taco'
     625                ) );
     626                $id2 = $this->factory->user->create( array(
     627                        'display_name' => 'Enchilada',
     628                        'user_login'   => 'enchilada'
     629                ) );
     630                $id3 = $this->factory->user->create( array(
     631                        'display_name' => 'Burrito',
     632                        'user_login'   => 'burrito'
     633                ) );
     634                $this->factory->user->create( array(
     635                        'display_name' => 'Hon Pizza',
     636                        'user_login'   => 'pizza'
     637                ) );
     638                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
     639                $request->set_param( 'slug', 'taco,burrito , enchilada');
     640                $request->set_param( 'orderby', 'slug' );
     641                $request->set_param( 'order', 'desc' );
     642                $response = $this->server->dispatch( $request );
     643                $this->assertEquals( 200, $response->get_status() );
     644                $data = $response->get_data();
     645                $slugs = wp_list_pluck( $data, 'slug' );
     646                $this->assertEquals( array( 'taco', 'enchilada', 'burrito' ), $slugs );
    585647        }
    586648
  • branches/4.7/tests/qunit/fixtures/wp-api-generated.js

    r40336 r40426  
    24402440                            "required": false,
    24412441                            "description": "Limit result set to users with a specific slug.",
    2442                             "type": "string"
     2442                            "type": "array",
     2443                            "items": {
     2444                                "type": "string"
     2445                            }
    24432446                        },
    24442447                        "roles": {
Note: See TracChangeset for help on using the changeset viewer.