-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathclass-wp-users-abilities.php
More file actions
354 lines (321 loc) · 9.81 KB
/
class-wp-users-abilities.php
File metadata and controls
354 lines (321 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Registers core user abilities.
*
* This is a utility class to encapsulate the registration of user-related abilities.
* It is not intended to be instantiated or consumed directly by any other code or plugin.
*
* @package WordPress
* @subpackage Abilities_API
* @since 7.0.0
*
* @internal This class is not part of the public API.
* @access private
*/
declare( strict_types=1 );
/**
* Registers core user abilities.
*
* @since 7.0.0
* @access private
*/
class WP_Users_Abilities {
/**
* Registers all user-related abilities.
*
* @since 7.0.0
*
* @return void
*/
public static function register(): void {
self::register_get_user();
}
/**
* Registers the core/get-user ability.
*
* @since 7.0.0
*
* @return void
*/
private static function register_get_user(): void {
wp_register_ability(
'core/get-user',
array(
'label' => __( 'Get User' ),
'description' => __( 'Returns comprehensive profile details for a user by id, username, or email.' ),
'category' => 'user',
'input_schema' => self::get_user_input_schema(),
'output_schema' => self::get_user_output_schema(),
'execute_callback' => array( __CLASS__, 'execute_get_user' ),
'permission_callback' => array( __CLASS__, 'check_get_user_permission' ),
'meta' => array(
'annotations' => array(
'readonly' => true,
'destructive' => false,
'idempotent' => true,
),
'show_in_rest' => true,
),
)
);
}
/**
* Gets the input schema for the get-user ability.
*
* @since 7.0.0
*
* @return array<string, mixed> The input schema.
*/
private static function get_user_input_schema(): array {
return array(
'type' => 'object',
'oneOf' => array(
array( 'required' => array( 'id' ) ),
array( 'required' => array( 'username' ) ),
array( 'required' => array( 'email' ) ),
),
'properties' => array(
'email' => array(
'type' => 'string',
'format' => 'email',
'description' => __( 'User email address.' ),
),
'id' => array(
'type' => 'integer',
'description' => __( 'User ID.' ),
),
'username' => array(
'type' => 'string',
'description' => __( 'User login name.' ),
),
),
'additionalProperties' => false,
);
}
/**
* Determines whether sensitive fields can be returned for a user.
*
* @since 7.0.0
*
* @param WP_User $user The target user.
* @return bool Whether sensitive fields can be returned.
*/
private static function can_view_sensitive_user_fields( WP_User $user ): bool {
return get_current_user_id() === $user->ID || current_user_can( 'edit_user', $user->ID );
}
/**
* Determines whether roles can be returned for a user.
*
* @since 7.0.0
*
* @param WP_User $user The target user.
* @return bool Whether roles can be returned.
*/
private static function can_view_user_roles( WP_User $user ): bool {
return current_user_can( 'list_users' ) || current_user_can( 'edit_user', $user->ID );
}
/**
* Gets the output schema for the get-user ability.
*
* @since 7.0.0
*
* @return array<string, mixed> The output schema.
*/
private static function get_user_output_schema(): array {
$avatar_urls_schema = array(
'type' => 'object',
'description' => __( 'Avatar URLs for the user.' ),
);
if ( get_option( 'show_avatars' ) ) {
$avatar_properties = array();
foreach ( rest_get_avatar_sizes() as $size ) {
$avatar_properties[ $size ] = array(
/* translators: %d: Avatar image size in pixels. */
'description' => sprintf( __( 'Avatar URL with image size of %d pixels.' ), $size ),
'type' => 'string',
'format' => 'uri',
);
}
$avatar_urls_schema['properties'] = $avatar_properties;
}
return array(
'type' => 'object',
'required' => array(
'id',
'username',
),
'properties' => array(
'avatar_urls' => $avatar_urls_schema,
'description' => array(
'type' => 'string',
'description' => __( 'Description of the user.' ),
),
'display_name' => array(
'type' => 'string',
'description' => __( 'Display name for the user.' ),
),
'email' => array(
'type' => 'string',
'format' => 'email',
'description' => __( 'The email address for the user.' ),
),
'first_name' => array(
'type' => 'string',
'description' => __( 'First name for the user.' ),
),
'id' => array(
'type' => 'integer',
'description' => __( 'Unique identifier for the user.' ),
),
'last_name' => array(
'type' => 'string',
'description' => __( 'Last name for the user.' ),
),
'link' => array(
'type' => 'string',
'format' => 'uri',
'description' => __( 'Author URL of the user.' ),
),
'locale' => array(
'type' => 'string',
'enum' => array_merge( array( '', 'en_US' ), get_available_languages() ),
'description' => __( 'Locale for the user.' ),
),
'nickname' => array(
'type' => 'string',
'description' => __( 'The nickname for the user.' ),
),
'registered_date' => array(
'type' => 'string',
'format' => 'date-time',
'description' => __( 'Registration date for the user in ISO 8601 format.' ),
),
'roles' => array(
'type' => 'array',
'description' => __( 'Roles assigned to the user when the current user can view them.' ),
'items' => array(
'type' => 'string',
),
),
'slug' => array(
'type' => 'string',
'description' => __( 'An alphanumeric identifier for the user.' ),
),
'url' => array(
'type' => 'string',
'format' => 'uri',
'description' => __( 'URL of the user.' ),
),
'username' => array(
'type' => 'string',
'description' => __( 'Login name for the user.' ),
),
),
'additionalProperties' => false,
);
}
/**
* Normalizes a list of values into an array of strings.
*
* @since 7.0.0
*
* @param mixed $values Values to normalize.
* @return array<int, string> Normalized string list.
*/
private static function normalize_string_list( $values ): array {
$normalized = array();
foreach ( (array) $values as $value ) {
if ( null === $value || is_scalar( $value ) || ( is_object( $value ) && is_callable( array( $value, '__toString' ) ) ) ) {
$normalized[] = (string) $value;
}
}
return $normalized;
}
/**
* Finds a user by id, username, or email from input parameters.
*
* @since 7.0.0
*
* @param array<string, mixed> $input The input parameters.
* @return WP_User|false The user object if found, false otherwise.
*/
private static function find_user( array $input ) {
if ( ! empty( $input['id'] ) && $input['id'] > 0 ) {
return get_user_by( 'ID', (int) $input['id'] );
}
if ( ! empty( $input['username'] ) ) {
return get_user_by( 'login', sanitize_user( $input['username'] ) );
}
if ( ! empty( $input['email'] ) ) {
return get_user_by( 'email', sanitize_email( $input['email'] ) );
}
return false;
}
/**
* Permission callback for the get-user ability.
*
* @since 7.0.0
*
* @param array<string, mixed> $input The input parameters.
* @return bool Whether the user has permission.
*/
public static function check_get_user_permission( array $input = array() ): bool {
// Must be logged in.
if ( ! is_user_logged_in() ) {
return false;
}
$user = self::find_user( $input );
// Allow privileged users to query unknown targets and receive not-found responses.
if ( ! $user || ! $user->exists() ) {
return current_user_can( 'edit_users' );
}
// Users can view their own profile.
if ( get_current_user_id() === $user->ID ) {
return true;
}
// Otherwise require permission to edit the specific target user.
return current_user_can( 'edit_user', $user->ID );
}
/**
* Executes the get-user ability.
*
* @since 7.0.0
*
* @param array<string, mixed> $input The input parameters.
* @return array<string, mixed>|WP_Error The user data or error.
*/
public static function execute_get_user( array $input = array() ) {
$input = is_array( $input ) ? $input : array();
$user = self::find_user( $input );
if ( ! $user || ! $user->exists() ) {
return new WP_Error( 'user_not_found', __( 'User not found.' ), array( 'status' => 404 ) );
}
$user_id = (int) $user->ID;
$can_view_sensitive_user_fields = self::can_view_sensitive_user_fields( $user );
$result = array(
'id' => $user_id,
'display_name' => (string) $user->display_name,
'description' => (string) $user->description,
'url' => (string) $user->user_url,
'link' => (string) get_author_posts_url( $user_id, $user->user_nicename ),
'slug' => (string) $user->user_nicename,
'avatar_urls' => rest_get_avatar_urls( $user ),
);
if ( $can_view_sensitive_user_fields ) {
$result['username'] = (string) $user->user_login;
$result['email'] = (string) $user->user_email;
$result['first_name'] = (string) $user->first_name;
$result['last_name'] = (string) $user->last_name;
$result['nickname'] = (string) $user->nickname;
$result['locale'] = (string) get_user_locale( $user );
$registered_timestamp = strtotime( (string) $user->user_registered );
if ( false !== $registered_timestamp ) {
$result['registered_date'] = gmdate( 'c', $registered_timestamp );
}
}
if ( self::can_view_user_roles( $user ) ) {
$result['roles'] = self::normalize_string_list( $user->roles );
}
return $result;
}
}