| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
class WP_Roles { |
|---|
| 30 |
|
|---|
| 31 |
* List of roles and capabilities. |
|---|
| 32 |
* |
|---|
| 33 |
* @since 2.0.0 |
|---|
| 34 |
* @access public |
|---|
| 35 |
* @var array |
|---|
| 36 |
*/ |
|---|
| 37 |
var $roles; |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
* List of the role objects. |
|---|
| 41 |
* |
|---|
| 42 |
* @since 2.0.0 |
|---|
| 43 |
* @access public |
|---|
| 44 |
* @var array |
|---|
| 45 |
*/ |
|---|
| 46 |
var $role_objects = array(); |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* List of role names. |
|---|
| 50 |
* |
|---|
| 51 |
* @since 2.0.0 |
|---|
| 52 |
* @access public |
|---|
| 53 |
* @var array |
|---|
| 54 |
*/ |
|---|
| 55 |
var $role_names = array(); |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Option name for storing role list. |
|---|
| 59 |
* |
|---|
| 60 |
* @since 2.0.0 |
|---|
| 61 |
* @access public |
|---|
| 62 |
* @var string |
|---|
| 63 |
*/ |
|---|
| 64 |
var $role_key; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Whether to use the database for retrieval and storage. |
|---|
| 68 |
* |
|---|
| 69 |
* @since 2.1.0 |
|---|
| 70 |
* @access public |
|---|
| 71 |
* @var bool |
|---|
| 72 |
*/ |
|---|
| 73 |
var $use_db = true; |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
* PHP4 Constructor - Call {@link WP_Roles::_init()} method. |
|---|
| 77 |
* |
|---|
| 78 |
* @since 2.0.0 |
|---|
| 79 |
* @access public |
|---|
| 80 |
* |
|---|
| 81 |
* @return WP_Roles |
|---|
| 82 |
*/ |
|---|
| 83 |
function WP_Roles() { |
|---|
| 84 |
$this->_init(); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
* Setup the object properties. |
|---|
| 89 |
* |
|---|
| 90 |
* The role key is set to the current prefix for the $wpdb object with |
|---|
| 91 |
* 'user_roles' appended. If the $wp_user_roles global is set, then it will |
|---|
| 92 |
* be used and the role option will not be updated or used. |
|---|
| 93 |
* |
|---|
| 94 |
* @since 2.1.0 |
|---|
| 95 |
* @access protected |
|---|
| 96 |
* @uses $wpdb Used to get the database prefix. |
|---|
| 97 |
* @global array $wp_user_roles Used to set the 'roles' property value. |
|---|
| 98 |
*/ |
|---|
| 99 |
function _init () { |
|---|
| 100 |
global $wpdb; |
|---|
| 101 |
global $wp_user_roles; |
|---|
| 102 |
$this->role_key = $wpdb->prefix . 'user_roles'; |
|---|
| 103 |
if ( ! empty( $wp_user_roles ) ) { |
|---|
| 104 |
$this->roles = $wp_user_roles; |
|---|
| 105 |
$this->use_db = false; |
|---|
| 106 |
} else { |
|---|
| 107 |
$this->roles = get_option( $this->role_key ); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
if ( empty( $this->roles ) ) |
|---|
| 111 |
return; |
|---|
| 112 |
|
|---|
| 113 |
$this->role_objects = array(); |
|---|
| 114 |
$this->role_names = array(); |
|---|
| 115 |
foreach ( (array) $this->roles as $role => $data ) { |
|---|
| 116 |
$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); |
|---|
| 117 |
$this->role_names[$role] = $this->roles[$role]['name']; |
|---|
| 118 |
} |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
* Add role name with capabilities to list. |
|---|
| 123 |
* |
|---|
| 124 |
* Updates the list of roles, if the role doesn't already exist. |
|---|
| 125 |
* |
|---|
| 126 |
* @since 2.0.0 |
|---|
| 127 |
* @access public |
|---|
| 128 |
* |
|---|
| 129 |
* @param string $role Role name. |
|---|
| 130 |
* @param string $display_name Role display name. |
|---|
| 131 |
* @param array $capabilities List of role capabilities. |
|---|
| 132 |
* @return null|WP_Role WP_Role object if role is added, null if already exists. |
|---|
| 133 |
*/ |
|---|
| 134 |
function add_role( $role, $display_name, $capabilities = array() ) { |
|---|
| 135 |
if ( isset( $this->roles[$role] ) ) |
|---|
| 136 |
return; |
|---|
| 137 |
|
|---|
| 138 |
$this->roles[$role] = array( |
|---|
| 139 |
'name' => $display_name, |
|---|
| 140 |
'capabilities' => $capabilities |
|---|
| 141 |
); |
|---|
| 142 |
if ( $this->use_db ) |
|---|
| 143 |
update_option( $this->role_key, $this->roles ); |
|---|
| 144 |
$this->role_objects[$role] = new WP_Role( $role, $capabilities ); |
|---|
| 145 |
$this->role_names[$role] = $display_name; |
|---|
| 146 |
return $this->role_objects[$role]; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
* Remove role by name. |
|---|
| 151 |
* |
|---|
| 152 |
* @since 2.0.0 |
|---|
| 153 |
* @access public |
|---|
| 154 |
* |
|---|
| 155 |
* @param string $role Role name. |
|---|
| 156 |
*/ |
|---|
| 157 |
function remove_role( $role ) { |
|---|
| 158 |
if ( ! isset( $this->role_objects[$role] ) ) |
|---|
| 159 |
return; |
|---|
| 160 |
|
|---|
| 161 |
unset( $this->role_objects[$role] ); |
|---|
| 162 |
unset( $this->role_names[$role] ); |
|---|
| 163 |
unset( $this->roles[$role] ); |
|---|
| 164 |
|
|---|
| 165 |
if ( $this->use_db ) |
|---|
| 166 |
update_option( $this->role_key, $this->roles ); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
* Add capability to role. |
|---|
| 171 |
* |
|---|
| 172 |
* @since 2.0.0 |
|---|
| 173 |
* @access public |
|---|
| 174 |
* |
|---|
| 175 |
* @param string $role Role name. |
|---|
| 176 |
* @param string $cap Capability name. |
|---|
| 177 |
* @param bool $grant Optional, default is true. Whether role is capable of preforming capability. |
|---|
| 178 |
*/ |
|---|
| 179 |
function add_cap( $role, $cap, $grant = true ) { |
|---|
| 180 |
$this->roles[$role]['capabilities'][$cap] = $grant; |
|---|
| 181 |
if ( $this->use_db ) |
|---|
| 182 |
update_option( $this->role_key, $this->roles ); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
* Remove capability from role. |
|---|
| 187 |
* |
|---|
| 188 |
* @since 2.0.0 |
|---|
| 189 |
* @access public |
|---|
| 190 |
* |
|---|
| 191 |
* @param string $role Role name. |
|---|
| 192 |
* @param string $cap Capability name. |
|---|
| 193 |
*/ |
|---|
| 194 |
function remove_cap( $role, $cap ) { |
|---|
| 195 |
unset( $this->roles[$role]['capabilities'][$cap] ); |
|---|
| 196 |
if ( $this->use_db ) |
|---|
| 197 |
update_option( $this->role_key, $this->roles ); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
* Retrieve role object by name. |
|---|
| 202 |
* |
|---|
| 203 |
* @since 2.0.0 |
|---|
| 204 |
* @access public |
|---|
| 205 |
* |
|---|
| 206 |
* @param string $role Role name. |
|---|
| 207 |
* @return object|null Null, if role does not exist. WP_Role object, if found. |
|---|
| 208 |
*/ |
|---|
| 209 |
function &get_role( $role ) { |
|---|
| 210 |
if ( isset( $this->role_objects[$role] ) ) |
|---|
| 211 |
return $this->role_objects[$role]; |
|---|
| 212 |
else |
|---|
| 213 |
return null; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
* Retrieve list of role names. |
|---|
| 218 |
* |
|---|
| 219 |
* @since 2.0.0 |
|---|
| 220 |
* @access public |
|---|
| 221 |
* |
|---|
| 222 |
* @return array List of role names. |
|---|
| 223 |
*/ |
|---|
| 224 |
function get_names() { |
|---|
| 225 |
return $this->role_names; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
* Whether role name is currently in the list of available roles. |
|---|
| 230 |
* |
|---|
| 231 |
* @since 2.0.0 |
|---|
| 232 |
* @access public |
|---|
| 233 |
* |
|---|
| 234 |
* @param string $role Role name to look up. |
|---|
| 235 |
* @return bool |
|---|
| 236 |
*/ |
|---|
| 237 |
function is_role( $role ) |
|---|
| 238 |
{ |
|---|
| 239 |
return isset( $this->role_names[$role] ); |
|---|
| 240 |
} |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
class WP_Role { |
|---|
| 251 |
|
|---|
| 252 |
* Role name. |
|---|
| 253 |
* |
|---|
| 254 |
* @since 2.0.0 |
|---|
| 255 |
* @access public |
|---|
| 256 |
* @var string |
|---|
| 257 |
*/ |
|---|
| 258 |
var $name; |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
* List of capabilities the role contains. |
|---|
| 262 |
* |
|---|
| 263 |
* @since 2.0.0 |
|---|
| 264 |
* @access public |
|---|
| 265 |
* @var array |
|---|
| 266 |
*/ |
|---|
| 267 |
var $capabilities; |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
* PHP4 Constructor - Setup object properties. |
|---|
| 271 |
* |
|---|
| 272 |
* The list of capabilities, must have the key as the name of the capability |
|---|
| 273 |
* and the value a boolean of whether it is granted to the role or not. |
|---|
| 274 |
* |
|---|
| 275 |
* @since 2.0.0 |
|---|
| 276 |
* @access public |
|---|
| 277 |
* |
|---|
| 278 |
* @param string $role Role name. |
|---|
| 279 |
* @param array $capabilities List of capabilities. |
|---|
| 280 |
* @return WP_Role |
|---|
| 281 |
*/ |
|---|
| 282 |
function WP_Role( $role, $capabilities ) { |
|---|
| 283 |
$this->name = $role; |
|---|
| 284 |
$this->capabilities = $capabilities; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
* Assign role a capability. |
|---|
| 289 |
* |
|---|
| 290 |
* @see WP_Roles::add_cap() Method uses implementation for role. |
|---|
| 291 |
* @since 2.0.0 |
|---|
| 292 |
* @access public |
|---|
| 293 |
* |
|---|
| 294 |
* @param string $cap Capability name. |
|---|
| 295 |
* @param bool $grant Whether role has capability privilege. |
|---|
| 296 |
*/ |
|---|
| 297 |
function add_cap( $cap, $grant = true ) { |
|---|
| 298 |
global $wp_roles; |
|---|
| 299 |
|
|---|
| 300 |
if ( ! isset( $wp_roles ) ) |
|---|
| 301 |
$wp_roles = new WP_Roles(); |
|---|
| 302 |
|
|---|
| 303 |
$this->capabilities[$cap] = $grant; |
|---|
| 304 |
$wp_roles->add_cap( $this->name, $cap, $grant ); |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
* Remove capability from role. |
|---|
| 309 |
* |
|---|
| 310 |
* This is a container for {@link WP_Roles::remove_cap()} to remove the |
|---|
| 311 |
* capability from the role. That is to say, that {@link |
|---|
| 312 |
* WP_Roles::remove_cap()} implements the functionality, but it also makes |
|---|
| 313 |
* sense to use this class, because you don't need to enter the role name. |
|---|
| 314 |
* |
|---|
| 315 |
* @since 2.0.0 |
|---|
| 316 |
* @access public |
|---|
| 317 |
* |
|---|
| 318 |
* @param string $cap Capability name. |
|---|
| 319 |
*/ |
|---|
| 320 |
function remove_cap( $cap ) { |
|---|
| 321 |
global $wp_roles; |
|---|
| 322 |
|
|---|
| 323 |
if ( ! isset( $wp_roles ) ) |
|---|
| 324 |
$wp_roles = new WP_Roles(); |
|---|
| 325 |
|
|---|
| 326 |
unset( $this->capabilities[$cap] ); |
|---|
| 327 |
$wp_roles->remove_cap( $this->name, $cap ); |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
* Whether role has capability. |
|---|
| 332 |
* |
|---|
| 333 |
* The capabilities is passed through the 'role_has_cap' filter. The first |
|---|
| 334 |
* parameter for the hook is the list of capabilities the class has |
|---|
| 335 |
* assigned. The second parameter is the capability name to look for. The |
|---|
| 336 |
* third and final parameter for the hook is the role name. |
|---|
| 337 |
* |
|---|
| 338 |
* @since 2.0.0 |
|---|
| 339 |
* @access public |
|---|
| 340 |
* |
|---|
| 341 |
* @param string $cap Capability name. |
|---|
| 342 |
* @return bool True, if user has capability. False, if doesn't have capability. |
|---|
| 343 |
*/ |
|---|
| 344 |
function has_cap( $cap ) { |
|---|
| 345 |
$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name ); |
|---|
| 346 |
if ( !empty( $capabilities[$cap] ) ) |
|---|
| 347 |
return $capabilities[$cap]; |
|---|
| 348 |
else |
|---|
| 349 |
return false; |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
} |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
class WP_User { |
|---|
| 362 |
|
|---|
| 363 |
* User data container. |
|---|
| 364 |
* |
|---|
| 365 |
* This will be set as properties of the object. |
|---|
| 366 |
* |
|---|
| 367 |
* @since 2.0.0 |
|---|
| 368 |
* @access private |
|---|
| 369 |
* @var array |
|---|
| 370 |
*/ |
|---|
| 371 |
var $data; |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
* The user's ID. |
|---|
| 375 |
* |
|---|
| 376 |
* @since 2.1.0 |
|---|
| 377 |
* @access public |
|---|
| 378 |
* @var int |
|---|
| 379 |
*/ |
|---|
| 380 |
var $ID = 0; |
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
* The deprecated user's ID. |
|---|
| 384 |
* |
|---|
| 385 |
* @since 2.0.0 |
|---|
| 386 |
* @access public |
|---|
| 387 |
* @deprecated Use WP_User::$ID |
|---|
| 388 |
* @see WP_User::$ID |
|---|
| 389 |
* @var int |
|---|
| 390 |
*/ |
|---|
| 391 |
var $id = 0; |
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
* The individual capabilities the user has been given. |
|---|
| 395 |
* |
|---|
| 396 |
* @since 2.0.0 |
|---|
| 397 |
* @access public |
|---|
| 398 |
* @var array |
|---|
| 399 |
*/ |
|---|
| 400 |
var $caps = array(); |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
* User metadata option name. |
|---|
| 404 |
* |
|---|
| 405 |
* @since 2.0.0 |
|---|
| 406 |
* @access public |
|---|
| 407 |
* @var string |
|---|
| 408 |
*/ |
|---|
| 409 |
var $cap_key; |
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
* The roles the user is part of. |
|---|
| 413 |
* |
|---|
| 414 |
* @since 2.0.0 |
|---|
| 415 |
* @access public |
|---|
| 416 |
* @var array |
|---|
| 417 |
*/ |
|---|
| 418 |
var $roles = array(); |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
* All capabilities the user has, including individual and role based. |
|---|
| 422 |
* |
|---|
| 423 |
* @since 2.0.0 |
|---|
| 424 |
* @access public |
|---|
| 425 |
* @var array |
|---|
| 426 |
*/ |
|---|
| 427 |
var $allcaps = array(); |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
* First name of the user. |
|---|
| 431 |
* |
|---|
| 432 |
* Created to prevent notices. |
|---|
| 433 |
* |
|---|
| 434 |
* @since 2.7.0 |
|---|
| 435 |
* @access public |
|---|
| 436 |
* @var string |
|---|
| 437 |
*/ |
|---|
| 438 |
var $first_name = ''; |
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
* Last name of the user. |
|---|
| 442 |
* |
|---|
| 443 |
* Created to prevent notices. |
|---|
| 444 |
* |
|---|
| 445 |
* @since 2.7.0 |
|---|
| 446 |
* @access public |
|---|
| 447 |
* @var string |
|---|
| 448 |
*/ |
|---|
| 449 |
var $last_name = ''; |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
* PHP4 Constructor - Sets up the object properties. |
|---|
| 453 |
* |
|---|
| 454 |
* Retrieves the userdata and then assigns all of the data keys to direct |
|---|
| 455 |
* properties of the object. Calls {@link WP_User::_init_caps()} after |
|---|
| 456 |
* setting up the object's user data properties. |
|---|
| 457 |
* |
|---|
| 458 |
* @since 2.0.0 |
|---|
| 459 |
* @access public |
|---|
| 460 |
* |
|---|
| 461 |
* @param int|string $id User's ID or username |
|---|
| 462 |
* @param int $name Optional. User's username |
|---|
| 463 |
* @return WP_User |
|---|
| 464 |
*/ |
|---|
| 465 |
function WP_User( $id, $name = '' ) { |
|---|
| 466 |
|
|---|
| 467 |
if ( empty( $id ) && empty( $name ) ) |
|---|
| 468 |
return; |
|---|
| 469 |
|
|---|
| 470 |
if ( ! is_numeric( $id ) ) { |
|---|
| 471 |
$name = $id; |
|---|
| 472 |
$id = 0; |
|---|
| 473 |
} |
|---|
| 474 |
|
|---|
| 475 |
if ( ! empty( $id ) ) |
|---|
| 476 |
$this->data = get_userdata( $id ); |
|---|
| 477 |
else |
|---|
| 478 |
$this->data = get_userdatabylogin( $name ); |
|---|
| 479 |
|
|---|
| 480 |
if ( empty( $this->data->ID ) ) |
|---|
| 481 |
return; |
|---|
| 482 |
|
|---|
| 483 |
foreach ( get_object_vars( $this->data ) as $key => $value ) { |
|---|
| 484 |
$this->{$key} = $value; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
$this->id = $this->ID; |
|---|
| 488 |
$this->_init_caps(); |
|---|
| 489 |
} |
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
* Setup capability object properties. |
|---|
| 493 |
* |
|---|
| 494 |
* Will set the value for the 'cap_key' property to current database table |
|---|
| 495 |
* prefix, followed by 'capabilities'. Will then check to see if the |
|---|
| 496 |
* property matching the 'cap_key' exists and is an array. If so, it will be |
|---|
| 497 |
* used. |
|---|
| 498 |
* |
|---|
| 499 |
* @since 2.1.0 |
|---|
| 500 |
* @access protected |
|---|
| 501 |
*/ |
|---|
| 502 |
function _init_caps() { |
|---|
| 503 |
global $wpdb; |
|---|
| 504 |
$this->cap_key = $wpdb->prefix . 'capabilities'; |
|---|
| 505 |
$this->caps = &$this->{$this->cap_key}; |
|---|
| 506 |
if ( ! is_array( $this->caps ) ) |
|---|
| 507 |
$this->caps = array(); |
|---|
| 508 |
$this->get_role_caps(); |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
* Retrieve all of the role capabilities and merge with individual capabilities. |
|---|
| 513 |
* |
|---|
| 514 |
* All of the capabilities of the roles the user belongs to are merged with |
|---|
| 515 |
* the users individual roles. This also means that the user can be denied |
|---|
| 516 |
* specific roles that their role might have, but the specific user isn't |
|---|
| 517 |
* granted permission to. |
|---|
| 518 |
* |
|---|
| 519 |
* @since 2.0.0 |
|---|
| 520 |
* @uses $wp_roles |
|---|
| 521 |
* @access public |
|---|
| 522 |
*/ |
|---|
| 523 |
function get_role_caps() { |
|---|
| 524 |
global $wp_roles; |
|---|
| 525 |
|
|---|
| 526 |
if ( ! isset( $wp_roles ) ) |
|---|
| 527 |
$wp_roles = new WP_Roles(); |
|---|
| 528 |
|
|---|
| 529 |
|
|---|
| 530 |
if ( is_array( $this->caps ) ) |
|---|
| 531 |
$this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) ); |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
$this->allcaps = array(); |
|---|
| 535 |
foreach ( (array) $this->roles as $role ) { |
|---|
| 536 |
$role = $wp_roles->get_role( $role ); |
|---|
| 537 |
$this->allcaps = array_merge( $this->allcaps, $role->capabilities ); |
|---|
| 538 |
} |
|---|
| 539 |
$this->allcaps = array_merge( $this->allcaps, $this->caps ); |
|---|
| 540 |
} |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
* Add role to user. |
|---|
| 544 |
* |
|---|
| 545 |
* Updates the user's meta data option with capabilities and roles. |
|---|
| 546 |
* |
|---|
| 547 |
* @since 2.0.0 |
|---|
| 548 |
* @access public |
|---|
| 549 |
* |
|---|
| 550 |
* @param string $role Role name. |
|---|
| 551 |
*/ |
|---|
| 552 |
function add_role( $role ) { |
|---|
| 553 |
$this->caps[$role] = true; |
|---|
| 554 |
update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
|---|
| 555 |
$this->get_role_caps(); |
|---|
| 556 |
$this->update_user_level_from_caps(); |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
* Remove role from user. |
|---|
| 561 |
* |
|---|
| 562 |
* @since 2.0.0 |
|---|
| 563 |
* @access public |
|---|
| 564 |
* |
|---|
| 565 |
* @param string $role Role name. |
|---|
| 566 |
*/ |
|---|
| 567 |
function remove_role( $role ) { |
|---|
| 568 |
if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) ) |
|---|
| 569 |
return; |
|---|
| 570 |
unset( $this->caps[$role] ); |
|---|
| 571 |
update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
|---|
| 572 |
$this->get_role_caps(); |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
* Set the role of the user. |
|---|
| 577 |
* |
|---|
| 578 |
* This will remove the previous roles of the user and assign the user the |
|---|
| 579 |
* new one. You can set the role to an empty string and it will remove all |
|---|
| 580 |
* of the roles from the user. |
|---|
| 581 |
* |
|---|
| 582 |
* @since 2.0.0 |
|---|
| 583 |
* @access public |
|---|
| 584 |
* |
|---|
| 585 |
* @param string $role Role name. |
|---|
| 586 |
*/ |
|---|
| 587 |
function set_role( $role ) { |
|---|
| 588 |
foreach ( (array) $this->roles as $oldrole ) |
|---|
| 589 |
unset( $this->caps[$oldrole] ); |
|---|
| 590 |
if ( !empty( $role ) ) { |
|---|
| 591 |
$this->caps[$role] = true; |
|---|
| 592 |
$this->roles = array( $role => true ); |
|---|
| 593 |
} else { |
|---|
| 594 |
$this->roles = false; |
|---|
| 595 |
} |
|---|
| 596 |
update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
|---|
| 597 |
$this->get_role_caps(); |
|---|
| 598 |
$this->update_user_level_from_caps(); |
|---|
| 599 |
} |
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 |
* Choose the maximum level the user has. |
|---|
| 603 |
* |
|---|
| 604 |
* Will compare the level from the $item parameter against the $max |
|---|
| 605 |
* parameter. If the item is incorrect, then just the $max parameter value |
|---|
| 606 |
* will be returned. |
|---|
| 607 |
* |
|---|
| 608 |
* Used to get the max level based on the capabilities the user has. This |
|---|
| 609 |
* is also based on roles, so if the user is assigned the Administrator role |
|---|
| 610 |
* then the capability 'level_10' will exist and the user will get that |
|---|
| 611 |
* value. |
|---|
| 612 |
* |
|---|
| 613 |
* @since 2.0.0 |
|---|
| 614 |
* @access public |
|---|
| 615 |
* |
|---|
| 616 |
* @param int $max Max level of user. |
|---|
| 617 |
* @param string $item Level capability name. |
|---|
| 618 |
* @return int Max Level. |
|---|
| 619 |
*/ |
|---|
| 620 |
function level_reduction( $max, $item ) { |
|---|
| 621 |
if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { |
|---|
| 622 |
$level = intval( $matches[1] ); |
|---|
| 623 |
return max( $max, $level ); |
|---|
| 624 |
} else { |
|---|
| 625 |
return $max; |
|---|
| 626 |
} |
|---|
| 627 |
} |
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
* Update the maximum user level for the user. |
|---|
| 631 |
* |
|---|
| 632 |
* Updates the 'user_level' user metadata (includes prefix that is the |
|---|
| 633 |
* database table prefix) with the maximum user level. Gets the value from |
|---|
| 634 |
* the all of the capabilities that the user has. |
|---|
| 635 |
* |
|---|
| 636 |
* @since 2.0.0 |
|---|
| 637 |
* @access public |
|---|
| 638 |
*/ |
|---|
| 639 |
function update_user_level_from_caps() { |
|---|
| 640 |
global $wpdb; |
|---|
| 641 |
$this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 ); |
|---|
| 642 |
update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level ); |
|---|
| 643 |
} |
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
* Add capability and grant or deny access to capability. |
|---|
| 647 |
* |
|---|
| 648 |
* @since 2.0.0 |
|---|
| 649 |
* @access public |
|---|
| 650 |
* |
|---|
| 651 |
* @param string $cap Capability name. |
|---|
| 652 |
* @param bool $grant Whether to grant capability to user. |
|---|
| 653 |
*/ |
|---|
| 654 |
function add_cap( $cap, $grant = true ) { |
|---|
| 655 |
$this->caps[$cap] = $grant; |
|---|
| 656 |
update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
* Remove capability from user. |
|---|
| 661 |
* |
|---|
| 662 |
* @since 2.0.0 |
|---|
| 663 |
* @access public |
|---|
| 664 |
* |
|---|
| 665 |
* @param string $cap Capability name. |
|---|
| 666 |
*/ |
|---|
| 667 |
function remove_cap( $cap ) { |
|---|
| 668 |
if ( empty( $this->caps[$cap] ) ) return; |
|---|
| 669 |
unset( $this->caps[$cap] ); |
|---|
| 670 |
update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
|---|
| 671 |
} |
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
* Remove all of the capabilities of the user. |
|---|
| 675 |
* |
|---|
| 676 |
* @since 2.1.0 |
|---|
| 677 |
* @access public |
|---|
| 678 |
*/ |
|---|
| 679 |
function remove_all_caps() { |
|---|
| 680 |
global $wpdb; |
|---|
| 681 |
$this->caps = array(); |
|---|
| 682 |
update_usermeta( $this->ID, $this->cap_key, '' ); |
|---|
|
|---|