Make WordPress Core


Ignore:
Timestamp:
08/26/2015 04:57:48 AM (11 years ago)
Author:
wonderboymusic
Message:

Roles: move classes into their own file. capbilities.php loads the new files, so this is 100% BC if someone is loading capbilities.php directly. New files created using svn cp.

Creates:
class-wp-roles.php
class-wp-role.php
class-wp-user.php
capbilities-functions.php

capbilities.php contains only top-level code. Class files only contains classes. Functions file only contains functions.

See #33413.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-role.php

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