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