← All changes
|
json-endpoints/class.wpcom-json-api-list-users-endpoint.php
+25
-3
13.3.3
→
16.3-a.1
View file →
| @@ -1,6 +1,10 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit( 0 ); | |
| 5 | +} | |
| 6 | + | |
| 3 | 7 | /** |
| 4 | 8 | * List users endpoint. |
| 5 | 9 | */ |
| 6 | 10 | new WPCOM_JSON_API_List_Users_Endpoint( |
| @@ -13,8 +17,10 @@ | ||
| 13 | 17 | 'path' => '/sites/%s/users', |
| 14 | 18 | 'path_labels' => array( |
| 15 | 19 | '$site' => '(int|string) Site ID or domain', |
| 16 | 20 | ), |
| 21 | + 'rest_route' => '/users', | |
| 22 | + 'rest_min_jp_version' => '15.9', | |
| 17 | 23 | |
| 18 | 24 | 'query_parameters' => array( |
| 19 | 25 | 'number' => '(int=20) Limit the total number of authors returned.', |
| 20 | 26 | 'offset' => '(int=0) The first n authors to be skipped in the returned array.', |
| @@ -37,8 +43,9 @@ | ||
| 37 | 43 | 'type' => "(string) Specify the post type to query authors for. Only works when combined with the `authors_only` flag. Defaults to 'post'. Post types besides post and page need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.", |
| 38 | 44 | 'search' => '(string) Find matching users.', |
| 39 | 45 | 'search_columns' => "(array) Specify which columns to check for matching users. Can be any of 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', and 'display_name'. Only works when combined with `search` parameter.", |
| 40 | 46 | 'role' => '(string) Specify a specific user role to fetch.', |
| 47 | + 'capability' => '(string) Specify a specific capability to fetch. You can specify multiple by comma-separating them, in which case the user needs to match all capabilities provided.', | |
| 41 | 48 | ), |
| 42 | 49 | |
| 43 | 50 | 'response_format' => array( |
| 44 | 51 | 'found' => '(int) The total number of authors found that match the request (ignoring limits and offsets).', |
| @@ -80,8 +87,10 @@ | ||
| 80 | 87 | /** |
| 81 | 88 | * List users endpoint class. |
| 82 | 89 | * |
| 83 | 90 | * /sites/%s/users/ -> $blog_id |
| 91 | + * | |
| 92 | + * @phan-constructor-used-for-side-effects | |
| 84 | 93 | */ |
| 85 | 94 | class WPCOM_JSON_API_List_Users_Endpoint extends WPCOM_JSON_API_Endpoint { |
| 86 | 95 | |
| 87 | 96 | /** |
| @@ -94,8 +103,15 @@ | ||
| 94 | 103 | 'users' => '(array:author) Array of user objects', |
| 95 | 104 | ); |
| 96 | 105 | |
| 97 | 106 | /** |
| 107 | + * Columns in which to search for a user match. | |
| 108 | + * | |
| 109 | + * @var array | |
| 110 | + */ | |
| 111 | + public $search_columns; | |
| 112 | + | |
| 113 | + /** | |
| 98 | 114 | * API callback. |
| 99 | 115 | * |
| 100 | 116 | * @param string $path - the path. |
| 101 | 117 | * @param string $blog_id - the blog ID. |
| @@ -158,14 +174,18 @@ | ||
| 158 | 174 | if ( ! empty( $args['role'] ) ) { |
| 159 | 175 | $query['role'] = $args['role']; |
| 160 | 176 | } |
| 161 | 177 | |
| 178 | + if ( ! empty( $args['capability'] ) ) { | |
| 179 | + $query['capability'] = $args['capability']; | |
| 180 | + } | |
| 181 | + | |
| 162 | 182 | $user_query = new WP_User_Query( $query ); |
| 163 | 183 | |
| 164 | 184 | remove_filter( 'user_search_columns', array( $this, 'api_user_override_search_columns' ) ); |
| 165 | 185 | |
| 166 | 186 | $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 167 | - $include_viewers = (bool) isset( $args['include_viewers'] ) && $args['include_viewers'] && $is_wpcom; | |
| 187 | + $include_viewers = isset( $args['include_viewers'] ) && $args['include_viewers'] && $is_wpcom; | |
| 168 | 188 | |
| 169 | 189 | $page = ( (int) ( $args['offset'] / $args['number'] ) ) + 1; |
| 170 | 190 | $viewers = $include_viewers ? get_private_blog_users( |
| 171 | 191 | $blog_id, |
| @@ -175,16 +195,18 @@ | ||
| 175 | 195 | ) |
| 176 | 196 | ) : array(); |
| 177 | 197 | $viewers = array_map( array( $this, 'get_author' ), $viewers ); |
| 178 | 198 | |
| 179 | - // we restrict search field to name when include_viewers is true. | |
| 199 | + // When include_viewers is true, search by username or email. | |
| 180 | 200 | if ( $include_viewers && ! empty( $args['search'] ) ) { |
| 181 | 201 | $viewers = array_filter( |
| 182 | 202 | $viewers, |
| 183 | 203 | function ( $viewer ) use ( $args ) { |
| 204 | + // Convert to WP_User so expected fields are available. | |
| 205 | + $wp_viewer = new WP_User( $viewer->ID ); | |
| 184 | 206 | // remove special database search characters from search term |
| 185 | 207 | $search_term = str_replace( '*', '', $args['search'] ); |
| 186 | - return strpos( $viewer->name, $search_term ) !== false; | |
| 208 | + return ( str_contains( $wp_viewer->user_login, $search_term ) || str_contains( $wp_viewer->user_email, $search_term ) || str_contains( $wp_viewer->display_name, $search_term ) ); | |
| 187 | 209 | } |
| 188 | 210 | ); |
| 189 | 211 | } |
| 190 | 212 | |