1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User API: WP_User class |
4
|
|
|
* |
5
|
|
|
* @package WordPress |
6
|
|
|
* @subpackage Users |
7
|
|
|
* @since 4.4.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Core class used to implement the WP_User object. |
12
|
|
|
* |
13
|
|
|
* @since 2.0.0 |
14
|
|
|
* |
15
|
|
|
* @property string $nickname |
16
|
|
|
* @property string $description |
17
|
|
|
* @property string $user_description |
18
|
|
|
* @property string $first_name |
19
|
|
|
* @property string $user_firstname |
20
|
|
|
* @property string $last_name |
21
|
|
|
* @property string $user_lastname |
22
|
|
|
* @property string $user_login |
23
|
|
|
* @property string $user_pass |
24
|
|
|
* @property string $user_nicename |
25
|
|
|
* @property string $user_email |
26
|
|
|
* @property string $user_url |
27
|
|
|
* @property string $user_registered |
28
|
|
|
* @property string $user_activation_key |
29
|
|
|
* @property string $user_status |
30
|
|
|
* @property int $user_level |
31
|
|
|
* @property string $display_name |
32
|
|
|
* @property string $spam |
33
|
|
|
* @property string $deleted |
34
|
|
|
* @property string $locale |
35
|
|
|
*/ |
36
|
|
|
class WP_User { |
37
|
|
|
/** |
38
|
|
|
* User data container. |
39
|
|
|
* |
40
|
|
|
* @since 2.0.0 |
41
|
|
|
* @var object |
42
|
|
|
*/ |
43
|
|
|
public $data; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The user's ID. |
47
|
|
|
* |
48
|
|
|
* @since 2.1.0 |
49
|
|
|
* @access public |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
public $ID = 0; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The individual capabilities the user has been given. |
56
|
|
|
* |
57
|
|
|
* @since 2.0.0 |
58
|
|
|
* @access public |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
public $caps = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* User metadata option name. |
65
|
|
|
* |
66
|
|
|
* @since 2.0.0 |
67
|
|
|
* @access public |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $cap_key; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The roles the user is part of. |
74
|
|
|
* |
75
|
|
|
* @since 2.0.0 |
76
|
|
|
* @access public |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
public $roles = array(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* All capabilities the user has, including individual and role based. |
83
|
|
|
* |
84
|
|
|
* @since 2.0.0 |
85
|
|
|
* @access public |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
public $allcaps = array(); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The filter context applied to user data fields. |
92
|
|
|
* |
93
|
|
|
* @since 2.9.0 |
94
|
|
|
* @access public |
95
|
|
|
* @var string |
96
|
|
|
*/ |
97
|
|
|
public $filter = null; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @static |
101
|
|
|
* @since 3.3.0 |
102
|
|
|
* @access private |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private static $back_compat_keys; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Constructor. |
109
|
|
|
* |
110
|
|
|
* Retrieves the userdata and passes it to WP_User::init(). |
111
|
|
|
* |
112
|
|
|
* @since 2.0.0 |
113
|
|
|
* @access public |
114
|
|
|
* |
115
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
116
|
|
|
* |
117
|
|
|
* @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
|
|
|
|
118
|
|
|
* @param string $name Optional. User's username |
119
|
|
|
* @param int $blog_id Optional Site ID, defaults to current site. |
120
|
|
|
*/ |
121
|
|
|
public function __construct( $id = 0, $name = '', $blog_id = '' ) { |
122
|
|
|
if ( ! isset( self::$back_compat_keys ) ) { |
123
|
|
|
$prefix = $GLOBALS['wpdb']->prefix; |
124
|
|
|
self::$back_compat_keys = array( |
125
|
|
|
'user_firstname' => 'first_name', |
126
|
|
|
'user_lastname' => 'last_name', |
127
|
|
|
'user_description' => 'description', |
128
|
|
|
'user_level' => $prefix . 'user_level', |
129
|
|
|
$prefix . 'usersettings' => $prefix . 'user-settings', |
130
|
|
|
$prefix . 'usersettingstime' => $prefix . 'user-settings-time', |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( $id instanceof WP_User ) { |
135
|
|
|
$this->init( $id->data, $blog_id ); |
136
|
|
|
return; |
137
|
|
|
} elseif ( is_object( $id ) ) { |
138
|
|
|
$this->init( $id, $blog_id ); |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ( ! empty( $id ) && ! is_numeric( $id ) ) { |
143
|
|
|
$name = $id; |
144
|
|
|
$id = 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ( $id ) { |
148
|
|
|
$data = self::get_data_by( 'id', $id ); |
149
|
|
|
} else { |
150
|
|
|
$data = self::get_data_by( 'login', $name ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( $data ) { |
154
|
|
|
$this->init( $data, $blog_id ); |
155
|
|
|
} else { |
156
|
|
|
$this->data = new stdClass; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sets up object properties, including capabilities. |
162
|
|
|
* |
163
|
|
|
* @since 3.3.0 |
164
|
|
|
* |
165
|
|
|
* @param object $data User DB row object. |
166
|
|
|
* @param int $blog_id Optional. The site ID to initialize for. |
167
|
|
|
*/ |
168
|
|
|
public function init( $data, $blog_id = '' ) { |
169
|
|
|
$this->data = $data; |
170
|
|
|
$this->ID = (int) $data->ID; |
171
|
|
|
|
172
|
|
|
$this->for_blog( $blog_id ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return only the main user fields |
177
|
|
|
* |
178
|
|
|
* @since 3.3.0 |
179
|
|
|
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
180
|
|
|
* |
181
|
|
|
* @static |
182
|
|
|
* |
183
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
184
|
|
|
* |
185
|
|
|
* @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. |
186
|
|
|
* @param string|int $value The field value |
187
|
|
|
* @return object|false Raw user object |
188
|
|
|
*/ |
189
|
|
|
public static function get_data_by( $field, $value ) { |
190
|
|
|
global $wpdb; |
191
|
|
|
|
192
|
|
|
// 'ID' is an alias of 'id'. |
193
|
|
|
if ( 'ID' === $field ) { |
194
|
|
|
$field = 'id'; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ( 'id' == $field ) { |
198
|
|
|
// Make sure the value is numeric to avoid casting objects, for example, |
199
|
|
|
// to int 1. |
200
|
|
|
if ( ! is_numeric( $value ) ) |
201
|
|
|
return false; |
202
|
|
|
$value = intval( $value ); |
203
|
|
|
if ( $value < 1 ) |
204
|
|
|
return false; |
205
|
|
|
} else { |
206
|
|
|
$value = trim( $value ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if ( !$value ) |
210
|
|
|
return false; |
211
|
|
|
|
212
|
|
|
switch ( $field ) { |
213
|
|
|
case 'id': |
214
|
|
|
$user_id = $value; |
215
|
|
|
$db_field = 'ID'; |
216
|
|
|
break; |
217
|
|
|
case 'slug': |
218
|
|
|
$user_id = wp_cache_get($value, 'userslugs'); |
219
|
|
|
$db_field = 'user_nicename'; |
220
|
|
|
break; |
221
|
|
|
case 'email': |
222
|
|
|
$user_id = wp_cache_get($value, 'useremail'); |
223
|
|
|
$db_field = 'user_email'; |
224
|
|
|
break; |
225
|
|
|
case 'login': |
226
|
|
|
$value = sanitize_user( $value ); |
227
|
|
|
$user_id = wp_cache_get($value, 'userlogins'); |
228
|
|
|
$db_field = 'user_login'; |
229
|
|
|
break; |
230
|
|
|
default: |
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ( false !== $user_id ) { |
235
|
|
|
if ( $user = wp_cache_get( $user_id, 'users' ) ) |
236
|
|
|
return $user; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if ( !$user = $wpdb->get_row( $wpdb->prepare( |
240
|
|
|
"SELECT * FROM $wpdb->users WHERE $db_field = %s", $value |
241
|
|
|
) ) ) |
242
|
|
|
return false; |
243
|
|
|
|
244
|
|
|
update_user_caches( $user ); |
245
|
|
|
|
246
|
|
|
return $user; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Makes private/protected methods readable for backward compatibility. |
251
|
|
|
* |
252
|
|
|
* @since 4.3.0 |
253
|
|
|
* @access public |
254
|
|
|
* |
255
|
|
|
* @param callable $name Method to call. |
256
|
|
|
* @param array $arguments Arguments to pass when calling. |
257
|
|
|
* @return mixed|false Return value of the callback, false otherwise. |
258
|
|
|
*/ |
259
|
|
|
public function __call( $name, $arguments ) { |
260
|
|
|
if ( '_init_caps' === $name ) { |
261
|
|
|
return call_user_func_array( array( $this, $name ), $arguments ); |
262
|
|
|
} |
263
|
|
|
return false; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Magic method for checking the existence of a certain custom field. |
268
|
|
|
* |
269
|
|
|
* @since 3.3.0 |
270
|
|
|
* @access public |
271
|
|
|
* |
272
|
|
|
* @param string $key User meta key to check if set. |
273
|
|
|
* @return bool Whether the given user meta key is set. |
274
|
|
|
*/ |
275
|
|
|
public function __isset( $key ) { |
276
|
|
View Code Duplication |
if ( 'id' == $key ) { |
277
|
|
|
_deprecated_argument( 'WP_User->id', '2.1.0', |
278
|
|
|
sprintf( |
279
|
|
|
/* translators: %s: WP_User->ID */ |
280
|
|
|
__( 'Use %s instead.' ), |
281
|
|
|
'<code>WP_User->ID</code>' |
282
|
|
|
) |
283
|
|
|
); |
284
|
|
|
$key = 'ID'; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if ( isset( $this->data->$key ) ) |
288
|
|
|
return true; |
289
|
|
|
|
290
|
|
|
if ( isset( self::$back_compat_keys[ $key ] ) ) |
291
|
|
|
$key = self::$back_compat_keys[ $key ]; |
292
|
|
|
|
293
|
|
|
return metadata_exists( 'user', $this->ID, $key ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Magic method for accessing custom fields. |
298
|
|
|
* |
299
|
|
|
* @since 3.3.0 |
300
|
|
|
* @access public |
301
|
|
|
* |
302
|
|
|
* @param string $key User meta key to retrieve. |
303
|
|
|
* @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. |
304
|
|
|
*/ |
305
|
|
|
public function __get( $key ) { |
306
|
|
View Code Duplication |
if ( 'id' == $key ) { |
307
|
|
|
_deprecated_argument( 'WP_User->id', '2.1.0', |
308
|
|
|
sprintf( |
309
|
|
|
/* translators: %s: WP_User->ID */ |
310
|
|
|
__( 'Use %s instead.' ), |
311
|
|
|
'<code>WP_User->ID</code>' |
312
|
|
|
) |
313
|
|
|
); |
314
|
|
|
return $this->ID; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if ( isset( $this->data->$key ) ) { |
318
|
|
|
$value = $this->data->$key; |
319
|
|
|
} else { |
320
|
|
|
if ( isset( self::$back_compat_keys[ $key ] ) ) |
321
|
|
|
$key = self::$back_compat_keys[ $key ]; |
322
|
|
|
$value = get_user_meta( $this->ID, $key, true ); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ( $this->filter ) { |
326
|
|
|
$value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $value; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Magic method for setting custom user fields. |
334
|
|
|
* |
335
|
|
|
* This method does not update custom fields in the database. It only stores |
336
|
|
|
* the value on the WP_User instance. |
337
|
|
|
* |
338
|
|
|
* @since 3.3.0 |
339
|
|
|
* @access public |
340
|
|
|
* |
341
|
|
|
* @param string $key User meta key. |
342
|
|
|
* @param mixed $value User meta value. |
343
|
|
|
*/ |
344
|
|
|
public function __set( $key, $value ) { |
345
|
|
View Code Duplication |
if ( 'id' == $key ) { |
346
|
|
|
_deprecated_argument( 'WP_User->id', '2.1.0', |
347
|
|
|
sprintf( |
348
|
|
|
/* translators: %s: WP_User->ID */ |
349
|
|
|
__( 'Use %s instead.' ), |
350
|
|
|
'<code>WP_User->ID</code>' |
351
|
|
|
) |
352
|
|
|
); |
353
|
|
|
$this->ID = $value; |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$this->data->$key = $value; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Magic method for unsetting a certain custom field. |
362
|
|
|
* |
363
|
|
|
* @since 4.4.0 |
364
|
|
|
* @access public |
365
|
|
|
* |
366
|
|
|
* @param string $key User meta key to unset. |
367
|
|
|
*/ |
368
|
|
|
public function __unset( $key ) { |
369
|
|
|
if ( 'id' == $key ) { |
370
|
|
|
_deprecated_argument( 'WP_User->id', '2.1.0', |
371
|
|
|
sprintf( |
372
|
|
|
/* translators: %s: WP_User->ID */ |
373
|
|
|
__( 'Use %s instead.' ), |
374
|
|
|
'<code>WP_User->ID</code>' |
375
|
|
|
) |
376
|
|
|
); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if ( isset( $this->data->$key ) ) { |
380
|
|
|
unset( $this->data->$key ); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if ( isset( self::$back_compat_keys[ $key ] ) ) { |
384
|
|
|
unset( self::$back_compat_keys[ $key ] ); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Determine whether the user exists in the database. |
390
|
|
|
* |
391
|
|
|
* @since 3.4.0 |
392
|
|
|
* @access public |
393
|
|
|
* |
394
|
|
|
* @return bool True if user exists in the database, false if not. |
395
|
|
|
*/ |
396
|
|
|
public function exists() { |
397
|
|
|
return ! empty( $this->ID ); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Retrieve the value of a property or meta key. |
402
|
|
|
* |
403
|
|
|
* Retrieves from the users and usermeta table. |
404
|
|
|
* |
405
|
|
|
* @since 3.3.0 |
406
|
|
|
* |
407
|
|
|
* @param string $key Property |
408
|
|
|
* @return mixed |
409
|
|
|
*/ |
410
|
|
|
public function get( $key ) { |
411
|
|
|
return $this->__get( $key ); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Determine whether a property or meta key is set |
416
|
|
|
* |
417
|
|
|
* Consults the users and usermeta tables. |
418
|
|
|
* |
419
|
|
|
* @since 3.3.0 |
420
|
|
|
* |
421
|
|
|
* @param string $key Property |
422
|
|
|
* @return bool |
423
|
|
|
*/ |
424
|
|
|
public function has_prop( $key ) { |
425
|
|
|
return $this->__isset( $key ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Return an array representation. |
430
|
|
|
* |
431
|
|
|
* @since 3.5.0 |
432
|
|
|
* |
433
|
|
|
* @return array Array representation. |
434
|
|
|
*/ |
435
|
|
|
public function to_array() { |
436
|
|
|
return get_object_vars( $this->data ); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Set up capability object properties. |
441
|
|
|
* |
442
|
|
|
* Will set the value for the 'cap_key' property to current database table |
443
|
|
|
* prefix, followed by 'capabilities'. Will then check to see if the |
444
|
|
|
* property matching the 'cap_key' exists and is an array. If so, it will be |
445
|
|
|
* used. |
446
|
|
|
* |
447
|
|
|
* @access protected |
448
|
|
|
* @since 2.1.0 |
449
|
|
|
* |
450
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
451
|
|
|
* |
452
|
|
|
* @param string $cap_key Optional capability key |
453
|
|
|
*/ |
454
|
|
|
protected function _init_caps( $cap_key = '' ) { |
455
|
|
|
global $wpdb; |
456
|
|
|
|
457
|
|
|
if ( empty($cap_key) ) |
458
|
|
|
$this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; |
459
|
|
|
else |
460
|
|
|
$this->cap_key = $cap_key; |
461
|
|
|
|
462
|
|
|
$this->caps = get_user_meta( $this->ID, $this->cap_key, true ); |
|
|
|
|
463
|
|
|
|
464
|
|
|
if ( ! is_array( $this->caps ) ) |
465
|
|
|
$this->caps = array(); |
466
|
|
|
|
467
|
|
|
$this->get_role_caps(); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Retrieve all of the role capabilities and merge with individual capabilities. |
472
|
|
|
* |
473
|
|
|
* All of the capabilities of the roles the user belongs to are merged with |
474
|
|
|
* the users individual roles. This also means that the user can be denied |
475
|
|
|
* specific roles that their role might have, but the specific user isn't |
476
|
|
|
* granted permission to. |
477
|
|
|
* |
478
|
|
|
* @since 2.0.0 |
479
|
|
|
* @access public |
480
|
|
|
* |
481
|
|
|
* @return array List of all capabilities for the user. |
482
|
|
|
*/ |
483
|
|
|
public function get_role_caps() { |
484
|
|
|
$wp_roles = wp_roles(); |
485
|
|
|
|
486
|
|
|
//Filter out caps that are not role names and assign to $this->roles |
487
|
|
|
if ( is_array( $this->caps ) ) |
488
|
|
|
$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); |
489
|
|
|
|
490
|
|
|
//Build $allcaps from role caps, overlay user's $caps |
491
|
|
|
$this->allcaps = array(); |
492
|
|
|
foreach ( (array) $this->roles as $role ) { |
493
|
|
|
$the_role = $wp_roles->get_role( $role ); |
494
|
|
|
$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); |
495
|
|
|
} |
496
|
|
|
$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); |
497
|
|
|
|
498
|
|
|
return $this->allcaps; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Add role to user. |
503
|
|
|
* |
504
|
|
|
* Updates the user's meta data option with capabilities and roles. |
505
|
|
|
* |
506
|
|
|
* @since 2.0.0 |
507
|
|
|
* @access public |
508
|
|
|
* |
509
|
|
|
* @param string $role Role name. |
510
|
|
|
*/ |
511
|
|
View Code Duplication |
public function add_role( $role ) { |
512
|
|
|
if ( empty( $role ) ) { |
513
|
|
|
return; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$this->caps[$role] = true; |
517
|
|
|
update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
518
|
|
|
$this->get_role_caps(); |
519
|
|
|
$this->update_user_level_from_caps(); |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Fires immediately after the user has been given a new role. |
523
|
|
|
* |
524
|
|
|
* @since 4.3.0 |
525
|
|
|
* |
526
|
|
|
* @param int $user_id The user ID. |
527
|
|
|
* @param string $role The new role. |
528
|
|
|
*/ |
529
|
|
|
do_action( 'add_user_role', $this->ID, $role ); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Remove role from user. |
534
|
|
|
* |
535
|
|
|
* @since 2.0.0 |
536
|
|
|
* @access public |
537
|
|
|
* |
538
|
|
|
* @param string $role Role name. |
539
|
|
|
*/ |
540
|
|
View Code Duplication |
public function remove_role( $role ) { |
541
|
|
|
if ( !in_array($role, $this->roles) ) |
542
|
|
|
return; |
543
|
|
|
unset( $this->caps[$role] ); |
544
|
|
|
update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
545
|
|
|
$this->get_role_caps(); |
546
|
|
|
$this->update_user_level_from_caps(); |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* Fires immediately after a role as been removed from a user. |
550
|
|
|
* |
551
|
|
|
* @since 4.3.0 |
552
|
|
|
* |
553
|
|
|
* @param int $user_id The user ID. |
554
|
|
|
* @param string $role The removed role. |
555
|
|
|
*/ |
556
|
|
|
do_action( 'remove_user_role', $this->ID, $role ); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Set the role of the user. |
561
|
|
|
* |
562
|
|
|
* This will remove the previous roles of the user and assign the user the |
563
|
|
|
* new one. You can set the role to an empty string and it will remove all |
564
|
|
|
* of the roles from the user. |
565
|
|
|
* |
566
|
|
|
* @since 2.0.0 |
567
|
|
|
* @access public |
568
|
|
|
* |
569
|
|
|
* @param string $role Role name. |
570
|
|
|
*/ |
571
|
|
|
public function set_role( $role ) { |
572
|
|
|
if ( 1 == count( $this->roles ) && $role == current( $this->roles ) ) |
573
|
|
|
return; |
574
|
|
|
|
575
|
|
|
foreach ( (array) $this->roles as $oldrole ) |
576
|
|
|
unset( $this->caps[$oldrole] ); |
577
|
|
|
|
578
|
|
|
$old_roles = $this->roles; |
579
|
|
|
if ( !empty( $role ) ) { |
580
|
|
|
$this->caps[$role] = true; |
581
|
|
|
$this->roles = array( $role => true ); |
582
|
|
|
} else { |
583
|
|
|
$this->roles = false; |
|
|
|
|
584
|
|
|
} |
585
|
|
|
update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
586
|
|
|
$this->get_role_caps(); |
587
|
|
|
$this->update_user_level_from_caps(); |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Fires after the user's role has changed. |
591
|
|
|
* |
592
|
|
|
* @since 2.9.0 |
593
|
|
|
* @since 3.6.0 Added $old_roles to include an array of the user's previous roles. |
594
|
|
|
* |
595
|
|
|
* @param int $user_id The user ID. |
596
|
|
|
* @param string $role The new role. |
597
|
|
|
* @param array $old_roles An array of the user's previous roles. |
598
|
|
|
*/ |
599
|
|
|
do_action( 'set_user_role', $this->ID, $role, $old_roles ); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Choose the maximum level the user has. |
604
|
|
|
* |
605
|
|
|
* Will compare the level from the $item parameter against the $max |
606
|
|
|
* parameter. If the item is incorrect, then just the $max parameter value |
607
|
|
|
* will be returned. |
608
|
|
|
* |
609
|
|
|
* Used to get the max level based on the capabilities the user has. This |
610
|
|
|
* is also based on roles, so if the user is assigned the Administrator role |
611
|
|
|
* then the capability 'level_10' will exist and the user will get that |
612
|
|
|
* value. |
613
|
|
|
* |
614
|
|
|
* @since 2.0.0 |
615
|
|
|
* @access public |
616
|
|
|
* |
617
|
|
|
* @param int $max Max level of user. |
618
|
|
|
* @param string $item Level capability name. |
619
|
|
|
* @return int Max Level. |
620
|
|
|
*/ |
621
|
|
|
public function level_reduction( $max, $item ) { |
622
|
|
|
if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { |
623
|
|
|
$level = intval( $matches[1] ); |
624
|
|
|
return max( $max, $level ); |
625
|
|
|
} else { |
626
|
|
|
return $max; |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Update the maximum user level for the user. |
632
|
|
|
* |
633
|
|
|
* Updates the 'user_level' user metadata (includes prefix that is the |
634
|
|
|
* database table prefix) with the maximum user level. Gets the value from |
635
|
|
|
* the all of the capabilities that the user has. |
636
|
|
|
* |
637
|
|
|
* @since 2.0.0 |
638
|
|
|
* @access public |
639
|
|
|
* |
640
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
641
|
|
|
*/ |
642
|
|
|
public function update_user_level_from_caps() { |
643
|
|
|
global $wpdb; |
644
|
|
|
$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 ); |
645
|
|
|
update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Add capability and grant or deny access to capability. |
650
|
|
|
* |
651
|
|
|
* @since 2.0.0 |
652
|
|
|
* @access public |
653
|
|
|
* |
654
|
|
|
* @param string $cap Capability name. |
655
|
|
|
* @param bool $grant Whether to grant capability to user. |
656
|
|
|
*/ |
657
|
|
|
public function add_cap( $cap, $grant = true ) { |
658
|
|
|
$this->caps[$cap] = $grant; |
659
|
|
|
update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
660
|
|
|
$this->get_role_caps(); |
661
|
|
|
$this->update_user_level_from_caps(); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Remove capability from user. |
666
|
|
|
* |
667
|
|
|
* @since 2.0.0 |
668
|
|
|
* @access public |
669
|
|
|
* |
670
|
|
|
* @param string $cap Capability name. |
671
|
|
|
*/ |
672
|
|
|
public function remove_cap( $cap ) { |
673
|
|
|
if ( ! isset( $this->caps[ $cap ] ) ) { |
674
|
|
|
return; |
675
|
|
|
} |
676
|
|
|
unset( $this->caps[ $cap ] ); |
677
|
|
|
update_user_meta( $this->ID, $this->cap_key, $this->caps ); |
678
|
|
|
$this->get_role_caps(); |
679
|
|
|
$this->update_user_level_from_caps(); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* Remove all of the capabilities of the user. |
684
|
|
|
* |
685
|
|
|
* @since 2.1.0 |
686
|
|
|
* @access public |
687
|
|
|
* |
688
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
689
|
|
|
*/ |
690
|
|
|
public function remove_all_caps() { |
691
|
|
|
global $wpdb; |
692
|
|
|
$this->caps = array(); |
693
|
|
|
delete_user_meta( $this->ID, $this->cap_key ); |
694
|
|
|
delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' ); |
695
|
|
|
$this->get_role_caps(); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Whether user has capability or role name. |
700
|
|
|
* |
701
|
|
|
* While checking against particular roles in place of a capability is supported |
702
|
|
|
* in part, this practice is discouraged as it may produce unreliable results. |
703
|
|
|
* |
704
|
|
|
* @since 2.0.0 |
705
|
|
|
* @access public |
706
|
|
|
* |
707
|
|
|
* @see map_meta_cap() |
708
|
|
|
* |
709
|
|
|
* @param string $cap Capability name. |
710
|
|
|
* @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap. |
|
|
|
|
711
|
|
|
* "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used |
712
|
|
|
* by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', |
713
|
|
|
* 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed |
714
|
|
|
* to map_meta_cap(). |
715
|
|
|
* @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is |
716
|
|
|
* passed, whether the current user has the given meta capability for the given object. |
717
|
|
|
*/ |
718
|
|
|
public function has_cap( $cap ) { |
719
|
|
|
if ( is_numeric( $cap ) ) { |
720
|
|
|
_deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); |
721
|
|
|
$cap = $this->translate_level_to_cap( $cap ); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
$args = array_slice( func_get_args(), 1 ); |
725
|
|
|
$args = array_merge( array( $cap, $this->ID ), $args ); |
726
|
|
|
$caps = call_user_func_array( 'map_meta_cap', $args ); |
727
|
|
|
|
728
|
|
|
// Multisite super admin has all caps by definition, Unless specifically denied. |
729
|
|
|
if ( is_multisite() && is_super_admin( $this->ID ) ) { |
730
|
|
|
if ( in_array('do_not_allow', $caps) ) |
|
|
|
|
731
|
|
|
return false; |
732
|
|
|
return true; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Dynamically filter a user's capabilities. |
737
|
|
|
* |
738
|
|
|
* @since 2.0.0 |
739
|
|
|
* @since 3.7.0 Added the user object. |
740
|
|
|
* |
741
|
|
|
* @param array $allcaps An array of all the user's capabilities. |
742
|
|
|
* @param array $caps Actual capabilities for meta capability. |
743
|
|
|
* @param array $args Optional parameters passed to has_cap(), typically object ID. |
744
|
|
|
* @param WP_User $user The user object. |
745
|
|
|
*/ |
746
|
|
|
$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); |
747
|
|
|
|
748
|
|
|
// Everyone is allowed to exist. |
749
|
|
|
$capabilities['exist'] = true; |
750
|
|
|
|
751
|
|
|
// Must have ALL requested caps. |
752
|
|
|
foreach ( (array) $caps as $cap ) { |
753
|
|
|
if ( empty( $capabilities[ $cap ] ) ) |
754
|
|
|
return false; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
return true; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Convert numeric level to level capability name. |
762
|
|
|
* |
763
|
|
|
* Prepends 'level_' to level number. |
764
|
|
|
* |
765
|
|
|
* @since 2.0.0 |
766
|
|
|
* @access public |
767
|
|
|
* |
768
|
|
|
* @param int $level Level number, 1 to 10. |
769
|
|
|
* @return string |
770
|
|
|
*/ |
771
|
|
|
public function translate_level_to_cap( $level ) { |
772
|
|
|
return 'level_' . $level; |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
/** |
776
|
|
|
* Set the site to operate on. Defaults to the current site. |
777
|
|
|
* |
778
|
|
|
* @since 3.0.0 |
779
|
|
|
* |
780
|
|
|
* @global wpdb $wpdb WordPress database abstraction object. |
781
|
|
|
* |
782
|
|
|
* @param int $blog_id Optional. Site ID, defaults to current site. |
783
|
|
|
*/ |
784
|
|
|
public function for_blog( $blog_id = '' ) { |
785
|
|
|
global $wpdb; |
786
|
|
|
if ( ! empty( $blog_id ) ) |
787
|
|
|
$cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; |
788
|
|
|
else |
789
|
|
|
$cap_key = ''; |
790
|
|
|
$this->_init_caps( $cap_key ); |
791
|
|
|
} |
792
|
|
|
} |
793
|
|
|
|