Make WordPress Core


Ignore:
Timestamp:
08/06/2008 08:50:30 PM (18 years ago)
Author:
markjaquith
Message:

Whitespacing/coding standards cleanup for capabilities.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/capabilities.php

    r8572 r8573  
    1717                global $wp_user_roles;
    1818                $this->role_key = $wpdb->prefix . 'user_roles';
    19                 if ( ! empty($wp_user_roles) ) {
     19                if ( ! empty( $wp_user_roles ) ) {
    2020                        $this->roles = $wp_user_roles;
    2121                        $this->use_db = false;
    2222                } else {
    23                         $this->roles = get_option($this->role_key);
    24                 }
    25 
    26                 if ( empty($this->roles) )
     23                        $this->roles = get_option( $this->role_key );
     24                }
     25
     26                if ( empty( $this->roles ) )
    2727                        return;
    2828
    2929                $this->role_objects = array();
    3030                $this->role_names =  array();
    31                 foreach ( (array) $this->roles as $role => $data) {
    32                         $this->role_objects[$role] = new WP_Role($role, $this->roles[$role]['capabilities']);
     31                foreach ( (array) $this->roles as $role => $data ) {
     32                        $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
    3333                        $this->role_names[$role] = $this->roles[$role]['name'];
    3434                }
    3535        }
    3636
    37         function add_role($role, $display_name, $capabilities = array()) {
    38                 if ( isset($this->roles[$role]) )
     37        function add_role( $role, $display_name, $capabilities = array() ) {
     38                if ( isset( $this->roles[$role] ) )
    3939                        return;
    4040
    4141                $this->roles[$role] = array(
    4242                        'name' => $display_name,
    43                         'capabilities' => $capabilities);
     43                        'capabilities' => $capabilities
     44                        );
    4445                if ( $this->use_db )
    45                         update_option($this->role_key, $this->roles);
    46                 $this->role_objects[$role] = new WP_Role($role, $capabilities);
     46                        update_option( $this->role_key, $this->roles );
     47                $this->role_objects[$role] = new WP_Role( $role, $capabilities );
    4748                $this->role_names[$role] = $display_name;
    4849                return $this->role_objects[$role];
    4950        }
    5051
    51         function remove_role($role) {
    52                 if ( ! isset($this->role_objects[$role]) )
    53                         return;
    54 
    55                 unset($this->role_objects[$role]);
    56                 unset($this->role_names[$role]);
    57                 unset($this->roles[$role]);
     52        function remove_role( $role ) {
     53                if ( ! isset( $this->role_objects[$role] ) )
     54                        return;
     55
     56                unset( $this->role_objects[$role] );
     57                unset( $this->role_names[$role] );
     58                unset( $this->roles[$role] );
    5859
    5960                if ( $this->use_db )
    60                         update_option($this->role_key, $this->roles);
    61         }
    62 
    63         function add_cap($role, $cap, $grant = true) {
     61                        update_option( $this->role_key, $this->roles );
     62        }
     63
     64        function add_cap( $role, $cap, $grant = true ) {
    6465                $this->roles[$role]['capabilities'][$cap] = $grant;
    6566                if ( $this->use_db )
    66                         update_option($this->role_key, $this->roles);
    67         }
    68 
    69         function remove_cap($role, $cap) {
    70                 unset($this->roles[$role]['capabilities'][$cap]);
     67                        update_option( $this->role_key, $this->roles );
     68        }
     69
     70        function remove_cap( $role, $cap ) {
     71                unset( $this->roles[$role]['capabilities'][$cap] );
    7172                if ( $this->use_db )
    72                         update_option($this->role_key, $this->roles);
    73         }
    74 
    75         function &get_role($role) {
    76                 if ( isset($this->role_objects[$role]) )
     73                        update_option( $this->role_key, $this->roles );
     74        }
     75
     76        function &get_role( $role ) {
     77                if ( isset( $this->role_objects[$role] ) )
    7778                        return $this->role_objects[$role];
    7879                else
     
    8485        }
    8586
    86         function is_role($role)
     87        function is_role( $role )
    8788        {
    88                 return isset($this->role_names[$role]);
     89                return isset( $this->role_names[$role] );
    8990        }
    9091}
     
    9495        var $capabilities;
    9596
    96         function WP_Role($role, $capabilities) {
     97        function WP_Role( $role, $capabilities ) {
    9798                $this->name = $role;
    9899                $this->capabilities = $capabilities;
    99100        }
    100101
    101         function add_cap($cap, $grant = true) {
     102        function add_cap( $cap, $grant = true ) {
    102103                global $wp_roles;
    103104
    104                 if ( ! isset($wp_roles) )
     105                if ( ! isset( $wp_roles ) )
    105106                        $wp_roles = new WP_Roles();
    106107
    107108                $this->capabilities[$cap] = $grant;
    108                 $wp_roles->add_cap($this->name, $cap, $grant);
    109         }
    110 
    111         function remove_cap($cap) {
     109                $wp_roles->add_cap( $this->name, $cap, $grant );
     110        }
     111
     112        function remove_cap( $cap ) {
    112113                global $wp_roles;
    113114
    114                 if ( ! isset($wp_roles) )
     115                if ( ! isset( $wp_roles ) )
    115116                        $wp_roles = new WP_Roles();
    116117
    117                 unset($this->capabilities[$cap]);
    118                 $wp_roles->remove_cap($this->name, $cap);
    119         }
    120 
    121         function has_cap($cap) {
    122                 $capabilities = apply_filters('role_has_cap', $this->capabilities, $cap, $this->name);
    123                 if ( !empty($capabilities[$cap]) )
     118                unset( $this->capabilities[$cap] );
     119                $wp_roles->remove_cap( $this->name, $cap );
     120        }
     121
     122        function has_cap( $cap ) {
     123                $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
     124                if ( !empty( $capabilities[$cap] ) )
    124125                        return $capabilities[$cap];
    125126                else
     
    138139        var $allcaps = array();
    139140
    140         function WP_User($id, $name = '') {
    141 
    142                 if ( empty($id) && empty($name) )
    143                         return;
    144 
    145                 if ( ! is_numeric($id) ) {
     141        function WP_User( $id, $name = '' ) {
     142
     143                if ( empty( $id ) && empty( $name ) )
     144                        return;
     145
     146                if ( ! is_numeric( $id ) ) {
    146147                        $name = $id;
    147148                        $id = 0;
    148149                }
    149150
    150                 if ( ! empty($id) )
    151                         $this->data = get_userdata($id);
     151                if ( ! empty( $id ) )
     152                        $this->data = get_userdata( $id );
    152153                else
    153                         $this->data = get_userdatabylogin($name);
    154 
    155                 if ( empty($this->data->ID) )
    156                         return;
    157 
    158                 foreach (get_object_vars($this->data) as $key => $value) {
     154                        $this->data = get_userdatabylogin( $name );
     155
     156                if ( empty( $this->data->ID ) )
     157                        return;
     158
     159                foreach ( get_object_vars( $this->data ) as $key => $value ) {
    159160                        $this->{$key} = $value;
    160161                }
     
    168169                $this->cap_key = $wpdb->prefix . 'capabilities';
    169170                $this->caps = &$this->{$this->cap_key};
    170                 if ( ! is_array($this->caps) )
     171                if ( ! is_array( $this->caps ) )
    171172                        $this->caps = array();
    172173                $this->get_role_caps();
     
    176177                global $wp_roles;
    177178
    178                 if ( ! isset($wp_roles) )
     179                if ( ! isset( $wp_roles ) )
    179180                        $wp_roles = new WP_Roles();
    180181
    181182                //Filter out caps that are not role names and assign to $this->roles
    182                 if(is_array($this->caps))
    183                         $this->roles = array_filter(array_keys($this->caps), array(&$wp_roles, 'is_role'));
     183                if ( is_array( $this->caps ) )
     184                        $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
    184185
    185186                //Build $allcaps from role caps, overlay user's $caps
    186187                $this->allcaps = array();
    187                 foreach( (array) $this->roles as $role) {
    188                         $role = $wp_roles->get_role($role);
    189                         $this->allcaps = array_merge($this->allcaps, $role->capabilities);
    190                 }
    191                 $this->allcaps = array_merge($this->allcaps, $this->caps);
    192         }
    193 
    194         function add_role($role) {
     188                foreach ( (array) $this->roles as $role ) {
     189                        $role = $wp_roles->get_role( $role );
     190                        $this->allcaps = array_merge( $this->allcaps, $role->capabilities );
     191                }
     192                $this->allcaps = array_merge( $this->allcaps, $this->caps );
     193        }
     194
     195        function add_role( $role ) {
    195196                $this->caps[$role] = true;
    196                 update_usermeta($this->ID, $this->cap_key, $this->caps);
     197                update_usermeta( $this->ID, $this->cap_key, $this->caps );
    197198                $this->get_role_caps();
    198199                $this->update_user_level_from_caps();
    199200        }
    200201
    201         function remove_role($role) {
    202                 if ( empty($this->roles[$role]) || (count($this->roles) <= 1) )
    203                         return;
    204                 unset($this->caps[$role]);
    205                 update_usermeta($this->ID, $this->cap_key, $this->caps);
     202        function remove_role( $role ) {
     203                if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
     204                        return;
     205                unset( $this->caps[$role] );
     206                update_usermeta( $this->ID, $this->cap_key, $this->caps );
    206207                $this->get_role_caps();
    207208        }
    208209
    209         function set_role($role) {
    210                 foreach( (array) $this->roles as $oldrole)
    211                         unset($this->caps[$oldrole]);
    212                 if ( !empty($role) ) {
     210        function set_role( $role ) {
     211                foreach ( (array) $this->roles as $oldrole )
     212                        unset( $this->caps[$oldrole] );
     213                if ( !empty( $role ) ) {
    213214                        $this->caps[$role] = true;
    214                         $this->roles = array($role => true);
     215                        $this->roles = array( $role => true );
    215216                } else {
    216217                        $this->roles = false;
    217218                }
    218                 update_usermeta($this->ID, $this->cap_key, $this->caps);
     219                update_usermeta( $this->ID, $this->cap_key, $this->caps );
    219220                $this->get_role_caps();
    220221                $this->update_user_level_from_caps();
    221222        }
    222223
    223         function level_reduction($max, $item) {
    224                 if(preg_match('/^level_(10|[0-9])$/i', $item, $matches)) {
    225                         $level = intval($matches[1]);
    226                         return max($max, $level);
     224        function level_reduction( $max, $item ) {
     225                if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
     226                        $level = intval( $matches[1] );
     227                        return max( $max, $level );
    227228                } else {
    228229                        return $max;
     
    232233        function update_user_level_from_caps() {
    233234                global $wpdb;
    234                 $this->user_level = array_reduce(array_keys($this->allcaps),    array(&$this, 'level_reduction'), 0);
    235                 update_usermeta($this->ID, $wpdb->prefix.'user_level', $this->user_level);
    236         }
    237 
    238         function add_cap($cap, $grant = true) {
     235                $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
     236                update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
     237        }
     238
     239        function add_cap( $cap, $grant = true ) {
    239240                $this->caps[$cap] = $grant;
    240                 update_usermeta($this->ID, $this->cap_key, $this->caps);
    241         }
    242 
    243         function remove_cap($cap) {
    244                 if ( empty($this->caps[$cap]) ) return;
    245                 unset($this->caps[$cap]);
    246                 update_usermeta($this->ID, $this->cap_key, $this->caps);
     241                update_usermeta( $this->ID, $this->cap_key, $this->caps );
     242        }
     243
     244        function remove_cap( $cap ) {
     245                if ( empty( $this->caps[$cap] ) ) return;
     246                unset( $this->caps[$cap] );
     247                update_usermeta( $this->ID, $this->cap_key, $this->caps );
    247248        }
    248249
     
    250251                global $wpdb;
    251252                $this->caps = array();
    252                 update_usermeta($this->ID, $this->cap_key, '');
    253                 update_usermeta($this->ID, $wpdb->prefix.'user_level', '');
     253                update_usermeta( $this->ID, $this->cap_key, '' );
     254                update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
    254255                $this->get_role_caps();
    255256        }
    256257
    257         //has_cap(capability_or_role_name) or
    258         //has_cap('edit_post', post_id)
    259         function has_cap($cap) {
    260                 if ( is_numeric($cap) )
    261                         $cap = $this->translate_level_to_cap($cap);
    262 
    263                 $args = array_slice(func_get_args(), 1);
    264                 $args = array_merge(array($cap, $this->ID), $args);
    265                 $caps = call_user_func_array('map_meta_cap', $args);
     258        // has_cap( capability_or_role_name ) or
     259        // has_cap( 'edit_post', post_id )
     260        function has_cap( $cap ) {
     261                if ( is_numeric( $cap ) )
     262                        $cap = $this->translate_level_to_cap( $cap );
     263
     264                $args = array_slice( func_get_args(), 1 );
     265                $args = array_merge( array( $cap, $this->ID ), $args );
     266                $caps = call_user_func_array( 'map_meta_cap', $args );
    266267                // Must have ALL requested caps
    267                 $capabilities = apply_filters('user_has_cap', $this->allcaps, $caps, $args);
    268                 foreach ( (array) $caps as $cap) {
     268                $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
     269                foreach ( (array) $caps as $cap ) {
    269270                        //echo "Checking cap $cap<br />";
    270                         if(empty($capabilities[$cap]) || !$capabilities[$cap])
     271                        if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
    271272                                return false;
    272273                }
     
    275276        }
    276277
    277         function translate_level_to_cap($level) {
     278        function translate_level_to_cap( $level ) {
    278279                return 'level_' . $level;
    279280        }
     
    282283
    283284// Map meta capabilities to primitive capabilities.
    284 function map_meta_cap($cap, $user_id) {
    285         $args = array_slice(func_get_args(), 2);
     285function map_meta_cap( $cap, $user_id ) {
     286        $args = array_slice( func_get_args(), 2 );
    286287        $caps = array();
    287288
    288         switch ($cap) {
     289        switch ( $cap ) {
    289290        case 'delete_user':
    290291                $caps[] = 'delete_users';
    291292                break;
    292293        case 'edit_user':
    293                 if ( !isset($args[0]) || $user_id != $args[0] ) {
     294                if ( !isset( $args[0] ) || $user_id != $args[0] ) {
    294295                        $caps[] = 'edit_users';
    295296                }
    296297                break;
    297298        case 'delete_post':
    298                 $author_data = get_userdata($user_id);
     299                $author_data = get_userdata( $user_id );
    299300                //echo "post ID: {$args[0]}<br />";
    300                 $post = get_post($args[0]);
     301                $post = get_post( $args[0] );
    301302                if ( 'page' == $post->post_type ) {
    302                         $args = array_merge(array('delete_page', $user_id), $args);
    303                         return call_user_func_array('map_meta_cap', $args);
    304                 }
    305                 $post_author_data = get_userdata($post->post_author);
     303                        $args = array_merge( array( 'delete_page', $user_id ), $args );
     304                        return call_user_func_array( 'map_meta_cap', $args );
     305                }
     306                $post_author_data = get_userdata( $post->post_author );
    306307                //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
    307308                // If the user is the author...
    308                 if ($user_id == $post_author_data->ID) {
     309                if ( $user_id == $post_author_data->ID ) {
    309310                        // If the post is published...
    310                         if ($post->post_status == 'publish')
     311                        if ( 'publish' == $post->post_status )
    311312                                $caps[] = 'delete_published_posts';
    312313                        else
     
    317318                        $caps[] = 'delete_others_posts';
    318319                        // The post is published, extra cap required.
    319                         if ($post->post_status == 'publish')
     320                        if ( 'publish' == $post->post_status )
    320321                                $caps[] = 'delete_published_posts';
    321                         else if ($post->post_status == 'private')
     322                        elseif ( 'private' == $post->post_status )
    322323                                $caps[] = 'delete_private_posts';
    323324                }
    324325                break;
    325326        case 'delete_page':
    326                 $author_data = get_userdata($user_id);
     327                $author_data = get_userdata( $user_id );
    327328                //echo "post ID: {$args[0]}<br />";
    328                 $page = get_page($args[0]);
    329                 $page_author_data = get_userdata($page->post_author);
     329                $page = get_page( $args[0] );
     330                $page_author_data = get_userdata( $page->post_author );
    330331                //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
    331332                // If the user is the author...
    332                 if ($user_id == $page_author_data->ID) {
     333                if ( $user_id == $page_author_data->ID ) {
    333334                        // If the page is published...
    334                         if ($page->post_status == 'publish')
     335                        if ( $page->post_status == 'publish' )
    335336                                $caps[] = 'delete_published_pages';
    336337                        else
     
    341342                        $caps[] = 'delete_others_pages';
    342343                        // The page is published, extra cap required.
    343                         if ($page->post_status == 'publish')
     344                        if ( $page->post_status == 'publish' )
    344345                                $caps[] = 'delete_published_pages';
    345                         else if ($page->post_status == 'private')
     346                        elseif ( $page->post_status == 'private' )
    346347                                $caps[] = 'delete_private_pages';
    347348                }
     
    350351                // edit_others_posts
    351352        case 'edit_post':
    352                 $author_data = get_userdata($user_id);
     353                $author_data = get_userdata( $user_id );
    353354                //echo "post ID: {$args[0]}<br />";
    354                 $post = get_post($args[0]);
     355                $post = get_post( $args[0] );
    355356                if ( 'page' == $post->post_type ) {
    356                         $args = array_merge(array('edit_page', $user_id), $args);
    357                         return call_user_func_array('map_meta_cap', $args);
    358                 }
    359                 $post_author_data = get_userdata($post->post_author);
     357                        $args = array_merge( array( 'edit_page', $user_id ), $args );
     358                        return call_user_func_array( 'map_meta_cap', $args );
     359                }
     360                $post_author_data = get_userdata( $post->post_author );
    360361                //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
    361362                // If the user is the author...
    362                 if ($user_id == $post_author_data->ID) {
     363                if ( $user_id == $post_author_data->ID ) {
    363364                        // If the post is published...
    364                         if ($post->post_status == 'publish')
     365                        if ( 'publish' == $post->post_status )
    365366                                $caps[] = 'edit_published_posts';
    366367                        else
     
    371372                        $caps[] = 'edit_others_posts';
    372373                        // The post is published, extra cap required.
    373                         if ($post->post_status == 'publish')
     374                        if ( 'publish' == $post->post_status )
    374375                                $caps[] = 'edit_published_posts';
    375                         else if ($post->post_status == 'private')
     376                        elseif ( 'private' == $post->post_status )
    376377                                $caps[] = 'edit_private_posts';
    377378                }
    378379                break;
    379380        case 'edit_page':
    380                 $author_data = get_userdata($user_id);
     381                $author_data = get_userdata( $user_id );
    381382                //echo "post ID: {$args[0]}<br />";
    382                 $page = get_page($args[0]);
    383                 $page_author_data = get_userdata($page->post_author);
     383                $page = get_page( $args[0] );
     384                $page_author_data = get_userdata( $page->post_author );
    384385                //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
    385386                // If the user is the author...
    386                 if ($user_id == $page_author_data->ID) {
     387                if ( $user_id == $page_author_data->ID ) {
    387388                        // If the page is published...
    388                         if ($page->post_status == 'publish')
     389                        if ( 'publish' == $page->post_status )
    389390                                $caps[] = 'edit_published_pages';
    390391                        else
     
    395396                        $caps[] = 'edit_others_pages';
    396397                        // The page is published, extra cap required.
    397                         if ($page->post_status == 'publish')
     398                        if ( 'publish' == $page->post_status )
    398399                                $caps[] = 'edit_published_pages';
    399                         else if ($page->post_status == 'private')
     400                        elseif ( 'private' == $page->post_status )
    400401                                $caps[] = 'edit_private_pages';
    401402                }
    402403                break;
    403404        case 'read_post':
    404                 $post = get_post($args[0]);
     405                $post = get_post( $args[0] );
    405406                if ( 'page' == $post->post_type ) {
    406                         $args = array_merge(array('read_page', $user_id), $args);
    407                         return call_user_func_array('map_meta_cap', $args);
     407                        $args = array_merge( array( 'read_page', $user_id ), $args );
     408                        return call_user_func_array( 'map_meta_cap', $args );
    408409                }
    409410
     
    413414                }
    414415
    415                 $author_data = get_userdata($user_id);
    416                 $post_author_data = get_userdata($post->post_author);
    417                 if ($user_id == $post_author_data->ID)
     416                $author_data = get_userdata( $user_id );
     417                $post_author_data = get_userdata( $post->post_author );
     418                if ( $user_id == $post_author_data->ID )
    418419                        $caps[] = 'read';
    419420                else
     
    421422                break;
    422423        case 'read_page':
    423                 $page = get_page($args[0]);
     424                $page = get_page( $args[0] );
    424425
    425426                if ( 'private' != $page->post_status ) {
     
    428429                }
    429430
    430                 $author_data = get_userdata($user_id);
    431                 $page_author_data = get_userdata($page->post_author);
    432                 if ($user_id == $page_author_data->ID)
     431                $author_data = get_userdata( $user_id );
     432                $page_author_data = get_userdata( $page->post_author );
     433                if ( $user_id == $page_author_data->ID )
    433434                        $caps[] = 'read';
    434435                else
     
    444445
    445446// Capability checking wrapper around the global $current_user object.
    446 function current_user_can($capability) {
     447function current_user_can( $capability ) {
    447448        $current_user = wp_get_current_user();
    448449
    449         if ( empty($current_user) )
     450        if ( empty( $current_user ) )
    450451                return false;
    451452
    452         $args = array_slice(func_get_args(), 1);
    453         $args = array_merge(array($capability), $args);
    454 
    455         return call_user_func_array(array(&$current_user, 'has_cap'), $args);
     453        $args = array_slice( func_get_args(), 1 );
     454        $args = array_merge( array( $capability ), $args );
     455
     456        return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
    456457}
    457458
    458459// Convenience wrappers around $wp_roles.
    459 function get_role($role) {
     460function get_role( $role ) {
    460461        global $wp_roles;
    461462
    462         if ( ! isset($wp_roles) )
     463        if ( ! isset( $wp_roles ) )
    463464                $wp_roles = new WP_Roles();
    464465
    465         return $wp_roles->get_role($role);
    466 }
    467 
    468 function add_role($role, $display_name, $capabilities = array()) {
     466        return $wp_roles->get_role( $role );
     467}
     468
     469function add_role( $role, $display_name, $capabilities = array() ) {
    469470        global $wp_roles;
    470471
    471         if ( ! isset($wp_roles) )
     472        if ( ! isset( $wp_roles ) )
    472473                $wp_roles = new WP_Roles();
    473474
    474         return $wp_roles->add_role($role, $display_name, $capabilities);
    475 }
    476 
    477 function remove_role($role) {
     475        return $wp_roles->add_role( $role, $display_name, $capabilities );
     476}
     477
     478function remove_role( $role ) {
    478479        global $wp_roles;
    479480
    480         if ( ! isset($wp_roles) )
     481        if ( ! isset( $wp_roles ) )
    481482                $wp_roles = new WP_Roles();
    482483
    483         return $wp_roles->remove_role($role);
     484        return $wp_roles->remove_role( $role );
    484485}
    485486
Note: See TracChangeset for help on using the changeset viewer.