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