WP_User::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
 */
35
class WP_User {
36
	/**
37
	 * User data container.
38
	 *
39
	 * @since 2.0.0
40
	 * @var object
41
	 */
42
	public $data;
43
44
	/**
45
	 * The user's ID.
46
	 *
47
	 * @since 2.1.0
48
	 * @access public
49
	 * @var int
50
	 */
51
	public $ID = 0;
52
53
	/**
54
	 * The individual capabilities the user has been given.
55
	 *
56
	 * @since 2.0.0
57
	 * @access public
58
	 * @var array
59
	 */
60
	public $caps = array();
61
62
	/**
63
	 * User metadata option name.
64
	 *
65
	 * @since 2.0.0
66
	 * @access public
67
	 * @var string
68
	 */
69
	public $cap_key;
70
71
	/**
72
	 * The roles the user is part of.
73
	 *
74
	 * @since 2.0.0
75
	 * @access public
76
	 * @var array
77
	 */
78
	public $roles = array();
79
80
	/**
81
	 * All capabilities the user has, including individual and role based.
82
	 *
83
	 * @since 2.0.0
84
	 * @access public
85
	 * @var array
86
	 */
87
	public $allcaps = array();
88
89
	/**
90
	 * The filter context applied to user data fields.
91
	 *
92
	 * @since 2.9.0
93
	 * @access private
94
	 * @var string
95
	 */
96
	var $filter = null;
97
98
	/**
99
	 * @static
100
	 * @access private
101
	 * @var array
102
	 */
103
	private static $back_compat_keys;
104
105
	/**
106
	 * @since 4.7.0
107
	 * @access protected
108
	 * @var wpdb
109
	 */
110
	protected $db;
111
112
	/**
113
	 * Constructor.
114
	 *
115
	 * Retrieves the userdata and passes it to WP_User::init().
116
	 *
117
	 * @since 2.0.0
118
	 * @access public
119
	 *
120
	 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
121
	 * @param string $name Optional. User's username
122
	 * @param int $blog_id Optional Site ID, defaults to current site.
123
	 */
124
	public function __construct( $id = 0, $name = '', $blog_id = '' ) {
125
		$this->db = $GLOBALS['wpdb'];
126
127
		if ( ! isset( self::$back_compat_keys ) ) {
128
			$prefix = $this->db->prefix;
129
			self::$back_compat_keys = array(
130
				'user_firstname' => 'first_name',
131
				'user_lastname' => 'last_name',
132
				'user_description' => 'description',
133
				'user_level' => $prefix . 'user_level',
134
				$prefix . 'usersettings' => $prefix . 'user-settings',
135
				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
136
			);
137
		}
138
139
		if ( $id instanceof WP_User ) {
140
			$this->init( $id->data, $blog_id );
141
			return;
142
		} elseif ( is_object( $id ) ) {
143
			$this->init( $id, $blog_id );
144
			return;
145
		}
146
147
		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
148
			$name = $id;
149
			$id = 0;
150
		}
151
152
		if ( $id ) {
153
			$data = self::get_data_by( 'id', $id );
154
		} else {
155
			$data = self::get_data_by( 'login', $name );
156
		}
157
158
		if ( $data ) {
159
			$this->init( $data, $blog_id );
160
		} else {
161
			$this->data = new stdClass;
162
		}
163
	}
164
165
	/**
166
	 * Sets up object properties, including capabilities.
167
	 *
168
	 * @param object $data    User DB row object.
169
	 * @param int    $blog_id Optional. The site ID to initialize for.
170
	 */
171
	public function init( $data, $blog_id = '' ) {
172
		$this->data = $data;
173
		$this->ID = (int) $data->ID;
174
175
		$this->for_blog( $blog_id );
176
	}
177
178
	/**
179
	 * Return only the main user fields
180
	 *
181
	 * @since 3.3.0
182
	 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
183
	 *
184
	 * @static
185
	 *
186
	 * @global wpdb $wpdb WordPress database abstraction object.
187
	 *
188
	 * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
189
	 * @param string|int $value The field value
190
	 * @return object|false Raw user object
191
	 */
192
	public static function get_data_by( $field, $value ) {
193
		global $wpdb;
194
195
		// 'ID' is an alias of 'id'.
196
		if ( 'ID' === $field ) {
197
			$field = 'id';
198
		}
199
200
		if ( 'id' == $field ) {
201
			// Make sure the value is numeric to avoid casting objects, for example,
202
			// to int 1.
203
			if ( ! is_numeric( $value ) )
204
				return false;
205
			$value = intval( $value );
206
			if ( $value < 1 )
207
				return false;
208
		} else {
209
			$value = trim( $value );
210
		}
211
212
		if ( !$value )
213
			return false;
214
215
		switch ( $field ) {
216
			case 'id':
217
				$user_id = $value;
218
				$db_field = 'ID';
219
				break;
220
			case 'slug':
221
				$user_id = wp_cache_get($value, 'userslugs');
222
				$db_field = 'user_nicename';
223
				break;
224
			case 'email':
225
				$user_id = wp_cache_get($value, 'useremail');
226
				$db_field = 'user_email';
227
				break;
228
			case 'login':
229
				$value = sanitize_user( $value );
230
				$user_id = wp_cache_get($value, 'userlogins');
231
				$db_field = 'user_login';
232
				break;
233
			default:
234
				return false;
235
		}
236
237
		if ( false !== $user_id ) {
238
			if ( $user = wp_cache_get( $user_id, 'users' ) )
239
				return $user;
240
		}
241
242
		if ( !$user = $wpdb->get_row( $wpdb->prepare(
243
			"SELECT * FROM {$wpdb->users} WHERE $db_field = %s", $value
244
		) ) ) {
245
			return false;
246
		}
247
		update_user_caches( $user );
248
249
		return $user;
250
	}
251
252
	/**
253
	 * Makes private/protected methods readable for backward compatibility.
254
	 *
255
	 * @since 4.3.0
256
	 * @access public
257
	 *
258
	 * @param callable $name      Method to call.
259
	 * @param array    $arguments Arguments to pass when calling.
260
	 * @return mixed|false Return value of the callback, false otherwise.
261
	 */
262
	public function __call( $name, $arguments ) {
263
		if ( '_init_caps' === $name ) {
264
			return call_user_func_array( array( $this, $name ), $arguments );
265
		}
266
		return false;
267
	}
268
269
	/**
270
	 * Magic method for checking the existence of a certain custom field.
271
	 *
272
	 * @since 3.3.0
273
	 * @access public
274
	 *
275
	 * @param string $key User meta key to check if set.
276
	 * @return bool Whether the given user meta key is set.
277
	 */
278
	public function __isset( $key ) {
279 View Code Duplication
		if ( 'id' == $key ) {
280
			_deprecated_argument( 'WP_User->id', '2.1.0',
281
				sprintf(
282
					/* translators: %s: WP_User->ID */
283
					__( 'Use %s instead.' ),
284
					'<code>WP_User->ID</code>'
285
				)
286
			);
287
			$key = 'ID';
288
		}
289
290
		if ( isset( $this->data->$key ) )
291
			return true;
292
293
		if ( isset( self::$back_compat_keys[ $key ] ) )
294
			$key = self::$back_compat_keys[ $key ];
295
296
		return metadata_exists( 'user', $this->ID, $key );
297
	}
298
299
	/**
300
	 * Magic method for accessing custom fields.
301
	 *
302
	 * @since 3.3.0
303
	 * @access public
304
	 *
305
	 * @param string $key User meta key to retrieve.
306
	 * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
307
	 */
308
	public function __get( $key ) {
309 View Code Duplication
		if ( 'id' == $key ) {
310
			_deprecated_argument( 'WP_User->id', '2.1.0',
311
				sprintf(
312
					/* translators: %s: WP_User->ID */
313
					__( 'Use %s instead.' ),
314
					'<code>WP_User->ID</code>'
315
				)
316
			);
317
			return $this->ID;
318
		}
319
320
		if ( isset( $this->data->$key ) ) {
321
			$value = $this->data->$key;
322
		} else {
323
			if ( isset( self::$back_compat_keys[ $key ] ) )
324
				$key = self::$back_compat_keys[ $key ];
325
			$value = get_user_meta( $this->ID, $key, true );
326
		}
327
328
		if ( $this->filter ) {
329
			$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
330
		}
331
332
		return $value;
333
	}
334
335
	/**
336
	 * Magic method for setting custom user fields.
337
	 *
338
	 * This method does not update custom fields in the database. It only stores
339
	 * the value on the WP_User instance.
340
	 *
341
	 * @since 3.3.0
342
	 * @access public
343
	 *
344
	 * @param string $key   User meta key.
345
	 * @param mixed  $value User meta value.
346
	 */
347
	public function __set( $key, $value ) {
348 View Code Duplication
		if ( 'id' == $key ) {
349
			_deprecated_argument( 'WP_User->id', '2.1.0',
350
				sprintf(
351
					/* translators: %s: WP_User->ID */
352
					__( 'Use %s instead.' ),
353
					'<code>WP_User->ID</code>'
354
				)
355
			);
356
			$this->ID = $value;
357
			return;
358
		}
359
360
		$this->data->$key = $value;
361
	}
362
363
	/**
364
	 * Magic method for unsetting a certain custom field.
365
	 *
366
	 * @since 4.4.0
367
	 * @access public
368
	 *
369
	 * @param string $key User meta key to unset.
370
	 */
371
	public function __unset( $key ) {
372
		if ( 'id' == $key ) {
373
			_deprecated_argument( 'WP_User->id', '2.1.0',
374
				sprintf(
375
					/* translators: %s: WP_User->ID */
376
					__( 'Use %s instead.' ),
377
					'<code>WP_User->ID</code>'
378
				)
379
			);
380
		}
381
382
		if ( isset( $this->data->$key ) ) {
383
			unset( $this->data->$key );
384
		}
385
386
		if ( isset( self::$back_compat_keys[ $key ] ) ) {
387
			unset( self::$back_compat_keys[ $key ] );
388
		}
389
	}
390
391
	/**
392
	 * Determine whether the user exists in the database.
393
	 *
394
	 * @since 3.4.0
395
	 * @access public
396
	 *
397
	 * @return bool True if user exists in the database, false if not.
398
	 */
399
	public function exists() {
400
		return ! empty( $this->ID );
401
	}
402
403
	/**
404
	 * Retrieve the value of a property or meta key.
405
	 *
406
	 * Retrieves from the users and usermeta table.
407
	 *
408
	 * @since 3.3.0
409
	 *
410
	 * @param string $key Property
411
	 * @return mixed
412
	 */
413
	public function get( $key ) {
414
		return $this->__get( $key );
415
	}
416
417
	/**
418
	 * Determine whether a property or meta key is set
419
	 *
420
	 * Consults the users and usermeta tables.
421
	 *
422
	 * @since 3.3.0
423
	 *
424
	 * @param string $key Property
425
	 * @return bool
426
	 */
427
	public function has_prop( $key ) {
428
		return $this->__isset( $key );
429
	}
430
431
	/**
432
	 * Return an array representation.
433
	 *
434
	 * @since 3.5.0
435
	 *
436
	 * @return array Array representation.
437
	 */
438
	public function to_array() {
439
		return get_object_vars( $this->data );
440
	}
441
442
	/**
443
	 * Set up capability object properties.
444
	 *
445
	 * Will set the value for the 'cap_key' property to current database table
446
	 * prefix, followed by 'capabilities'. Will then check to see if the
447
	 * property matching the 'cap_key' exists and is an array. If so, it will be
448
	 * used.
449
	 *
450
	 * @access protected
451
	 * @since 2.1.0
452
	 *
453
	 * @param string $cap_key Optional capability key
454
	 */
455
	protected function _init_caps( $cap_key = '' ) {
456
		if ( empty( $cap_key ) ) {
457
			$this->cap_key = $this->db->get_blog_prefix() . 'capabilities';
458
		} else {
459
			$this->cap_key = $cap_key;
460
		}
461
		$this->caps = get_user_meta( $this->ID, $this->cap_key, true );
0 ignored issues
show
Documentation Bug introduced by
It seems like get_user_meta($this->ID, $this->cap_key, true) of type * is incompatible with the declared type array of property $caps.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
462
463
		if ( ! is_array( $this->caps ) )
464
			$this->caps = array();
465
466
		$this->get_role_caps();
467
	}
468
469
	/**
470
	 * Retrieve all of the role capabilities and merge with individual capabilities.
471
	 *
472
	 * All of the capabilities of the roles the user belongs to are merged with
473
	 * the users individual roles. This also means that the user can be denied
474
	 * specific roles that their role might have, but the specific user isn't
475
	 * granted permission to.
476
	 *
477
	 * @since 2.0.0
478
	 * @access public
479
	 *
480
	 * @return array List of all capabilities for the user.
481
	 */
482
	public function get_role_caps() {
483
		$wp_roles = wp_roles();
484
485
		//Filter out caps that are not role names and assign to $this->roles
486
		if ( is_array( $this->caps ) )
487
			$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
488
489
		//Build $allcaps from role caps, overlay user's $caps
490
		$this->allcaps = array();
491
		foreach ( (array) $this->roles as $role ) {
492
			$the_role = $wp_roles->get_role( $role );
493
			$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
494
		}
495
		$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
496
497
		return $this->allcaps;
498
	}
499
500
	/**
501
	 * Add role to user.
502
	 *
503
	 * Updates the user's meta data option with capabilities and roles.
504
	 *
505
	 * @since 2.0.0
506
	 * @access public
507
	 *
508
	 * @param string $role Role name.
509
	 */
510 View Code Duplication
	public function add_role( $role ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
511
		if ( empty( $role ) ) {
512
			return;
513
		}
514
515
		$this->caps[$role] = true;
516
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
517
		$this->get_role_caps();
518
		$this->update_user_level_from_caps();
519
520
		/**
521
		 * Fires immediately after the user has been given a new role.
522
		 *
523
		 * @since 4.3.0
524
		 *
525
		 * @param int    $user_id The user ID.
526
		 * @param string $role    The new role.
527
		 */
528
		do_action( 'add_user_role', $this->ID, $role );
529
	}
530
531
	/**
532
	 * Remove role from user.
533
	 *
534
	 * @since 2.0.0
535
	 * @access public
536
	 *
537
	 * @param string $role Role name.
538
	 */
539 View Code Duplication
	public function remove_role( $role ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
540
		if ( !in_array($role, $this->roles) )
541
			return;
542
		unset( $this->caps[$role] );
543
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
544
		$this->get_role_caps();
545
		$this->update_user_level_from_caps();
546
547
		/**
548
		 * Fires immediately after a role as been removed from a user.
549
		 *
550
		 * @since 4.3.0
551
		 *
552
		 * @param int    $user_id The user ID.
553
		 * @param string $role    The removed role.
554
		 */
555
		do_action( 'remove_user_role', $this->ID, $role );
556
	}
557
558
	/**
559
	 * Set the role of the user.
560
	 *
561
	 * This will remove the previous roles of the user and assign the user the
562
	 * new one. You can set the role to an empty string and it will remove all
563
	 * of the roles from the user.
564
	 *
565
	 * @since 2.0.0
566
	 * @access public
567
	 *
568
	 * @param string $role Role name.
569
	 */
570
	public function set_role( $role ) {
571
		if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
572
			return;
573
574
		foreach ( (array) $this->roles as $oldrole )
575
			unset( $this->caps[$oldrole] );
576
577
		$old_roles = $this->roles;
578
		if ( !empty( $role ) ) {
579
			$this->caps[$role] = true;
580
			$this->roles = array( $role => true );
581
		} else {
582
			$this->roles = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $roles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
583
		}
584
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
585
		$this->get_role_caps();
586
		$this->update_user_level_from_caps();
587
588
		/**
589
		 * Fires after the user's role has changed.
590
		 *
591
		 * @since 2.9.0
592
		 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
593
		 *
594
		 * @param int    $user_id   The user ID.
595
		 * @param string $role      The new role.
596
		 * @param array  $old_roles An array of the user's previous roles.
597
		 */
598
		do_action( 'set_user_role', $this->ID, $role, $old_roles );
599
	}
600
601
	/**
602
	 * Choose the maximum level the user has.
603
	 *
604
	 * Will compare the level from the $item parameter against the $max
605
	 * parameter. If the item is incorrect, then just the $max parameter value
606
	 * will be returned.
607
	 *
608
	 * Used to get the max level based on the capabilities the user has. This
609
	 * is also based on roles, so if the user is assigned the Administrator role
610
	 * then the capability 'level_10' will exist and the user will get that
611
	 * value.
612
	 *
613
	 * @since 2.0.0
614
	 * @access public
615
	 *
616
	 * @param int $max Max level of user.
617
	 * @param string $item Level capability name.
618
	 * @return int Max Level.
619
	 */
620
	public function level_reduction( $max, $item ) {
621
		if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
622
			$level = intval( $matches[1] );
623
			return max( $max, $level );
624
		} else {
625
			return $max;
626
		}
627
	}
628
629
	/**
630
	 * Update the maximum user level for the user.
631
	 *
632
	 * Updates the 'user_level' user metadata (includes prefix that is the
633
	 * database table prefix) with the maximum user level. Gets the value from
634
	 * the all of the capabilities that the user has.
635
	 *
636
	 * @since 2.0.0
637
	 * @access public
638
	 */
639
	public function update_user_level_from_caps() {
640
		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
641
		update_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level', $this->user_level );
642
	}
643
644
	/**
645
	 * Add capability and grant or deny access to capability.
646
	 *
647
	 * @since 2.0.0
648
	 * @access public
649
	 *
650
	 * @param string $cap Capability name.
651
	 * @param bool $grant Whether to grant capability to user.
652
	 */
653
	public function add_cap( $cap, $grant = true ) {
654
		$this->caps[$cap] = $grant;
655
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
656
		$this->get_role_caps();
657
		$this->update_user_level_from_caps();
658
	}
659
660
	/**
661
	 * Remove capability from user.
662
	 *
663
	 * @since 2.0.0
664
	 * @access public
665
	 *
666
	 * @param string $cap Capability name.
667
	 */
668
	public function remove_cap( $cap ) {
669
		if ( ! isset( $this->caps[ $cap ] ) ) {
670
			return;
671
		}
672
		unset( $this->caps[ $cap ] );
673
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
674
		$this->get_role_caps();
675
		$this->update_user_level_from_caps();
676
	}
677
678
	/**
679
	 * Remove all of the capabilities of the user.
680
	 *
681
	 * @since 2.1.0
682
	 * @access public
683
	 */
684
	public function remove_all_caps() {
685
		$this->caps = array();
686
		delete_user_meta( $this->ID, $this->cap_key );
687
		delete_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level' );
688
		$this->get_role_caps();
689
	}
690
691
	/**
692
	 * Whether user has capability or role name.
693
	 *
694
	 * While checking against particular roles in place of a capability is supported
695
	 * in part, this practice is discouraged as it may produce unreliable results.
696
	 *
697
	 * @since 2.0.0
698
	 * @access public
699
	 *
700
	 * @see map_meta_cap()
701
	 *
702
	 * @param string $cap           Capability name.
703
	 * @param int    $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
0 ignored issues
show
Bug introduced by
There is no parameter named $object_id,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
704
	 *                              "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
705
	 *                              by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
706
	 *                              'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed
707
	 *                              to map_meta_cap().
708
	 * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is
709
	 *              passed, whether the current user has the given meta capability for the given object.
710
	 */
711
	public function has_cap( $cap ) {
712
		if ( is_numeric( $cap ) ) {
713
			_deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
714
			$cap = $this->translate_level_to_cap( $cap );
715
		}
716
717
		$args = array_slice( func_get_args(), 1 );
718
		$args = array_merge( array( $cap, $this->ID ), $args );
719
		$caps = call_user_func_array( 'map_meta_cap', $args );
720
721
		// Multisite super admin has all caps by definition, Unless specifically denied.
722
		if ( is_multisite() && is_super_admin( $this->ID ) ) {
723
			if ( in_array('do_not_allow', $caps) )
724
				return false;
725
			return true;
726
		}
727
728
		/**
729
		 * Dynamically filter a user's capabilities.
730
		 *
731
		 * @since 2.0.0
732
		 * @since 3.7.0 Added the user object.
733
		 *
734
		 * @param array   $allcaps An array of all the user's capabilities.
735
		 * @param array   $caps    Actual capabilities for meta capability.
736
		 * @param array   $args    Optional parameters passed to has_cap(), typically object ID.
737
		 * @param WP_User $user    The user object.
738
		 */
739
		$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
740
741
		// Everyone is allowed to exist.
742
		$capabilities['exist'] = true;
743
744
		// Must have ALL requested caps.
745
		foreach ( (array) $caps as $cap ) {
746
			if ( empty( $capabilities[ $cap ] ) )
747
				return false;
748
		}
749
750
		return true;
751
	}
752
753
	/**
754
	 * Convert numeric level to level capability name.
755
	 *
756
	 * Prepends 'level_' to level number.
757
	 *
758
	 * @since 2.0.0
759
	 * @access public
760
	 *
761
	 * @param int $level Level number, 1 to 10.
762
	 * @return string
763
	 */
764
	public function translate_level_to_cap( $level ) {
765
		return 'level_' . $level;
766
	}
767
768
	/**
769
	 * Set the site to operate on. Defaults to the current site.
770
	 *
771
	 * @since 3.0.0
772
	 *
773
	 * @param int $blog_id Optional. Site ID, defaults to current site.
774
	 */
775
	public function for_blog( $blog_id = '' ) {
776
		if ( ! empty( $blog_id ) ) {
777
			$cap_key = $this->db->get_blog_prefix( $blog_id ) . 'capabilities';
778
		} else {
779
			$cap_key = '';
780
		}
781
		$this->_init_caps( $cap_key );
782
	}
783
}
784