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-user.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 
    298 /**
    299  * WordPress Role class.
    300  *
    301  * @since 2.0.0
    302  * @package WordPress
    303  * @subpackage User
    304  */
    305 class WP_Role {
    306     /**
    307      * Role name.
    308      *
    309      * @since 2.0.0
    310      * @access public
    311      * @var string
    312      */
    313     public $name;
    314 
    315     /**
    316      * List of capabilities the role contains.
    317      *
    318      * @since 2.0.0
    319      * @access public
    320      * @var array
    321      */
    322     public $capabilities;
    323 
    324     /**
    325      * Constructor - Set up object properties.
    326      *
    327      * The list of capabilities, must have the key as the name of the capability
    328      * and the value a boolean of whether it is granted to the role.
    329      *
    330      * @since 2.0.0
    331      * @access public
    332      *
    333      * @param string $role Role name.
    334      * @param array $capabilities List of capabilities.
    335      */
    336     public function __construct( $role, $capabilities ) {
    337         $this->name = $role;
    338         $this->capabilities = $capabilities;
    339     }
    340 
    341     /**
    342      * Assign role a capability.
    343      *
    344      * @since 2.0.0
    345      * @access public
    346      *
    347      * @param string $cap Capability name.
    348      * @param bool $grant Whether role has capability privilege.
    349      */
    350     public function add_cap( $cap, $grant = true ) {
    351         $this->capabilities[$cap] = $grant;
    352         wp_roles()->add_cap( $this->name, $cap, $grant );
    353     }
    354 
    355     /**
    356      * Remove capability from role.
    357      *
    358      * This is a container for {@link WP_Roles::remove_cap()} to remove the
    359      * capability from the role. That is to say, that {@link
    360      * WP_Roles::remove_cap()} implements the functionality, but it also makes
    361      * sense to use this class, because you don't need to enter the role name.
    362      *
    363      * @since 2.0.0
    364      * @access public
    365      *
    366      * @param string $cap Capability name.
    367      */
    368     public function remove_cap( $cap ) {
    369         unset( $this->capabilities[$cap] );
    370         wp_roles()->remove_cap( $this->name, $cap );
    371     }
    372 
    373     /**
    374      * Whether role has capability.
    375      *
    376      * The capabilities is passed through the 'role_has_cap' filter. The first
    377      * parameter for the hook is the list of capabilities the class has
    378      * assigned. The second parameter is the capability name to look for. The
    379      * third and final parameter for the hook is the role name.
    380      *
    381      * @since 2.0.0
    382      * @access public
    383      *
    384      * @param string $cap Capability name.
    385      * @return bool True, if user has capability. False, if doesn't have capability.
    386      */
    387     public function has_cap( $cap ) {
    388         /**
    389          * Filter which capabilities a role has.
    390          *
    391          * @since 2.0.0
    392          *
    393          * @param array  $capabilities Array of role capabilities.
    394          * @param string $cap          Capability name.
    395          * @param string $name         Role name.
    396          */
    397         $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
    398         if ( !empty( $capabilities[$cap] ) )
    399             return $capabilities[$cap];
    400         else
    401             return false;
    402     }
    403 
    404 }
    405 
    4062/**
    4073 * WordPress User class.
     
    1104700    }
    1105701}
    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.