web/wp-includes/capabilities.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    71 	 * @var bool
    71 	 * @var bool
    72 	 */
    72 	 */
    73 	var $use_db = true;
    73 	var $use_db = true;
    74 
    74 
    75 	/**
    75 	/**
    76 	 * PHP4 Constructor - Call {@link WP_Roles::_init()} method.
    76 	 * Constructor
    77 	 *
    77 	 *
    78 	 * @since 2.0.0
    78 	 * @since 2.0.0
    79 	 * @access public
    79 	 */
    80 	 *
    80 	function __construct() {
    81 	 * @return WP_Roles
       
    82 	 */
       
    83 	function WP_Roles() {
       
    84 		$this->_init();
    81 		$this->_init();
    85 	}
    82 	}
    86 
    83 
    87 	/**
    84 	/**
    88 	 * Setup the object properties.
    85 	 * Set up the object properties.
    89 	 *
    86 	 *
    90 	 * The role key is set to the current prefix for the $wpdb object with
    87 	 * 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
    88 	 * '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.
    89 	 * be used and the role option will not be updated or used.
    93 	 *
    90 	 *
    95 	 * @access protected
    92 	 * @access protected
    96 	 * @uses $wpdb Used to get the database prefix.
    93 	 * @uses $wpdb Used to get the database prefix.
    97 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    94 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    98 	 */
    95 	 */
    99 	function _init () {
    96 	function _init () {
   100 		global $wpdb;
    97 		global $wpdb, $wp_user_roles;
   101 		global $wp_user_roles;
       
   102 		$this->role_key = $wpdb->prefix . 'user_roles';
    98 		$this->role_key = $wpdb->prefix . 'user_roles';
   103 		if ( ! empty( $wp_user_roles ) ) {
    99 		if ( ! empty( $wp_user_roles ) ) {
   104 			$this->roles = $wp_user_roles;
   100 			$this->roles = $wp_user_roles;
   105 			$this->use_db = false;
   101 			$this->use_db = false;
   106 		} else {
   102 		} else {
   123 	 *
   119 	 *
   124 	 * Updates the list of roles, if the role doesn't already exist.
   120 	 * Updates the list of roles, if the role doesn't already exist.
   125 	 *
   121 	 *
   126 	 * The capabilities are defined in the following format `array( 'read' => true );`
   122 	 * The capabilities are defined in the following format `array( 'read' => true );`
   127 	 * To explicitly deny a role a capability you set the value for that capability to false.
   123 	 * To explicitly deny a role a capability you set the value for that capability to false.
   128 	 * 
   124 	 *
   129 	 * @since 2.0.0
   125 	 * @since 2.0.0
   130 	 * @access public
   126 	 * @access public
   131 	 *
   127 	 *
   132 	 * @param string $role Role name.
   128 	 * @param string $role Role name.
   133 	 * @param string $display_name Role display name.
   129 	 * @param string $display_name Role display name.
   207 	 * @access public
   203 	 * @access public
   208 	 *
   204 	 *
   209 	 * @param string $role Role name.
   205 	 * @param string $role Role name.
   210 	 * @return object|null Null, if role does not exist. WP_Role object, if found.
   206 	 * @return object|null Null, if role does not exist. WP_Role object, if found.
   211 	 */
   207 	 */
   212 	function &get_role( $role ) {
   208 	function get_role( $role ) {
   213 		if ( isset( $this->role_objects[$role] ) )
   209 		if ( isset( $this->role_objects[$role] ) )
   214 			return $this->role_objects[$role];
   210 			return $this->role_objects[$role];
   215 		else
   211 		else
   216 			return null;
   212 			return null;
   217 	}
   213 	}
   268 	 * @var array
   264 	 * @var array
   269 	 */
   265 	 */
   270 	var $capabilities;
   266 	var $capabilities;
   271 
   267 
   272 	/**
   268 	/**
   273 	 * PHP4 Constructor - Setup object properties.
   269 	 * Constructor - Set up object properties.
   274 	 *
   270 	 *
   275 	 * The list of capabilities, must have the key as the name of the capability
   271 	 * The list of capabilities, must have the key as the name of the capability
   276 	 * and the value a boolean of whether it is granted to the role or not.
   272 	 * and the value a boolean of whether it is granted to the role.
   277 	 *
   273 	 *
   278 	 * @since 2.0.0
   274 	 * @since 2.0.0
   279 	 * @access public
   275 	 * @access public
   280 	 *
   276 	 *
   281 	 * @param string $role Role name.
   277 	 * @param string $role Role name.
   282 	 * @param array $capabilities List of capabilities.
   278 	 * @param array $capabilities List of capabilities.
   283 	 * @return WP_Role
   279 	 */
   284 	 */
   280 	function __construct( $role, $capabilities ) {
   285 	function WP_Role( $role, $capabilities ) {
       
   286 		$this->name = $role;
   281 		$this->name = $role;
   287 		$this->capabilities = $capabilities;
   282 		$this->capabilities = $capabilities;
   288 	}
   283 	}
   289 
   284 
   290 	/**
   285 	/**
   363  */
   358  */
   364 class WP_User {
   359 class WP_User {
   365 	/**
   360 	/**
   366 	 * User data container.
   361 	 * User data container.
   367 	 *
   362 	 *
   368 	 * This will be set as properties of the object.
       
   369 	 *
       
   370 	 * @since 2.0.0
   363 	 * @since 2.0.0
   371 	 * @access private
   364 	 * @access private
   372 	 * @var array
   365 	 * @var array
   373 	 */
   366 	 */
   374 	var $data;
   367 	var $data;
   381 	 * @var int
   374 	 * @var int
   382 	 */
   375 	 */
   383 	var $ID = 0;
   376 	var $ID = 0;
   384 
   377 
   385 	/**
   378 	/**
   386 	 * The deprecated user's ID.
       
   387 	 *
       
   388 	 * @since 2.0.0
       
   389 	 * @access public
       
   390 	 * @deprecated Use WP_User::$ID
       
   391 	 * @see WP_User::$ID
       
   392 	 * @var int
       
   393 	 */
       
   394 	var $id = 0;
       
   395 
       
   396 	/**
       
   397 	 * The individual capabilities the user has been given.
   379 	 * The individual capabilities the user has been given.
   398 	 *
   380 	 *
   399 	 * @since 2.0.0
   381 	 * @since 2.0.0
   400 	 * @access public
   382 	 * @access public
   401 	 * @var array
   383 	 * @var array
   426 	 * @since 2.0.0
   408 	 * @since 2.0.0
   427 	 * @access public
   409 	 * @access public
   428 	 * @var array
   410 	 * @var array
   429 	 */
   411 	 */
   430 	var $allcaps = array();
   412 	var $allcaps = array();
   431 
       
   432 	/**
       
   433 	 * First name of the user.
       
   434 	 *
       
   435 	 * Created to prevent notices.
       
   436 	 *
       
   437 	 * @since 2.7.0
       
   438 	 * @access public
       
   439 	 * @var string
       
   440 	 */
       
   441 	var $first_name = '';
       
   442 
       
   443 	/**
       
   444 	 * Last name of the user.
       
   445 	 *
       
   446 	 * Created to prevent notices.
       
   447 	 *
       
   448 	 * @since 2.7.0
       
   449 	 * @access public
       
   450 	 * @var string
       
   451 	 */
       
   452 	var $last_name = '';
       
   453 
   413 
   454 	/**
   414 	/**
   455 	 * The filter context applied to user data fields.
   415 	 * The filter context applied to user data fields.
   456 	 *
   416 	 *
   457 	 * @since 2.9.0
   417 	 * @since 2.9.0
   458 	 * @access private
   418 	 * @access private
   459 	 * @var string
   419 	 * @var string
   460 	 */
   420 	 */
   461 	var $filter = null;
   421 	var $filter = null;
   462 
   422 
   463 	/**
   423 	private static $back_compat_keys;
   464 	 * PHP4 Constructor - Sets up the object properties.
   424 
   465 	 *
   425 	/**
   466 	 * Retrieves the userdata and then assigns all of the data keys to direct
   426 	 * Constructor
   467 	 * properties of the object. Calls {@link WP_User::_init_caps()} after
   427 	 *
   468 	 * setting up the object's user data properties.
   428 	 * Retrieves the userdata and passes it to {@link WP_User::init()}.
   469 	 *
   429 	 *
   470 	 * @since 2.0.0
   430 	 * @since 2.0.0
   471 	 * @access public
   431 	 * @access public
   472 	 *
   432 	 *
   473 	 * @param int|string $id User's ID or username
   433 	 * @param int|string $id User's ID
   474 	 * @param int $name Optional. User's username
   434 	 * @param string $name Optional. User's username
       
   435 	 * @param int $blog_id Optional Blog ID, defaults to current blog.
   475 	 * @return WP_User
   436 	 * @return WP_User
   476 	 */
   437 	 */
   477 	function WP_User( $id, $name = '' ) {
   438 	function __construct( $id = 0, $name = '', $blog_id = '' ) {
   478 
   439 		if ( ! isset( self::$back_compat_keys ) ) {
   479 		if ( empty( $id ) && empty( $name ) )
   440 			$prefix = $GLOBALS['wpdb']->prefix;
   480 			return;
   441 			self::$back_compat_keys = array(
   481 
   442 				'user_firstname' => 'first_name',
   482 		if ( ! is_numeric( $id ) ) {
   443 				'user_lastname' => 'last_name',
       
   444 				'user_description' => 'description',
       
   445 				'user_level' => $prefix . 'user_level',
       
   446 				$prefix . 'usersettings' => $prefix . 'user-settings',
       
   447 				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
       
   448 			);
       
   449 		}
       
   450 
       
   451 		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
   483 			$name = $id;
   452 			$name = $id;
   484 			$id = 0;
   453 			$id = 0;
   485 		}
   454 		}
   486 
   455 
   487 		if ( ! empty( $id ) )
   456 		if ( $id )
   488 			$this->data = get_userdata( $id );
   457 			$data = self::get_data_by( 'id', $id );
   489 		else
   458 		else
   490 			$this->data = get_userdatabylogin( $name );
   459 			$data = self::get_data_by( 'login', $name );
   491 
   460 
   492 		if ( empty( $this->data->ID ) )
   461 		if ( $data )
       
   462 			$this->init( $data, $blog_id );
       
   463 	}
       
   464 
       
   465 	/**
       
   466 	 * Sets up object properties, including capabilities.
       
   467 	 *
       
   468 	 * @param object $data User DB row object
       
   469 	 * @param int $blog_id Optional. The blog id to initialize for
       
   470 	 */
       
   471 	function init( $data, $blog_id = '' ) {
       
   472 		$this->data = $data;
       
   473 		$this->ID = (int) $data->ID;
       
   474 
       
   475 		$this->for_blog( $blog_id );
       
   476 	}
       
   477 
       
   478 	/**
       
   479 	 * Return only the main user fields
       
   480 	 *
       
   481 	 * @since 3.3.0
       
   482 	 *
       
   483 	 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
       
   484 	 * @param string|int $value The field value
       
   485 	 * @return object Raw user object
       
   486 	 */
       
   487 	static function get_data_by( $field, $value ) {
       
   488 		global $wpdb;
       
   489 
       
   490 		if ( 'id' == $field ) {
       
   491 			// Make sure the value is numeric to avoid casting objects, for example,
       
   492 			// to int 1.
       
   493 			if ( ! is_numeric( $value ) )
       
   494 				return false;
       
   495 			$value = absint( $value );
       
   496 		} else {
       
   497 			$value = trim( $value );
       
   498 		}
       
   499 
       
   500 		if ( !$value )
       
   501 			return false;
       
   502 
       
   503 		switch ( $field ) {
       
   504 			case 'id':
       
   505 				$user_id = $value;
       
   506 				$db_field = 'ID';
       
   507 				break;
       
   508 			case 'slug':
       
   509 				$user_id = wp_cache_get($value, 'userslugs');
       
   510 				$db_field = 'user_nicename';
       
   511 				break;
       
   512 			case 'email':
       
   513 				$user_id = wp_cache_get($value, 'useremail');
       
   514 				$db_field = 'user_email';
       
   515 				break;
       
   516 			case 'login':
       
   517 				$value = sanitize_user( $value );
       
   518 				$user_id = wp_cache_get($value, 'userlogins');
       
   519 				$db_field = 'user_login';
       
   520 				break;
       
   521 			default:
       
   522 				return false;
       
   523 		}
       
   524 
       
   525 		if ( false !== $user_id ) {
       
   526 			if ( $user = wp_cache_get( $user_id, 'users' ) )
       
   527 				return $user;
       
   528 		}
       
   529 
       
   530 		if ( !$user = $wpdb->get_row( $wpdb->prepare(
       
   531 			"SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
       
   532 		) ) )
       
   533 			return false;
       
   534 
       
   535 		update_user_caches( $user );
       
   536 
       
   537 		return $user;
       
   538 	}
       
   539 
       
   540 	/**
       
   541 	 * Magic method for checking the existence of a certain custom field
       
   542 	 *
       
   543 	 * @since 3.3.0
       
   544 	 */
       
   545 	function __isset( $key ) {
       
   546 		if ( 'id' == $key ) {
       
   547 			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
       
   548 			$key = 'ID';
       
   549 		}
       
   550 
       
   551 		if ( isset( $this->data->$key ) )
       
   552 			return true;
       
   553 
       
   554 		if ( isset( self::$back_compat_keys[ $key ] ) )
       
   555 			$key = self::$back_compat_keys[ $key ];
       
   556 
       
   557 		return metadata_exists( 'user', $this->ID, $key );
       
   558 	}
       
   559 
       
   560 	/**
       
   561 	 * Magic method for accessing custom fields
       
   562 	 *
       
   563 	 * @since 3.3.0
       
   564 	 */
       
   565 	function __get( $key ) {
       
   566 		if ( 'id' == $key ) {
       
   567 			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
       
   568 			return $this->ID;
       
   569 		}
       
   570 
       
   571 		if ( isset( $this->data->$key ) ) {
       
   572 			$value = $this->data->$key;
       
   573 		} else {
       
   574 			if ( isset( self::$back_compat_keys[ $key ] ) )
       
   575 				$key = self::$back_compat_keys[ $key ];
       
   576 			$value = get_user_meta( $this->ID, $key, true );
       
   577 		}
       
   578 
       
   579 		if ( $this->filter ) {
       
   580 			$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
       
   581 		}
       
   582 
       
   583 		return $value;
       
   584 	}
       
   585 
       
   586 	/**
       
   587 	 * Magic method for setting custom fields
       
   588 	 *
       
   589 	 * @since 3.3.0
       
   590 	 */
       
   591 	function __set( $key, $value ) {
       
   592 		if ( 'id' == $key ) {
       
   593 			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
       
   594 			$this->ID = $value;
   493 			return;
   595 			return;
   494 
   596 		}
   495 		foreach ( get_object_vars( $this->data ) as $key => $value ) {
   597 
   496 			$this->{$key} = $value;
   598 		$this->data->$key = $value;
   497 		}
   599 	}
   498 
   600 
   499 		$this->id = $this->ID;
   601 	/**
   500 		$this->_init_caps();
   602 	 * Determine whether the user exists in the database.
   501 	}
   603 	 *
   502 
   604 	 * @since 3.4.0
   503 	/**
   605 	 * @access public
   504 	 * Setup capability object properties.
   606 	 *
       
   607 	 * @return bool True if user exists in the database, false if not.
       
   608 	 */
       
   609 	function exists() {
       
   610 		return ! empty( $this->ID );
       
   611 	}
       
   612 
       
   613 	/**
       
   614 	 * Retrieve the value of a property or meta key.
       
   615 	 *
       
   616 	 * Retrieves from the users and usermeta table.
       
   617 	 *
       
   618 	 * @since 3.3.0
       
   619 	 *
       
   620 	 * @param string $key Property
       
   621 	 */
       
   622 	function get( $key ) {
       
   623 		return $this->__get( $key );
       
   624 	}
       
   625 
       
   626 	/**
       
   627 	 * Determine whether a property or meta key is set
       
   628 	 *
       
   629 	 * Consults the users and usermeta tables.
       
   630 	 *
       
   631 	 * @since 3.3.0
       
   632 	 *
       
   633 	 * @param string $key Property
       
   634 	 */
       
   635 	function has_prop( $key ) {
       
   636 		return $this->__isset( $key );
       
   637 	}
       
   638 
       
   639 	/**
       
   640 	 * Set up capability object properties.
   505 	 *
   641 	 *
   506 	 * Will set the value for the 'cap_key' property to current database table
   642 	 * Will set the value for the 'cap_key' property to current database table
   507 	 * prefix, followed by 'capabilities'. Will then check to see if the
   643 	 * prefix, followed by 'capabilities'. Will then check to see if the
   508 	 * property matching the 'cap_key' exists and is an array. If so, it will be
   644 	 * property matching the 'cap_key' exists and is an array. If so, it will be
   509 	 * used.
   645 	 * used.
   510 	 *
   646 	 *
       
   647 	 * @access protected
   511 	 * @since 2.1.0
   648 	 * @since 2.1.0
   512 	 * @access protected
   649 	 *
   513 	 */
   650 	 * @param string $cap_key Optional capability key
   514 	function _init_caps() {
   651 	 */
       
   652 	function _init_caps( $cap_key = '' ) {
   515 		global $wpdb;
   653 		global $wpdb;
   516 		$this->cap_key = $wpdb->prefix . 'capabilities';
   654 
   517 		$this->caps = &$this->{$this->cap_key};
   655 		if ( empty($cap_key) )
       
   656 			$this->cap_key = $wpdb->prefix . 'capabilities';
       
   657 		else
       
   658 			$this->cap_key = $cap_key;
       
   659 
       
   660 		$this->caps = get_user_meta( $this->ID, $this->cap_key, true );
       
   661 
   518 		if ( ! is_array( $this->caps ) )
   662 		if ( ! is_array( $this->caps ) )
   519 			$this->caps = array();
   663 			$this->caps = array();
       
   664 
   520 		$this->get_role_caps();
   665 		$this->get_role_caps();
   521 	}
   666 	}
   522 
   667 
   523 	/**
   668 	/**
   524 	 * Retrieve all of the role capabilities and merge with individual capabilities.
   669 	 * Retrieve all of the role capabilities and merge with individual capabilities.
   543 			$this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
   688 			$this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
   544 
   689 
   545 		//Build $allcaps from role caps, overlay user's $caps
   690 		//Build $allcaps from role caps, overlay user's $caps
   546 		$this->allcaps = array();
   691 		$this->allcaps = array();
   547 		foreach ( (array) $this->roles as $role ) {
   692 		foreach ( (array) $this->roles as $role ) {
   548 			$role =& $wp_roles->get_role( $role );
   693 			$the_role = $wp_roles->get_role( $role );
   549 			$this->allcaps = array_merge( (array) $this->allcaps, (array) $role->capabilities );
   694 			$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
   550 		}
   695 		}
   551 		$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
   696 		$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
   552 	}
   697 	}
   553 
   698 
   554 	/**
   699 	/**
   561 	 *
   706 	 *
   562 	 * @param string $role Role name.
   707 	 * @param string $role Role name.
   563 	 */
   708 	 */
   564 	function add_role( $role ) {
   709 	function add_role( $role ) {
   565 		$this->caps[$role] = true;
   710 		$this->caps[$role] = true;
   566 		update_usermeta( $this->ID, $this->cap_key, $this->caps );
   711 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   567 		$this->get_role_caps();
   712 		$this->get_role_caps();
   568 		$this->update_user_level_from_caps();
   713 		$this->update_user_level_from_caps();
   569 	}
   714 	}
   570 
   715 
   571 	/**
   716 	/**
   575 	 * @access public
   720 	 * @access public
   576 	 *
   721 	 *
   577 	 * @param string $role Role name.
   722 	 * @param string $role Role name.
   578 	 */
   723 	 */
   579 	function remove_role( $role ) {
   724 	function remove_role( $role ) {
   580 		if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
   725 		if ( !in_array($role, $this->roles) )
   581 			return;
   726 			return;
   582 		unset( $this->caps[$role] );
   727 		unset( $this->caps[$role] );
   583 		update_usermeta( $this->ID, $this->cap_key, $this->caps );
   728 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   584 		$this->get_role_caps();
   729 		$this->get_role_caps();
       
   730 		$this->update_user_level_from_caps();
   585 	}
   731 	}
   586 
   732 
   587 	/**
   733 	/**
   588 	 * Set the role of the user.
   734 	 * Set the role of the user.
   589 	 *
   735 	 *
   597 	 * @param string $role Role name.
   743 	 * @param string $role Role name.
   598 	 */
   744 	 */
   599 	function set_role( $role ) {
   745 	function set_role( $role ) {
   600 		foreach ( (array) $this->roles as $oldrole )
   746 		foreach ( (array) $this->roles as $oldrole )
   601 			unset( $this->caps[$oldrole] );
   747 			unset( $this->caps[$oldrole] );
       
   748 
       
   749 		if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
       
   750 			return;
       
   751 
   602 		if ( !empty( $role ) ) {
   752 		if ( !empty( $role ) ) {
   603 			$this->caps[$role] = true;
   753 			$this->caps[$role] = true;
   604 			$this->roles = array( $role => true );
   754 			$this->roles = array( $role => true );
   605 		} else {
   755 		} else {
   606 			$this->roles = false;
   756 			$this->roles = false;
   607 		}
   757 		}
   608 		update_usermeta( $this->ID, $this->cap_key, $this->caps );
   758 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   609 		$this->get_role_caps();
   759 		$this->get_role_caps();
   610 		$this->update_user_level_from_caps();
   760 		$this->update_user_level_from_caps();
   611 		do_action( 'set_user_role', $this->ID, $role );
   761 		do_action( 'set_user_role', $this->ID, $role );
   612 	}
   762 	}
   613 
   763 
   649 	 * @since 2.0.0
   799 	 * @since 2.0.0
   650 	 * @access public
   800 	 * @access public
   651 	 */
   801 	 */
   652 	function update_user_level_from_caps() {
   802 	function update_user_level_from_caps() {
   653 		global $wpdb;
   803 		global $wpdb;
   654 		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
   804 		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
   655 		update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
   805 		update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
   656 	}
   806 	}
   657 
   807 
   658 	/**
   808 	/**
   659 	 * Add capability and grant or deny access to capability.
   809 	 * Add capability and grant or deny access to capability.
   660 	 *
   810 	 *
   664 	 * @param string $cap Capability name.
   814 	 * @param string $cap Capability name.
   665 	 * @param bool $grant Whether to grant capability to user.
   815 	 * @param bool $grant Whether to grant capability to user.
   666 	 */
   816 	 */
   667 	function add_cap( $cap, $grant = true ) {
   817 	function add_cap( $cap, $grant = true ) {
   668 		$this->caps[$cap] = $grant;
   818 		$this->caps[$cap] = $grant;
   669 		update_usermeta( $this->ID, $this->cap_key, $this->caps );
   819 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   670 	}
   820 	}
   671 
   821 
   672 	/**
   822 	/**
   673 	 * Remove capability from user.
   823 	 * Remove capability from user.
   674 	 *
   824 	 *
   676 	 * @access public
   826 	 * @access public
   677 	 *
   827 	 *
   678 	 * @param string $cap Capability name.
   828 	 * @param string $cap Capability name.
   679 	 */
   829 	 */
   680 	function remove_cap( $cap ) {
   830 	function remove_cap( $cap ) {
   681 		if ( empty( $this->caps[$cap] ) ) return;
   831 		if ( empty( $this->caps[$cap] ) )
       
   832 			return;
   682 		unset( $this->caps[$cap] );
   833 		unset( $this->caps[$cap] );
   683 		update_usermeta( $this->ID, $this->cap_key, $this->caps );
   834 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   684 	}
   835 	}
   685 
   836 
   686 	/**
   837 	/**
   687 	 * Remove all of the capabilities of the user.
   838 	 * Remove all of the capabilities of the user.
   688 	 *
   839 	 *
   690 	 * @access public
   841 	 * @access public
   691 	 */
   842 	 */
   692 	function remove_all_caps() {
   843 	function remove_all_caps() {
   693 		global $wpdb;
   844 		global $wpdb;
   694 		$this->caps = array();
   845 		$this->caps = array();
   695 		update_usermeta( $this->ID, $this->cap_key, '' );
   846 		delete_user_meta( $this->ID, $this->cap_key );
   696 		update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
   847 		delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
   697 		$this->get_role_caps();
   848 		$this->get_role_caps();
   698 	}
   849 	}
   699 
   850 
   700 	/**
   851 	/**
   701 	 * Whether user has capability or role name.
   852 	 * Whether user has capability or role name.
   702 	 *
   853 	 *
   703 	 * This is useful for looking up whether the user has a specific role
   854 	 * This is useful for looking up whether the user has a specific role
   704 	 * assigned to the user. The second optional parameter can also be used to
   855 	 * assigned to the user. The second optional parameter can also be used to
   705 	 * check for capabilities against a specfic post.
   856 	 * check for capabilities against a specific object, such as a post or user.
   706 	 *
   857 	 *
   707 	 * @since 2.0.0
   858 	 * @since 2.0.0
   708 	 * @access public
   859 	 * @access public
   709 	 *
   860 	 *
   710 	 * @param string|int $cap Capability or role name to search.
   861 	 * @param string|int $cap Capability or role name to search.
   711 	 * @param int $post_id Optional. Post ID to check capability against specific post.
       
   712 	 * @return bool True, if user has capability; false, if user does not have capability.
   862 	 * @return bool True, if user has capability; false, if user does not have capability.
   713 	 */
   863 	 */
   714 	function has_cap( $cap ) {
   864 	function has_cap( $cap ) {
   715 		if ( is_numeric( $cap ) )
   865 		if ( is_numeric( $cap ) ) {
       
   866 			_deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
   716 			$cap = $this->translate_level_to_cap( $cap );
   867 			$cap = $this->translate_level_to_cap( $cap );
       
   868 		}
   717 
   869 
   718 		$args = array_slice( func_get_args(), 1 );
   870 		$args = array_slice( func_get_args(), 1 );
   719 		$args = array_merge( array( $cap, $this->ID ), $args );
   871 		$args = array_merge( array( $cap, $this->ID ), $args );
   720 		$caps = call_user_func_array( 'map_meta_cap', $args );
   872 		$caps = call_user_func_array( 'map_meta_cap', $args );
       
   873 
       
   874 		// Multisite super admin has all caps by definition, Unless specifically denied.
       
   875 		if ( is_multisite() && is_super_admin( $this->ID ) ) {
       
   876 			if ( in_array('do_not_allow', $caps) )
       
   877 				return false;
       
   878 			return true;
       
   879 		}
       
   880 
   721 		// Must have ALL requested caps
   881 		// Must have ALL requested caps
   722 		$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
   882 		$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
       
   883 		$capabilities['exist'] = true; // Everyone is allowed to exist
   723 		foreach ( (array) $caps as $cap ) {
   884 		foreach ( (array) $caps as $cap ) {
   724 			//echo "Checking cap $cap<br />";
   885 			if ( empty( $capabilities[ $cap ] ) )
   725 			if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
       
   726 				return false;
   886 				return false;
   727 		}
   887 		}
   728 
   888 
   729 		return true;
   889 		return true;
   730 	}
   890 	}
   742 	 */
   902 	 */
   743 	function translate_level_to_cap( $level ) {
   903 	function translate_level_to_cap( $level ) {
   744 		return 'level_' . $level;
   904 		return 'level_' . $level;
   745 	}
   905 	}
   746 
   906 
       
   907 	/**
       
   908 	 * Set the blog to operate on. Defaults to the current blog.
       
   909 	 *
       
   910 	 * @since 3.0.0
       
   911 	 *
       
   912 	 * @param int $blog_id Optional Blog ID, defaults to current blog.
       
   913 	 */
       
   914 	function for_blog( $blog_id = '' ) {
       
   915 		global $wpdb;
       
   916 		if ( ! empty( $blog_id ) )
       
   917 			$cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
       
   918 		else
       
   919 			$cap_key = '';
       
   920 		$this->_init_caps( $cap_key );
       
   921 	}
   747 }
   922 }
   748 
   923 
   749 /**
   924 /**
   750  * Map meta capabilities to primitive capabilities.
   925  * Map meta capabilities to primitive capabilities.
   751  *
   926  *
   752  * This does not actually compare whether the user ID has the actual capability,
   927  * This does not actually compare whether the user ID has the actual capability,
   753  * just what the capability or capabilities are. Meta capability list value can
   928  * just what the capability or capabilities are. Meta capability list value can
   754  * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
   929  * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
   755  * 'edit_page', 'read_post', or 'read_page'.
   930  * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
   756  *
   931  *
   757  * @since 2.0.0
   932  * @since 2.0.0
   758  *
   933  *
   759  * @param string $cap Capability name.
   934  * @param string $cap Capability name.
   760  * @param int $user_id User ID.
   935  * @param int $user_id User ID.
   763 function map_meta_cap( $cap, $user_id ) {
   938 function map_meta_cap( $cap, $user_id ) {
   764 	$args = array_slice( func_get_args(), 2 );
   939 	$args = array_slice( func_get_args(), 2 );
   765 	$caps = array();
   940 	$caps = array();
   766 
   941 
   767 	switch ( $cap ) {
   942 	switch ( $cap ) {
   768 	case 'delete_user':
   943 	case 'remove_user':
   769 		$caps[] = 'delete_users';
   944 		$caps[] = 'remove_users';
       
   945 		break;
       
   946 	case 'promote_user':
       
   947 		$caps[] = 'promote_users';
   770 		break;
   948 		break;
   771 	case 'edit_user':
   949 	case 'edit_user':
   772 		if ( !isset( $args[0] ) || $user_id != $args[0] ) {
   950 		// Allow user to edit itself
   773 			$caps[] = 'edit_users';
   951 		if ( isset( $args[0] ) && $user_id == $args[0] )
   774 		}
   952 			break;
       
   953 		// Fall through
       
   954 	case 'edit_users':
       
   955 		// If multisite these caps are allowed only for super admins.
       
   956 		if ( is_multisite() && !is_super_admin( $user_id ) )
       
   957 			$caps[] = 'do_not_allow';
       
   958 		else
       
   959 			$caps[] = 'edit_users'; // Explicit due to primitive fall through
   775 		break;
   960 		break;
   776 	case 'delete_post':
   961 	case 'delete_post':
       
   962 	case 'delete_page':
   777 		$author_data = get_userdata( $user_id );
   963 		$author_data = get_userdata( $user_id );
   778 		//echo "post ID: {$args[0]}<br />";
       
   779 		$post = get_post( $args[0] );
   964 		$post = get_post( $args[0] );
   780 		if ( 'page' == $post->post_type ) {
   965 
   781 			$args = array_merge( array( 'delete_page', $user_id ), $args );
   966 		if ( 'revision' == $post->post_type ) {
   782 			return call_user_func_array( 'map_meta_cap', $args );
   967 			$post = get_post( $post->post_parent );
   783 		}
   968 		}
   784 
   969 
   785 		if ('' != $post->post_author) {
   970 		$post_type = get_post_type_object( $post->post_type );
       
   971 
       
   972 		if ( ! $post_type->map_meta_cap ) {
       
   973 			$caps[] = $post_type->cap->$cap;
       
   974 			// Prior to 3.1 we would re-call map_meta_cap here.
       
   975 			if ( 'delete_post' == $cap )
       
   976 				$cap = $post_type->cap->$cap;
       
   977 			break;
       
   978 		}
       
   979 
       
   980 		if ( '' != $post->post_author ) {
   786 			$post_author_data = get_userdata( $post->post_author );
   981 			$post_author_data = get_userdata( $post->post_author );
   787 		} else {
   982 		} else {
   788 			//No author set yet so default to current user for cap checks
   983 			// No author set yet, so default to current user for cap checks.
   789 			$post_author_data = $author_data;
   984 			$post_author_data = $author_data;
   790 		}
   985 		}
   791 
   986 
   792 		// If the user is the author...
   987 		// If the user is the author...
   793 		if ( $user_id == $post_author_data->ID ) {
   988 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
   794 			// If the post is published...
   989 			// If the post is published...
   795 			if ( 'publish' == $post->post_status ) {
   990 			if ( 'publish' == $post->post_status ) {
   796 				$caps[] = 'delete_published_posts';
   991 				$caps[] = $post_type->cap->delete_published_posts;
   797 			} elseif ( 'trash' == $post->post_status ) {
   992 			} elseif ( 'trash' == $post->post_status ) {
   798 				if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
   993 				if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
   799 					$caps[] = 'delete_published_posts';
   994 					$caps[] = $post_type->cap->delete_published_posts;
   800 			} else {
   995 			} else {
   801 				// If the post is draft...
   996 				// If the post is draft...
   802 				$caps[] = 'delete_posts';
   997 				$caps[] = $post_type->cap->delete_posts;
   803 			}
   998 			}
   804 		} else {
   999 		} else {
   805 			// The user is trying to edit someone else's post.
  1000 			// The user is trying to edit someone else's post.
   806 			$caps[] = 'delete_others_posts';
  1001 			$caps[] = $post_type->cap->delete_others_posts;
   807 			// The post is published, extra cap required.
  1002 			// The post is published, extra cap required.
   808 			if ( 'publish' == $post->post_status )
  1003 			if ( 'publish' == $post->post_status )
   809 				$caps[] = 'delete_published_posts';
  1004 				$caps[] = $post_type->cap->delete_published_posts;
   810 			elseif ( 'private' == $post->post_status )
  1005 			elseif ( 'private' == $post->post_status )
   811 				$caps[] = 'delete_private_posts';
  1006 				$caps[] = $post_type->cap->delete_private_posts;
   812 		}
       
   813 		break;
       
   814 	case 'delete_page':
       
   815 		$author_data = get_userdata( $user_id );
       
   816 		//echo "post ID: {$args[0]}<br />";
       
   817 		$page = get_page( $args[0] );
       
   818 		$page_author_data = get_userdata( $page->post_author );
       
   819 		//echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
       
   820 		// If the user is the author...
       
   821 
       
   822 		if ('' != $page->post_author) {
       
   823 			$page_author_data = get_userdata( $page->post_author );
       
   824 		} else {
       
   825 			//No author set yet so default to current user for cap checks
       
   826 			$page_author_data = $author_data;
       
   827 		}
       
   828 
       
   829 		if ( $user_id == $page_author_data->ID ) {
       
   830 			// If the page is published...
       
   831 			if ( $page->post_status == 'publish' ) {
       
   832 				$caps[] = 'delete_published_pages';
       
   833 			} elseif ( 'trash' == $page->post_status ) {
       
   834 				if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
       
   835 					$caps[] = 'delete_published_pages';
       
   836 			} else {
       
   837 				// If the page is draft...
       
   838 				$caps[] = 'delete_pages';
       
   839 			}
       
   840 		} else {
       
   841 			// The user is trying to edit someone else's page.
       
   842 			$caps[] = 'delete_others_pages';
       
   843 			// The page is published, extra cap required.
       
   844 			if ( $page->post_status == 'publish' )
       
   845 				$caps[] = 'delete_published_pages';
       
   846 			elseif ( $page->post_status == 'private' )
       
   847 				$caps[] = 'delete_private_pages';
       
   848 		}
  1007 		}
   849 		break;
  1008 		break;
   850 		// edit_post breaks down to edit_posts, edit_published_posts, or
  1009 		// edit_post breaks down to edit_posts, edit_published_posts, or
   851 		// edit_others_posts
  1010 		// edit_others_posts
   852 	case 'edit_post':
  1011 	case 'edit_post':
       
  1012 	case 'edit_page':
   853 		$author_data = get_userdata( $user_id );
  1013 		$author_data = get_userdata( $user_id );
   854 		//echo "post ID: {$args[0]}<br />";
       
   855 		$post = get_post( $args[0] );
  1014 		$post = get_post( $args[0] );
   856 		if ( 'page' == $post->post_type ) {
  1015 
   857 			$args = array_merge( array( 'edit_page', $user_id ), $args );
  1016 		if ( 'revision' == $post->post_type ) {
   858 			return call_user_func_array( 'map_meta_cap', $args );
  1017 			$post = get_post( $post->post_parent );
   859 		}
  1018 		}
   860 		$post_author_data = get_userdata( $post->post_author );
  1019 
       
  1020 		$post_type = get_post_type_object( $post->post_type );
       
  1021 
       
  1022 		if ( ! $post_type->map_meta_cap ) {
       
  1023 			$caps[] = $post_type->cap->$cap;
       
  1024 			// Prior to 3.1 we would re-call map_meta_cap here.
       
  1025 			if ( 'edit_post' == $cap )
       
  1026 				$cap = $post_type->cap->$cap;
       
  1027 			break;
       
  1028 		}
       
  1029 
       
  1030 		if ( '' != $post->post_author ) {
       
  1031 			$post_author_data = get_userdata( $post->post_author );
       
  1032 		} else {
       
  1033 			// No author set yet, so default to current user for cap checks.
       
  1034 			$post_author_data = $author_data;
       
  1035 		}
       
  1036 
   861 		//echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
  1037 		//echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
   862 		// If the user is the author...
  1038 		// If the user is the author...
   863 		if ( $user_id == $post_author_data->ID ) {
  1039 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
   864 			// If the post is published...
  1040 			// If the post is published...
   865 			if ( 'publish' == $post->post_status ) {
  1041 			if ( 'publish' == $post->post_status ) {
   866 				$caps[] = 'edit_published_posts';
  1042 				$caps[] = $post_type->cap->edit_published_posts;
   867 			} elseif ( 'trash' == $post->post_status ) {
  1043 			} elseif ( 'trash' == $post->post_status ) {
   868 				if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
  1044 				if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
   869 					$caps[] = 'edit_published_posts';
  1045 					$caps[] = $post_type->cap->edit_published_posts;
   870 			} else {
  1046 			} else {
   871 				// If the post is draft...
  1047 				// If the post is draft...
   872 				$caps[] = 'edit_posts';
  1048 				$caps[] = $post_type->cap->edit_posts;
   873 			}
  1049 			}
   874 		} else {
  1050 		} else {
   875 			// The user is trying to edit someone else's post.
  1051 			// The user is trying to edit someone else's post.
   876 			$caps[] = 'edit_others_posts';
  1052 			$caps[] = $post_type->cap->edit_others_posts;
   877 			// The post is published, extra cap required.
  1053 			// The post is published, extra cap required.
   878 			if ( 'publish' == $post->post_status )
  1054 			if ( 'publish' == $post->post_status )
   879 				$caps[] = 'edit_published_posts';
  1055 				$caps[] = $post_type->cap->edit_published_posts;
   880 			elseif ( 'private' == $post->post_status )
  1056 			elseif ( 'private' == $post->post_status )
   881 				$caps[] = 'edit_private_posts';
  1057 				$caps[] = $post_type->cap->edit_private_posts;
   882 		}
       
   883 		break;
       
   884 	case 'edit_page':
       
   885 		$author_data = get_userdata( $user_id );
       
   886 		//echo "post ID: {$args[0]}<br />";
       
   887 		$page = get_page( $args[0] );
       
   888 		$page_author_data = get_userdata( $page->post_author );
       
   889 		//echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
       
   890 		// If the user is the author...
       
   891 		if ( $user_id == $page_author_data->ID ) {
       
   892 			// If the page is published...
       
   893 			if ( 'publish' == $page->post_status ) {
       
   894 				$caps[] = 'edit_published_pages';
       
   895 			} elseif ( 'trash' == $page->post_status ) {
       
   896 				if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
       
   897 					$caps[] = 'edit_published_pages';
       
   898 			} else {
       
   899 				// If the page is draft...
       
   900 				$caps[] = 'edit_pages';
       
   901 			}
       
   902 		} else {
       
   903 			// The user is trying to edit someone else's page.
       
   904 			$caps[] = 'edit_others_pages';
       
   905 			// The page is published, extra cap required.
       
   906 			if ( 'publish' == $page->post_status )
       
   907 				$caps[] = 'edit_published_pages';
       
   908 			elseif ( 'private' == $page->post_status )
       
   909 				$caps[] = 'edit_private_pages';
       
   910 		}
  1058 		}
   911 		break;
  1059 		break;
   912 	case 'read_post':
  1060 	case 'read_post':
       
  1061 	case 'read_page':
       
  1062 		$author_data = get_userdata( $user_id );
   913 		$post = get_post( $args[0] );
  1063 		$post = get_post( $args[0] );
   914 		if ( 'page' == $post->post_type ) {
  1064 
   915 			$args = array_merge( array( 'read_page', $user_id ), $args );
  1065 		if ( 'revision' == $post->post_type ) {
   916 			return call_user_func_array( 'map_meta_cap', $args );
  1066 			$post = get_post( $post->post_parent );
   917 		}
  1067 		}
   918 
  1068 
   919 		if ( 'private' != $post->post_status ) {
  1069 		$post_type = get_post_type_object( $post->post_type );
   920 			$caps[] = 'read';
  1070 
       
  1071 		if ( ! $post_type->map_meta_cap ) {
       
  1072 			$caps[] = $post_type->cap->$cap;
       
  1073 			// Prior to 3.1 we would re-call map_meta_cap here.
       
  1074 			if ( 'read_post' == $cap )
       
  1075 				$cap = $post_type->cap->$cap;
   921 			break;
  1076 			break;
   922 		}
  1077 		}
   923 
  1078 
   924 		$author_data = get_userdata( $user_id );
  1079 		$status_obj = get_post_status_object( $post->post_status );
   925 		$post_author_data = get_userdata( $post->post_author );
  1080 		if ( $status_obj->public ) {
   926 		if ( $user_id == $post_author_data->ID )
  1081 			$caps[] = $post_type->cap->read;
   927 			$caps[] = 'read';
  1082 			break;
       
  1083 		}
       
  1084 
       
  1085 		if ( '' != $post->post_author ) {
       
  1086 			$post_author_data = get_userdata( $post->post_author );
       
  1087 		} else {
       
  1088 			// No author set yet, so default to current user for cap checks.
       
  1089 			$post_author_data = $author_data;
       
  1090 		}
       
  1091 
       
  1092 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
       
  1093 			$caps[] = $post_type->cap->read;
       
  1094 		elseif ( $status_obj->private )
       
  1095 			$caps[] = $post_type->cap->read_private_posts;
   928 		else
  1096 		else
   929 			$caps[] = 'read_private_posts';
  1097 			$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
   930 		break;
  1098 		break;
   931 	case 'read_page':
  1099 	case 'edit_post_meta':
   932 		$page = get_page( $args[0] );
  1100 	case 'delete_post_meta':
   933 
  1101 	case 'add_post_meta':
   934 		if ( 'private' != $page->post_status ) {
  1102 		$post = get_post( $args[0] );
   935 			$caps[] = 'read';
  1103 		$post_type_object = get_post_type_object( $post->post_type );
   936 			break;
  1104 		$caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
   937 		}
  1105 
   938 
  1106 		$meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
   939 		$author_data = get_userdata( $user_id );
  1107 
   940 		$page_author_data = get_userdata( $page->post_author );
  1108 		if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
   941 		if ( $user_id == $page_author_data->ID )
  1109 			$allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
   942 			$caps[] = 'read';
  1110 			if ( ! $allowed )
   943 		else
  1111 				$caps[] = $cap;
   944 			$caps[] = 'read_private_pages';
  1112 		} elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
       
  1113 			$caps[] = $cap;
       
  1114 		}
       
  1115 		break;
       
  1116 	case 'edit_comment':
       
  1117 		$comment = get_comment( $args[0] );
       
  1118 		$post = get_post( $comment->comment_post_ID );
       
  1119 		$post_type_object = get_post_type_object( $post->post_type );
       
  1120 
       
  1121 		$caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
   945 		break;
  1122 		break;
   946 	case 'unfiltered_upload':
  1123 	case 'unfiltered_upload':
   947 		if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS == true )
  1124 		if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
       
  1125 			$caps[] = $cap;
       
  1126 		else
       
  1127 			$caps[] = 'do_not_allow';
       
  1128 		break;
       
  1129 	case 'unfiltered_html' :
       
  1130 		// Disallow unfiltered_html for all users, even admins and super admins.
       
  1131 		if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
       
  1132 			$caps[] = 'do_not_allow';
       
  1133 		elseif ( is_multisite() && ! is_super_admin( $user_id ) )
       
  1134 			$caps[] = 'do_not_allow';
       
  1135 		else
       
  1136 			$caps[] = $cap;
       
  1137 		break;
       
  1138 	case 'edit_files':
       
  1139 	case 'edit_plugins':
       
  1140 	case 'edit_themes':
       
  1141 		if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
       
  1142 			$caps[] = 'do_not_allow';
       
  1143 			break;
       
  1144 		}
       
  1145 		// Fall through if not DISALLOW_FILE_EDIT.
       
  1146 	case 'update_plugins':
       
  1147 	case 'delete_plugins':
       
  1148 	case 'install_plugins':
       
  1149 	case 'update_themes':
       
  1150 	case 'delete_themes':
       
  1151 	case 'install_themes':
       
  1152 	case 'update_core':
       
  1153 		// Disallow anything that creates, deletes, or edits core, plugin, or theme files.
       
  1154 		// Files in uploads are excepted.
       
  1155 		if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
       
  1156 			$caps[] = 'do_not_allow';
       
  1157 			break;
       
  1158 		}
       
  1159 		// Fall through if not DISALLOW_FILE_MODS.
       
  1160 	case 'delete_user':
       
  1161 	case 'delete_users':
       
  1162 		// If multisite these caps are allowed only for super admins.
       
  1163 		if ( is_multisite() && !is_super_admin( $user_id ) ) {
       
  1164 			$caps[] = 'do_not_allow';
       
  1165 		} else {
       
  1166 			if ( 'delete_user' == $cap )
       
  1167 				$cap = 'delete_users';
       
  1168 			$caps[] = $cap;
       
  1169 		}
       
  1170 		break;
       
  1171 	case 'create_users':
       
  1172 		if ( !is_multisite() )
       
  1173 			$caps[] = $cap;
       
  1174 		elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
   948 			$caps[] = $cap;
  1175 			$caps[] = $cap;
   949 		else
  1176 		else
   950 			$caps[] = 'do_not_allow';
  1177 			$caps[] = 'do_not_allow';
   951 		break;
  1178 		break;
   952 	default:
  1179 	default:
       
  1180 		// Handle meta capabilities for custom post types.
       
  1181 		$post_type_meta_caps = _post_type_meta_capabilities();
       
  1182 		if ( isset( $post_type_meta_caps[ $cap ] ) ) {
       
  1183 			$args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
       
  1184 			return call_user_func_array( 'map_meta_cap', $args );
       
  1185 		}
       
  1186 
   953 		// If no meta caps match, return the original cap.
  1187 		// If no meta caps match, return the original cap.
   954 		$caps[] = $cap;
  1188 		$caps[] = $cap;
   955 	}
  1189 	}
   956 
  1190 
   957 	return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
  1191 	return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
   972 		return false;
  1206 		return false;
   973 
  1207 
   974 	$args = array_slice( func_get_args(), 1 );
  1208 	$args = array_slice( func_get_args(), 1 );
   975 	$args = array_merge( array( $capability ), $args );
  1209 	$args = array_merge( array( $capability ), $args );
   976 
  1210 
   977 	return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
  1211 	return call_user_func_array( array( $current_user, 'has_cap' ), $args );
       
  1212 }
       
  1213 
       
  1214 /**
       
  1215  * Whether current user has a capability or role for a given blog.
       
  1216  *
       
  1217  * @since 3.0.0
       
  1218  *
       
  1219  * @param int $blog_id Blog ID
       
  1220  * @param string $capability Capability or role name.
       
  1221  * @return bool
       
  1222  */
       
  1223 function current_user_can_for_blog( $blog_id, $capability ) {
       
  1224 	$current_user = wp_get_current_user();
       
  1225 
       
  1226 	if ( empty( $current_user ) )
       
  1227 		return false;
       
  1228 
       
  1229 	// Create new object to avoid stomping the global current_user.
       
  1230 	$user = new WP_User( $current_user->ID) ;
       
  1231 
       
  1232 	// Set the blog id. @todo add blog id arg to WP_User constructor?
       
  1233 	$user->for_blog( $blog_id );
       
  1234 
       
  1235 	$args = array_slice( func_get_args(), 2 );
       
  1236 	$args = array_merge( array( $capability ), $args );
       
  1237 
       
  1238 	return call_user_func_array( array( &$user, 'has_cap' ), $args );
   978 }
  1239 }
   979 
  1240 
   980 /**
  1241 /**
   981  * Whether author of supplied post has capability or role.
  1242  * Whether author of supplied post has capability or role.
   982  *
  1243  *
   990 	if ( !$post = get_post($post) )
  1251 	if ( !$post = get_post($post) )
   991 		return false;
  1252 		return false;
   992 
  1253 
   993 	$author = new WP_User( $post->post_author );
  1254 	$author = new WP_User( $post->post_author );
   994 
  1255 
   995 	if ( empty( $author ) )
  1256 	if ( empty( $author->ID ) )
   996 		return false;
  1257 		return false;
   997 
  1258 
   998 	$args = array_slice( func_get_args(), 2 );
  1259 	$args = array_slice( func_get_args(), 2 );
   999 	$args = array_merge( array( $capability ), $args );
  1260 	$args = array_merge( array( $capability ), $args );
  1000 
  1261 
  1001 	return call_user_func_array( array( &$author, 'has_cap' ), $args );
  1262 	return call_user_func_array( array( &$author, 'has_cap' ), $args );
       
  1263 }
       
  1264 
       
  1265 /**
       
  1266  * Whether a particular user has capability or role.
       
  1267  *
       
  1268  * @since 3.1.0
       
  1269  *
       
  1270  * @param int|object $user User ID or object.
       
  1271  * @param string $capability Capability or role name.
       
  1272  * @return bool
       
  1273  */
       
  1274 function user_can( $user, $capability ) {
       
  1275 	if ( ! is_object( $user ) )
       
  1276 		$user = new WP_User( $user );
       
  1277 
       
  1278 	if ( ! $user || ! $user->exists() )
       
  1279 		return false;
       
  1280 
       
  1281 	$args = array_slice( func_get_args(), 2 );
       
  1282 	$args = array_merge( array( $capability ), $args );
       
  1283 
       
  1284 	return call_user_func_array( array( &$user, 'has_cap' ), $args );
  1002 }
  1285 }
  1003 
  1286 
  1004 /**
  1287 /**
  1005  * Retrieve role object.
  1288  * Retrieve role object.
  1006  *
  1289  *
  1025  * @see WP_Roles::add_role() Uses method to add role.
  1308  * @see WP_Roles::add_role() Uses method to add role.
  1026  * @since 2.0.0
  1309  * @since 2.0.0
  1027  *
  1310  *
  1028  * @param string $role Role name.
  1311  * @param string $role Role name.
  1029  * @param string $display_name Display name for role.
  1312  * @param string $display_name Display name for role.
  1030  * @param array $capabilities List of capabilities.
  1313  * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
  1031  * @return null|WP_Role WP_Role object if role is added, null if already exists.
  1314  * @return null|WP_Role WP_Role object if role is added, null if already exists.
  1032  */
  1315  */
  1033 function add_role( $role, $display_name, $capabilities = array() ) {
  1316 function add_role( $role, $display_name, $capabilities = array() ) {
  1034 	global $wp_roles;
  1317 	global $wp_roles;
  1035 
  1318 
  1055 		$wp_roles = new WP_Roles();
  1338 		$wp_roles = new WP_Roles();
  1056 
  1339 
  1057 	return $wp_roles->remove_role( $role );
  1340 	return $wp_roles->remove_role( $role );
  1058 }
  1341 }
  1059 
  1342 
  1060 ?>
  1343 /**
       
  1344  * Retrieve a list of super admins.
       
  1345  *
       
  1346  * @since 3.0.0
       
  1347  *
       
  1348  * @uses $super_admins Super admins global variable, if set.
       
  1349  *
       
  1350  * @return array List of super admin logins
       
  1351  */
       
  1352 function get_super_admins() {
       
  1353 	global $super_admins;
       
  1354 
       
  1355 	if ( isset($super_admins) )
       
  1356 		return $super_admins;
       
  1357 	else
       
  1358 		return get_site_option( 'site_admins', array('admin') );
       
  1359 }
       
  1360 
       
  1361 /**
       
  1362  * Determine if user is a site admin.
       
  1363  *
       
  1364  * @since 3.0.0
       
  1365  *
       
  1366  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
       
  1367  * @return bool True if the user is a site admin.
       
  1368  */
       
  1369 function is_super_admin( $user_id = false ) {
       
  1370 	if ( $user_id )
       
  1371 		$user = new WP_User( $user_id );
       
  1372 	else
       
  1373 		$user = wp_get_current_user();
       
  1374 
       
  1375 	if ( ! $user->exists() )
       
  1376 		return false;
       
  1377 
       
  1378 	if ( is_multisite() ) {
       
  1379 		$super_admins = get_super_admins();
       
  1380 		if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
       
  1381 			return true;
       
  1382 	} else {
       
  1383 		if ( $user->has_cap('delete_users') )
       
  1384 			return true;
       
  1385 	}
       
  1386 
       
  1387 	return false;
       
  1388 }