]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/capabilities.php
30c38b2cbcbc8118d5e787813ba2803489565a15
[autoinstalls/wordpress.git] / wp-includes / capabilities.php
1 <?php
2 /**
3  * WordPress Roles and Capabilities.
4  *
5  * @package WordPress
6  * @subpackage User
7  */
8
9 /**
10  * WordPress User Roles.
11  *
12  * The role option is simple, the structure is organized by role name that store
13  * the name in value of the 'name' key. The capabilities are stored as an array
14  * in the value of the 'capability' key.
15  *
16  * <code>
17  * array (
18  *              'rolename' => array (
19  *                      'name' => 'rolename',
20  *                      'capabilities' => array()
21  *              )
22  * )
23  * </code>
24  *
25  * @since 2.0.0
26  * @package WordPress
27  * @subpackage User
28  */
29 class WP_Roles {
30         /**
31          * List of roles and capabilities.
32          *
33          * @since 2.0.0
34          * @access public
35          * @var array
36          */
37         public $roles;
38
39         /**
40          * List of the role objects.
41          *
42          * @since 2.0.0
43          * @access public
44          * @var array
45          */
46         public $role_objects = array();
47
48         /**
49          * List of role names.
50          *
51          * @since 2.0.0
52          * @access public
53          * @var array
54          */
55         public $role_names = array();
56
57         /**
58          * Option name for storing role list.
59          *
60          * @since 2.0.0
61          * @access public
62          * @var string
63          */
64         public $role_key;
65
66         /**
67          * Whether to use the database for retrieval and storage.
68          *
69          * @since 2.1.0
70          * @access public
71          * @var bool
72          */
73         public $use_db = true;
74
75         /**
76          * Constructor
77          *
78          * @since 2.0.0
79          */
80         public function __construct() {
81                 $this->_init();
82         }
83
84         /**
85          * Make private/protected methods readable for backwards compatibility.
86          *
87          * @since 4.0.0
88          * @access public
89          *
90          * @param callable $name      Method to call.
91          * @param array    $arguments Arguments to pass when calling.
92          * @return mixed|bool Return value of the callback, false otherwise.
93          */
94         public function __call( $name, $arguments ) {
95                 return call_user_func_array( array( $this, $name ), $arguments );
96         }
97
98         /**
99          * Set up the object properties.
100          *
101          * The role key is set to the current prefix for the $wpdb object with
102          * 'user_roles' appended. If the $wp_user_roles global is set, then it will
103          * be used and the role option will not be updated or used.
104          *
105          * @since 2.1.0
106          * @access protected
107          * @uses $wpdb Used to get the database prefix.
108          * @global array $wp_user_roles Used to set the 'roles' property value.
109          */
110         protected function _init() {
111                 global $wpdb, $wp_user_roles;
112                 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
113                 if ( ! empty( $wp_user_roles ) ) {
114                         $this->roles = $wp_user_roles;
115                         $this->use_db = false;
116                 } else {
117                         $this->roles = get_option( $this->role_key );
118                 }
119
120                 if ( empty( $this->roles ) )
121                         return;
122
123                 $this->role_objects = array();
124                 $this->role_names =  array();
125                 foreach ( array_keys( $this->roles ) as $role ) {
126                         $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
127                         $this->role_names[$role] = $this->roles[$role]['name'];
128                 }
129         }
130
131         /**
132          * Reinitialize the object
133          *
134          * Recreates the role objects. This is typically called only by switch_to_blog()
135          * after switching wpdb to a new blog ID.
136          *
137          * @since 3.5.0
138          * @access public
139          */
140         public function reinit() {
141                 // There is no need to reinit if using the wp_user_roles global.
142                 if ( ! $this->use_db )
143                         return;
144
145                 global $wpdb;
146
147                 // Duplicated from _init() to avoid an extra function call.
148                 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
149                 $this->roles = get_option( $this->role_key );
150                 if ( empty( $this->roles ) )
151                         return;
152
153                 $this->role_objects = array();
154                 $this->role_names =  array();
155                 foreach ( array_keys( $this->roles ) as $role ) {
156                         $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
157                         $this->role_names[$role] = $this->roles[$role]['name'];
158                 }
159         }
160
161         /**
162          * Add role name with capabilities to list.
163          *
164          * Updates the list of roles, if the role doesn't already exist.
165          *
166          * The capabilities are defined in the following format `array( 'read' => true );`
167          * To explicitly deny a role a capability you set the value for that capability to false.
168          *
169          * @since 2.0.0
170          * @access public
171          *
172          * @param string $role Role name.
173          * @param string $display_name Role display name.
174          * @param array $capabilities List of role capabilities in the above format.
175          * @return WP_Role|null WP_Role object if role is added, null if already exists.
176          */
177         public function add_role( $role, $display_name, $capabilities = array() ) {
178                 if ( isset( $this->roles[$role] ) )
179                         return;
180
181                 $this->roles[$role] = array(
182                         'name' => $display_name,
183                         'capabilities' => $capabilities
184                         );
185                 if ( $this->use_db )
186                         update_option( $this->role_key, $this->roles );
187                 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
188                 $this->role_names[$role] = $display_name;
189                 return $this->role_objects[$role];
190         }
191
192         /**
193          * Remove role by name.
194          *
195          * @since 2.0.0
196          * @access public
197          *
198          * @param string $role Role name.
199          */
200         public function remove_role( $role ) {
201                 if ( ! isset( $this->role_objects[$role] ) )
202                         return;
203
204                 unset( $this->role_objects[$role] );
205                 unset( $this->role_names[$role] );
206                 unset( $this->roles[$role] );
207
208                 if ( $this->use_db )
209                         update_option( $this->role_key, $this->roles );
210
211                 if ( get_option( 'default_role' ) == $role )
212                         update_option( 'default_role', 'subscriber' );
213         }
214
215         /**
216          * Add capability to role.
217          *
218          * @since 2.0.0
219          * @access public
220          *
221          * @param string $role Role name.
222          * @param string $cap Capability name.
223          * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
224          */
225         public function add_cap( $role, $cap, $grant = true ) {
226                 if ( ! isset( $this->roles[$role] ) )
227                         return;
228
229                 $this->roles[$role]['capabilities'][$cap] = $grant;
230                 if ( $this->use_db )
231                         update_option( $this->role_key, $this->roles );
232         }
233
234         /**
235          * Remove capability from role.
236          *
237          * @since 2.0.0
238          * @access public
239          *
240          * @param string $role Role name.
241          * @param string $cap Capability name.
242          */
243         public function remove_cap( $role, $cap ) {
244                 if ( ! isset( $this->roles[$role] ) )
245                         return;
246
247                 unset( $this->roles[$role]['capabilities'][$cap] );
248                 if ( $this->use_db )
249                         update_option( $this->role_key, $this->roles );
250         }
251
252         /**
253          * Retrieve role object by name.
254          *
255          * @since 2.0.0
256          * @access public
257          *
258          * @param string $role Role name.
259          * @return WP_Role|null WP_Role object if found, null if the role does not exist.
260          */
261         public function get_role( $role ) {
262                 if ( isset( $this->role_objects[$role] ) )
263                         return $this->role_objects[$role];
264                 else
265                         return null;
266         }
267
268         /**
269          * Retrieve list of role names.
270          *
271          * @since 2.0.0
272          * @access public
273          *
274          * @return array List of role names.
275          */
276         public function get_names() {
277                 return $this->role_names;
278         }
279
280         /**
281          * Whether role name is currently in the list of available roles.
282          *
283          * @since 2.0.0
284          * @access public
285          *
286          * @param string $role Role name to look up.
287          * @return bool
288          */
289         public function is_role( $role ) {
290                 return isset( $this->role_names[$role] );
291         }
292 }
293
294 /**
295  * WordPress Role class.
296  *
297  * @since 2.0.0
298  * @package WordPress
299  * @subpackage User
300  */
301 class WP_Role {
302         /**
303          * Role name.
304          *
305          * @since 2.0.0
306          * @access public
307          * @var string
308          */
309         public $name;
310
311         /**
312          * List of capabilities the role contains.
313          *
314          * @since 2.0.0
315          * @access public
316          * @var array
317          */
318         public $capabilities;
319
320         /**
321          * Constructor - Set up object properties.
322          *
323          * The list of capabilities, must have the key as the name of the capability
324          * and the value a boolean of whether it is granted to the role.
325          *
326          * @since 2.0.0
327          * @access public
328          *
329          * @param string $role Role name.
330          * @param array $capabilities List of capabilities.
331          */
332         public function __construct( $role, $capabilities ) {
333                 $this->name = $role;
334                 $this->capabilities = $capabilities;
335         }
336
337         /**
338          * Assign role a capability.
339          *
340          * @see WP_Roles::add_cap() Method uses implementation for role.
341          * @since 2.0.0
342          * @access public
343          *
344          * @param string $cap Capability name.
345          * @param bool $grant Whether role has capability privilege.
346          */
347         public function add_cap( $cap, $grant = true ) {
348                 global $wp_roles;
349
350                 if ( ! isset( $wp_roles ) )
351                         $wp_roles = new WP_Roles();
352
353                 $this->capabilities[$cap] = $grant;
354                 $wp_roles->add_cap( $this->name, $cap, $grant );
355         }
356
357         /**
358          * Remove capability from role.
359          *
360          * This is a container for {@link WP_Roles::remove_cap()} to remove the
361          * capability from the role. That is to say, that {@link
362          * WP_Roles::remove_cap()} implements the functionality, but it also makes
363          * sense to use this class, because you don't need to enter the role name.
364          *
365          * @since 2.0.0
366          * @access public
367          *
368          * @param string $cap Capability name.
369          */
370         public function remove_cap( $cap ) {
371                 global $wp_roles;
372
373                 if ( ! isset( $wp_roles ) )
374                         $wp_roles = new WP_Roles();
375
376                 unset( $this->capabilities[$cap] );
377                 $wp_roles->remove_cap( $this->name, $cap );
378         }
379
380         /**
381          * Whether role has capability.
382          *
383          * The capabilities is passed through the 'role_has_cap' filter. The first
384          * parameter for the hook is the list of capabilities the class has
385          * assigned. The second parameter is the capability name to look for. The
386          * third and final parameter for the hook is the role name.
387          *
388          * @since 2.0.0
389          * @access public
390          *
391          * @param string $cap Capability name.
392          * @return bool True, if user has capability. False, if doesn't have capability.
393          */
394         public function has_cap( $cap ) {
395                 /**
396                  * Filter which capabilities a role has.
397                  *
398                  * @since 2.0.0
399                  *
400                  * @param array  $capabilities Array of role capabilities.
401                  * @param string $cap          Capability name.
402                  * @param string $name         Role name.
403                  */
404                 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
405                 if ( !empty( $capabilities[$cap] ) )
406                         return $capabilities[$cap];
407                 else
408                         return false;
409         }
410
411 }
412
413 /**
414  * WordPress User class.
415  *
416  * @since 2.0.0
417  * @package WordPress
418  * @subpackage User
419  */
420 class WP_User {
421         /**
422          * User data container.
423          *
424          * @since 2.0.0
425          * @access private
426          * @var array
427          */
428         var $data;
429
430         /**
431          * The user's ID.
432          *
433          * @since 2.1.0
434          * @access public
435          * @var int
436          */
437         public $ID = 0;
438
439         /**
440          * The individual capabilities the user has been given.
441          *
442          * @since 2.0.0
443          * @access public
444          * @var array
445          */
446         public $caps = array();
447
448         /**
449          * User metadata option name.
450          *
451          * @since 2.0.0
452          * @access public
453          * @var string
454          */
455         public $cap_key;
456
457         /**
458          * The roles the user is part of.
459          *
460          * @since 2.0.0
461          * @access public
462          * @var array
463          */
464         public $roles = array();
465
466         /**
467          * All capabilities the user has, including individual and role based.
468          *
469          * @since 2.0.0
470          * @access public
471          * @var array
472          */
473         public $allcaps = array();
474
475         /**
476          * The filter context applied to user data fields.
477          *
478          * @since 2.9.0
479          * @access private
480          * @var string
481          */
482         var $filter = null;
483
484         private static $back_compat_keys;
485
486         /**
487          * Constructor
488          *
489          * Retrieves the userdata and passes it to {@link WP_User::init()}.
490          *
491          * @since 2.0.0
492          * @access public
493          *
494          * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
495          * @param string $name Optional. User's username
496          * @param int $blog_id Optional Blog ID, defaults to current blog.
497          * @return WP_User
498          */
499         public function __construct( $id = 0, $name = '', $blog_id = '' ) {
500                 if ( ! isset( self::$back_compat_keys ) ) {
501                         $prefix = $GLOBALS['wpdb']->prefix;
502                         self::$back_compat_keys = array(
503                                 'user_firstname' => 'first_name',
504                                 'user_lastname' => 'last_name',
505                                 'user_description' => 'description',
506                                 'user_level' => $prefix . 'user_level',
507                                 $prefix . 'usersettings' => $prefix . 'user-settings',
508                                 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
509                         );
510                 }
511
512                 if ( is_a( $id, 'WP_User' ) ) {
513                         $this->init( $id->data, $blog_id );
514                         return;
515                 } elseif ( is_object( $id ) ) {
516                         $this->init( $id, $blog_id );
517                         return;
518                 }
519
520                 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
521                         $name = $id;
522                         $id = 0;
523                 }
524
525                 if ( $id )
526                         $data = self::get_data_by( 'id', $id );
527                 else
528                         $data = self::get_data_by( 'login', $name );
529
530                 if ( $data )
531                         $this->init( $data, $blog_id );
532         }
533
534         /**
535          * Sets up object properties, including capabilities.
536          *
537          * @param object $data User DB row object
538          * @param int $blog_id Optional. The blog id to initialize for
539          */
540         public function init( $data, $blog_id = '' ) {
541                 $this->data = $data;
542                 $this->ID = (int) $data->ID;
543
544                 $this->for_blog( $blog_id );
545         }
546
547         /**
548          * Return only the main user fields
549          *
550          * @since 3.3.0
551          *
552          * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
553          * @param string|int $value The field value
554          * @return object Raw user object
555          */
556         public static function get_data_by( $field, $value ) {
557                 global $wpdb;
558
559                 if ( 'id' == $field ) {
560                         // Make sure the value is numeric to avoid casting objects, for example,
561                         // to int 1.
562                         if ( ! is_numeric( $value ) )
563                                 return false;
564                         $value = intval( $value );
565                         if ( $value < 1 )
566                                 return false;
567                 } else {
568                         $value = trim( $value );
569                 }
570
571                 if ( !$value )
572                         return false;
573
574                 switch ( $field ) {
575                         case 'id':
576                                 $user_id = $value;
577                                 $db_field = 'ID';
578                                 break;
579                         case 'slug':
580                                 $user_id = wp_cache_get($value, 'userslugs');
581                                 $db_field = 'user_nicename';
582                                 break;
583                         case 'email':
584                                 $user_id = wp_cache_get($value, 'useremail');
585                                 $db_field = 'user_email';
586                                 break;
587                         case 'login':
588                                 $value = sanitize_user( $value );
589                                 $user_id = wp_cache_get($value, 'userlogins');
590                                 $db_field = 'user_login';
591                                 break;
592                         default:
593                                 return false;
594                 }
595
596                 if ( false !== $user_id ) {
597                         if ( $user = wp_cache_get( $user_id, 'users' ) )
598                                 return $user;
599                 }
600
601                 if ( !$user = $wpdb->get_row( $wpdb->prepare(
602                         "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
603                 ) ) )
604                         return false;
605
606                 update_user_caches( $user );
607
608                 return $user;
609         }
610
611         /**
612          * Magic method for checking the existence of a certain custom field
613          *
614          * @since 3.3.0
615          */
616         public function __isset( $key ) {
617                 if ( 'id' == $key ) {
618                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
619                         $key = 'ID';
620                 }
621
622                 if ( isset( $this->data->$key ) )
623                         return true;
624
625                 if ( isset( self::$back_compat_keys[ $key ] ) )
626                         $key = self::$back_compat_keys[ $key ];
627
628                 return metadata_exists( 'user', $this->ID, $key );
629         }
630
631         /**
632          * Magic method for accessing custom fields
633          *
634          * @since 3.3.0
635          */
636         public function __get( $key ) {
637                 if ( 'id' == $key ) {
638                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
639                         return $this->ID;
640                 }
641
642                 if ( isset( $this->data->$key ) ) {
643                         $value = $this->data->$key;
644                 } else {
645                         if ( isset( self::$back_compat_keys[ $key ] ) )
646                                 $key = self::$back_compat_keys[ $key ];
647                         $value = get_user_meta( $this->ID, $key, true );
648                 }
649
650                 if ( $this->filter ) {
651                         $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
652                 }
653
654                 return $value;
655         }
656
657         /**
658          * Magic method for setting custom fields
659          *
660          * @since 3.3.0
661          */
662         public function __set( $key, $value ) {
663                 if ( 'id' == $key ) {
664                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
665                         $this->ID = $value;
666                         return;
667                 }
668
669                 $this->data->$key = $value;
670         }
671
672         /**
673          * Determine whether the user exists in the database.
674          *
675          * @since 3.4.0
676          * @access public
677          *
678          * @return bool True if user exists in the database, false if not.
679          */
680         public function exists() {
681                 return ! empty( $this->ID );
682         }
683
684         /**
685          * Retrieve the value of a property or meta key.
686          *
687          * Retrieves from the users and usermeta table.
688          *
689          * @since 3.3.0
690          *
691          * @param string $key Property
692          */
693         public function get( $key ) {
694                 return $this->__get( $key );
695         }
696
697         /**
698          * Determine whether a property or meta key is set
699          *
700          * Consults the users and usermeta tables.
701          *
702          * @since 3.3.0
703          *
704          * @param string $key Property
705          */
706         public function has_prop( $key ) {
707                 return $this->__isset( $key );
708         }
709
710         /**
711          * Return an array representation.
712          *
713          * @since 3.5.0
714          *
715          * @return array Array representation.
716          */
717         public function to_array() {
718                 return get_object_vars( $this->data );
719         }
720
721         /**
722          * Set up capability object properties.
723          *
724          * Will set the value for the 'cap_key' property to current database table
725          * prefix, followed by 'capabilities'. Will then check to see if the
726          * property matching the 'cap_key' exists and is an array. If so, it will be
727          * used.
728          *
729          * @access protected
730          * @since 2.1.0
731          *
732          * @param string $cap_key Optional capability key
733          */
734         function _init_caps( $cap_key = '' ) {
735                 global $wpdb;
736
737                 if ( empty($cap_key) )
738                         $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
739                 else
740                         $this->cap_key = $cap_key;
741
742                 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
743
744                 if ( ! is_array( $this->caps ) )
745                         $this->caps = array();
746
747                 $this->get_role_caps();
748         }
749
750         /**
751          * Retrieve all of the role capabilities and merge with individual capabilities.
752          *
753          * All of the capabilities of the roles the user belongs to are merged with
754          * the users individual roles. This also means that the user can be denied
755          * specific roles that their role might have, but the specific user isn't
756          * granted permission to.
757          *
758          * @since 2.0.0
759          * @uses $wp_roles
760          * @access public
761          *
762          * @return array List of all capabilities for the user.
763          */
764         public function get_role_caps() {
765                 global $wp_roles;
766
767                 if ( ! isset( $wp_roles ) )
768                         $wp_roles = new WP_Roles();
769
770                 //Filter out caps that are not role names and assign to $this->roles
771                 if ( is_array( $this->caps ) )
772                         $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
773
774                 //Build $allcaps from role caps, overlay user's $caps
775                 $this->allcaps = array();
776                 foreach ( (array) $this->roles as $role ) {
777                         $the_role = $wp_roles->get_role( $role );
778                         $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
779                 }
780                 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
781
782                 return $this->allcaps;
783         }
784
785         /**
786          * Add role to user.
787          *
788          * Updates the user's meta data option with capabilities and roles.
789          *
790          * @since 2.0.0
791          * @access public
792          *
793          * @param string $role Role name.
794          */
795         public function add_role( $role ) {
796                 $this->caps[$role] = true;
797                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
798                 $this->get_role_caps();
799                 $this->update_user_level_from_caps();
800         }
801
802         /**
803          * Remove role from user.
804          *
805          * @since 2.0.0
806          * @access public
807          *
808          * @param string $role Role name.
809          */
810         public function remove_role( $role ) {
811                 if ( !in_array($role, $this->roles) )
812                         return;
813                 unset( $this->caps[$role] );
814                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
815                 $this->get_role_caps();
816                 $this->update_user_level_from_caps();
817         }
818
819         /**
820          * Set the role of the user.
821          *
822          * This will remove the previous roles of the user and assign the user the
823          * new one. You can set the role to an empty string and it will remove all
824          * of the roles from the user.
825          *
826          * @since 2.0.0
827          * @access public
828          *
829          * @param string $role Role name.
830          */
831         public function set_role( $role ) {
832                 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
833                         return;
834
835                 foreach ( (array) $this->roles as $oldrole )
836                         unset( $this->caps[$oldrole] );
837
838                 $old_roles = $this->roles;
839                 if ( !empty( $role ) ) {
840                         $this->caps[$role] = true;
841                         $this->roles = array( $role => true );
842                 } else {
843                         $this->roles = false;
844                 }
845                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
846                 $this->get_role_caps();
847                 $this->update_user_level_from_caps();
848
849                 /**
850                  * Fires after the user's role has changed.
851                  *
852                  * @since 2.9.0
853                  * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
854                  *
855                  * @param int    $user_id   The user ID.
856                  * @param string $role      The new role.
857                  * @param array  $old_roles An array of the user's previous roles.
858                  */
859                 do_action( 'set_user_role', $this->ID, $role, $old_roles );
860         }
861
862         /**
863          * Choose the maximum level the user has.
864          *
865          * Will compare the level from the $item parameter against the $max
866          * parameter. If the item is incorrect, then just the $max parameter value
867          * will be returned.
868          *
869          * Used to get the max level based on the capabilities the user has. This
870          * is also based on roles, so if the user is assigned the Administrator role
871          * then the capability 'level_10' will exist and the user will get that
872          * value.
873          *
874          * @since 2.0.0
875          * @access public
876          *
877          * @param int $max Max level of user.
878          * @param string $item Level capability name.
879          * @return int Max Level.
880          */
881         public function level_reduction( $max, $item ) {
882                 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
883                         $level = intval( $matches[1] );
884                         return max( $max, $level );
885                 } else {
886                         return $max;
887                 }
888         }
889
890         /**
891          * Update the maximum user level for the user.
892          *
893          * Updates the 'user_level' user metadata (includes prefix that is the
894          * database table prefix) with the maximum user level. Gets the value from
895          * the all of the capabilities that the user has.
896          *
897          * @since 2.0.0
898          * @access public
899          */
900         public function update_user_level_from_caps() {
901                 global $wpdb;
902                 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
903                 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
904         }
905
906         /**
907          * Add capability and grant or deny access to capability.
908          *
909          * @since 2.0.0
910          * @access public
911          *
912          * @param string $cap Capability name.
913          * @param bool $grant Whether to grant capability to user.
914          */
915         public function add_cap( $cap, $grant = true ) {
916                 $this->caps[$cap] = $grant;
917                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
918         }
919
920         /**
921          * Remove capability from user.
922          *
923          * @since 2.0.0
924          * @access public
925          *
926          * @param string $cap Capability name.
927          */
928         public function remove_cap( $cap ) {
929                 if ( ! isset( $this->caps[$cap] ) )
930                         return;
931                 unset( $this->caps[$cap] );
932                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
933         }
934
935         /**
936          * Remove all of the capabilities of the user.
937          *
938          * @since 2.1.0
939          * @access public
940          */
941         public function remove_all_caps() {
942                 global $wpdb;
943                 $this->caps = array();
944                 delete_user_meta( $this->ID, $this->cap_key );
945                 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
946                 $this->get_role_caps();
947         }
948
949         /**
950          * Whether user has capability or role name.
951          *
952          * This is useful for looking up whether the user has a specific role
953          * assigned to the user. The second optional parameter can also be used to
954          * check for capabilities against a specific object, such as a post or user.
955          *
956          * @since 2.0.0
957          * @access public
958          *
959          * @param string|int $cap Capability or role name to search.
960          * @return bool True, if user has capability; false, if user does not have capability.
961          */
962         public function has_cap( $cap ) {
963                 if ( is_numeric( $cap ) ) {
964                         _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
965                         $cap = $this->translate_level_to_cap( $cap );
966                 }
967
968                 $args = array_slice( func_get_args(), 1 );
969                 $args = array_merge( array( $cap, $this->ID ), $args );
970                 $caps = call_user_func_array( 'map_meta_cap', $args );
971
972                 // Multisite super admin has all caps by definition, Unless specifically denied.
973                 if ( is_multisite() && is_super_admin( $this->ID ) ) {
974                         if ( in_array('do_not_allow', $caps) )
975                                 return false;
976                         return true;
977                 }
978
979                 /**
980                  * Dynamically filter a user's capabilities.
981                  *
982                  * @since 2.0.0
983                  * @since 3.7.0 Added the user object.
984                  *
985                  * @param array   $allcaps An array of all the role's capabilities.
986                  * @param array   $caps    Actual capabilities for meta capability.
987                  * @param array   $args    Optional parameters passed to has_cap(), typically object ID.
988                  * @param WP_User $user    The user object.
989                  */
990                 // Must have ALL requested caps
991                 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
992                 $capabilities['exist'] = true; // Everyone is allowed to exist
993                 foreach ( (array) $caps as $cap ) {
994                         if ( empty( $capabilities[ $cap ] ) )
995                                 return false;
996                 }
997
998                 return true;
999         }
1000
1001         /**
1002          * Convert numeric level to level capability name.
1003          *
1004          * Prepends 'level_' to level number.
1005          *
1006          * @since 2.0.0
1007          * @access public
1008          *
1009          * @param int $level Level number, 1 to 10.
1010          * @return string
1011          */
1012         public function translate_level_to_cap( $level ) {
1013                 return 'level_' . $level;
1014         }
1015
1016         /**
1017          * Set the blog to operate on. Defaults to the current blog.
1018          *
1019          * @since 3.0.0
1020          *
1021          * @param int $blog_id Optional Blog ID, defaults to current blog.
1022          */
1023         public function for_blog( $blog_id = '' ) {
1024                 global $wpdb;
1025                 if ( ! empty( $blog_id ) )
1026                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1027                 else
1028                         $cap_key = '';
1029                 $this->_init_caps( $cap_key );
1030         }
1031 }
1032
1033 /**
1034  * Map meta capabilities to primitive capabilities.
1035  *
1036  * This does not actually compare whether the user ID has the actual capability,
1037  * just what the capability or capabilities are. Meta capability list value can
1038  * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1039  * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1040  *
1041  * @since 2.0.0
1042  *
1043  * @param string $cap Capability name.
1044  * @param int $user_id User ID.
1045  * @return array Actual capabilities for meta capability.
1046  */
1047 function map_meta_cap( $cap, $user_id ) {
1048         $args = array_slice( func_get_args(), 2 );
1049         $caps = array();
1050
1051         switch ( $cap ) {
1052         case 'remove_user':
1053                 $caps[] = 'remove_users';
1054                 break;
1055         case 'promote_user':
1056                 $caps[] = 'promote_users';
1057                 break;
1058         case 'edit_user':
1059         case 'edit_users':
1060                 // Allow user to edit itself
1061                 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1062                         break;
1063
1064                 // If multisite these caps are allowed only for super admins.
1065                 if ( is_multisite() && !is_super_admin( $user_id ) )
1066                         $caps[] = 'do_not_allow';
1067                 else
1068                         $caps[] = 'edit_users'; // edit_user maps to edit_users.
1069                 break;
1070         case 'delete_post':
1071         case 'delete_page':
1072                 $post = get_post( $args[0] );
1073
1074                 if ( 'revision' == $post->post_type ) {
1075                         $post = get_post( $post->post_parent );
1076                 }
1077
1078                 $post_type = get_post_type_object( $post->post_type );
1079
1080                 if ( ! $post_type->map_meta_cap ) {
1081                         $caps[] = $post_type->cap->$cap;
1082                         // Prior to 3.1 we would re-call map_meta_cap here.
1083                         if ( 'delete_post' == $cap )
1084                                 $cap = $post_type->cap->$cap;
1085                         break;
1086                 }
1087
1088                 // If the post author is set and the user is the author...
1089                 if ( $post->post_author && $user_id == $post->post_author ) {
1090                         // If the post is published...
1091                         if ( 'publish' == $post->post_status ) {
1092                                 $caps[] = $post_type->cap->delete_published_posts;
1093                         } elseif ( 'trash' == $post->post_status ) {
1094                                 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1095                                         $caps[] = $post_type->cap->delete_published_posts;
1096                                 }
1097                         } else {
1098                                 // If the post is draft...
1099                                 $caps[] = $post_type->cap->delete_posts;
1100                         }
1101                 } else {
1102                         // The user is trying to edit someone else's post.
1103                         $caps[] = $post_type->cap->delete_others_posts;
1104                         // The post is published, extra cap required.
1105                         if ( 'publish' == $post->post_status ) {
1106                                 $caps[] = $post_type->cap->delete_published_posts;
1107                         } elseif ( 'private' == $post->post_status ) {
1108                                 $caps[] = $post_type->cap->delete_private_posts;
1109                         }
1110                 }
1111                 break;
1112                 // edit_post breaks down to edit_posts, edit_published_posts, or
1113                 // edit_others_posts
1114         case 'edit_post':
1115         case 'edit_page':
1116                 $post = get_post( $args[0] );
1117                 if ( empty( $post ) )
1118                         break;
1119
1120                 if ( 'revision' == $post->post_type ) {
1121                         $post = get_post( $post->post_parent );
1122                 }
1123
1124                 $post_type = get_post_type_object( $post->post_type );
1125
1126                 if ( ! $post_type->map_meta_cap ) {
1127                         $caps[] = $post_type->cap->$cap;
1128                         // Prior to 3.1 we would re-call map_meta_cap here.
1129                         if ( 'edit_post' == $cap )
1130                                 $cap = $post_type->cap->$cap;
1131                         break;
1132                 }
1133
1134                 // If the post author is set and the user is the author...
1135                 if ( $post->post_author && $user_id == $post->post_author ) {
1136                         // If the post is published...
1137                         if ( 'publish' == $post->post_status ) {
1138                                 $caps[] = $post_type->cap->edit_published_posts;
1139                         } elseif ( 'trash' == $post->post_status ) {
1140                                 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1141                                         $caps[] = $post_type->cap->edit_published_posts;
1142                                 }
1143                         } else {
1144                                 // If the post is draft...
1145                                 $caps[] = $post_type->cap->edit_posts;
1146                         }
1147                 } else {
1148                         // The user is trying to edit someone else's post.
1149                         $caps[] = $post_type->cap->edit_others_posts;
1150                         // The post is published, extra cap required.
1151                         if ( 'publish' == $post->post_status ) {
1152                                 $caps[] = $post_type->cap->edit_published_posts;
1153                         } elseif ( 'private' == $post->post_status ) {
1154                                 $caps[] = $post_type->cap->edit_private_posts;
1155                         }
1156                 }
1157                 break;
1158         case 'read_post':
1159         case 'read_page':
1160                 $post = get_post( $args[0] );
1161
1162                 if ( 'revision' == $post->post_type ) {
1163                         $post = get_post( $post->post_parent );
1164                 }
1165
1166                 $post_type = get_post_type_object( $post->post_type );
1167
1168                 if ( ! $post_type->map_meta_cap ) {
1169                         $caps[] = $post_type->cap->$cap;
1170                         // Prior to 3.1 we would re-call map_meta_cap here.
1171                         if ( 'read_post' == $cap )
1172                                 $cap = $post_type->cap->$cap;
1173                         break;
1174                 }
1175
1176                 $status_obj = get_post_status_object( $post->post_status );
1177                 if ( $status_obj->public ) {
1178                         $caps[] = $post_type->cap->read;
1179                         break;
1180                 }
1181
1182                 if ( $post->post_author && $user_id == $post->post_author ) {
1183                         $caps[] = $post_type->cap->read;
1184                 } elseif ( $status_obj->private ) {
1185                         $caps[] = $post_type->cap->read_private_posts;
1186                 } else {
1187                         $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1188                 }
1189                 break;
1190         case 'publish_post':
1191                 $post = get_post( $args[0] );
1192                 $post_type = get_post_type_object( $post->post_type );
1193
1194                 $caps[] = $post_type->cap->publish_posts;
1195                 break;
1196         case 'edit_post_meta':
1197         case 'delete_post_meta':
1198         case 'add_post_meta':
1199                 $post = get_post( $args[0] );
1200                 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1201
1202                 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1203
1204                 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1205                         /**
1206                          * Filter whether the user is allowed to add post meta to a post.
1207                          *
1208                          * The dynamic portion of the hook name, $meta_key, refers to the
1209                          * meta key passed to map_meta_cap().
1210                          *
1211                          * @since 3.3.0
1212                          *
1213                          * @param bool   $allowed  Whether the user can add the post meta. Default false.
1214                          * @param string $meta_key The meta key.
1215                          * @param int    $post_id  Post ID.
1216                          * @param int    $user_id  User ID.
1217                          * @param string $cap      Capability name.
1218                          * @param array  $caps     User capabilities.
1219                          */
1220                         $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1221                         if ( ! $allowed )
1222                                 $caps[] = $cap;
1223                 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1224                         $caps[] = $cap;
1225                 }
1226                 break;
1227         case 'edit_comment':
1228                 $comment = get_comment( $args[0] );
1229                 if ( empty( $comment ) )
1230                         break;
1231                 $post = get_post( $comment->comment_post_ID );
1232                 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1233                 break;
1234         case 'unfiltered_upload':
1235                 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
1236                         $caps[] = $cap;
1237                 else
1238                         $caps[] = 'do_not_allow';
1239                 break;
1240         case 'unfiltered_html' :
1241                 // Disallow unfiltered_html for all users, even admins and super admins.
1242                 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1243                         $caps[] = 'do_not_allow';
1244                 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1245                         $caps[] = 'do_not_allow';
1246                 else
1247                         $caps[] = $cap;
1248                 break;
1249         case 'edit_files':
1250         case 'edit_plugins':
1251         case 'edit_themes':
1252                 // Disallow the file editors.
1253                 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1254                         $caps[] = 'do_not_allow';
1255                 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1256                         $caps[] = 'do_not_allow';
1257                 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1258                         $caps[] = 'do_not_allow';
1259                 else
1260                         $caps[] = $cap;
1261                 break;
1262         case 'update_plugins':
1263         case 'delete_plugins':
1264         case 'install_plugins':
1265         case 'upload_plugins':
1266         case 'update_themes':
1267         case 'delete_themes':
1268         case 'install_themes':
1269         case 'upload_themes':
1270         case 'update_core':
1271                 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1272                 // Files in uploads are excepted.
1273                 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
1274                         $caps[] = 'do_not_allow';
1275                 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
1276                         $caps[] = 'do_not_allow';
1277                 } elseif ( 'upload_themes' === $cap ) {
1278                         $caps[] = 'install_themes';
1279                 } elseif ( 'upload_plugins' === $cap ) {
1280                         $caps[] = 'install_plugins';
1281                 } else {
1282                         $caps[] = $cap;
1283                 }
1284                 break;
1285         case 'activate_plugins':
1286                 $caps[] = $cap;
1287                 if ( is_multisite() ) {
1288                         // update_, install_, and delete_ are handled above with is_super_admin().
1289                         $menu_perms = get_site_option( 'menu_items', array() );
1290                         if ( empty( $menu_perms['plugins'] ) )
1291                                 $caps[] = 'manage_network_plugins';
1292                 }
1293                 break;
1294         case 'delete_user':
1295         case 'delete_users':
1296                 // If multisite only super admins can delete users.
1297                 if ( is_multisite() && ! is_super_admin( $user_id ) )
1298                         $caps[] = 'do_not_allow';
1299                 else
1300                         $caps[] = 'delete_users'; // delete_user maps to delete_users.
1301                 break;
1302         case 'create_users':
1303                 if ( !is_multisite() )
1304                         $caps[] = $cap;
1305                 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1306                         $caps[] = $cap;
1307                 else
1308                         $caps[] = 'do_not_allow';
1309                 break;
1310         case 'manage_links' :
1311                 if ( get_option( 'link_manager_enabled' ) )
1312                         $caps[] = $cap;
1313                 else
1314                         $caps[] = 'do_not_allow';
1315                 break;
1316         case 'customize' :
1317                 $caps[] = 'edit_theme_options';
1318                 break;
1319         default:
1320                 // Handle meta capabilities for custom post types.
1321                 $post_type_meta_caps = _post_type_meta_capabilities();
1322                 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1323                         $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1324                         return call_user_func_array( 'map_meta_cap', $args );
1325                 }
1326
1327                 // If no meta caps match, return the original cap.
1328                 $caps[] = $cap;
1329         }
1330
1331         /**
1332          * Filter a user's capabilities depending on specific context and/or privilege.
1333          *
1334          * @since 2.8.0
1335          *
1336          * @param array  $caps    Returns the user's actual capabilities.
1337          * @param string $cap     Capability name.
1338          * @param int    $user_id The user ID.
1339          * @param array  $args    Adds the context to the cap. Typically the object ID.
1340          */
1341         return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1342 }
1343
1344 /**
1345  * Whether current user has capability or role.
1346  *
1347  * @since 2.0.0
1348  *
1349  * @param string $capability Capability or role name.
1350  * @return bool
1351  */
1352 function current_user_can( $capability ) {
1353         $current_user = wp_get_current_user();
1354
1355         if ( empty( $current_user ) )
1356                 return false;
1357
1358         $args = array_slice( func_get_args(), 1 );
1359         $args = array_merge( array( $capability ), $args );
1360
1361         return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1362 }
1363
1364 /**
1365  * Whether current user has a capability or role for a given blog.
1366  *
1367  * @since 3.0.0
1368  *
1369  * @param int $blog_id Blog ID
1370  * @param string $capability Capability or role name.
1371  * @return bool
1372  */
1373 function current_user_can_for_blog( $blog_id, $capability ) {
1374         if ( is_multisite() )
1375                 switch_to_blog( $blog_id );
1376
1377         $current_user = wp_get_current_user();
1378
1379         if ( empty( $current_user ) )
1380                 return false;
1381
1382         $args = array_slice( func_get_args(), 2 );
1383         $args = array_merge( array( $capability ), $args );
1384
1385         $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1386
1387         if ( is_multisite() )
1388                 restore_current_blog();
1389
1390         return $can;
1391 }
1392
1393 /**
1394  * Whether author of supplied post has capability or role.
1395  *
1396  * @since 2.9.0
1397  *
1398  * @param int|object $post Post ID or post object.
1399  * @param string $capability Capability or role name.
1400  * @return bool
1401  */
1402 function author_can( $post, $capability ) {
1403         if ( !$post = get_post($post) )
1404                 return false;
1405
1406         $author = get_userdata( $post->post_author );
1407
1408         if ( ! $author )
1409                 return false;
1410
1411         $args = array_slice( func_get_args(), 2 );
1412         $args = array_merge( array( $capability ), $args );
1413
1414         return call_user_func_array( array( $author, 'has_cap' ), $args );
1415 }
1416
1417 /**
1418  * Whether a particular user has capability or role.
1419  *
1420  * @since 3.1.0
1421  *
1422  * @param int|object $user User ID or object.
1423  * @param string $capability Capability or role name.
1424  * @return bool
1425  */
1426 function user_can( $user, $capability ) {
1427         if ( ! is_object( $user ) )
1428                 $user = get_userdata( $user );
1429
1430         if ( ! $user || ! $user->exists() )
1431                 return false;
1432
1433         $args = array_slice( func_get_args(), 2 );
1434         $args = array_merge( array( $capability ), $args );
1435
1436         return call_user_func_array( array( $user, 'has_cap' ), $args );
1437 }
1438
1439 /**
1440  * Retrieve role object.
1441  *
1442  * @see WP_Roles::get_role() Uses method to retrieve role object.
1443  * @since 2.0.0
1444  *
1445  * @param string $role Role name.
1446  * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1447  */
1448 function get_role( $role ) {
1449         global $wp_roles;
1450
1451         if ( ! isset( $wp_roles ) )
1452                 $wp_roles = new WP_Roles();
1453
1454         return $wp_roles->get_role( $role );
1455 }
1456
1457 /**
1458  * Add role, if it does not exist.
1459  *
1460  * @see WP_Roles::add_role() Uses method to add role.
1461  * @since 2.0.0
1462  *
1463  * @param string $role Role name.
1464  * @param string $display_name Display name for role.
1465  * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1466  * @return WP_Role|null WP_Role object if role is added, null if already exists.
1467  */
1468 function add_role( $role, $display_name, $capabilities = array() ) {
1469         global $wp_roles;
1470
1471         if ( ! isset( $wp_roles ) )
1472                 $wp_roles = new WP_Roles();
1473
1474         return $wp_roles->add_role( $role, $display_name, $capabilities );
1475 }
1476
1477 /**
1478  * Remove role, if it exists.
1479  *
1480  * @see WP_Roles::remove_role() Uses method to remove role.
1481  * @since 2.0.0
1482  *
1483  * @param string $role Role name.
1484  */
1485 function remove_role( $role ) {
1486         global $wp_roles;
1487
1488         if ( ! isset( $wp_roles ) )
1489                 $wp_roles = new WP_Roles();
1490
1491         $wp_roles->remove_role( $role );
1492 }
1493
1494 /**
1495  * Retrieve a list of super admins.
1496  *
1497  * @since 3.0.0
1498  *
1499  * @uses $super_admins Super admins global variable, if set.
1500  *
1501  * @return array List of super admin logins
1502  */
1503 function get_super_admins() {
1504         global $super_admins;
1505
1506         if ( isset($super_admins) )
1507                 return $super_admins;
1508         else
1509                 return get_site_option( 'site_admins', array('admin') );
1510 }
1511
1512 /**
1513  * Determine if user is a site admin.
1514  *
1515  * @since 3.0.0
1516  *
1517  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1518  * @return bool True if the user is a site admin.
1519  */
1520 function is_super_admin( $user_id = false ) {
1521         if ( ! $user_id || $user_id == get_current_user_id() )
1522                 $user = wp_get_current_user();
1523         else
1524                 $user = get_userdata( $user_id );
1525
1526         if ( ! $user || ! $user->exists() )
1527                 return false;
1528
1529         if ( is_multisite() ) {
1530                 $super_admins = get_super_admins();
1531                 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1532                         return true;
1533         } else {
1534                 if ( $user->has_cap('delete_users') )
1535                         return true;
1536         }
1537
1538         return false;
1539 }