| [ Index ] |
PHP Cross Reference of bbPress |
[Summary view] [Print] [Text view]
1 <?php 2 // Last sync [WP11537] 3 4 /** 5 * BackPress User class. 6 * 7 * @since 2.0.0 8 * @package BackPress 9 * @subpackage User 10 */ 11 class BP_User { 12 /** 13 * User data container. 14 * 15 * This will be set as properties of the object. 16 * 17 * @since 2.0.0 18 * @access private 19 * @var array 20 */ 21 var $data; 22 23 /** 24 * The user's ID. 25 * 26 * @since 2.1.0 27 * @access public 28 * @var int 29 */ 30 var $ID = 0; 31 32 /** 33 * The deprecated user's ID. 34 * 35 * @since 2.0.0 36 * @access public 37 * @deprecated Use BP_User::$ID 38 * @see BP_User::$ID 39 * @var int 40 */ 41 var $id = 0; 42 43 /** 44 * The individual capabilities the user has been given. 45 * 46 * @since 2.0.0 47 * @access public 48 * @var array 49 */ 50 var $caps = array(); 51 52 /** 53 * User metadata option name. 54 * 55 * @since 2.0.0 56 * @access public 57 * @var string 58 */ 59 var $cap_key; 60 61 /** 62 * The roles the user is part of. 63 * 64 * @since 2.0.0 65 * @access public 66 * @var array 67 */ 68 var $roles = array(); 69 70 /** 71 * All capabilities the user has, including individual and role based. 72 * 73 * @since 2.0.0 74 * @access public 75 * @var array 76 */ 77 var $allcaps = array(); 78 79 /** 80 * First name of the user. 81 * 82 * Created to prevent notices. 83 * 84 * @since 2.7.0 85 * @access public 86 * @var string 87 */ 88 var $first_name = ''; 89 90 /** 91 * Last name of the user. 92 * 93 * Created to prevent notices. 94 * 95 * @since 2.7.0 96 * @access public 97 * @var string 98 */ 99 var $last_name = ''; 100 101 /** 102 * PHP4 Constructor - Sets up the object properties. 103 * 104 * Retrieves the userdata and then assigns all of the data keys to direct 105 * properties of the object. Calls {@link BP_User::_init_caps()} after 106 * setting up the object's user data properties. 107 * 108 * @since 2.0.0 109 * @access public 110 * 111 * @param int|string $id User's ID or username 112 * @param int $name Optional. User's username 113 * @return BP_User 114 */ 115 function BP_User( $id, $name = '' ) { 116 global $wp_users_object; 117 118 if ( empty( $id ) && empty( $name ) ) 119 return; 120 121 if ( ! is_numeric( $id ) ) { 122 $name = $id; 123 $id = 0; 124 } 125 126 if ( ! empty( $id ) ) 127 $this->data = $wp_users_object->get_user( $id ); 128 else 129 $this->data = $wp_users_object->get_user( $name, array( 'by' => 'login' ) ); 130 131 if ( empty( $this->data->ID ) ) 132 return; 133 134 foreach ( get_object_vars( $this->data ) as $key => $value ) { 135 $this->{$key} = $value; 136 } 137 138 $this->id = $this->ID; 139 $this->_init_caps(); 140 } 141 142 /** 143 * Setup capability object properties. 144 * 145 * Will set the value for the 'cap_key' property to current database table 146 * prefix, followed by 'capabilities'. Will then check to see if the 147 * property matching the 'cap_key' exists and is an array. If so, it will be 148 * used. 149 * 150 * @since 2.1.0 151 * @access protected 152 */ 153 function _init_caps() { 154 global $wp_users_object; 155 $this->cap_key = $wp_users_object->db->prefix . 'capabilities'; 156 $this->caps = &$this->{$this->cap_key}; 157 if ( ! is_array( $this->caps ) ) 158 $this->caps = array(); 159 $this->get_role_caps(); 160 } 161 162 /** 163 * Retrieve all of the role capabilities and merge with individual capabilities. 164 * 165 * All of the capabilities of the roles the user belongs to are merged with 166 * the users individual roles. This also means that the user can be denied 167 * specific roles that their role might have, but the specific user isn't 168 * granted permission to. 169 * 170 * @since 2.0.0 171 * @uses $wp_roles 172 * @access public 173 */ 174 function get_role_caps() { 175 global $wp_roles, $wp_users_object; 176 177 if ( ! isset( $wp_roles ) ) 178 $wp_roles = new BP_Roles( $wp_users_object->db ); 179 180 //Filter out caps that are not role names and assign to $this->roles 181 if ( is_array( $this->caps ) ) 182 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) ); 183 184 //Build $allcaps from role caps, overlay user's $caps 185 $this->allcaps = array(); 186 foreach ( (array) $this->roles as $role ) { 187 $role = $wp_roles->get_role( $role ); 188 $this->allcaps = array_merge( (array) $this->allcaps, (array) $role->capabilities ); 189 } 190 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 191 } 192 193 /** 194 * Add role to user. 195 * 196 * Updates the user's meta data option with capabilities and roles. 197 * 198 * @since 2.0.0 199 * @access public 200 * 201 * @param string $role Role name. 202 */ 203 function add_role( $role ) { 204 $this->caps[$role] = true; 205 $this->update_user(); 206 } 207 208 /** 209 * Remove role from user. 210 * 211 * @since 2.0.0 212 * @access public 213 * 214 * @param string $role Role name. 215 */ 216 function remove_role( $role ) { 217 if ( empty( $this->caps[$role] ) || ( count( $this->caps ) <= 1 ) ) 218 return; 219 unset( $this->caps[$role] ); 220 $this->update_user(); 221 } 222 223 /** 224 * Set the role of the user. 225 * 226 * This will remove the previous roles of the user and assign the user the 227 * new one. You can set the role to an empty string and it will remove all 228 * of the roles from the user. 229 * 230 * @since 2.0.0 231 * @access public 232 * 233 * @param string $role Role name. 234 */ 235 function set_role( $role ) { 236 foreach ( (array) $this->roles as $oldrole ) 237 unset( $this->caps[$oldrole] ); 238 if ( !empty( $role ) ) { 239 $this->caps[$role] = true; 240 $this->roles = array( $role => true ); 241 } else { 242 $this->roles = false; 243 } 244 $this->update_user(); 245 } 246 247 function update_user() { 248 global $wp_users_object; 249 $wp_users_object->update_meta( array( 'id' => $this->ID, 'meta_key' => $this->cap_key, 'meta_value' => $this->caps ) ); 250 $this->get_role_caps(); 251 //$this->update_user_level_from_caps(); // WP 252 } 253 254 /** 255 * Choose the maximum level the user has. 256 * 257 * Will compare the level from the $item parameter against the $max 258 * parameter. If the item is incorrect, then just the $max parameter value 259 * will be returned. 260 * 261 * Used to get the max level based on the capabilities the user has. This 262 * is also based on roles, so if the user is assigned the Administrator role 263 * then the capability 'level_10' will exist and the user will get that 264 * value. 265 * 266 * @since 2.0.0 267 * @access public 268 * 269 * @param int $max Max level of user. 270 * @param string $item Level capability name. 271 * @return int Max Level. 272 */ 273 /* 274 function level_reduction( $max, $item ) { 275 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { 276 $level = intval( $matches[1] ); 277 return max( $max, $level ); 278 } else { 279 return $max; 280 } 281 } 282 */ 283 284 /** 285 * Update the maximum user level for the user. 286 * 287 * Updates the 'user_level' user metadata (includes prefix that is the 288 * database table prefix) with the maximum user level. Gets the value from 289 * the all of the capabilities that the user has. 290 * 291 * @since 2.0.0 292 * @access public 293 */ 294 /* 295 function update_user_level_from_caps() { 296 global $wp_users_object; 297 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 ); 298 update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level ); 299 } 300 */ 301 302 /* 303 function translate_level_to_cap($level) { 304 return 'level_' . $level; 305 } 306 */ 307 308 /** 309 * Add capability and grant or deny access to capability. 310 * 311 * @since 2.0.0 312 * @access public 313 * 314 * @param string $cap Capability name. 315 * @param bool $grant Whether to grant capability to user. 316 */ 317 function add_cap( $cap, $grant = true ) { 318 $this->caps[$cap] = $grant; 319 $this->update_user(); 320 } 321 322 /** 323 * Remove capability from user. 324 * 325 * @since 2.0.0 326 * @access public 327 * 328 * @param string $cap Capability name. 329 */ 330 function remove_cap( $cap ) { 331 if ( empty( $this->caps[$cap] ) ) return; 332 unset( $this->caps[$cap] ); 333 $this->update_user(); 334 } 335 336 /** 337 * Remove all of the capabilities of the user. 338 * 339 * @since 2.1.0 340 * @access public 341 */ 342 function remove_all_caps() { 343 global $wp_users_object; 344 $this->caps = array(); 345 $wp_users_object->delete_meta( $this->ID, $this->cap_key ); 346 $this->get_role_caps(); 347 } 348 349 /** 350 * Whether user has capability or role name. 351 * 352 * This is useful for looking up whether the user has a specific role 353 * assigned to the user. The second optional parameter can also be used to 354 * check for capabilities against a specfic post. 355 * 356 * @since 2.0.0 357 * @access public 358 * 359 * @param string|int $cap Capability or role name to search. 360 * @param int $post_id Optional. Post ID to check capability against specific post. 361 * @return bool True, if user has capability; false, if user does not have capability. 362 */ 363 function has_cap( $cap ) { 364 global $wp_roles; 365 366 if ( is_numeric( $cap ) ) 367 $cap = $this->translate_level_to_cap( $cap ); 368 369 $args = array_slice( func_get_args(), 1 ); 370 $args = array_merge( array( $cap, $this->ID ), $args ); 371 $caps = call_user_func_array( array(&$wp_roles, 'map_meta_cap'), $args ); 372 // Must have ALL requested caps 373 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args ); 374 foreach ( (array) $caps as $cap ) { 375 //echo "Checking cap $cap<br />"; 376 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] ) 377 return false; 378 } 379 380 return true; 381 } 382 383 /** 384 * Convert numeric level to level capability name. 385 * 386 * Prepends 'level_' to level number. 387 * 388 * @since 2.0.0 389 * @access public 390 * 391 * @param int $level Level number, 1 to 10. 392 * @return string 393 */ 394 function translate_level_to_cap( $level ) { 395 return 'level_' . $level; 396 } 397 398 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jun 19 03:58:38 2013 | Hosted by follow the white rabbit. |