Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Add support for search_columns argument to the REST API #7909

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public function get_items_permissions_check( $request ) {
* Retrieves all users.
*
* @since 4.7.0
* @since 6.8.0 Added support for the search_columns query param.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
Expand Down Expand Up @@ -331,6 +332,27 @@ public function get_items( $request ) {
if ( ! current_user_can( 'list_users' ) ) {
$prepared_args['search_columns'] = array( 'ID', 'user_login', 'user_nicename', 'display_name' );
}
$search_columns = $request->get_param( 'search_columns' );
$valid_columns = isset( $prepared_args['search_columns'] )
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
? $prepared_args['search_columns']
: array( 'ID', 'user_login', 'user_nicename', 'user_email', 'display_name' );
$search_columns_mapping = array(
'id' => 'ID',
'username' => 'user_login',
'slug' => 'user_nicename',
'email' => 'user_email',
'name' => 'display_name',
);
$search_columns = array_map(
static function ( $column ) use ( $search_columns_mapping ) {
return $search_columns_mapping[ $column ];
},
$search_columns
);
$search_columns = array_intersect( $search_columns, $valid_columns );
if ( ! empty( $search_columns ) ) {
$prepared_args['search_columns'] = $search_columns;
}
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
}
/**
Expand Down Expand Up @@ -1608,6 +1630,16 @@ public function get_collection_params() {
),
);

$query_params['search_columns'] = array(
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
'default' => array(),
'description' => __( 'Array of column names to be searched.' ),
'type' => 'array',
'items' => array(
'enum' => array( 'email', 'name', 'id', 'username', 'slug' ),
'type' => 'string',
),
);

/**
* Filters REST API collection parameters for the users controller.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/tests/rest-api/rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public function test_registered_query_params() {
'search',
'slug',
'who',
'search_columns',
'has_published_posts',
),
$keys
Expand Down Expand Up @@ -712,6 +713,39 @@ public function test_get_items_search_fields() {
$this->assertCount( 0, $response->get_data() );
}

public function test_get_items_search_columns() {
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'search', 'yololololo' );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( 0, $response->get_data() );

$yolo_id = self::factory()->user->create(
array(
'display_name' => 'Adam',
'user_email' => 'yololololo@example.localhost',
)
);

wp_set_current_user( self::$user );
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'search', 'yololololo' );
$request->set_param( 'search_columns', 'email' );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( 1, $response->get_data() );

$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'search', 'yololololo' );
$request->set_param( 'search_columns', 'name' );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( 0, $response->get_data() );

$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
$request->set_param( 'search', 'Adam' );
$request->set_param( 'search_columns', 'name' );
$response = rest_get_server()->dispatch( $request );
$this->assertCount( 1, $response->get_data() );
}

public function test_get_items_slug_query() {
wp_set_current_user( self::$user );

Expand Down
16 changes: 16 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -9595,6 +9595,22 @@ mockedApiResponse.Schema = {
}
},
"required": false
},
"search_columns": {
"default": [],
"description": "Array of column names to be searched.",
"type": "array",
"items": {
"enum": [
"email",
"name",
"id",
"username",
"slug"
],
"type": "string"
},
"required": false
}
}
},
Expand Down
Loading