]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/capabilities.php
WordPress 3.5.1
[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 null|WP_Role 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 object|null Null, if role does not exist. WP_Role object, if found.
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 = absint( $value );
539                 } else {
540                         $value = trim( $value );
541                 }
542
543                 if ( !$value )
544                         return false;
545
546                 switch ( $field ) {
547                         case 'id':
548                                 $user_id = $value;
549                                 $db_field = 'ID';
550                                 break;
551                         case 'slug':
552                                 $user_id = wp_cache_get($value, 'userslugs');
553                                 $db_field = 'user_nicename';
554                                 break;
555                         case 'email':
556                                 $user_id = wp_cache_get($value, 'useremail');
557                                 $db_field = 'user_email';
558                                 break;
559                         case 'login':
560                                 $value = sanitize_user( $value );
561                                 $user_id = wp_cache_get($value, 'userlogins');
562                                 $db_field = 'user_login';
563                                 break;
564                         default:
565                                 return false;
566                 }
567
568                 if ( false !== $user_id ) {
569                         if ( $user = wp_cache_get( $user_id, 'users' ) )
570                                 return $user;
571                 }
572
573                 if ( !$user = $wpdb->get_row( $wpdb->prepare(
574                         "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
575                 ) ) )
576                         return false;
577
578                 update_user_caches( $user );
579
580                 return $user;
581         }
582
583         /**
584          * Magic method for checking the existence of a certain custom field
585          *
586          * @since 3.3.0
587          */
588         function __isset( $key ) {
589                 if ( 'id' == $key ) {
590                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
591                         $key = 'ID';
592                 }
593
594                 if ( isset( $this->data->$key ) )
595                         return true;
596
597                 if ( isset( self::$back_compat_keys[ $key ] ) )
598                         $key = self::$back_compat_keys[ $key ];
599
600                 return metadata_exists( 'user', $this->ID, $key );
601         }
602
603         /**
604          * Magic method for accessing custom fields
605          *
606          * @since 3.3.0
607          */
608         function __get( $key ) {
609                 if ( 'id' == $key ) {
610                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
611                         return $this->ID;
612                 }
613
614                 if ( isset( $this->data->$key ) ) {
615                         $value = $this->data->$key;
616                 } else {
617                         if ( isset( self::$back_compat_keys[ $key ] ) )
618                                 $key = self::$back_compat_keys[ $key ];
619                         $value = get_user_meta( $this->ID, $key, true );
620                 }
621
622                 if ( $this->filter ) {
623                         $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
624                 }
625
626                 return $value;
627         }
628
629         /**
630          * Magic method for setting custom fields
631          *
632          * @since 3.3.0
633          */
634         function __set( $key, $value ) {
635                 if ( 'id' == $key ) {
636                         _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
637                         $this->ID = $value;
638                         return;
639                 }
640
641                 $this->data->$key = $value;
642         }
643
644         /**
645          * Determine whether the user exists in the database.
646          *
647          * @since 3.4.0
648          * @access public
649          *
650          * @return bool True if user exists in the database, false if not.
651          */
652         function exists() {
653                 return ! empty( $this->ID );
654         }
655
656         /**
657          * Retrieve the value of a property or meta key.
658          *
659          * Retrieves from the users and usermeta table.
660          *
661          * @since 3.3.0
662          *
663          * @param string $key Property
664          */
665         function get( $key ) {
666                 return $this->__get( $key );
667         }
668
669         /**
670          * Determine whether a property or meta key is set
671          *
672          * Consults the users and usermeta tables.
673          *
674          * @since 3.3.0
675          *
676          * @param string $key Property
677          */
678         function has_prop( $key ) {
679                 return $this->__isset( $key );
680         }
681
682         /*
683          * Return an array representation.
684          *
685          * @since 3.5.0
686          *
687          * @return array Array representation.
688          */
689         function to_array() {
690                 return get_object_vars( $this->data );
691         }
692
693         /**
694          * Set up capability object properties.
695          *
696          * Will set the value for the 'cap_key' property to current database table
697          * prefix, followed by 'capabilities'. Will then check to see if the
698          * property matching the 'cap_key' exists and is an array. If so, it will be
699          * used.
700          *
701          * @access protected
702          * @since 2.1.0
703          *
704          * @param string $cap_key Optional capability key
705          */
706         function _init_caps( $cap_key = '' ) {
707                 global $wpdb;
708
709                 if ( empty($cap_key) )
710                         $this->cap_key = $wpdb->prefix . 'capabilities';
711                 else
712                         $this->cap_key = $cap_key;
713
714                 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
715
716                 if ( ! is_array( $this->caps ) )
717                         $this->caps = array();
718
719                 $this->get_role_caps();
720         }
721
722         /**
723          * Retrieve all of the role capabilities and merge with individual capabilities.
724          *
725          * All of the capabilities of the roles the user belongs to are merged with
726          * the users individual roles. This also means that the user can be denied
727          * specific roles that their role might have, but the specific user isn't
728          * granted permission to.
729          *
730          * @since 2.0.0
731          * @uses $wp_roles
732          * @access public
733          */
734         function get_role_caps() {
735                 global $wp_roles;
736
737                 if ( ! isset( $wp_roles ) )
738                         $wp_roles = new WP_Roles();
739
740                 //Filter out caps that are not role names and assign to $this->roles
741                 if ( is_array( $this->caps ) )
742                         $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
743
744                 //Build $allcaps from role caps, overlay user's $caps
745                 $this->allcaps = array();
746                 foreach ( (array) $this->roles as $role ) {
747                         $the_role = $wp_roles->get_role( $role );
748                         $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
749                 }
750                 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
751         }
752
753         /**
754          * Add role to user.
755          *
756          * Updates the user's meta data option with capabilities and roles.
757          *
758          * @since 2.0.0
759          * @access public
760          *
761          * @param string $role Role name.
762          */
763         function add_role( $role ) {
764                 $this->caps[$role] = true;
765                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
766                 $this->get_role_caps();
767                 $this->update_user_level_from_caps();
768         }
769
770         /**
771          * Remove role from user.
772          *
773          * @since 2.0.0
774          * @access public
775          *
776          * @param string $role Role name.
777          */
778         function remove_role( $role ) {
779                 if ( !in_array($role, $this->roles) )
780                         return;
781                 unset( $this->caps[$role] );
782                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
783                 $this->get_role_caps();
784                 $this->update_user_level_from_caps();
785         }
786
787         /**
788          * Set the role of the user.
789          *
790          * This will remove the previous roles of the user and assign the user the
791          * new one. You can set the role to an empty string and it will remove all
792          * of the roles from the user.
793          *
794          * @since 2.0.0
795          * @access public
796          *
797          * @param string $role Role name.
798          */
799         function set_role( $role ) {
800                 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
801                         return;
802
803                 foreach ( (array) $this->roles as $oldrole )
804                         unset( $this->caps[$oldrole] );
805
806                 if ( !empty( $role ) ) {
807                         $this->caps[$role] = true;
808                         $this->roles = array( $role => true );
809                 } else {
810                         $this->roles = false;
811                 }
812                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
813                 $this->get_role_caps();
814                 $this->update_user_level_from_caps();
815                 do_action( 'set_user_role', $this->ID, $role );
816         }
817
818         /**
819          * Choose the maximum level the user has.
820          *
821          * Will compare the level from the $item parameter against the $max
822          * parameter. If the item is incorrect, then just the $max parameter value
823          * will be returned.
824          *
825          * Used to get the max level based on the capabilities the user has. This
826          * is also based on roles, so if the user is assigned the Administrator role
827          * then the capability 'level_10' will exist and the user will get that
828          * value.
829          *
830          * @since 2.0.0
831          * @access public
832          *
833          * @param int $max Max level of user.
834          * @param string $item Level capability name.
835          * @return int Max Level.
836          */
837         function level_reduction( $max, $item ) {
838                 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
839                         $level = intval( $matches[1] );
840                         return max( $max, $level );
841                 } else {
842                         return $max;
843                 }
844         }
845
846         /**
847          * Update the maximum user level for the user.
848          *
849          * Updates the 'user_level' user metadata (includes prefix that is the
850          * database table prefix) with the maximum user level. Gets the value from
851          * the all of the capabilities that the user has.
852          *
853          * @since 2.0.0
854          * @access public
855          */
856         function update_user_level_from_caps() {
857                 global $wpdb;
858                 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
859                 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
860         }
861
862         /**
863          * Add capability and grant or deny access to capability.
864          *
865          * @since 2.0.0
866          * @access public
867          *
868          * @param string $cap Capability name.
869          * @param bool $grant Whether to grant capability to user.
870          */
871         function add_cap( $cap, $grant = true ) {
872                 $this->caps[$cap] = $grant;
873                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
874         }
875
876         /**
877          * Remove capability from user.
878          *
879          * @since 2.0.0
880          * @access public
881          *
882          * @param string $cap Capability name.
883          */
884         function remove_cap( $cap ) {
885                 if ( ! isset( $this->caps[$cap] ) )
886                         return;
887                 unset( $this->caps[$cap] );
888                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
889         }
890
891         /**
892          * Remove all of the capabilities of the user.
893          *
894          * @since 2.1.0
895          * @access public
896          */
897         function remove_all_caps() {
898                 global $wpdb;
899                 $this->caps = array();
900                 delete_user_meta( $this->ID, $this->cap_key );
901                 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
902                 $this->get_role_caps();
903         }
904
905         /**
906          * Whether user has capability or role name.
907          *
908          * This is useful for looking up whether the user has a specific role
909          * assigned to the user. The second optional parameter can also be used to
910          * check for capabilities against a specific object, such as a post or user.
911          *
912          * @since 2.0.0
913          * @access public
914          *
915          * @param string|int $cap Capability or role name to search.
916          * @return bool True, if user has capability; false, if user does not have capability.
917          */
918         function has_cap( $cap ) {
919                 if ( is_numeric( $cap ) ) {
920                         _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
921                         $cap = $this->translate_level_to_cap( $cap );
922                 }
923
924                 $args = array_slice( func_get_args(), 1 );
925                 $args = array_merge( array( $cap, $this->ID ), $args );
926                 $caps = call_user_func_array( 'map_meta_cap', $args );
927
928                 // Multisite super admin has all caps by definition, Unless specifically denied.
929                 if ( is_multisite() && is_super_admin( $this->ID ) ) {
930                         if ( in_array('do_not_allow', $caps) )
931                                 return false;
932                         return true;
933                 }
934
935                 // Must have ALL requested caps
936                 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
937                 $capabilities['exist'] = true; // Everyone is allowed to exist
938                 foreach ( (array) $caps as $cap ) {
939                         if ( empty( $capabilities[ $cap ] ) )
940                                 return false;
941                 }
942
943                 return true;
944         }
945
946         /**
947          * Convert numeric level to level capability name.
948          *
949          * Prepends 'level_' to level number.
950          *
951          * @since 2.0.0
952          * @access public
953          *
954          * @param int $level Level number, 1 to 10.
955          * @return string
956          */
957         function translate_level_to_cap( $level ) {
958                 return 'level_' . $level;
959         }
960
961         /**
962          * Set the blog to operate on. Defaults to the current blog.
963          *
964          * @since 3.0.0
965          *
966          * @param int $blog_id Optional Blog ID, defaults to current blog.
967          */
968         function for_blog( $blog_id = '' ) {
969                 global $wpdb;
970                 if ( ! empty( $blog_id ) )
971                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
972                 else
973                         $cap_key = '';
974                 $this->_init_caps( $cap_key );
975         }
976 }
977
978 /**
979  * Map meta capabilities to primitive capabilities.
980  *
981  * This does not actually compare whether the user ID has the actual capability,
982  * just what the capability or capabilities are. Meta capability list value can
983  * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
984  * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
985  *
986  * @since 2.0.0
987  *
988  * @param string $cap Capability name.
989  * @param int $user_id User ID.
990  * @return array Actual capabilities for meta capability.
991  */
992 function map_meta_cap( $cap, $user_id ) {
993         $args = array_slice( func_get_args(), 2 );
994         $caps = array();
995
996         switch ( $cap ) {
997         case 'remove_user':
998                 $caps[] = 'remove_users';
999                 break;
1000         case 'promote_user':
1001                 $caps[] = 'promote_users';
1002                 break;
1003         case 'edit_user':
1004         case 'edit_users':
1005                 // Allow user to edit itself
1006                 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1007                         break;
1008
1009                 // If multisite these caps are allowed only for super admins.
1010                 if ( is_multisite() && !is_super_admin( $user_id ) )
1011                         $caps[] = 'do_not_allow';
1012                 else
1013                         $caps[] = 'edit_users'; // edit_user maps to edit_users.
1014                 break;
1015         case 'delete_post':
1016         case 'delete_page':
1017                 $post = get_post( $args[0] );
1018
1019                 if ( 'revision' == $post->post_type ) {
1020                         $post = get_post( $post->post_parent );
1021                 }
1022
1023                 $post_type = get_post_type_object( $post->post_type );
1024
1025                 if ( ! $post_type->map_meta_cap ) {
1026                         $caps[] = $post_type->cap->$cap;
1027                         // Prior to 3.1 we would re-call map_meta_cap here.
1028                         if ( 'delete_post' == $cap )
1029                                 $cap = $post_type->cap->$cap;
1030                         break;
1031                 }
1032
1033                 $post_author_id = $post->post_author;
1034
1035                 // If no author set yet, default to current user for cap checks.
1036                 if ( ! $post_author_id )
1037                         $post_author_id = $user_id;
1038
1039                 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1040
1041                 // If the user is the author...
1042                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
1043                         // If the post is published...
1044                         if ( 'publish' == $post->post_status ) {
1045                                 $caps[] = $post_type->cap->delete_published_posts;
1046                         } elseif ( 'trash' == $post->post_status ) {
1047                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1048                                         $caps[] = $post_type->cap->delete_published_posts;
1049                         } else {
1050                                 // If the post is draft...
1051                                 $caps[] = $post_type->cap->delete_posts;
1052                         }
1053                 } else {
1054                         // The user is trying to edit someone else's post.
1055                         $caps[] = $post_type->cap->delete_others_posts;
1056                         // The post is published, extra cap required.
1057                         if ( 'publish' == $post->post_status )
1058                                 $caps[] = $post_type->cap->delete_published_posts;
1059                         elseif ( 'private' == $post->post_status )
1060                                 $caps[] = $post_type->cap->delete_private_posts;
1061                 }
1062                 break;
1063                 // edit_post breaks down to edit_posts, edit_published_posts, or
1064                 // edit_others_posts
1065         case 'edit_post':
1066         case 'edit_page':
1067                 $post = get_post( $args[0] );
1068
1069                 if ( 'revision' == $post->post_type ) {
1070                         $post = get_post( $post->post_parent );
1071                 }
1072
1073                 $post_type = get_post_type_object( $post->post_type );
1074
1075                 if ( ! $post_type->map_meta_cap ) {
1076                         $caps[] = $post_type->cap->$cap;
1077                         // Prior to 3.1 we would re-call map_meta_cap here.
1078                         if ( 'edit_post' == $cap )
1079                                 $cap = $post_type->cap->$cap;
1080                         break;
1081                 }
1082
1083                 $post_author_id = $post->post_author;
1084
1085                 // If no author set yet, default to current user for cap checks.
1086                 if ( ! $post_author_id )
1087                         $post_author_id = $user_id;
1088
1089                 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1090
1091                 // If the user is the author...
1092                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
1093                         // If the post is published...
1094                         if ( 'publish' == $post->post_status ) {
1095                                 $caps[] = $post_type->cap->edit_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->edit_published_posts;
1099                         } else {
1100                                 // If the post is draft...
1101                                 $caps[] = $post_type->cap->edit_posts;
1102                         }
1103                 } else {
1104                         // The user is trying to edit someone else's post.
1105                         $caps[] = $post_type->cap->edit_others_posts;
1106                         // The post is published, extra cap required.
1107                         if ( 'publish' == $post->post_status )
1108                                 $caps[] = $post_type->cap->edit_published_posts;
1109                         elseif ( 'private' == $post->post_status )
1110                                 $caps[] = $post_type->cap->edit_private_posts;
1111                 }
1112                 break;
1113         case 'read_post':
1114         case 'read_page':
1115                 $post = get_post( $args[0] );
1116
1117                 if ( 'revision' == $post->post_type ) {
1118                         $post = get_post( $post->post_parent );
1119                 }
1120
1121                 $post_type = get_post_type_object( $post->post_type );
1122
1123                 if ( ! $post_type->map_meta_cap ) {
1124                         $caps[] = $post_type->cap->$cap;
1125                         // Prior to 3.1 we would re-call map_meta_cap here.
1126                         if ( 'read_post' == $cap )
1127                                 $cap = $post_type->cap->$cap;
1128                         break;
1129                 }
1130
1131                 $status_obj = get_post_status_object( $post->post_status );
1132                 if ( $status_obj->public ) {
1133                         $caps[] = $post_type->cap->read;
1134                         break;
1135                 }
1136
1137                 $post_author_id = $post->post_author;
1138
1139                 // If no author set yet, default to current user for cap checks.
1140                 if ( ! $post_author_id )
1141                         $post_author_id = $user_id;
1142
1143                 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1144
1145                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
1146                         $caps[] = $post_type->cap->read;
1147                 elseif ( $status_obj->private )
1148                         $caps[] = $post_type->cap->read_private_posts;
1149                 else
1150                         $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1151                 break;
1152         case 'publish_post':
1153                 $post = get_post( $args[0] );
1154                 $post_type = get_post_type_object( $post->post_type );
1155
1156                 $caps[] = $post_type->cap->publish_posts;
1157                 break;
1158         case 'edit_post_meta':
1159         case 'delete_post_meta':
1160         case 'add_post_meta':
1161                 $post = get_post( $args[0] );
1162                 $post_type_object = get_post_type_object( $post->post_type );
1163                 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
1164
1165                 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1166
1167                 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1168                         $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1169                         if ( ! $allowed )
1170                                 $caps[] = $cap;
1171                 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1172                         $caps[] = $cap;
1173                 }
1174                 break;
1175         case 'edit_comment':
1176                 $comment = get_comment( $args[0] );
1177                 $post = get_post( $comment->comment_post_ID );
1178                 $post_type_object = get_post_type_object( $post->post_type );
1179
1180                 $caps = map_meta_cap( $post_type_object->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 object
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 null|WP_Role 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 }