]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/capabilities.php
Wordpress 3.1-scripts
[autoinstalls/wordpress.git] / wp-includes / capabilities.php
1 <?php
2 /**
3  * WordPress Roles and Capabilities.
4  *
5  * @package WordPress
6  * @subpackage User
7  */
8
9 /**
10  * WordPress User Roles.
11  *
12  * The role option is simple, the structure is organized by role name that store
13  * the name in value of the 'name' key. The capabilities are stored as an array
14  * in the value of the 'capability' key.
15  *
16  * <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          * PHP4 Constructor - Call {@link WP_Roles::_init()} method.
77          *
78          * @since 2.0.0
79          * @access public
80          *
81          * @return WP_Roles
82          */
83         function WP_Roles() {
84                 $this->_init();
85         }
86
87         /**
88          * Set up the object properties.
89          *
90          * The role key is set to the current prefix for the $wpdb object with
91          * 'user_roles' appended. If the $wp_user_roles global is set, then it will
92          * be used and the role option will not be updated or used.
93          *
94          * @since 2.1.0
95          * @access protected
96          * @uses $wpdb Used to get the database prefix.
97          * @global array $wp_user_roles Used to set the 'roles' property value.
98          */
99         function _init () {
100                 global $wpdb, $wp_user_roles;
101                 $this->role_key = $wpdb->prefix . 'user_roles';
102                 if ( ! empty( $wp_user_roles ) ) {
103                         $this->roles = $wp_user_roles;
104                         $this->use_db = false;
105                 } else {
106                         $this->roles = get_option( $this->role_key );
107                 }
108
109                 if ( empty( $this->roles ) )
110                         return;
111
112                 $this->role_objects = array();
113                 $this->role_names =  array();
114                 foreach ( (array) $this->roles as $role => $data ) {
115                         $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
116                         $this->role_names[$role] = $this->roles[$role]['name'];
117                 }
118         }
119
120         /**
121          * Add role name with capabilities to list.
122          *
123          * Updates the list of roles, if the role doesn't already exist.
124          *
125          * The capabilities are defined in the following format `array( 'read' => true );`
126          * To explicitly deny a role a capability you set the value for that capability to false.
127          *
128          * @since 2.0.0
129          * @access public
130          *
131          * @param string $role Role name.
132          * @param string $display_name Role display name.
133          * @param array $capabilities List of role capabilities in the above format.
134          * @return null|WP_Role WP_Role object if role is added, null if already exists.
135          */
136         function add_role( $role, $display_name, $capabilities = array() ) {
137                 if ( isset( $this->roles[$role] ) )
138                         return;
139
140                 $this->roles[$role] = array(
141                         'name' => $display_name,
142                         'capabilities' => $capabilities
143                         );
144                 if ( $this->use_db )
145                         update_option( $this->role_key, $this->roles );
146                 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
147                 $this->role_names[$role] = $display_name;
148                 return $this->role_objects[$role];
149         }
150
151         /**
152          * Remove role by name.
153          *
154          * @since 2.0.0
155          * @access public
156          *
157          * @param string $role Role name.
158          */
159         function remove_role( $role ) {
160                 if ( ! isset( $this->role_objects[$role] ) )
161                         return;
162
163                 unset( $this->role_objects[$role] );
164                 unset( $this->role_names[$role] );
165                 unset( $this->roles[$role] );
166
167                 if ( $this->use_db )
168                         update_option( $this->role_key, $this->roles );
169         }
170
171         /**
172          * Add capability to role.
173          *
174          * @since 2.0.0
175          * @access public
176          *
177          * @param string $role Role name.
178          * @param string $cap Capability name.
179          * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
180          */
181         function add_cap( $role, $cap, $grant = true ) {
182                 $this->roles[$role]['capabilities'][$cap] = $grant;
183                 if ( $this->use_db )
184                         update_option( $this->role_key, $this->roles );
185         }
186
187         /**
188          * Remove capability from role.
189          *
190          * @since 2.0.0
191          * @access public
192          *
193          * @param string $role Role name.
194          * @param string $cap Capability name.
195          */
196         function remove_cap( $role, $cap ) {
197                 unset( $this->roles[$role]['capabilities'][$cap] );
198                 if ( $this->use_db )
199                         update_option( $this->role_key, $this->roles );
200         }
201
202         /**
203          * Retrieve role object by name.
204          *
205          * @since 2.0.0
206          * @access public
207          *
208          * @param string $role Role name.
209          * @return object|null Null, if role does not exist. WP_Role object, if found.
210          */
211         function &get_role( $role ) {
212                 if ( isset( $this->role_objects[$role] ) )
213                         return $this->role_objects[$role];
214                 else
215                         return null;
216         }
217
218         /**
219          * Retrieve list of role names.
220          *
221          * @since 2.0.0
222          * @access public
223          *
224          * @return array List of role names.
225          */
226         function get_names() {
227                 return $this->role_names;
228         }
229
230         /**
231          * Whether role name is currently in the list of available roles.
232          *
233          * @since 2.0.0
234          * @access public
235          *
236          * @param string $role Role name to look up.
237          * @return bool
238          */
239         function is_role( $role )
240         {
241                 return isset( $this->role_names[$role] );
242         }
243 }
244
245 /**
246  * WordPress Role class.
247  *
248  * @since 2.0.0
249  * @package WordPress
250  * @subpackage User
251  */
252 class WP_Role {
253         /**
254          * Role name.
255          *
256          * @since 2.0.0
257          * @access public
258          * @var string
259          */
260         var $name;
261
262         /**
263          * List of capabilities the role contains.
264          *
265          * @since 2.0.0
266          * @access public
267          * @var array
268          */
269         var $capabilities;
270
271         /**
272          * PHP4 Constructor - Set up object properties.
273          *
274          * The list of capabilities, must have the key as the name of the capability
275          * and the value a boolean of whether it is granted to the role.
276          *
277          * @since 2.0.0
278          * @access public
279          *
280          * @param string $role Role name.
281          * @param array $capabilities List of capabilities.
282          * @return WP_Role
283          */
284         function WP_Role( $role, $capabilities ) {
285                 $this->name = $role;
286                 $this->capabilities = $capabilities;
287         }
288
289         /**
290          * Assign role a capability.
291          *
292          * @see WP_Roles::add_cap() Method uses implementation for role.
293          * @since 2.0.0
294          * @access public
295          *
296          * @param string $cap Capability name.
297          * @param bool $grant Whether role has capability privilege.
298          */
299         function add_cap( $cap, $grant = true ) {
300                 global $wp_roles;
301
302                 if ( ! isset( $wp_roles ) )
303                         $wp_roles = new WP_Roles();
304
305                 $this->capabilities[$cap] = $grant;
306                 $wp_roles->add_cap( $this->name, $cap, $grant );
307         }
308
309         /**
310          * Remove capability from role.
311          *
312          * This is a container for {@link WP_Roles::remove_cap()} to remove the
313          * capability from the role. That is to say, that {@link
314          * WP_Roles::remove_cap()} implements the functionality, but it also makes
315          * sense to use this class, because you don't need to enter the role name.
316          *
317          * @since 2.0.0
318          * @access public
319          *
320          * @param string $cap Capability name.
321          */
322         function remove_cap( $cap ) {
323                 global $wp_roles;
324
325                 if ( ! isset( $wp_roles ) )
326                         $wp_roles = new WP_Roles();
327
328                 unset( $this->capabilities[$cap] );
329                 $wp_roles->remove_cap( $this->name, $cap );
330         }
331
332         /**
333          * Whether role has capability.
334          *
335          * The capabilities is passed through the 'role_has_cap' filter. The first
336          * parameter for the hook is the list of capabilities the class has
337          * assigned. The second parameter is the capability name to look for. The
338          * third and final parameter for the hook is the role name.
339          *
340          * @since 2.0.0
341          * @access public
342          *
343          * @param string $cap Capability name.
344          * @return bool True, if user has capability. False, if doesn't have capability.
345          */
346         function has_cap( $cap ) {
347                 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
348                 if ( !empty( $capabilities[$cap] ) )
349                         return $capabilities[$cap];
350                 else
351                         return false;
352         }
353
354 }
355
356 /**
357  * WordPress User class.
358  *
359  * @since 2.0.0
360  * @package WordPress
361  * @subpackage User
362  */
363 class WP_User {
364         /**
365          * User data container.
366          *
367          * This will be set as properties of the object.
368          *
369          * @since 2.0.0
370          * @access private
371          * @var array
372          */
373         var $data;
374
375         /**
376          * The user's ID.
377          *
378          * @since 2.1.0
379          * @access public
380          * @var int
381          */
382         var $ID = 0;
383
384         /**
385          * The deprecated user's ID.
386          *
387          * @since 2.0.0
388          * @access public
389          * @deprecated Use WP_User::$ID
390          * @see WP_User::$ID
391          * @var int
392          */
393         var $id = 0;
394
395         /**
396          * The individual capabilities the user has been given.
397          *
398          * @since 2.0.0
399          * @access public
400          * @var array
401          */
402         var $caps = array();
403
404         /**
405          * User metadata option name.
406          *
407          * @since 2.0.0
408          * @access public
409          * @var string
410          */
411         var $cap_key;
412
413         /**
414          * The roles the user is part of.
415          *
416          * @since 2.0.0
417          * @access public
418          * @var array
419          */
420         var $roles = array();
421
422         /**
423          * All capabilities the user has, including individual and role based.
424          *
425          * @since 2.0.0
426          * @access public
427          * @var array
428          */
429         var $allcaps = array();
430
431         /**
432          * First name of the user.
433          *
434          * Created to prevent notices.
435          *
436          * @since 2.7.0
437          * @access public
438          * @var string
439          */
440         var $first_name = '';
441
442         /**
443          * Last name of the user.
444          *
445          * Created to prevent notices.
446          *
447          * @since 2.7.0
448          * @access public
449          * @var string
450          */
451         var $last_name = '';
452
453         /**
454          * The filter context applied to user data fields.
455          *
456          * @since 2.9.0
457          * @access private
458          * @var string
459          */
460         var $filter = null;
461
462         /**
463          * PHP4 Constructor - Sets up the object properties.
464          *
465          * Retrieves the userdata and then assigns all of the data keys to direct
466          * properties of the object. Calls {@link WP_User::_init_caps()} after
467          * setting up the object's user data properties.
468          *
469          * @since 2.0.0
470          * @access public
471          *
472          * @param int|string $id User's ID or username
473          * @param int $name Optional. User's username
474          * @param int $blog_id Optional Blog ID, defaults to current blog.
475          * @return WP_User
476          */
477         function WP_User( $id, $name = '', $blog_id = '' ) {
478
479                 if ( empty( $id ) && empty( $name ) )
480                         return;
481
482                 if ( ! is_numeric( $id ) ) {
483                         $name = $id;
484                         $id = 0;
485                 }
486
487                 if ( ! empty( $id ) )
488                         $this->data = get_userdata( $id );
489                 else
490                         $this->data = get_userdatabylogin( $name );
491
492                 if ( empty( $this->data->ID ) )
493                         return;
494
495                 foreach ( get_object_vars( $this->data ) as $key => $value ) {
496                         $this->{$key} = $value;
497                 }
498
499                 $this->id = $this->ID;
500                 $this->for_blog( $blog_id );
501         }
502
503         /**
504          * Set up capability object properties.
505          *
506          * Will set the value for the 'cap_key' property to current database table
507          * prefix, followed by 'capabilities'. Will then check to see if the
508          * property matching the 'cap_key' exists and is an array. If so, it will be
509          * used.
510          *
511          * @since 2.1.0
512          *
513          * @param string $cap_key Optional capability key
514          * @access protected
515          */
516         function _init_caps( $cap_key = '' ) {
517                 global $wpdb;
518                 if ( empty($cap_key) )
519                         $this->cap_key = $wpdb->prefix . 'capabilities';
520                 else
521                         $this->cap_key = $cap_key;
522                 $this->caps = &$this->{$this->cap_key};
523                 if ( ! is_array( $this->caps ) )
524                         $this->caps = array();
525                 $this->get_role_caps();
526         }
527
528         /**
529          * Retrieve all of the role capabilities and merge with individual capabilities.
530          *
531          * All of the capabilities of the roles the user belongs to are merged with
532          * the users individual roles. This also means that the user can be denied
533          * specific roles that their role might have, but the specific user isn't
534          * granted permission to.
535          *
536          * @since 2.0.0
537          * @uses $wp_roles
538          * @access public
539          */
540         function get_role_caps() {
541                 global $wp_roles;
542
543                 if ( ! isset( $wp_roles ) )
544                         $wp_roles = new WP_Roles();
545
546                 //Filter out caps that are not role names and assign to $this->roles
547                 if ( is_array( $this->caps ) )
548                         $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
549
550                 //Build $allcaps from role caps, overlay user's $caps
551                 $this->allcaps = array();
552                 foreach ( (array) $this->roles as $role ) {
553                         $the_role =& $wp_roles->get_role( $role );
554                         $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
555                 }
556                 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
557         }
558
559         /**
560          * Add role to user.
561          *
562          * Updates the user's meta data option with capabilities and roles.
563          *
564          * @since 2.0.0
565          * @access public
566          *
567          * @param string $role Role name.
568          */
569         function add_role( $role ) {
570                 $this->caps[$role] = true;
571                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
572                 $this->get_role_caps();
573                 $this->update_user_level_from_caps();
574         }
575
576         /**
577          * Remove role from user.
578          *
579          * @since 2.0.0
580          * @access public
581          *
582          * @param string $role Role name.
583          */
584         function remove_role( $role ) {
585                 if ( !in_array($role, $this->roles) )
586                         return;
587                 unset( $this->caps[$role] );
588                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
589                 $this->get_role_caps();
590                 $this->update_user_level_from_caps();
591         }
592
593         /**
594          * Set the role of the user.
595          *
596          * This will remove the previous roles of the user and assign the user the
597          * new one. You can set the role to an empty string and it will remove all
598          * of the roles from the user.
599          *
600          * @since 2.0.0
601          * @access public
602          *
603          * @param string $role Role name.
604          */
605         function set_role( $role ) {
606                 foreach ( (array) $this->roles as $oldrole )
607                         unset( $this->caps[$oldrole] );
608
609                 if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
610                         return;
611
612                 if ( !empty( $role ) ) {
613                         $this->caps[$role] = true;
614                         $this->roles = array( $role => true );
615                 } else {
616                         $this->roles = false;
617                 }
618                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
619                 $this->get_role_caps();
620                 $this->update_user_level_from_caps();
621                 do_action( 'set_user_role', $this->ID, $role );
622         }
623
624         /**
625          * Choose the maximum level the user has.
626          *
627          * Will compare the level from the $item parameter against the $max
628          * parameter. If the item is incorrect, then just the $max parameter value
629          * will be returned.
630          *
631          * Used to get the max level based on the capabilities the user has. This
632          * is also based on roles, so if the user is assigned the Administrator role
633          * then the capability 'level_10' will exist and the user will get that
634          * value.
635          *
636          * @since 2.0.0
637          * @access public
638          *
639          * @param int $max Max level of user.
640          * @param string $item Level capability name.
641          * @return int Max Level.
642          */
643         function level_reduction( $max, $item ) {
644                 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
645                         $level = intval( $matches[1] );
646                         return max( $max, $level );
647                 } else {
648                         return $max;
649                 }
650         }
651
652         /**
653          * Update the maximum user level for the user.
654          *
655          * Updates the 'user_level' user metadata (includes prefix that is the
656          * database table prefix) with the maximum user level. Gets the value from
657          * the all of the capabilities that the user has.
658          *
659          * @since 2.0.0
660          * @access public
661          */
662         function update_user_level_from_caps() {
663                 global $wpdb;
664                 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
665                 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
666         }
667
668         /**
669          * Add capability and grant or deny access to capability.
670          *
671          * @since 2.0.0
672          * @access public
673          *
674          * @param string $cap Capability name.
675          * @param bool $grant Whether to grant capability to user.
676          */
677         function add_cap( $cap, $grant = true ) {
678                 $this->caps[$cap] = $grant;
679                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
680         }
681
682         /**
683          * Remove capability from user.
684          *
685          * @since 2.0.0
686          * @access public
687          *
688          * @param string $cap Capability name.
689          */
690         function remove_cap( $cap ) {
691                 if ( empty( $this->caps[$cap] ) )
692                         return;
693                 unset( $this->caps[$cap] );
694                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
695         }
696
697         /**
698          * Remove all of the capabilities of the user.
699          *
700          * @since 2.1.0
701          * @access public
702          */
703         function remove_all_caps() {
704                 global $wpdb;
705                 $this->caps = array();
706                 delete_user_meta( $this->ID, $this->cap_key );
707                 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
708                 $this->get_role_caps();
709         }
710
711         /**
712          * Whether user has capability or role name.
713          *
714          * This is useful for looking up whether the user has a specific role
715          * assigned to the user. The second optional parameter can also be used to
716          * check for capabilities against a specfic post.
717          *
718          * @since 2.0.0
719          * @access public
720          *
721          * @param string|int $cap Capability or role name to search.
722          * @param int $post_id Optional. Post ID to check capability against specific post.
723          * @return bool True, if user has capability; false, if user does not have capability.
724          */
725         function has_cap( $cap ) {
726                 if ( is_numeric( $cap ) ) {
727                         _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
728                         $cap = $this->translate_level_to_cap( $cap );
729                 }
730
731                 $args = array_slice( func_get_args(), 1 );
732                 $args = array_merge( array( $cap, $this->ID ), $args );
733                 $caps = call_user_func_array( 'map_meta_cap', $args );
734
735                 // Multisite super admin has all caps by definition, Unless specifically denied.
736                 if ( is_multisite() && is_super_admin( $this->ID ) ) {
737                         if ( in_array('do_not_allow', $caps) )
738                                 return false;
739                         return true;
740                 }
741
742                 // Must have ALL requested caps
743                 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
744                 $capabilities['exist'] = true; // Everyone is allowed to exist
745                 foreach ( (array) $caps as $cap ) {
746                         //echo "Checking cap $cap<br />";
747                         if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
748                                 return false;
749                 }
750
751                 return true;
752         }
753
754         /**
755          * Convert numeric level to level capability name.
756          *
757          * Prepends 'level_' to level number.
758          *
759          * @since 2.0.0
760          * @access public
761          *
762          * @param int $level Level number, 1 to 10.
763          * @return string
764          */
765         function translate_level_to_cap( $level ) {
766                 return 'level_' . $level;
767         }
768
769         /**
770          * Set the blog to operate on. Defaults to the current blog.
771          *
772          * @since 3.0.0
773          *
774          * @param int $blog_id Optional Blog ID, defaults to current blog.
775          */
776         function for_blog( $blog_id = '' ) {
777                 global $wpdb;
778                 if ( ! empty( $blog_id ) )
779                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
780                 else
781                         $cap_key = '';
782                 $this->_init_caps( $cap_key );
783         }
784 }
785
786 /**
787  * Map meta capabilities to primitive capabilities.
788  *
789  * This does not actually compare whether the user ID has the actual capability,
790  * just what the capability or capabilities are. Meta capability list value can
791  * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
792  * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
793  *
794  * @since 2.0.0
795  *
796  * @param string $cap Capability name.
797  * @param int $user_id User ID.
798  * @return array Actual capabilities for meta capability.
799  */
800 function map_meta_cap( $cap, $user_id ) {
801         $args = array_slice( func_get_args(), 2 );
802         $caps = array();
803
804         switch ( $cap ) {
805         case 'remove_user':
806                 $caps[] = 'remove_users';
807                 break;
808         case 'promote_user':
809                 $caps[] = 'promote_users';
810                 break;
811         case 'edit_user':
812                 // Allow user to edit itself
813                 if ( isset( $args[0] ) && $user_id == $args[0] )
814                         break;
815                 // Fall through
816         case 'edit_users':
817                 // If multisite these caps are allowed only for super admins.
818                 if ( is_multisite() && !is_super_admin( $user_id ) )
819                         $caps[] = 'do_not_allow';
820                 else
821                         $caps[] = 'edit_users'; // Explicit due to primitive fall through
822                 break;
823         case 'delete_post':
824         case 'delete_page':
825                 $author_data = get_userdata( $user_id );
826                 $post = get_post( $args[0] );
827                 $post_type = get_post_type_object( $post->post_type );
828
829                 if ( ! $post_type->map_meta_cap ) {
830                         $caps[] = $post_type->cap->$cap;
831                         // Prior to 3.1 we would re-call map_meta_cap here.
832                         if ( 'delete_post' == $cap )
833                                 $cap = $post_type->cap->$cap;
834                         break;
835                 }
836
837                 if ( '' != $post->post_author ) {
838                         $post_author_data = get_userdata( $post->post_author );
839                 } else {
840                         // No author set yet, so default to current user for cap checks.
841                         $post_author_data = $author_data;
842                 }
843
844                 // If the user is the author...
845                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
846                         // If the post is published...
847                         if ( 'publish' == $post->post_status ) {
848                                 $caps[] = $post_type->cap->delete_published_posts;
849                         } elseif ( 'trash' == $post->post_status ) {
850                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
851                                         $caps[] = $post_type->cap->delete_published_posts;
852                         } else {
853                                 // If the post is draft...
854                                 $caps[] = $post_type->cap->delete_posts;
855                         }
856                 } else {
857                         // The user is trying to edit someone else's post.
858                         $caps[] = $post_type->cap->delete_others_posts;
859                         // The post is published, extra cap required.
860                         if ( 'publish' == $post->post_status )
861                                 $caps[] = $post_type->cap->delete_published_posts;
862                         elseif ( 'private' == $post->post_status )
863                                 $caps[] = $post_type->cap->delete_private_posts;
864                 }
865                 break;
866                 // edit_post breaks down to edit_posts, edit_published_posts, or
867                 // edit_others_posts
868         case 'edit_post':
869         case 'edit_page':
870                 $author_data = get_userdata( $user_id );
871                 $post = get_post( $args[0] );
872                 $post_type = get_post_type_object( $post->post_type );
873
874                 if ( ! $post_type->map_meta_cap ) {
875                         $caps[] = $post_type->cap->$cap;
876                         // Prior to 3.1 we would re-call map_meta_cap here.
877                         if ( 'edit_post' == $cap )
878                                 $cap = $post_type->cap->$cap;
879                         break;
880                 }
881
882                 if ( '' != $post->post_author ) {
883                         $post_author_data = get_userdata( $post->post_author );
884                 } else {
885                         // No author set yet, so default to current user for cap checks.
886                         $post_author_data = $author_data;
887                 }
888
889                 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
890                 // If the user is the author...
891                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
892                         // If the post is published...
893                         if ( 'publish' == $post->post_status ) {
894                                 $caps[] = $post_type->cap->edit_published_posts;
895                         } elseif ( 'trash' == $post->post_status ) {
896                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
897                                         $caps[] = $post_type->cap->edit_published_posts;
898                         } else {
899                                 // If the post is draft...
900                                 $caps[] = $post_type->cap->edit_posts;
901                         }
902                 } else {
903                         // The user is trying to edit someone else's post.
904                         $caps[] = $post_type->cap->edit_others_posts;
905                         // The post is published, extra cap required.
906                         if ( 'publish' == $post->post_status )
907                                 $caps[] = $post_type->cap->edit_published_posts;
908                         elseif ( 'private' == $post->post_status )
909                                 $caps[] = $post_type->cap->edit_private_posts;
910                 }
911                 break;
912         case 'read_post':
913         case 'read_page':
914                 $author_data = get_userdata( $user_id );
915                 $post = get_post( $args[0] );
916                 $post_type = get_post_type_object( $post->post_type );
917
918                 if ( ! $post_type->map_meta_cap ) {
919                         $caps[] = $post_type->cap->$cap;
920                         // Prior to 3.1 we would re-call map_meta_cap here.
921                         if ( 'read_post' == $cap )
922                                 $cap = $post_type->cap->$cap;
923                         break;
924                 }
925
926                 if ( 'private' != $post->post_status ) {
927                         $caps[] = $post_type->cap->read;
928                         break;
929                 }
930
931                 if ( '' != $post->post_author ) {
932                         $post_author_data = get_userdata( $post->post_author );
933                 } else {
934                         // No author set yet, so default to current user for cap checks.
935                         $post_author_data = $author_data;
936                 }
937
938                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
939                         $caps[] = $post_type->cap->read;
940                 else
941                         $caps[] = $post_type->cap->read_private_posts;
942                 break;
943         case 'edit_comment':
944                 $comment = get_comment( $args[0] );
945                 $post = get_post( $comment->comment_post_ID );
946                 $post_type_object = get_post_type_object( $post->post_type );
947
948                 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
949                 break;
950         case 'unfiltered_upload':
951                 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
952                         $caps[] = $cap;
953                 else
954                         $caps[] = 'do_not_allow';
955                 break;
956         case 'edit_files':
957         case 'edit_plugins':
958         case 'edit_themes':
959                 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
960                         $caps[] = 'do_not_allow';
961                         break;
962                 }
963                 // Fall through if not DISALLOW_FILE_EDIT.
964         case 'update_plugins':
965         case 'delete_plugins':
966         case 'install_plugins':
967         case 'update_themes':
968         case 'delete_themes':
969         case 'install_themes':
970         case 'update_core':
971                 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
972                 // Files in uploads are excepted.
973                 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
974                         $caps[] = 'do_not_allow';
975                         break;
976                 }
977                 // Fall through if not DISALLOW_FILE_MODS.
978         case 'unfiltered_html':
979                 // Disallow unfiltered_html for all users, even admins and super admins.
980                 if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
981                         $caps[] = 'do_not_allow';
982                         break;
983                 }
984                 // Fall through if not DISALLOW_UNFILTERED_HTML
985         case 'delete_user':
986         case 'delete_users':
987                 // If multisite these caps are allowed only for super admins.
988                 if ( is_multisite() && !is_super_admin( $user_id ) ) {
989                         $caps[] = 'do_not_allow';
990                 } else {
991                         if ( 'delete_user' == $cap )
992                                 $cap = 'delete_users';
993                         $caps[] = $cap;
994                 }
995                 break;
996         case 'create_users':
997                 if ( !is_multisite() )
998                         $caps[] = $cap;
999                 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1000                         $caps[] = $cap;
1001                 else
1002                         $caps[] = 'do_not_allow';
1003                 break;
1004         default:
1005                 // Handle meta capabilities for custom post types.
1006                 $post_type_meta_caps = _post_type_meta_capabilities();
1007                 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1008                         $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1009                         return call_user_func_array( 'map_meta_cap', $args );
1010                 }
1011
1012                 // If no meta caps match, return the original cap.
1013                 $caps[] = $cap;
1014         }
1015
1016         return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1017 }
1018
1019 /**
1020  * Whether current user has capability or role.
1021  *
1022  * @since 2.0.0
1023  *
1024  * @param string $capability Capability or role name.
1025  * @return bool
1026  */
1027 function current_user_can( $capability ) {
1028         $current_user = wp_get_current_user();
1029
1030         if ( empty( $current_user ) )
1031                 return false;
1032
1033         $args = array_slice( func_get_args(), 1 );
1034         $args = array_merge( array( $capability ), $args );
1035
1036         return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
1037 }
1038
1039 /**
1040  * Whether current user has a capability or role for a given blog.
1041  *
1042  * @since 3.0.0
1043  *
1044  * @param int $blog_id Blog ID
1045  * @param string $capability Capability or role name.
1046  * @return bool
1047  */
1048 function current_user_can_for_blog( $blog_id, $capability ) {
1049         $current_user = wp_get_current_user();
1050
1051         if ( empty( $current_user ) )
1052                 return false;
1053
1054         // Create new object to avoid stomping the global current_user.
1055         $user = new WP_User( $current_user->id) ;
1056
1057         // Set the blog id.  @todo add blog id arg to WP_User constructor?
1058         $user->for_blog( $blog_id );
1059
1060         $args = array_slice( func_get_args(), 2 );
1061         $args = array_merge( array( $capability ), $args );
1062
1063         return call_user_func_array( array( &$user, 'has_cap' ), $args );
1064 }
1065
1066 /**
1067  * Whether author of supplied post has capability or role.
1068  *
1069  * @since 2.9.0
1070  *
1071  * @param int|object $post Post ID or post object.
1072  * @param string $capability Capability or role name.
1073  * @return bool
1074  */
1075 function author_can( $post, $capability ) {
1076         if ( !$post = get_post($post) )
1077                 return false;
1078
1079         $author = new WP_User( $post->post_author );
1080
1081         if ( empty( $author->ID ) )
1082                 return false;
1083
1084         $args = array_slice( func_get_args(), 2 );
1085         $args = array_merge( array( $capability ), $args );
1086
1087         return call_user_func_array( array( &$author, 'has_cap' ), $args );
1088 }
1089
1090 /**
1091  * Whether a particular user has capability or role.
1092  *
1093  * @since 3.1.0
1094  *
1095  * @param int|object $user User ID or object.
1096  * @param string $capability Capability or role name.
1097  * @return bool
1098  */
1099 function user_can( $user, $capability ) {
1100         if ( ! is_object( $user ) )
1101                 $user = new WP_User( $user );
1102
1103         if ( ! $user || ! $user->ID )
1104                 return false;
1105
1106         $args = array_slice( func_get_args(), 2 );
1107         $args = array_merge( array( $capability ), $args );
1108
1109         return call_user_func_array( array( &$user, 'has_cap' ), $args );
1110 }
1111
1112 /**
1113  * Retrieve role object.
1114  *
1115  * @see WP_Roles::get_role() Uses method to retrieve role object.
1116  * @since 2.0.0
1117  *
1118  * @param string $role Role name.
1119  * @return object
1120  */
1121 function get_role( $role ) {
1122         global $wp_roles;
1123
1124         if ( ! isset( $wp_roles ) )
1125                 $wp_roles = new WP_Roles();
1126
1127         return $wp_roles->get_role( $role );
1128 }
1129
1130 /**
1131  * Add role, if it does not exist.
1132  *
1133  * @see WP_Roles::add_role() Uses method to add role.
1134  * @since 2.0.0
1135  *
1136  * @param string $role Role name.
1137  * @param string $display_name Display name for role.
1138  * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1139  * @return null|WP_Role WP_Role object if role is added, null if already exists.
1140  */
1141 function add_role( $role, $display_name, $capabilities = array() ) {
1142         global $wp_roles;
1143
1144         if ( ! isset( $wp_roles ) )
1145                 $wp_roles = new WP_Roles();
1146
1147         return $wp_roles->add_role( $role, $display_name, $capabilities );
1148 }
1149
1150 /**
1151  * Remove role, if it exists.
1152  *
1153  * @see WP_Roles::remove_role() Uses method to remove role.
1154  * @since 2.0.0
1155  *
1156  * @param string $role Role name.
1157  * @return null
1158  */
1159 function remove_role( $role ) {
1160         global $wp_roles;
1161
1162         if ( ! isset( $wp_roles ) )
1163                 $wp_roles = new WP_Roles();
1164
1165         return $wp_roles->remove_role( $role );
1166 }
1167
1168 /**
1169  * Retrieve a list of super admins.
1170  *
1171  * @since 3.0.0
1172  *
1173  * @uses $super_admins Super admins global variable, if set.
1174  *
1175  * @return array List of super admin logins
1176  */
1177 function get_super_admins() {
1178         global $super_admins;
1179
1180         if ( isset($super_admins) )
1181                 return $super_admins;
1182         else
1183                 return get_site_option( 'site_admins', array('admin') );
1184 }
1185
1186 /**
1187  * Determine if user is a site admin.
1188  *
1189  * @since 3.0.0
1190  *
1191  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1192  * @return bool True if the user is a site admin.
1193  */
1194 function is_super_admin( $user_id = false ) {
1195         if ( $user_id )
1196                 $user = new WP_User( $user_id );
1197         else
1198                 $user = wp_get_current_user();
1199
1200         if ( empty( $user->id ) )
1201                 return false;
1202
1203         if ( is_multisite() ) {
1204                 $super_admins = get_super_admins();
1205                 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1206                         return true;
1207         } else {
1208                 if ( $user->has_cap('delete_users') )
1209                         return true;
1210         }
1211
1212         return false;
1213 }
1214
1215 ?>