]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/capabilities.php
Wordpress 3.0.4-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          * @return WP_User
475          */
476         function WP_User( $id, $name = '' ) {
477
478                 if ( empty( $id ) && empty( $name ) )
479                         return;
480
481                 if ( ! is_numeric( $id ) ) {
482                         $name = $id;
483                         $id = 0;
484                 }
485
486                 if ( ! empty( $id ) )
487                         $this->data = get_userdata( $id );
488                 else
489                         $this->data = get_userdatabylogin( $name );
490
491                 if ( empty( $this->data->ID ) )
492                         return;
493
494                 foreach ( get_object_vars( $this->data ) as $key => $value ) {
495                         $this->{$key} = $value;
496                 }
497
498                 $this->id = $this->ID;
499                 $this->_init_caps();
500         }
501
502         /**
503          * Set up capability object properties.
504          *
505          * Will set the value for the 'cap_key' property to current database table
506          * prefix, followed by 'capabilities'. Will then check to see if the
507          * property matching the 'cap_key' exists and is an array. If so, it will be
508          * used.
509          *
510          * @since 2.1.0
511          *
512          * @param string $cap_key Optional capability key
513          * @access protected
514          */
515         function _init_caps( $cap_key = '' ) {
516                 global $wpdb;
517                 if ( empty($cap_key) )
518                         $this->cap_key = $wpdb->prefix . 'capabilities';
519                 else
520                         $this->cap_key = $cap_key;
521                 $this->caps = &$this->{$this->cap_key};
522                 if ( ! is_array( $this->caps ) )
523                         $this->caps = array();
524                 $this->get_role_caps();
525         }
526
527         /**
528          * Retrieve all of the role capabilities and merge with individual capabilities.
529          *
530          * All of the capabilities of the roles the user belongs to are merged with
531          * the users individual roles. This also means that the user can be denied
532          * specific roles that their role might have, but the specific user isn't
533          * granted permission to.
534          *
535          * @since 2.0.0
536          * @uses $wp_roles
537          * @access public
538          */
539         function get_role_caps() {
540                 global $wp_roles;
541
542                 if ( ! isset( $wp_roles ) )
543                         $wp_roles = new WP_Roles();
544
545                 //Filter out caps that are not role names and assign to $this->roles
546                 if ( is_array( $this->caps ) )
547                         $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
548
549                 //Build $allcaps from role caps, overlay user's $caps
550                 $this->allcaps = array();
551                 foreach ( (array) $this->roles as $role ) {
552                         $the_role =& $wp_roles->get_role( $role );
553                         $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
554                 }
555                 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
556         }
557
558         /**
559          * Add role to user.
560          *
561          * Updates the user's meta data option with capabilities and roles.
562          *
563          * @since 2.0.0
564          * @access public
565          *
566          * @param string $role Role name.
567          */
568         function add_role( $role ) {
569                 $this->caps[$role] = true;
570                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
571                 $this->get_role_caps();
572                 $this->update_user_level_from_caps();
573         }
574
575         /**
576          * Remove role from user.
577          *
578          * @since 2.0.0
579          * @access public
580          *
581          * @param string $role Role name.
582          */
583         function remove_role( $role ) {
584                 if ( !in_array($role, $this->roles) )
585                         return;
586                 unset( $this->caps[$role] );
587                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
588                 $this->get_role_caps();
589                 $this->update_user_level_from_caps();
590         }
591
592         /**
593          * Set the role of the user.
594          *
595          * This will remove the previous roles of the user and assign the user the
596          * new one. You can set the role to an empty string and it will remove all
597          * of the roles from the user.
598          *
599          * @since 2.0.0
600          * @access public
601          *
602          * @param string $role Role name.
603          */
604         function set_role( $role ) {
605                 foreach ( (array) $this->roles as $oldrole )
606                         unset( $this->caps[$oldrole] );
607                 if ( !empty( $role ) ) {
608                         $this->caps[$role] = true;
609                         $this->roles = array( $role => true );
610                 } else {
611                         $this->roles = false;
612                 }
613                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
614                 $this->get_role_caps();
615                 $this->update_user_level_from_caps();
616                 do_action( 'set_user_role', $this->ID, $role );
617         }
618
619         /**
620          * Choose the maximum level the user has.
621          *
622          * Will compare the level from the $item parameter against the $max
623          * parameter. If the item is incorrect, then just the $max parameter value
624          * will be returned.
625          *
626          * Used to get the max level based on the capabilities the user has. This
627          * is also based on roles, so if the user is assigned the Administrator role
628          * then the capability 'level_10' will exist and the user will get that
629          * value.
630          *
631          * @since 2.0.0
632          * @access public
633          *
634          * @param int $max Max level of user.
635          * @param string $item Level capability name.
636          * @return int Max Level.
637          */
638         function level_reduction( $max, $item ) {
639                 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
640                         $level = intval( $matches[1] );
641                         return max( $max, $level );
642                 } else {
643                         return $max;
644                 }
645         }
646
647         /**
648          * Update the maximum user level for the user.
649          *
650          * Updates the 'user_level' user metadata (includes prefix that is the
651          * database table prefix) with the maximum user level. Gets the value from
652          * the all of the capabilities that the user has.
653          *
654          * @since 2.0.0
655          * @access public
656          */
657         function update_user_level_from_caps() {
658                 global $wpdb;
659                 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
660                 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
661         }
662
663         /**
664          * Add capability and grant or deny access to capability.
665          *
666          * @since 2.0.0
667          * @access public
668          *
669          * @param string $cap Capability name.
670          * @param bool $grant Whether to grant capability to user.
671          */
672         function add_cap( $cap, $grant = true ) {
673                 $this->caps[$cap] = $grant;
674                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
675         }
676
677         /**
678          * Remove capability from user.
679          *
680          * @since 2.0.0
681          * @access public
682          *
683          * @param string $cap Capability name.
684          */
685         function remove_cap( $cap ) {
686                 if ( empty( $this->caps[$cap] ) )
687                         return;
688                 unset( $this->caps[$cap] );
689                 update_user_meta( $this->ID, $this->cap_key, $this->caps );
690         }
691
692         /**
693          * Remove all of the capabilities of the user.
694          *
695          * @since 2.1.0
696          * @access public
697          */
698         function remove_all_caps() {
699                 global $wpdb;
700                 $this->caps = array();
701                 delete_user_meta( $this->ID, $this->cap_key );
702                 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
703                 $this->get_role_caps();
704         }
705
706         /**
707          * Whether user has capability or role name.
708          *
709          * This is useful for looking up whether the user has a specific role
710          * assigned to the user. The second optional parameter can also be used to
711          * check for capabilities against a specfic post.
712          *
713          * @since 2.0.0
714          * @access public
715          *
716          * @param string|int $cap Capability or role name to search.
717          * @param int $post_id Optional. Post ID to check capability against specific post.
718          * @return bool True, if user has capability; false, if user does not have capability.
719          */
720         function has_cap( $cap ) {
721                 if ( is_numeric( $cap ) ) {
722                         _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
723                         $cap = $this->translate_level_to_cap( $cap );
724                 }
725
726                 $args = array_slice( func_get_args(), 1 );
727                 $args = array_merge( array( $cap, $this->ID ), $args );
728                 $caps = call_user_func_array( 'map_meta_cap', $args );
729
730                 // Multisite super admin has all caps by definition, Unless specifically denied.
731                 if ( is_multisite() && is_super_admin( $this->ID ) ) {
732                         if ( in_array('do_not_allow', $caps) )
733                                 return false;
734                         return true;
735                 }
736
737                 // Must have ALL requested caps
738                 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
739                 foreach ( (array) $caps as $cap ) {
740                         //echo "Checking cap $cap<br />";
741                         if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
742                                 return false;
743                 }
744
745                 return true;
746         }
747
748         /**
749          * Convert numeric level to level capability name.
750          *
751          * Prepends 'level_' to level number.
752          *
753          * @since 2.0.0
754          * @access public
755          *
756          * @param int $level Level number, 1 to 10.
757          * @return string
758          */
759         function translate_level_to_cap( $level ) {
760                 return 'level_' . $level;
761         }
762
763         /**
764          * Set the blog to operate on. Defaults to the current blog.
765          *
766          * @since 3.0.0
767          *
768          * @param int $blog_id Optional Blog ID, defaults to current blog.
769          */
770         function for_blog( $blog_id = '' ) {
771                 global $wpdb;
772                 if ( ! empty( $blog_id ) )
773                         $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
774                 else
775                         $cap_key = '';
776                 $this->_init_caps( $cap_key );
777         }
778 }
779
780 /**
781  * Map meta capabilities to primitive capabilities.
782  *
783  * This does not actually compare whether the user ID has the actual capability,
784  * just what the capability or capabilities are. Meta capability list value can
785  * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
786  * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
787  *
788  * @since 2.0.0
789  *
790  * @param string $cap Capability name.
791  * @param int $user_id User ID.
792  * @return array Actual capabilities for meta capability.
793  */
794 function map_meta_cap( $cap, $user_id ) {
795         $args = array_slice( func_get_args(), 2 );
796         $caps = array();
797
798         switch ( $cap ) {
799         case 'remove_user':
800                 $caps[] = 'remove_users';
801                 break;
802         case 'promote_user':
803                 $caps[] = 'promote_users';
804                 break;
805         case 'edit_user':
806                 // Allow user to edit itself
807                 if ( isset( $args[0] ) && $user_id == $args[0] )
808                         break;
809                 // Fall through
810         case 'edit_users':
811                 // If multisite these caps are allowed only for super admins.
812                 if ( is_multisite() && !is_super_admin( $user_id ) )
813                         $caps[] = 'do_not_allow';
814                 else
815                         $caps[] = 'edit_users'; // Explicit due to primitive fall through
816                 break;
817         case 'delete_post':
818                 $author_data = get_userdata( $user_id );
819                 //echo "post ID: {$args[0]}<br />";
820                 $post = get_post( $args[0] );
821                 $post_type = get_post_type_object( $post->post_type );
822                 if ( $post_type && 'post' != $post_type->capability_type ) {
823                         $args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
824                         return call_user_func_array( 'map_meta_cap', $args );
825                 }
826
827                 if ( '' != $post->post_author ) {
828                         $post_author_data = get_userdata( $post->post_author );
829                 } else {
830                         //No author set yet so default to current user for cap checks
831                         $post_author_data = $author_data;
832                 }
833
834                 // If the user is the author...
835                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
836                         // If the post is published...
837                         if ( 'publish' == $post->post_status ) {
838                                 $caps[] = 'delete_published_posts';
839                         } elseif ( 'trash' == $post->post_status ) {
840                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
841                                         $caps[] = 'delete_published_posts';
842                         } else {
843                                 // If the post is draft...
844                                 $caps[] = 'delete_posts';
845                         }
846                 } else {
847                         // The user is trying to edit someone else's post.
848                         $caps[] = 'delete_others_posts';
849                         // The post is published, extra cap required.
850                         if ( 'publish' == $post->post_status )
851                                 $caps[] = 'delete_published_posts';
852                         elseif ( 'private' == $post->post_status )
853                                 $caps[] = 'delete_private_posts';
854                 }
855                 break;
856         case 'delete_page':
857                 $author_data = get_userdata( $user_id );
858                 //echo "post ID: {$args[0]}<br />";
859                 $page = get_page( $args[0] );
860                 $page_author_data = get_userdata( $page->post_author );
861                 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
862                 // If the user is the author...
863
864                 if ('' != $page->post_author) {
865                         $page_author_data = get_userdata( $page->post_author );
866                 } else {
867                         //No author set yet so default to current user for cap checks
868                         $page_author_data = $author_data;
869                 }
870
871                 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
872                         // If the page is published...
873                         if ( $page->post_status == 'publish' ) {
874                                 $caps[] = 'delete_published_pages';
875                         } elseif ( 'trash' == $page->post_status ) {
876                                 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
877                                         $caps[] = 'delete_published_pages';
878                         } else {
879                                 // If the page is draft...
880                                 $caps[] = 'delete_pages';
881                         }
882                 } else {
883                         // The user is trying to edit someone else's page.
884                         $caps[] = 'delete_others_pages';
885                         // The page is published, extra cap required.
886                         if ( $page->post_status == 'publish' )
887                                 $caps[] = 'delete_published_pages';
888                         elseif ( $page->post_status == 'private' )
889                                 $caps[] = 'delete_private_pages';
890                 }
891                 break;
892                 // edit_post breaks down to edit_posts, edit_published_posts, or
893                 // edit_others_posts
894         case 'edit_post':
895                 $author_data = get_userdata( $user_id );
896                 //echo "post ID: {$args[0]}<br />";
897                 $post = get_post( $args[0] );
898                 $post_type = get_post_type_object( $post->post_type );
899                 if ( $post_type && 'post' != $post_type->capability_type ) {
900                         $args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
901                         return call_user_func_array( 'map_meta_cap', $args );
902                 }
903                 $post_author_data = get_userdata( $post->post_author );
904                 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
905                 // If the user is the author...
906                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
907                         // If the post is published...
908                         if ( 'publish' == $post->post_status ) {
909                                 $caps[] = 'edit_published_posts';
910                         } elseif ( 'trash' == $post->post_status ) {
911                                 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
912                                         $caps[] = 'edit_published_posts';
913                         } else {
914                                 // If the post is draft...
915                                 $caps[] = 'edit_posts';
916                         }
917                 } else {
918                         // The user is trying to edit someone else's post.
919                         $caps[] = 'edit_others_posts';
920                         // The post is published, extra cap required.
921                         if ( 'publish' == $post->post_status )
922                                 $caps[] = 'edit_published_posts';
923                         elseif ( 'private' == $post->post_status )
924                                 $caps[] = 'edit_private_posts';
925                 }
926                 break;
927         case 'edit_page':
928                 $author_data = get_userdata( $user_id );
929                 //echo "post ID: {$args[0]}<br />";
930                 $page = get_page( $args[0] );
931                 $page_author_data = get_userdata( $page->post_author );
932                 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
933                 // If the user is the author...
934                 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
935                         // If the page is published...
936                         if ( 'publish' == $page->post_status ) {
937                                 $caps[] = 'edit_published_pages';
938                         } elseif ( 'trash' == $page->post_status ) {
939                                 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
940                                         $caps[] = 'edit_published_pages';
941                         } else {
942                                 // If the page is draft...
943                                 $caps[] = 'edit_pages';
944                         }
945                 } else {
946                         // The user is trying to edit someone else's page.
947                         $caps[] = 'edit_others_pages';
948                         // The page is published, extra cap required.
949                         if ( 'publish' == $page->post_status )
950                                 $caps[] = 'edit_published_pages';
951                         elseif ( 'private' == $page->post_status )
952                                 $caps[] = 'edit_private_pages';
953                 }
954                 break;
955         case 'read_post':
956                 $post = get_post( $args[0] );
957                 $post_type = get_post_type_object( $post->post_type );
958                 if ( $post_type && 'post' != $post_type->capability_type ) {
959                         $args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
960                         return call_user_func_array( 'map_meta_cap', $args );
961                 }
962
963                 if ( 'private' != $post->post_status ) {
964                         $caps[] = 'read';
965                         break;
966                 }
967
968                 $author_data = get_userdata( $user_id );
969                 $post_author_data = get_userdata( $post->post_author );
970                 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
971                         $caps[] = 'read';
972                 else
973                         $caps[] = 'read_private_posts';
974                 break;
975         case 'read_page':
976                 $page = get_page( $args[0] );
977
978                 if ( 'private' != $page->post_status ) {
979                         $caps[] = 'read';
980                         break;
981                 }
982
983                 $author_data = get_userdata( $user_id );
984                 $page_author_data = get_userdata( $page->post_author );
985                 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID )
986                         $caps[] = 'read';
987                 else
988                         $caps[] = 'read_private_pages';
989                 break;
990         case 'unfiltered_upload':
991                 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
992                         $caps[] = $cap;
993                 else
994                         $caps[] = 'do_not_allow';
995                 break;
996         case 'edit_files':
997         case 'edit_plugins':
998         case 'edit_themes':
999                 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
1000                         $caps[] = 'do_not_allow';
1001                         break;
1002                 }
1003                 // Fall through if not DISALLOW_FILE_EDIT.
1004         case 'update_plugins':
1005         case 'delete_plugins':
1006         case 'install_plugins':
1007         case 'update_themes':
1008         case 'delete_themes':
1009         case 'install_themes':
1010         case 'update_core':
1011                 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
1012                 // Files in uploads are excepted.
1013                 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
1014                         $caps[] = 'do_not_allow';
1015                         break;
1016                 }
1017                 // Fall through if not DISALLOW_FILE_MODS.
1018         case 'unfiltered_html':
1019                 // Disallow unfiltered_html for all users, even admins and super admins.
1020                 if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
1021                         $caps[] = 'do_not_allow';
1022                         break;
1023                 }
1024                 // Fall through if not DISALLOW_UNFILTERED_HTML
1025         case 'delete_user':
1026         case 'delete_users':
1027                 // If multisite these caps are allowed only for super admins.
1028                 if ( is_multisite() && !is_super_admin( $user_id ) ) {
1029                         $caps[] = 'do_not_allow';
1030                 } else {
1031                         if ( 'delete_user' == $cap )
1032                                 $cap = 'delete_users';
1033                         $caps[] = $cap;
1034                 }
1035                 break;
1036         case 'create_users':
1037                 if ( is_multisite() && !get_site_option( 'add_new_users' ) )
1038                         $caps[] = 'do_not_allow';
1039                 else
1040                         $caps[] = $cap;
1041                 break;
1042         default:
1043                 // If no meta caps match, return the original cap.
1044                 $caps[] = $cap;
1045         }
1046
1047         return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1048 }
1049
1050 /**
1051  * Whether current user has capability or role.
1052  *
1053  * @since 2.0.0
1054  *
1055  * @param string $capability Capability or role name.
1056  * @return bool
1057  */
1058 function current_user_can( $capability ) {
1059         $current_user = wp_get_current_user();
1060
1061         if ( empty( $current_user ) )
1062                 return false;
1063
1064         $args = array_slice( func_get_args(), 1 );
1065         $args = array_merge( array( $capability ), $args );
1066
1067         return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
1068 }
1069
1070 /**
1071  * Whether current user has a capability or role for a given blog.
1072  *
1073  * @since 3.0.0
1074  *
1075  * @param int $blog_id Blog ID
1076  * @param string $capability Capability or role name.
1077  * @return bool
1078  */
1079 function current_user_can_for_blog( $blog_id, $capability ) {
1080         $current_user = wp_get_current_user();
1081
1082         if ( empty( $current_user ) )
1083                 return false;
1084
1085         // Create new object to avoid stomping the global current_user.
1086         $user = new WP_User( $current_user->id) ;
1087
1088         // Set the blog id.  @todo add blog id arg to WP_User constructor?
1089         $user->for_blog( $blog_id );
1090
1091         $args = array_slice( func_get_args(), 2 );
1092         $args = array_merge( array( $capability ), $args );
1093
1094         return call_user_func_array( array( &$user, 'has_cap' ), $args );
1095 }
1096
1097 /**
1098  * Whether author of supplied post has capability or role.
1099  *
1100  * @since 2.9.0
1101  *
1102  * @param int|object $post Post ID or post object.
1103  * @param string $capability Capability or role name.
1104  * @return bool
1105  */
1106 function author_can( $post, $capability ) {
1107         if ( !$post = get_post($post) )
1108                 return false;
1109
1110         $author = new WP_User( $post->post_author );
1111
1112         if ( empty( $author->ID ) )
1113                 return false;
1114
1115         $args = array_slice( func_get_args(), 2 );
1116         $args = array_merge( array( $capability ), $args );
1117
1118         return call_user_func_array( array( &$author, 'has_cap' ), $args );
1119 }
1120
1121 /**
1122  * Retrieve role object.
1123  *
1124  * @see WP_Roles::get_role() Uses method to retrieve role object.
1125  * @since 2.0.0
1126  *
1127  * @param string $role Role name.
1128  * @return object
1129  */
1130 function get_role( $role ) {
1131         global $wp_roles;
1132
1133         if ( ! isset( $wp_roles ) )
1134                 $wp_roles = new WP_Roles();
1135
1136         return $wp_roles->get_role( $role );
1137 }
1138
1139 /**
1140  * Add role, if it does not exist.
1141  *
1142  * @see WP_Roles::add_role() Uses method to add role.
1143  * @since 2.0.0
1144  *
1145  * @param string $role Role name.
1146  * @param string $display_name Display name for role.
1147  * @param array $capabilities List of capabilities.
1148  * @return null|WP_Role WP_Role object if role is added, null if already exists.
1149  */
1150 function add_role( $role, $display_name, $capabilities = array() ) {
1151         global $wp_roles;
1152
1153         if ( ! isset( $wp_roles ) )
1154                 $wp_roles = new WP_Roles();
1155
1156         return $wp_roles->add_role( $role, $display_name, $capabilities );
1157 }
1158
1159 /**
1160  * Remove role, if it exists.
1161  *
1162  * @see WP_Roles::remove_role() Uses method to remove role.
1163  * @since 2.0.0
1164  *
1165  * @param string $role Role name.
1166  * @return null
1167  */
1168 function remove_role( $role ) {
1169         global $wp_roles;
1170
1171         if ( ! isset( $wp_roles ) )
1172                 $wp_roles = new WP_Roles();
1173
1174         return $wp_roles->remove_role( $role );
1175 }
1176
1177 /**
1178  * Retrieve a list of super admins.
1179  *
1180  * @since 3.0.0
1181  *
1182  * @uses $super_admins Super admins global variable, if set.
1183  *
1184  * @return array List of super admin logins
1185  */
1186 function get_super_admins() {
1187         global $super_admins;
1188
1189         if ( isset($super_admins) )
1190                 return $super_admins;
1191         else
1192                 return get_site_option( 'site_admins', array('admin') );
1193 }
1194
1195 /**
1196  * Determine if user is a site admin.
1197  *
1198  * @since 3.0.0
1199  *
1200  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1201  * @return bool True if the user is a site admin.
1202  */
1203 function is_super_admin( $user_id = false ) {
1204         if ( ! $user_id ) {
1205                 $current_user = wp_get_current_user();
1206                 $user_id = ! empty($current_user) ? $current_user->id : 0;
1207         }
1208
1209         if ( ! $user_id )
1210                 return false;
1211
1212         $user = new WP_User($user_id);
1213
1214         if ( is_multisite() ) {
1215                 $super_admins = get_super_admins();
1216                 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1217                         return true;
1218         } else {
1219                 if ( $user->has_cap('delete_users') )
1220                         return true;
1221         }
1222
1223         return false;
1224 }
1225
1226 ?>