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