mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Roles and Capabilities.
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage User
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * WordPress User Roles.
mp-wp_genesis 11 *
mp-wp_genesis 12 * The role option is simple, the structure is organized by role name that store
mp-wp_genesis 13 * the name in value of the 'name' key. The capabilities are stored as an array
mp-wp_genesis 14 * in the value of the 'capability' key.
mp-wp_genesis 15 *
mp-wp_genesis 16 * <code>
mp-wp_genesis 17 * array (
mp-wp_genesis 18 * 'rolename' => array (
mp-wp_genesis 19 * 'name' => 'rolename',
mp-wp_genesis 20 * 'capabilities' => array()
mp-wp_genesis 21 * )
mp-wp_genesis 22 * )
mp-wp_genesis 23 * </code>
mp-wp_genesis 24 *
mp-wp_genesis 25 * @since 2.0.0
mp-wp_genesis 26 * @package WordPress
mp-wp_genesis 27 * @subpackage User
mp-wp_genesis 28 */
mp-wp_genesis 29 class WP_Roles {
mp-wp_genesis 30 /**
mp-wp_genesis 31 * List of roles and capabilities.
mp-wp_genesis 32 *
mp-wp_genesis 33 * @since 2.0.0
mp-wp_genesis 34 * @access public
mp-wp_genesis 35 * @var array
mp-wp_genesis 36 */
mp-wp_genesis 37 var $roles;
mp-wp_genesis 38
mp-wp_genesis 39 /**
mp-wp_genesis 40 * List of the role objects.
mp-wp_genesis 41 *
mp-wp_genesis 42 * @since 2.0.0
mp-wp_genesis 43 * @access public
mp-wp_genesis 44 * @var array
mp-wp_genesis 45 */
mp-wp_genesis 46 var $role_objects = array();
mp-wp_genesis 47
mp-wp_genesis 48 /**
mp-wp_genesis 49 * List of role names.
mp-wp_genesis 50 *
mp-wp_genesis 51 * @since 2.0.0
mp-wp_genesis 52 * @access public
mp-wp_genesis 53 * @var array
mp-wp_genesis 54 */
mp-wp_genesis 55 var $role_names = array();
mp-wp_genesis 56
mp-wp_genesis 57 /**
mp-wp_genesis 58 * Option name for storing role list.
mp-wp_genesis 59 *
mp-wp_genesis 60 * @since 2.0.0
mp-wp_genesis 61 * @access public
mp-wp_genesis 62 * @var string
mp-wp_genesis 63 */
mp-wp_genesis 64 var $role_key;
mp-wp_genesis 65
mp-wp_genesis 66 /**
mp-wp_genesis 67 * Whether to use the database for retrieval and storage.
mp-wp_genesis 68 *
mp-wp_genesis 69 * @since 2.1.0
mp-wp_genesis 70 * @access public
mp-wp_genesis 71 * @var bool
mp-wp_genesis 72 */
mp-wp_genesis 73 var $use_db = true;
mp-wp_genesis 74
mp-wp_genesis 75 /**
mp-wp_genesis 76 * PHP4 Constructor - Call {@link WP_Roles::_init()} method.
mp-wp_genesis 77 *
mp-wp_genesis 78 * @since 2.0.0
mp-wp_genesis 79 * @access public
mp-wp_genesis 80 *
mp-wp_genesis 81 * @return WP_Roles
mp-wp_genesis 82 */
mp-wp_genesis 83 function WP_Roles() {
mp-wp_genesis 84 $this->_init();
mp-wp_genesis 85 }
mp-wp_genesis 86
mp-wp_genesis 87 /**
mp-wp_genesis 88 * Setup the object properties.
mp-wp_genesis 89 *
mp-wp_genesis 90 * The role key is set to the current prefix for the $wpdb object with
mp-wp_genesis 91 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
mp-wp_genesis 92 * be used and the role option will not be updated or used.
mp-wp_genesis 93 *
mp-wp_genesis 94 * @since 2.1.0
mp-wp_genesis 95 * @access protected
mp-wp_genesis 96 * @uses $wpdb Used to get the database prefix.
mp-wp_genesis 97 * @global array $wp_user_roles Used to set the 'roles' property value.
mp-wp_genesis 98 */
mp-wp_genesis 99 function _init () {
mp-wp_genesis 100 global $wpdb;
mp-wp_genesis 101 global $wp_user_roles;
mp-wp_genesis 102 $this->role_key = $wpdb->prefix . 'user_roles';
mp-wp_genesis 103 if ( ! empty( $wp_user_roles ) ) {
mp-wp_genesis 104 $this->roles = $wp_user_roles;
mp-wp_genesis 105 $this->use_db = false;
mp-wp_genesis 106 } else {
mp-wp_genesis 107 $this->roles = get_option( $this->role_key );
mp-wp_genesis 108 }
mp-wp_genesis 109
mp-wp_genesis 110 if ( empty( $this->roles ) )
mp-wp_genesis 111 return;
mp-wp_genesis 112
mp-wp_genesis 113 $this->role_objects = array();
mp-wp_genesis 114 $this->role_names = array();
mp-wp_genesis 115 foreach ( (array) $this->roles as $role => $data ) {
mp-wp_genesis 116 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
mp-wp_genesis 117 $this->role_names[$role] = $this->roles[$role]['name'];
mp-wp_genesis 118 }
mp-wp_genesis 119 }
mp-wp_genesis 120
mp-wp_genesis 121 /**
mp-wp_genesis 122 * Add role name with capabilities to list.
mp-wp_genesis 123 *
mp-wp_genesis 124 * Updates the list of roles, if the role doesn't already exist.
mp-wp_genesis 125 *
mp-wp_genesis 126 * @since 2.0.0
mp-wp_genesis 127 * @access public
mp-wp_genesis 128 *
mp-wp_genesis 129 * @param string $role Role name.
mp-wp_genesis 130 * @param string $display_name Role display name.
mp-wp_genesis 131 * @param array $capabilities List of role capabilities.
mp-wp_genesis 132 * @return null|WP_Role WP_Role object if role is added, null if already exists.
mp-wp_genesis 133 */
mp-wp_genesis 134 function add_role( $role, $display_name, $capabilities = array() ) {
mp-wp_genesis 135 if ( isset( $this->roles[$role] ) )
mp-wp_genesis 136 return;
mp-wp_genesis 137
mp-wp_genesis 138 $this->roles[$role] = array(
mp-wp_genesis 139 'name' => $display_name,
mp-wp_genesis 140 'capabilities' => $capabilities
mp-wp_genesis 141 );
mp-wp_genesis 142 if ( $this->use_db )
mp-wp_genesis 143 update_option( $this->role_key, $this->roles );
mp-wp_genesis 144 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
mp-wp_genesis 145 $this->role_names[$role] = $display_name;
mp-wp_genesis 146 return $this->role_objects[$role];
mp-wp_genesis 147 }
mp-wp_genesis 148
mp-wp_genesis 149 /**
mp-wp_genesis 150 * Remove role by name.
mp-wp_genesis 151 *
mp-wp_genesis 152 * @since 2.0.0
mp-wp_genesis 153 * @access public
mp-wp_genesis 154 *
mp-wp_genesis 155 * @param string $role Role name.
mp-wp_genesis 156 */
mp-wp_genesis 157 function remove_role( $role ) {
mp-wp_genesis 158 if ( ! isset( $this->role_objects[$role] ) )
mp-wp_genesis 159 return;
mp-wp_genesis 160
mp-wp_genesis 161 unset( $this->role_objects[$role] );
mp-wp_genesis 162 unset( $this->role_names[$role] );
mp-wp_genesis 163 unset( $this->roles[$role] );
mp-wp_genesis 164
mp-wp_genesis 165 if ( $this->use_db )
mp-wp_genesis 166 update_option( $this->role_key, $this->roles );
mp-wp_genesis 167 }
mp-wp_genesis 168
mp-wp_genesis 169 /**
mp-wp_genesis 170 * Add capability to role.
mp-wp_genesis 171 *
mp-wp_genesis 172 * @since 2.0.0
mp-wp_genesis 173 * @access public
mp-wp_genesis 174 *
mp-wp_genesis 175 * @param string $role Role name.
mp-wp_genesis 176 * @param string $cap Capability name.
mp-wp_genesis 177 * @param bool $grant Optional, default is true. Whether role is capable of preforming capability.
mp-wp_genesis 178 */
mp-wp_genesis 179 function add_cap( $role, $cap, $grant = true ) {
mp-wp_genesis 180 $this->roles[$role]['capabilities'][$cap] = $grant;
mp-wp_genesis 181 if ( $this->use_db )
mp-wp_genesis 182 update_option( $this->role_key, $this->roles );
mp-wp_genesis 183 }
mp-wp_genesis 184
mp-wp_genesis 185 /**
mp-wp_genesis 186 * Remove capability from role.
mp-wp_genesis 187 *
mp-wp_genesis 188 * @since 2.0.0
mp-wp_genesis 189 * @access public
mp-wp_genesis 190 *
mp-wp_genesis 191 * @param string $role Role name.
mp-wp_genesis 192 * @param string $cap Capability name.
mp-wp_genesis 193 */
mp-wp_genesis 194 function remove_cap( $role, $cap ) {
mp-wp_genesis 195 unset( $this->roles[$role]['capabilities'][$cap] );
mp-wp_genesis 196 if ( $this->use_db )
mp-wp_genesis 197 update_option( $this->role_key, $this->roles );
mp-wp_genesis 198 }
mp-wp_genesis 199
mp-wp_genesis 200 /**
mp-wp_genesis 201 * Retrieve role object by name.
mp-wp_genesis 202 *
mp-wp_genesis 203 * @since 2.0.0
mp-wp_genesis 204 * @access public
mp-wp_genesis 205 *
mp-wp_genesis 206 * @param string $role Role name.
mp-wp_genesis 207 * @return object|null Null, if role does not exist. WP_Role object, if found.
mp-wp_genesis 208 */
mp-wp_genesis 209 function &get_role( $role ) {
mp-wp_genesis 210 if ( isset( $this->role_objects[$role] ) )
mp-wp_genesis 211 return $this->role_objects[$role];
mp-wp_genesis 212 else
mp-wp_genesis 213 return null;
mp-wp_genesis 214 }
mp-wp_genesis 215
mp-wp_genesis 216 /**
mp-wp_genesis 217 * Retrieve list of role names.
mp-wp_genesis 218 *
mp-wp_genesis 219 * @since 2.0.0
mp-wp_genesis 220 * @access public
mp-wp_genesis 221 *
mp-wp_genesis 222 * @return array List of role names.
mp-wp_genesis 223 */
mp-wp_genesis 224 function get_names() {
mp-wp_genesis 225 return $this->role_names;
mp-wp_genesis 226 }
mp-wp_genesis 227
mp-wp_genesis 228 /**
mp-wp_genesis 229 * Whether role name is currently in the list of available roles.
mp-wp_genesis 230 *
mp-wp_genesis 231 * @since 2.0.0
mp-wp_genesis 232 * @access public
mp-wp_genesis 233 *
mp-wp_genesis 234 * @param string $role Role name to look up.
mp-wp_genesis 235 * @return bool
mp-wp_genesis 236 */
mp-wp_genesis 237 function is_role( $role )
mp-wp_genesis 238 {
mp-wp_genesis 239 return isset( $this->role_names[$role] );
mp-wp_genesis 240 }
mp-wp_genesis 241 }
mp-wp_genesis 242
mp-wp_genesis 243 /**
mp-wp_genesis 244 * WordPress Role class.
mp-wp_genesis 245 *
mp-wp_genesis 246 * @since 2.0.0
mp-wp_genesis 247 * @package WordPress
mp-wp_genesis 248 * @subpackage User
mp-wp_genesis 249 */
mp-wp_genesis 250 class WP_Role {
mp-wp_genesis 251 /**
mp-wp_genesis 252 * Role name.
mp-wp_genesis 253 *
mp-wp_genesis 254 * @since 2.0.0
mp-wp_genesis 255 * @access public
mp-wp_genesis 256 * @var string
mp-wp_genesis 257 */
mp-wp_genesis 258 var $name;
mp-wp_genesis 259
mp-wp_genesis 260 /**
mp-wp_genesis 261 * List of capabilities the role contains.
mp-wp_genesis 262 *
mp-wp_genesis 263 * @since 2.0.0
mp-wp_genesis 264 * @access public
mp-wp_genesis 265 * @var array
mp-wp_genesis 266 */
mp-wp_genesis 267 var $capabilities;
mp-wp_genesis 268
mp-wp_genesis 269 /**
mp-wp_genesis 270 * PHP4 Constructor - Setup object properties.
mp-wp_genesis 271 *
mp-wp_genesis 272 * The list of capabilities, must have the key as the name of the capability
mp-wp_genesis 273 * and the value a boolean of whether it is granted to the role or not.
mp-wp_genesis 274 *
mp-wp_genesis 275 * @since 2.0.0
mp-wp_genesis 276 * @access public
mp-wp_genesis 277 *
mp-wp_genesis 278 * @param string $role Role name.
mp-wp_genesis 279 * @param array $capabilities List of capabilities.
mp-wp_genesis 280 * @return WP_Role
mp-wp_genesis 281 */
mp-wp_genesis 282 function WP_Role( $role, $capabilities ) {
mp-wp_genesis 283 $this->name = $role;
mp-wp_genesis 284 $this->capabilities = $capabilities;
mp-wp_genesis 285 }
mp-wp_genesis 286
mp-wp_genesis 287 /**
mp-wp_genesis 288 * Assign role a capability.
mp-wp_genesis 289 *
mp-wp_genesis 290 * @see WP_Roles::add_cap() Method uses implementation for role.
mp-wp_genesis 291 * @since 2.0.0
mp-wp_genesis 292 * @access public
mp-wp_genesis 293 *
mp-wp_genesis 294 * @param string $cap Capability name.
mp-wp_genesis 295 * @param bool $grant Whether role has capability privilege.
mp-wp_genesis 296 */
mp-wp_genesis 297 function add_cap( $cap, $grant = true ) {
mp-wp_genesis 298 global $wp_roles;
mp-wp_genesis 299
mp-wp_genesis 300 if ( ! isset( $wp_roles ) )
mp-wp_genesis 301 $wp_roles = new WP_Roles();
mp-wp_genesis 302
mp-wp_genesis 303 $this->capabilities[$cap] = $grant;
mp-wp_genesis 304 $wp_roles->add_cap( $this->name, $cap, $grant );
mp-wp_genesis 305 }
mp-wp_genesis 306
mp-wp_genesis 307 /**
mp-wp_genesis 308 * Remove capability from role.
mp-wp_genesis 309 *
mp-wp_genesis 310 * This is a container for {@link WP_Roles::remove_cap()} to remove the
mp-wp_genesis 311 * capability from the role. That is to say, that {@link
mp-wp_genesis 312 * WP_Roles::remove_cap()} implements the functionality, but it also makes
mp-wp_genesis 313 * sense to use this class, because you don't need to enter the role name.
mp-wp_genesis 314 *
mp-wp_genesis 315 * @since 2.0.0
mp-wp_genesis 316 * @access public
mp-wp_genesis 317 *
mp-wp_genesis 318 * @param string $cap Capability name.
mp-wp_genesis 319 */
mp-wp_genesis 320 function remove_cap( $cap ) {
mp-wp_genesis 321 global $wp_roles;
mp-wp_genesis 322
mp-wp_genesis 323 if ( ! isset( $wp_roles ) )
mp-wp_genesis 324 $wp_roles = new WP_Roles();
mp-wp_genesis 325
mp-wp_genesis 326 unset( $this->capabilities[$cap] );
mp-wp_genesis 327 $wp_roles->remove_cap( $this->name, $cap );
mp-wp_genesis 328 }
mp-wp_genesis 329
mp-wp_genesis 330 /**
mp-wp_genesis 331 * Whether role has capability.
mp-wp_genesis 332 *
mp-wp_genesis 333 * The capabilities is passed through the 'role_has_cap' filter. The first
mp-wp_genesis 334 * parameter for the hook is the list of capabilities the class has
mp-wp_genesis 335 * assigned. The second parameter is the capability name to look for. The
mp-wp_genesis 336 * third and final parameter for the hook is the role name.
mp-wp_genesis 337 *
mp-wp_genesis 338 * @since 2.0.0
mp-wp_genesis 339 * @access public
mp-wp_genesis 340 *
mp-wp_genesis 341 * @param string $cap Capability name.
mp-wp_genesis 342 * @return bool True, if user has capability. False, if doesn't have capability.
mp-wp_genesis 343 */
mp-wp_genesis 344 function has_cap( $cap ) {
mp-wp_genesis 345 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
mp-wp_genesis 346 if ( !empty( $capabilities[$cap] ) )
mp-wp_genesis 347 return $capabilities[$cap];
mp-wp_genesis 348 else
mp-wp_genesis 349 return false;
mp-wp_genesis 350 }
mp-wp_genesis 351
mp-wp_genesis 352 }
mp-wp_genesis 353
mp-wp_genesis 354 /**
mp-wp_genesis 355 * WordPress User class.
mp-wp_genesis 356 *
mp-wp_genesis 357 * @since 2.0.0
mp-wp_genesis 358 * @package WordPress
mp-wp_genesis 359 * @subpackage User
mp-wp_genesis 360 */
mp-wp_genesis 361 class WP_User {
mp-wp_genesis 362 /**
mp-wp_genesis 363 * User data container.
mp-wp_genesis 364 *
mp-wp_genesis 365 * This will be set as properties of the object.
mp-wp_genesis 366 *
mp-wp_genesis 367 * @since 2.0.0
mp-wp_genesis 368 * @access private
mp-wp_genesis 369 * @var array
mp-wp_genesis 370 */
mp-wp_genesis 371 var $data;
mp-wp_genesis 372
mp-wp_genesis 373 /**
mp-wp_genesis 374 * The user's ID.
mp-wp_genesis 375 *
mp-wp_genesis 376 * @since 2.1.0
mp-wp_genesis 377 * @access public
mp-wp_genesis 378 * @var int
mp-wp_genesis 379 */
mp-wp_genesis 380 var $ID = 0;
mp-wp_genesis 381
mp-wp_genesis 382 /**
mp-wp_genesis 383 * The deprecated user's ID.
mp-wp_genesis 384 *
mp-wp_genesis 385 * @since 2.0.0
mp-wp_genesis 386 * @access public
mp-wp_genesis 387 * @deprecated Use WP_User::$ID
mp-wp_genesis 388 * @see WP_User::$ID
mp-wp_genesis 389 * @var int
mp-wp_genesis 390 */
mp-wp_genesis 391 var $id = 0;
mp-wp_genesis 392
mp-wp_genesis 393 /**
mp-wp_genesis 394 * The individual capabilities the user has been given.
mp-wp_genesis 395 *
mp-wp_genesis 396 * @since 2.0.0
mp-wp_genesis 397 * @access public
mp-wp_genesis 398 * @var array
mp-wp_genesis 399 */
mp-wp_genesis 400 var $caps = array();
mp-wp_genesis 401
mp-wp_genesis 402 /**
mp-wp_genesis 403 * User metadata option name.
mp-wp_genesis 404 *
mp-wp_genesis 405 * @since 2.0.0
mp-wp_genesis 406 * @access public
mp-wp_genesis 407 * @var string
mp-wp_genesis 408 */
mp-wp_genesis 409 var $cap_key;
mp-wp_genesis 410
mp-wp_genesis 411 /**
mp-wp_genesis 412 * The roles the user is part of.
mp-wp_genesis 413 *
mp-wp_genesis 414 * @since 2.0.0
mp-wp_genesis 415 * @access public
mp-wp_genesis 416 * @var array
mp-wp_genesis 417 */
mp-wp_genesis 418 var $roles = array();
mp-wp_genesis 419
mp-wp_genesis 420 /**
mp-wp_genesis 421 * All capabilities the user has, including individual and role based.
mp-wp_genesis 422 *
mp-wp_genesis 423 * @since 2.0.0
mp-wp_genesis 424 * @access public
mp-wp_genesis 425 * @var array
mp-wp_genesis 426 */
mp-wp_genesis 427 var $allcaps = array();
mp-wp_genesis 428
mp-wp_genesis 429 /**
mp-wp_genesis 430 * First name of the user.
mp-wp_genesis 431 *
mp-wp_genesis 432 * Created to prevent notices.
mp-wp_genesis 433 *
mp-wp_genesis 434 * @since 2.7.0
mp-wp_genesis 435 * @access public
mp-wp_genesis 436 * @var string
mp-wp_genesis 437 */
mp-wp_genesis 438 var $first_name = '';
mp-wp_genesis 439
mp-wp_genesis 440 /**
mp-wp_genesis 441 * Last name of the user.
mp-wp_genesis 442 *
mp-wp_genesis 443 * Created to prevent notices.
mp-wp_genesis 444 *
mp-wp_genesis 445 * @since 2.7.0
mp-wp_genesis 446 * @access public
mp-wp_genesis 447 * @var string
mp-wp_genesis 448 */
mp-wp_genesis 449 var $last_name = '';
mp-wp_genesis 450
mp-wp_genesis 451 /**
mp-wp_genesis 452 * PHP4 Constructor - Sets up the object properties.
mp-wp_genesis 453 *
mp-wp_genesis 454 * Retrieves the userdata and then assigns all of the data keys to direct
mp-wp_genesis 455 * properties of the object. Calls {@link WP_User::_init_caps()} after
mp-wp_genesis 456 * setting up the object's user data properties.
mp-wp_genesis 457 *
mp-wp_genesis 458 * @since 2.0.0
mp-wp_genesis 459 * @access public
mp-wp_genesis 460 *
mp-wp_genesis 461 * @param int|string $id User's ID or username
mp-wp_genesis 462 * @param int $name Optional. User's username
mp-wp_genesis 463 * @return WP_User
mp-wp_genesis 464 */
mp-wp_genesis 465 function WP_User( $id, $name = '' ) {
mp-wp_genesis 466
mp-wp_genesis 467 if ( empty( $id ) && empty( $name ) )
mp-wp_genesis 468 return;
mp-wp_genesis 469
mp-wp_genesis 470 if ( ! is_numeric( $id ) ) {
mp-wp_genesis 471 $name = $id;
mp-wp_genesis 472 $id = 0;
mp-wp_genesis 473 }
mp-wp_genesis 474
mp-wp_genesis 475 if ( ! empty( $id ) )
mp-wp_genesis 476 $this->data = get_userdata( $id );
mp-wp_genesis 477 else
mp-wp_genesis 478 $this->data = get_userdatabylogin( $name );
mp-wp_genesis 479
mp-wp_genesis 480 if ( empty( $this->data->ID ) )
mp-wp_genesis 481 return;
mp-wp_genesis 482
mp-wp_genesis 483 foreach ( get_object_vars( $this->data ) as $key => $value ) {
mp-wp_genesis 484 $this->{$key} = $value;
mp-wp_genesis 485 }
mp-wp_genesis 486
mp-wp_genesis 487 $this->id = $this->ID;
mp-wp_genesis 488 $this->_init_caps();
mp-wp_genesis 489 }
mp-wp_genesis 490
mp-wp_genesis 491 /**
mp-wp_genesis 492 * Setup capability object properties.
mp-wp_genesis 493 *
mp-wp_genesis 494 * Will set the value for the 'cap_key' property to current database table
mp-wp_genesis 495 * prefix, followed by 'capabilities'. Will then check to see if the
mp-wp_genesis 496 * property matching the 'cap_key' exists and is an array. If so, it will be
mp-wp_genesis 497 * used.
mp-wp_genesis 498 *
mp-wp_genesis 499 * @since 2.1.0
mp-wp_genesis 500 * @access protected
mp-wp_genesis 501 */
mp-wp_genesis 502 function _init_caps() {
mp-wp_genesis 503 global $wpdb;
mp-wp_genesis 504 $this->cap_key = $wpdb->prefix . 'capabilities';
mp-wp_genesis 505 $this->caps = &$this->{$this->cap_key};
mp-wp_genesis 506 if ( ! is_array( $this->caps ) )
mp-wp_genesis 507 $this->caps = array();
mp-wp_genesis 508 $this->get_role_caps();
mp-wp_genesis 509 }
mp-wp_genesis 510
mp-wp_genesis 511 /**
mp-wp_genesis 512 * Retrieve all of the role capabilities and merge with individual capabilities.
mp-wp_genesis 513 *
mp-wp_genesis 514 * All of the capabilities of the roles the user belongs to are merged with
mp-wp_genesis 515 * the users individual roles. This also means that the user can be denied
mp-wp_genesis 516 * specific roles that their role might have, but the specific user isn't
mp-wp_genesis 517 * granted permission to.
mp-wp_genesis 518 *
mp-wp_genesis 519 * @since 2.0.0
mp-wp_genesis 520 * @uses $wp_roles
mp-wp_genesis 521 * @access public
mp-wp_genesis 522 */
mp-wp_genesis 523 function get_role_caps() {
mp-wp_genesis 524 global $wp_roles;
mp-wp_genesis 525
mp-wp_genesis 526 if ( ! isset( $wp_roles ) )
mp-wp_genesis 527 $wp_roles = new WP_Roles();
mp-wp_genesis 528
mp-wp_genesis 529 //Filter out caps that are not role names and assign to $this->roles
mp-wp_genesis 530 if ( is_array( $this->caps ) )
mp-wp_genesis 531 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
mp-wp_genesis 532
mp-wp_genesis 533 //Build $allcaps from role caps, overlay user's $caps
mp-wp_genesis 534 $this->allcaps = array();
mp-wp_genesis 535 foreach ( (array) $this->roles as $role ) {
mp-wp_genesis 536 $role = $wp_roles->get_role( $role );
mp-wp_genesis 537 $this->allcaps = array_merge( $this->allcaps, $role->capabilities );
mp-wp_genesis 538 }
mp-wp_genesis 539 $this->allcaps = array_merge( $this->allcaps, $this->caps );
mp-wp_genesis 540 }
mp-wp_genesis 541
mp-wp_genesis 542 /**
mp-wp_genesis 543 * Add role to user.
mp-wp_genesis 544 *
mp-wp_genesis 545 * Updates the user's meta data option with capabilities and roles.
mp-wp_genesis 546 *
mp-wp_genesis 547 * @since 2.0.0
mp-wp_genesis 548 * @access public
mp-wp_genesis 549 *
mp-wp_genesis 550 * @param string $role Role name.
mp-wp_genesis 551 */
mp-wp_genesis 552 function add_role( $role ) {
mp-wp_genesis 553 $this->caps[$role] = true;
mp-wp_genesis 554 update_usermeta( $this->ID, $this->cap_key, $this->caps );
mp-wp_genesis 555 $this->get_role_caps();
mp-wp_genesis 556 $this->update_user_level_from_caps();
mp-wp_genesis 557 }
mp-wp_genesis 558
mp-wp_genesis 559 /**
mp-wp_genesis 560 * Remove role from user.
mp-wp_genesis 561 *
mp-wp_genesis 562 * @since 2.0.0
mp-wp_genesis 563 * @access public
mp-wp_genesis 564 *
mp-wp_genesis 565 * @param string $role Role name.
mp-wp_genesis 566 */
mp-wp_genesis 567 function remove_role( $role ) {
mp-wp_genesis 568 if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
mp-wp_genesis 569 return;
mp-wp_genesis 570 unset( $this->caps[$role] );
mp-wp_genesis 571 update_usermeta( $this->ID, $this->cap_key, $this->caps );
mp-wp_genesis 572 $this->get_role_caps();
mp-wp_genesis 573 }
mp-wp_genesis 574
mp-wp_genesis 575 /**
mp-wp_genesis 576 * Set the role of the user.
mp-wp_genesis 577 *
mp-wp_genesis 578 * This will remove the previous roles of the user and assign the user the
mp-wp_genesis 579 * new one. You can set the role to an empty string and it will remove all
mp-wp_genesis 580 * of the roles from the user.
mp-wp_genesis 581 *
mp-wp_genesis 582 * @since 2.0.0
mp-wp_genesis 583 * @access public
mp-wp_genesis 584 *
mp-wp_genesis 585 * @param string $role Role name.
mp-wp_genesis 586 */
mp-wp_genesis 587 function set_role( $role ) {
mp-wp_genesis 588 foreach ( (array) $this->roles as $oldrole )
mp-wp_genesis 589 unset( $this->caps[$oldrole] );
mp-wp_genesis 590 if ( !empty( $role ) ) {
mp-wp_genesis 591 $this->caps[$role] = true;
mp-wp_genesis 592 $this->roles = array( $role => true );
mp-wp_genesis 593 } else {
mp-wp_genesis 594 $this->roles = false;
mp-wp_genesis 595 }
mp-wp_genesis 596 update_usermeta( $this->ID, $this->cap_key, $this->caps );
mp-wp_genesis 597 $this->get_role_caps();
mp-wp_genesis 598 $this->update_user_level_from_caps();
mp-wp_genesis 599 }
mp-wp_genesis 600
mp-wp_genesis 601 /**
mp-wp_genesis 602 * Choose the maximum level the user has.
mp-wp_genesis 603 *
mp-wp_genesis 604 * Will compare the level from the $item parameter against the $max
mp-wp_genesis 605 * parameter. If the item is incorrect, then just the $max parameter value
mp-wp_genesis 606 * will be returned.
mp-wp_genesis 607 *
mp-wp_genesis 608 * Used to get the max level based on the capabilities the user has. This
mp-wp_genesis 609 * is also based on roles, so if the user is assigned the Administrator role
mp-wp_genesis 610 * then the capability 'level_10' will exist and the user will get that
mp-wp_genesis 611 * value.
mp-wp_genesis 612 *
mp-wp_genesis 613 * @since 2.0.0
mp-wp_genesis 614 * @access public
mp-wp_genesis 615 *
mp-wp_genesis 616 * @param int $max Max level of user.
mp-wp_genesis 617 * @param string $item Level capability name.
mp-wp_genesis 618 * @return int Max Level.
mp-wp_genesis 619 */
mp-wp_genesis 620 function level_reduction( $max, $item ) {
mp-wp_genesis 621 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
mp-wp_genesis 622 $level = intval( $matches[1] );
mp-wp_genesis 623 return max( $max, $level );
mp-wp_genesis 624 } else {
mp-wp_genesis 625 return $max;
mp-wp_genesis 626 }
mp-wp_genesis 627 }
mp-wp_genesis 628
mp-wp_genesis 629 /**
mp-wp_genesis 630 * Update the maximum user level for the user.
mp-wp_genesis 631 *
mp-wp_genesis 632 * Updates the 'user_level' user metadata (includes prefix that is the
mp-wp_genesis 633 * database table prefix) with the maximum user level. Gets the value from
mp-wp_genesis 634 * the all of the capabilities that the user has.
mp-wp_genesis 635 *
mp-wp_genesis 636 * @since 2.0.0
mp-wp_genesis 637 * @access public
mp-wp_genesis 638 */
mp-wp_genesis 639 function update_user_level_from_caps() {
mp-wp_genesis 640 global $wpdb;
mp-wp_genesis 641 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
mp-wp_genesis 642 update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
mp-wp_genesis 643 }
mp-wp_genesis 644
mp-wp_genesis 645 /**
mp-wp_genesis 646 * Add capability and grant or deny access to capability.
mp-wp_genesis 647 *
mp-wp_genesis 648 * @since 2.0.0
mp-wp_genesis 649 * @access public
mp-wp_genesis 650 *
mp-wp_genesis 651 * @param string $cap Capability name.
mp-wp_genesis 652 * @param bool $grant Whether to grant capability to user.
mp-wp_genesis 653 */
mp-wp_genesis 654 function add_cap( $cap, $grant = true ) {
mp-wp_genesis 655 $this->caps[$cap] = $grant;
mp-wp_genesis 656 update_usermeta( $this->ID, $this->cap_key, $this->caps );
mp-wp_genesis 657 }
mp-wp_genesis 658
mp-wp_genesis 659 /**
mp-wp_genesis 660 * Remove capability from user.
mp-wp_genesis 661 *
mp-wp_genesis 662 * @since 2.0.0
mp-wp_genesis 663 * @access public
mp-wp_genesis 664 *
mp-wp_genesis 665 * @param string $cap Capability name.
mp-wp_genesis 666 */
mp-wp_genesis 667 function remove_cap( $cap ) {
mp-wp_genesis 668 if ( empty( $this->caps[$cap] ) ) return;
mp-wp_genesis 669 unset( $this->caps[$cap] );
mp-wp_genesis 670 update_usermeta( $this->ID, $this->cap_key, $this->caps );
mp-wp_genesis 671 }
mp-wp_genesis 672
mp-wp_genesis 673 /**
mp-wp_genesis 674 * Remove all of the capabilities of the user.
mp-wp_genesis 675 *
mp-wp_genesis 676 * @since 2.1.0
mp-wp_genesis 677 * @access public
mp-wp_genesis 678 */
mp-wp_genesis 679 function remove_all_caps() {
mp-wp_genesis 680 global $wpdb;
mp-wp_genesis 681 $this->caps = array();
mp-wp_genesis 682 update_usermeta( $this->ID, $this->cap_key, '' );
mp-wp_genesis 683 update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
mp-wp_genesis 684 $this->get_role_caps();
mp-wp_genesis 685 }
mp-wp_genesis 686
mp-wp_genesis 687 /**
mp-wp_genesis 688 * Whether user has capability or role name.
mp-wp_genesis 689 *
mp-wp_genesis 690 * This is useful for looking up whether the user has a specific role
mp-wp_genesis 691 * assigned to the user. The second optional parameter can also be used to
mp-wp_genesis 692 * check for capabilities against a specfic post.
mp-wp_genesis 693 *
mp-wp_genesis 694 * @since 2.0.0
mp-wp_genesis 695 * @access public
mp-wp_genesis 696 *
mp-wp_genesis 697 * @param string|int $cap Capability or role name to search.
mp-wp_genesis 698 * @param int $post_id Optional. Post ID to check capability against specific post.
mp-wp_genesis 699 * @return bool True, if user has capability; false, if user does not have capability.
mp-wp_genesis 700 */
mp-wp_genesis 701 function has_cap( $cap ) {
mp-wp_genesis 702 if ( is_numeric( $cap ) )
mp-wp_genesis 703 $cap = $this->translate_level_to_cap( $cap );
mp-wp_genesis 704
mp-wp_genesis 705 $args = array_slice( func_get_args(), 1 );
mp-wp_genesis 706 $args = array_merge( array( $cap, $this->ID ), $args );
mp-wp_genesis 707 $caps = call_user_func_array( 'map_meta_cap', $args );
mp-wp_genesis 708 // Must have ALL requested caps
mp-wp_genesis 709 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
mp-wp_genesis 710 foreach ( (array) $caps as $cap ) {
mp-wp_genesis 711 //echo "Checking cap $cap<br />";
mp-wp_genesis 712 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
mp-wp_genesis 713 return false;
mp-wp_genesis 714 }
mp-wp_genesis 715
mp-wp_genesis 716 return true;
mp-wp_genesis 717 }
mp-wp_genesis 718
mp-wp_genesis 719 /**
mp-wp_genesis 720 * Convert numeric level to level capability name.
mp-wp_genesis 721 *
mp-wp_genesis 722 * Prepends 'level_' to level number.
mp-wp_genesis 723 *
mp-wp_genesis 724 * @since 2.0.0
mp-wp_genesis 725 * @access public
mp-wp_genesis 726 *
mp-wp_genesis 727 * @param int $level Level number, 1 to 10.
mp-wp_genesis 728 * @return string
mp-wp_genesis 729 */
mp-wp_genesis 730 function translate_level_to_cap( $level ) {
mp-wp_genesis 731 return 'level_' . $level;
mp-wp_genesis 732 }
mp-wp_genesis 733
mp-wp_genesis 734 }
mp-wp_genesis 735
mp-wp_genesis 736 /**
mp-wp_genesis 737 * Map meta capabilities to primitive capabilities.
mp-wp_genesis 738 *
mp-wp_genesis 739 * This does not actually compare whether the user ID has the actual capability,
mp-wp_genesis 740 * just what the capability or capabilities are. Meta capability list value can
mp-wp_genesis 741 * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
mp-wp_genesis 742 * 'edit_page', 'read_post', or 'read_page'.
mp-wp_genesis 743 *
mp-wp_genesis 744 * @since 2.0.0
mp-wp_genesis 745 *
mp-wp_genesis 746 * @param string $cap Capability name.
mp-wp_genesis 747 * @param int $user_id User ID.
mp-wp_genesis 748 * @return array Actual capabilities for meta capability.
mp-wp_genesis 749 */
mp-wp_genesis 750 function map_meta_cap( $cap, $user_id ) {
mp-wp_genesis 751 $args = array_slice( func_get_args(), 2 );
mp-wp_genesis 752 $caps = array();
mp-wp_genesis 753
mp-wp_genesis 754 switch ( $cap ) {
mp-wp_genesis 755 case 'delete_user':
mp-wp_genesis 756 $caps[] = 'delete_users';
mp-wp_genesis 757 break;
mp-wp_genesis 758 case 'edit_user':
mp-wp_genesis 759 if ( !isset( $args[0] ) || $user_id != $args[0] ) {
mp-wp_genesis 760 $caps[] = 'edit_users';
mp-wp_genesis 761 }
mp-wp_genesis 762 break;
mp-wp_genesis 763 case 'delete_post':
mp-wp_genesis 764 $author_data = get_userdata( $user_id );
mp-wp_genesis 765 //echo "post ID: {$args[0]}<br />";
mp-wp_genesis 766 $post = get_post( $args[0] );
mp-wp_genesis 767 if ( 'page' == $post->post_type ) {
mp-wp_genesis 768 $args = array_merge( array( 'delete_page', $user_id ), $args );
mp-wp_genesis 769 return call_user_func_array( 'map_meta_cap', $args );
mp-wp_genesis 770 }
mp-wp_genesis 771 $post_author_data = get_userdata( $post->post_author );
mp-wp_genesis 772 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
mp-wp_genesis 773 // If the user is the author...
mp-wp_genesis 774 if ( $user_id == $post_author_data->ID ) {
mp-wp_genesis 775 // If the post is published...
mp-wp_genesis 776 if ( 'publish' == $post->post_status )
mp-wp_genesis 777 $caps[] = 'delete_published_posts';
mp-wp_genesis 778 else
mp-wp_genesis 779 // If the post is draft...
mp-wp_genesis 780 $caps[] = 'delete_posts';
mp-wp_genesis 781 } else {
mp-wp_genesis 782 // The user is trying to edit someone else's post.
mp-wp_genesis 783 $caps[] = 'delete_others_posts';
mp-wp_genesis 784 // The post is published, extra cap required.
mp-wp_genesis 785 if ( 'publish' == $post->post_status )
mp-wp_genesis 786 $caps[] = 'delete_published_posts';
mp-wp_genesis 787 elseif ( 'private' == $post->post_status )
mp-wp_genesis 788 $caps[] = 'delete_private_posts';
mp-wp_genesis 789 }
mp-wp_genesis 790 break;
mp-wp_genesis 791 case 'delete_page':
mp-wp_genesis 792 $author_data = get_userdata( $user_id );
mp-wp_genesis 793 //echo "post ID: {$args[0]}<br />";
mp-wp_genesis 794 $page = get_page( $args[0] );
mp-wp_genesis 795 $page_author_data = get_userdata( $page->post_author );
mp-wp_genesis 796 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
mp-wp_genesis 797 // If the user is the author...
mp-wp_genesis 798 if ( $user_id == $page_author_data->ID ) {
mp-wp_genesis 799 // If the page is published...
mp-wp_genesis 800 if ( $page->post_status == 'publish' )
mp-wp_genesis 801 $caps[] = 'delete_published_pages';
mp-wp_genesis 802 else
mp-wp_genesis 803 // If the page is draft...
mp-wp_genesis 804 $caps[] = 'delete_pages';
mp-wp_genesis 805 } else {
mp-wp_genesis 806 // The user is trying to edit someone else's page.
mp-wp_genesis 807 $caps[] = 'delete_others_pages';
mp-wp_genesis 808 // The page is published, extra cap required.
mp-wp_genesis 809 if ( $page->post_status == 'publish' )
mp-wp_genesis 810 $caps[] = 'delete_published_pages';
mp-wp_genesis 811 elseif ( $page->post_status == 'private' )
mp-wp_genesis 812 $caps[] = 'delete_private_pages';
mp-wp_genesis 813 }
mp-wp_genesis 814 break;
mp-wp_genesis 815 // edit_post breaks down to edit_posts, edit_published_posts, or
mp-wp_genesis 816 // edit_others_posts
mp-wp_genesis 817 case 'edit_post':
mp-wp_genesis 818 $author_data = get_userdata( $user_id );
mp-wp_genesis 819 //echo "post ID: {$args[0]}<br />";
mp-wp_genesis 820 $post = get_post( $args[0] );
mp-wp_genesis 821 if ( 'page' == $post->post_type ) {
mp-wp_genesis 822 $args = array_merge( array( 'edit_page', $user_id ), $args );
mp-wp_genesis 823 return call_user_func_array( 'map_meta_cap', $args );
mp-wp_genesis 824 }
mp-wp_genesis 825 $post_author_data = get_userdata( $post->post_author );
mp-wp_genesis 826 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
mp-wp_genesis 827 // If the user is the author...
mp-wp_genesis 828 if ( $user_id == $post_author_data->ID ) {
mp-wp_genesis 829 // If the post is published...
mp-wp_genesis 830 if ( 'publish' == $post->post_status )
mp-wp_genesis 831 $caps[] = 'edit_published_posts';
mp-wp_genesis 832 else
mp-wp_genesis 833 // If the post is draft...
mp-wp_genesis 834 $caps[] = 'edit_posts';
mp-wp_genesis 835 } else {
mp-wp_genesis 836 // The user is trying to edit someone else's post.
mp-wp_genesis 837 $caps[] = 'edit_others_posts';
mp-wp_genesis 838 // The post is published, extra cap required.
mp-wp_genesis 839 if ( 'publish' == $post->post_status )
mp-wp_genesis 840 $caps[] = 'edit_published_posts';
mp-wp_genesis 841 elseif ( 'private' == $post->post_status )
mp-wp_genesis 842 $caps[] = 'edit_private_posts';
mp-wp_genesis 843 }
mp-wp_genesis 844 break;
mp-wp_genesis 845 case 'edit_page':
mp-wp_genesis 846 $author_data = get_userdata( $user_id );
mp-wp_genesis 847 //echo "post ID: {$args[0]}<br />";
mp-wp_genesis 848 $page = get_page( $args[0] );
mp-wp_genesis 849 $page_author_data = get_userdata( $page->post_author );
mp-wp_genesis 850 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
mp-wp_genesis 851 // If the user is the author...
mp-wp_genesis 852 if ( $user_id == $page_author_data->ID ) {
mp-wp_genesis 853 // If the page is published...
mp-wp_genesis 854 if ( 'publish' == $page->post_status )
mp-wp_genesis 855 $caps[] = 'edit_published_pages';
mp-wp_genesis 856 else
mp-wp_genesis 857 // If the page is draft...
mp-wp_genesis 858 $caps[] = 'edit_pages';
mp-wp_genesis 859 } else {
mp-wp_genesis 860 // The user is trying to edit someone else's page.
mp-wp_genesis 861 $caps[] = 'edit_others_pages';
mp-wp_genesis 862 // The page is published, extra cap required.
mp-wp_genesis 863 if ( 'publish' == $page->post_status )
mp-wp_genesis 864 $caps[] = 'edit_published_pages';
mp-wp_genesis 865 elseif ( 'private' == $page->post_status )
mp-wp_genesis 866 $caps[] = 'edit_private_pages';
mp-wp_genesis 867 }
mp-wp_genesis 868 break;
mp-wp_genesis 869 case 'read_post':
mp-wp_genesis 870 $post = get_post( $args[0] );
mp-wp_genesis 871 if ( 'page' == $post->post_type ) {
mp-wp_genesis 872 $args = array_merge( array( 'read_page', $user_id ), $args );
mp-wp_genesis 873 return call_user_func_array( 'map_meta_cap', $args );
mp-wp_genesis 874 }
mp-wp_genesis 875
mp-wp_genesis 876 if ( 'private' != $post->post_status ) {
mp-wp_genesis 877 $caps[] = 'read';
mp-wp_genesis 878 break;
mp-wp_genesis 879 }
mp-wp_genesis 880
mp-wp_genesis 881 $author_data = get_userdata( $user_id );
mp-wp_genesis 882 $post_author_data = get_userdata( $post->post_author );
mp-wp_genesis 883 if ( $user_id == $post_author_data->ID )
mp-wp_genesis 884 $caps[] = 'read';
mp-wp_genesis 885 else
mp-wp_genesis 886 $caps[] = 'read_private_posts';
mp-wp_genesis 887 break;
mp-wp_genesis 888 case 'read_page':
mp-wp_genesis 889 $page = get_page( $args[0] );
mp-wp_genesis 890
mp-wp_genesis 891 if ( 'private' != $page->post_status ) {
mp-wp_genesis 892 $caps[] = 'read';
mp-wp_genesis 893 break;
mp-wp_genesis 894 }
mp-wp_genesis 895
mp-wp_genesis 896 $author_data = get_userdata( $user_id );
mp-wp_genesis 897 $page_author_data = get_userdata( $page->post_author );
mp-wp_genesis 898 if ( $user_id == $page_author_data->ID )
mp-wp_genesis 899 $caps[] = 'read';
mp-wp_genesis 900 else
mp-wp_genesis 901 $caps[] = 'read_private_pages';
mp-wp_genesis 902 break;
mp-wp_genesis 903 default:
mp-wp_genesis 904 // If no meta caps match, return the original cap.
mp-wp_genesis 905 $caps[] = $cap;
mp-wp_genesis 906 }
mp-wp_genesis 907
mp-wp_genesis 908 return $caps;
mp-wp_genesis 909 }
mp-wp_genesis 910
mp-wp_genesis 911 /**
mp-wp_genesis 912 * Whether current user has capability or role.
mp-wp_genesis 913 *
mp-wp_genesis 914 * @since 2.0.0
mp-wp_genesis 915 *
mp-wp_genesis 916 * @param string $capability Capability or role name.
mp-wp_genesis 917 * @return bool
mp-wp_genesis 918 */
mp-wp_genesis 919 function current_user_can( $capability ) {
mp-wp_genesis 920 $current_user = wp_get_current_user();
mp-wp_genesis 921
mp-wp_genesis 922 if ( empty( $current_user ) )
mp-wp_genesis 923 return false;
mp-wp_genesis 924
mp-wp_genesis 925 $args = array_slice( func_get_args(), 1 );
mp-wp_genesis 926 $args = array_merge( array( $capability ), $args );
mp-wp_genesis 927
mp-wp_genesis 928 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
mp-wp_genesis 929 }
mp-wp_genesis 930
mp-wp_genesis 931 /**
mp-wp_genesis 932 * Retrieve role object.
mp-wp_genesis 933 *
mp-wp_genesis 934 * @see WP_Roles::get_role() Uses method to retrieve role object.
mp-wp_genesis 935 * @since 2.0.0
mp-wp_genesis 936 *
mp-wp_genesis 937 * @param string $role Role name.
mp-wp_genesis 938 * @return object
mp-wp_genesis 939 */
mp-wp_genesis 940 function get_role( $role ) {
mp-wp_genesis 941 global $wp_roles;
mp-wp_genesis 942
mp-wp_genesis 943 if ( ! isset( $wp_roles ) )
mp-wp_genesis 944 $wp_roles = new WP_Roles();
mp-wp_genesis 945
mp-wp_genesis 946 return $wp_roles->get_role( $role );
mp-wp_genesis 947 }
mp-wp_genesis 948
mp-wp_genesis 949 /**
mp-wp_genesis 950 * Add role, if it does not exist.
mp-wp_genesis 951 *
mp-wp_genesis 952 * @see WP_Roles::add_role() Uses method to add role.
mp-wp_genesis 953 * @since 2.0.0
mp-wp_genesis 954 *
mp-wp_genesis 955 * @param string $role Role name.
mp-wp_genesis 956 * @param string $display_name Display name for role.
mp-wp_genesis 957 * @param array $capabilities List of capabilities.
mp-wp_genesis 958 * @return null|WP_Role WP_Role object if role is added, null if already exists.
mp-wp_genesis 959 */
mp-wp_genesis 960 function add_role( $role, $display_name, $capabilities = array() ) {
mp-wp_genesis 961 global $wp_roles;
mp-wp_genesis 962
mp-wp_genesis 963 if ( ! isset( $wp_roles ) )
mp-wp_genesis 964 $wp_roles = new WP_Roles();
mp-wp_genesis 965
mp-wp_genesis 966 return $wp_roles->add_role( $role, $display_name, $capabilities );
mp-wp_genesis 967 }
mp-wp_genesis 968
mp-wp_genesis 969 /**
mp-wp_genesis 970 * Remove role, if it exists.
mp-wp_genesis 971 *
mp-wp_genesis 972 * @see WP_Roles::remove_role() Uses method to remove role.
mp-wp_genesis 973 * @since 2.0.0
mp-wp_genesis 974 *
mp-wp_genesis 975 * @param string $role Role name.
mp-wp_genesis 976 * @return null
mp-wp_genesis 977 */
mp-wp_genesis 978 function remove_role( $role ) {
mp-wp_genesis 979 global $wp_roles;
mp-wp_genesis 980
mp-wp_genesis 981 if ( ! isset( $wp_roles ) )
mp-wp_genesis 982 $wp_roles = new WP_Roles();
mp-wp_genesis 983
mp-wp_genesis 984 return $wp_roles->remove_role( $role );
mp-wp_genesis 985 }
mp-wp_genesis 986
mp-wp_genesis 987 ?>