wp/wp-includes/capabilities.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress Roles and Capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage User
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * WordPress User Roles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * The role option is simple, the structure is organized by role name that store
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * the name in value of the 'name' key. The capabilities are stored as an array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * in the value of the 'capability' key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    16
 *     array (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    17
 *    		'rolename' => array (
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    18
 *    			'name' => 'rolename',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
 *    			'capabilities' => array()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
 *    		)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
 *     )
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * @subpackage User
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
class WP_Roles {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	 * List of roles and capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
	public $roles;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
	 * List of the role objects.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	public $role_objects = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	 * List of role names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
	public $role_names = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	 * Option name for storing role list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	public $role_key;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	 * Whether to use the database for retrieval and storage.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 * @var bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
	public $use_db = true;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	 * Constructor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
	public function __construct() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		$this->_init();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
	 * Make private/protected methods readable for backwards compatibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
	 * @access public
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
	 * @param callable $name      Method to call.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
	 * @param array    $arguments Arguments to pass when calling.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
	 * @return mixed|bool Return value of the callback, false otherwise.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
	public function __call( $name, $arguments ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
		if ( '_init' === $name ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
			return call_user_func_array( array( $this, $name ), $arguments );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		return false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * Set up the object properties.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 * The role key is set to the current prefix for the $wpdb object with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	 * be used and the role option will not be updated or used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	 * @access protected
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
	 * @global wpdb  $wpdb          WordPress database abstraction object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	 * @global array $wp_user_roles Used to set the 'roles' property value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
	protected function _init() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		global $wpdb, $wp_user_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		$this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		if ( ! empty( $wp_user_roles ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
			$this->roles = $wp_user_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
			$this->use_db = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
			$this->roles = get_option( $this->role_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		if ( empty( $this->roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		$this->role_objects = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		$this->role_names =  array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		foreach ( array_keys( $this->roles ) as $role ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
			$this->role_names[$role] = $this->roles[$role]['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
	 * Reinitialize the object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	 * Recreates the role objects. This is typically called only by switch_to_blog()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	 * after switching wpdb to a new blog ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
	public function reinit() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		// There is no need to reinit if using the wp_user_roles global.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
		if ( ! $this->use_db )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
		global $wpdb;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		// Duplicated from _init() to avoid an extra function call.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$this->roles = get_option( $this->role_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
		if ( empty( $this->roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		$this->role_objects = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		$this->role_names =  array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
		foreach ( array_keys( $this->roles ) as $role ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
			$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
			$this->role_names[$role] = $this->roles[$role]['name'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	 * Add role name with capabilities to list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	 * Updates the list of roles, if the role doesn't already exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	 * The capabilities are defined in the following format `array( 'read' => true );`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
	 * To explicitly deny a role a capability you set the value for that capability to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	 * @param string $display_name Role display name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	 * @param array $capabilities List of role capabilities in the above format.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	 * @return WP_Role|null WP_Role object if role is added, null if already exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   179
	public function add_role( $role, $display_name, $capabilities = array() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
		if ( isset( $this->roles[$role] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		$this->roles[$role] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
			'name' => $display_name,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
			'capabilities' => $capabilities
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
		if ( $this->use_db )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
			update_option( $this->role_key, $this->roles );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
		$this->role_objects[$role] = new WP_Role( $role, $capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
		$this->role_names[$role] = $display_name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		return $this->role_objects[$role];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
	 * Remove role by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
	public function remove_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
		if ( ! isset( $this->role_objects[$role] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
		unset( $this->role_objects[$role] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		unset( $this->role_names[$role] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
		unset( $this->roles[$role] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		if ( $this->use_db )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
			update_option( $this->role_key, $this->roles );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		if ( get_option( 'default_role' ) == $role )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
			update_option( 'default_role', 'subscriber' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
	 * Add capability to role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
	public function add_cap( $role, $cap, $grant = true ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
		if ( ! isset( $this->roles[$role] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		$this->roles[$role]['capabilities'][$cap] = $grant;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
		if ( $this->use_db )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
			update_option( $this->role_key, $this->roles );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
	 * Remove capability from role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
	public function remove_cap( $role, $cap ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		if ( ! isset( $this->roles[$role] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		unset( $this->roles[$role]['capabilities'][$cap] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		if ( $this->use_db )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
			update_option( $this->role_key, $this->roles );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
	 * Retrieve role object by name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
	 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	public function get_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
		if ( isset( $this->role_objects[$role] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
			return $this->role_objects[$role];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
			return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
	 * Retrieve list of role names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	 * @return array List of role names.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	public function get_names() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
		return $this->role_names;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
	 * Whether role name is currently in the list of available roles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
	 * @param string $role Role name to look up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
	public function is_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		return isset( $this->role_names[$role] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 * WordPress Role class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
 * @subpackage User
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
class WP_Role {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	 * Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
	public $name;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
	 * List of capabilities the role contains.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
	public $capabilities;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
	 * Constructor - Set up object properties.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	 * The list of capabilities, must have the key as the name of the capability
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	 * and the value a boolean of whether it is granted to the role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
	 * @param array $capabilities List of capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	public function __construct( $role, $capabilities ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		$this->name = $role;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
		$this->capabilities = $capabilities;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
	 * Assign role a capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
	 * @see WP_Roles::add_cap() Method uses implementation for role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
	 * @param bool $grant Whether role has capability privilege.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
	public function add_cap( $cap, $grant = true ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
			$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		$this->capabilities[$cap] = $grant;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
		$wp_roles->add_cap( $this->name, $cap, $grant );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	 * Remove capability from role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
	 * This is a container for {@link WP_Roles::remove_cap()} to remove the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	 * capability from the role. That is to say, that {@link
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
	 * WP_Roles::remove_cap()} implements the functionality, but it also makes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	 * sense to use this class, because you don't need to enter the role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	public function remove_cap( $cap ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
		if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
			$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
		unset( $this->capabilities[$cap] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
		$wp_roles->remove_cap( $this->name, $cap );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	 * Whether role has capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
	 * The capabilities is passed through the 'role_has_cap' filter. The first
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	 * parameter for the hook is the list of capabilities the class has
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	 * assigned. The second parameter is the capability name to look for. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
	 * third and final parameter for the hook is the role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	 * @return bool True, if user has capability. False, if doesn't have capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
	public function has_cap( $cap ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
		 * Filter which capabilities a role has.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
		 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
		 * @param array  $capabilities Array of role capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
		 * @param string $cap          Capability name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
		 * @param string $name         Role name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   405
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
		$capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
		if ( !empty( $capabilities[$cap] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
			return $capabilities[$cap];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 * WordPress User class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 * @subpackage User
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   421
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
 * @property string $nickname
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
 * @property string $user_description
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
 * @property string $user_firstname
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
 * @property string $user_lastname
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
 * @property string $user_login
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
 * @property string $user_pass
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   428
 * @property string $user_nicename
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   429
 * @property string $user_email
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
 * @property string $user_url
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
 * @property string $user_registered
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
 * @property string $user_activation_key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
 * @property string $user_status
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
 * @property string $display_name
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
 * @property string $spam
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
 * @property string $deleted
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
class WP_User {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	 * User data container.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
	 * @since 2.0.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
	 * @var object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   445
	public $data;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
	 * The user's ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   454
	public $ID = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	 * The individual capabilities the user has been given.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   463
	public $caps = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	 * User metadata option name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
	public $cap_key;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	 * The roles the user is part of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
	public $roles = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
	 * All capabilities the user has, including individual and role based.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	 * @var array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
	public $allcaps = array();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	 * The filter context applied to user data fields.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
	 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	var $filter = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	private static $back_compat_keys;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	 * Constructor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
	 * Retrieves the userdata and passes it to {@link WP_User::init()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
	 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	 * @param string $name Optional. User's username
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
	 * @param int $blog_id Optional Blog ID, defaults to current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
	public function __construct( $id = 0, $name = '', $blog_id = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
		if ( ! isset( self::$back_compat_keys ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
			$prefix = $GLOBALS['wpdb']->prefix;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
			self::$back_compat_keys = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
				'user_firstname' => 'first_name',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
				'user_lastname' => 'last_name',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
				'user_description' => 'description',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
				'user_level' => $prefix . 'user_level',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
				$prefix . 'usersettings' => $prefix . 'user-settings',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
			);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   528
		if ( $id instanceof WP_User ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
			$this->init( $id->data, $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
		} elseif ( is_object( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
			$this->init( $id, $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
			$name = $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
			$id = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   541
		if ( $id ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
			$data = self::get_data_by( 'id', $id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
			$data = self::get_data_by( 'login', $name );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   545
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   547
		if ( $data ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
			$this->init( $data, $blog_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   549
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
			$this->data = new stdClass;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   551
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
	 * Sets up object properties, including capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
	 * @param object $data User DB row object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
	 * @param int $blog_id Optional. The blog id to initialize for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
	public function init( $data, $blog_id = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		$this->data = $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
		$this->ID = (int) $data->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
		$this->for_blog( $blog_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
	 * Return only the main user fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
	 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	 * @param string|int $value The field value
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   574
	 * @return object|false Raw user object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   576
	public static function get_data_by( $field, $value ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
		if ( 'id' == $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
			// Make sure the value is numeric to avoid casting objects, for example,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
			// to int 1.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
			if ( ! is_numeric( $value ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
			$value = intval( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
			if ( $value < 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
			$value = trim( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
		if ( !$value )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
		switch ( $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
			case 'id':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
				$user_id = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
				$db_field = 'ID';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
			case 'slug':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
				$user_id = wp_cache_get($value, 'userslugs');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
				$db_field = 'user_nicename';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
			case 'email':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
				$user_id = wp_cache_get($value, 'useremail');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
				$db_field = 'user_email';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
			case 'login':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
				$value = sanitize_user( $value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
				$user_id = wp_cache_get($value, 'userlogins');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
				$db_field = 'user_login';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
			default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
		if ( false !== $user_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
			if ( $user = wp_cache_get( $user_id, 'users' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
				return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
		if ( !$user = $wpdb->get_row( $wpdb->prepare(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
			"SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
		) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		update_user_caches( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
		return $user;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	 * Magic method for checking the existence of a certain custom field
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
	 * @since 3.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   635
	 * @param string $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   636
	 * @return bool
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   638
	public function __isset( $key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
		if ( 'id' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
			$key = 'ID';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
		if ( isset( $this->data->$key ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		if ( isset( self::$back_compat_keys[ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
			$key = self::$back_compat_keys[ $key ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
		return metadata_exists( 'user', $this->ID, $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
	 * Magic method for accessing custom fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	 * @since 3.3.0
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   657
	 * @param string $key
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   658
	 * @return mixed
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   660
	public function __get( $key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
		if ( 'id' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
			return $this->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
		if ( isset( $this->data->$key ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
			$value = $this->data->$key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
			if ( isset( self::$back_compat_keys[ $key ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
				$key = self::$back_compat_keys[ $key ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
			$value = get_user_meta( $this->ID, $key, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
		if ( $this->filter ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
			$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
		return $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
	 * Magic method for setting custom fields
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   686
	public function __set( $key, $value ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
		if ( 'id' == $key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
			_deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
			$this->ID = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
		$this->data->$key = $value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
	 * Determine whether the user exists in the database.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
	 * @return bool True if user exists in the database, false if not.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   704
	public function exists() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
		return ! empty( $this->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
	 * Retrieve the value of a property or meta key.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
	 * Retrieves from the users and usermeta table.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
	 * @param string $key Property
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   717
	public function get( $key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
		return $this->__get( $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
	 * Determine whether a property or meta key is set
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
	 * Consults the users and usermeta tables.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
	 * @since 3.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
	 * @param string $key Property
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   730
	public function has_prop( $key ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		return $this->__isset( $key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   734
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
	 * Return an array representation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
	 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
	 * @return array Array representation.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   741
	public function to_array() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
		return get_object_vars( $this->data );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
	 * Set up capability object properties.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
	 * Will set the value for the 'cap_key' property to current database table
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
	 * prefix, followed by 'capabilities'. Will then check to see if the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
	 * property matching the 'cap_key' exists and is an array. If so, it will be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
	 * used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
	 * @access protected
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
	 * @param string $cap_key Optional capability key
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
	function _init_caps( $cap_key = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
		if ( empty($cap_key) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
			$this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
			$this->cap_key = $cap_key;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
		$this->caps = get_user_meta( $this->ID, $this->cap_key, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
		if ( ! is_array( $this->caps ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
			$this->caps = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
		$this->get_role_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
	 * Retrieve all of the role capabilities and merge with individual capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
	 * All of the capabilities of the roles the user belongs to are merged with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
	 * the users individual roles. This also means that the user can be denied
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
	 * specific roles that their role might have, but the specific user isn't
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
	 * granted permission to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
	 * @uses $wp_roles
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
	 * @return array List of all capabilities for the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   788
	public function get_role_caps() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
		if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
			$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
		//Filter out caps that are not role names and assign to $this->roles
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		if ( is_array( $this->caps ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
			$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
		//Build $allcaps from role caps, overlay user's $caps
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
		$this->allcaps = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
		foreach ( (array) $this->roles as $role ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
			$the_role = $wp_roles->get_role( $role );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
			$this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		$this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
		return $this->allcaps;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
	 * Add role to user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
	 * Updates the user's meta data option with capabilities and roles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   819
	public function add_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
		$this->caps[$role] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
		$this->get_role_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
		$this->update_user_level_from_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
	 * Remove role from user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   834
	public function remove_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
		if ( !in_array($role, $this->roles) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		unset( $this->caps[$role] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
		$this->get_role_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
		$this->update_user_level_from_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	 * Set the role of the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
	 * This will remove the previous roles of the user and assign the user the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
	 * new one. You can set the role to an empty string and it will remove all
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
	 * of the roles from the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
	 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   855
	public function set_role( $role ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
		if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
		foreach ( (array) $this->roles as $oldrole )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
			unset( $this->caps[$oldrole] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
		$old_roles = $this->roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
		if ( !empty( $role ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
			$this->caps[$role] = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
			$this->roles = array( $role => true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
			$this->roles = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
		$this->get_role_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
		$this->update_user_level_from_caps();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   872
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   873
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   874
		 * Fires after the user's role has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   875
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
		 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   877
		 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   878
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   879
		 * @param int    $user_id   The user ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   880
		 * @param string $role      The new role.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   881
		 * @param array  $old_roles An array of the user's previous roles.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   882
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
		do_action( 'set_user_role', $this->ID, $role, $old_roles );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
	 * Choose the maximum level the user has.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
	 * Will compare the level from the $item parameter against the $max
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
	 * parameter. If the item is incorrect, then just the $max parameter value
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
	 * will be returned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
	 * Used to get the max level based on the capabilities the user has. This
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
	 * is also based on roles, so if the user is assigned the Administrator role
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
	 * then the capability 'level_10' will exist and the user will get that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
	 * value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
	 * @param int $max Max level of user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
	 * @param string $item Level capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
	 * @return int Max Level.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   905
	public function level_reduction( $max, $item ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
		if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
			$level = intval( $matches[1] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
			return max( $max, $level );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
			return $max;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
	 * Update the maximum user level for the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
	 * Updates the 'user_level' user metadata (includes prefix that is the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
	 * database table prefix) with the maximum user level. Gets the value from
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
	 * the all of the capabilities that the user has.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
	public function update_user_level_from_caps() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
		update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
	 * Add capability and grant or deny access to capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
	 * @param bool $grant Whether to grant capability to user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
	public function add_cap( $cap, $grant = true ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
		$this->caps[$cap] = $grant;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   942
		$this->get_role_caps();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
		$this->update_user_level_from_caps();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
	 * Remove capability from user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
	 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   954
	public function remove_cap( $cap ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   955
		if ( ! isset( $this->caps[ $cap ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   957
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   958
		unset( $this->caps[ $cap ] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
		update_user_meta( $this->ID, $this->cap_key, $this->caps );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
		$this->get_role_caps();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
		$this->update_user_level_from_caps();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
	 * Remove all of the capabilities of the user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
	public function remove_all_caps() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
		$this->caps = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
		delete_user_meta( $this->ID, $this->cap_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
		delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
		$this->get_role_caps();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
	 * Whether user has capability or role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
	 * This is useful for looking up whether the user has a specific role
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
	 * assigned to the user. The second optional parameter can also be used to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
	 * check for capabilities against a specific object, such as a post or user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
	 * @param string|int $cap Capability or role name to search.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
	 * @return bool True, if user has capability; false, if user does not have capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   991
	public function has_cap( $cap ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
		if ( is_numeric( $cap ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
			_deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
			$cap = $this->translate_level_to_cap( $cap );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
		$args = array_slice( func_get_args(), 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
		$args = array_merge( array( $cap, $this->ID ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
		$caps = call_user_func_array( 'map_meta_cap', $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
		// Multisite super admin has all caps by definition, Unless specifically denied.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
		if ( is_multisite() && is_super_admin( $this->ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
			if ( in_array('do_not_allow', $caps) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1008
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
		 * Dynamically filter a user's capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1010
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
		 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1012
		 * @since 3.7.0 Added the user object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
		 * @param array   $allcaps An array of all the user's capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
		 * @param array   $caps    Actual capabilities for meta capability.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
		 * @param array   $args    Optional parameters passed to has_cap(), typically object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
		 * @param WP_User $user    The user object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1018
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		// Must have ALL requested caps
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
		$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
		$capabilities['exist'] = true; // Everyone is allowed to exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		foreach ( (array) $caps as $cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
			if ( empty( $capabilities[ $cap ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	 * Convert numeric level to level capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	 * Prepends 'level_' to level number.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
	 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
	 * @param int $level Level number, 1 to 10.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
	 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1041
	public function translate_level_to_cap( $level ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
		return 'level_' . $level;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
	 * Set the blog to operate on. Defaults to the current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
	 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
	 * @param int $blog_id Optional Blog ID, defaults to current blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1052
	public function for_blog( $blog_id = '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
		global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		if ( ! empty( $blog_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
			$cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
			$cap_key = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
		$this->_init_caps( $cap_key );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
 * Map meta capabilities to primitive capabilities.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
 * This does not actually compare whether the user ID has the actual capability,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
 * just what the capability or capabilities are. Meta capability list value can
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
 * @param string $cap Capability name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
 * @param int $user_id User ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
 * @return array Actual capabilities for meta capability.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
function map_meta_cap( $cap, $user_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	$args = array_slice( func_get_args(), 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
	$caps = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
	switch ( $cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
	case 'remove_user':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
		$caps[] = 'remove_users';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
	case 'promote_user':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		$caps[] = 'promote_users';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
	case 'edit_user':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
	case 'edit_users':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		// Allow user to edit itself
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
		if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		// If multisite these caps are allowed only for super admins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
		if ( is_multisite() && !is_super_admin( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
			$caps[] = 'edit_users'; // edit_user maps to edit_users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
	case 'delete_post':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
	case 'delete_page':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
		$post = get_post( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
		if ( 'revision' == $post->post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
			$post = get_post( $post->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
		$post_type = get_post_type_object( $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
		if ( ! $post_type->map_meta_cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
			$caps[] = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
			// Prior to 3.1 we would re-call map_meta_cap here.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
			if ( 'delete_post' == $cap )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
				$cap = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1117
		// If the post author is set and the user is the author...
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1118
		if ( $post->post_author && $user_id == $post->post_author ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
			// If the post is published...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
			if ( 'publish' == $post->post_status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
				$caps[] = $post_type->cap->delete_published_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
			} elseif ( 'trash' == $post->post_status ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1123
				if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
					$caps[] = $post_type->cap->delete_published_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1125
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
				// If the post is draft...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
				$caps[] = $post_type->cap->delete_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
			// The user is trying to edit someone else's post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
			$caps[] = $post_type->cap->delete_others_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
			// The post is published, extra cap required.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1134
			if ( 'publish' == $post->post_status ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
				$caps[] = $post_type->cap->delete_published_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1136
			} elseif ( 'private' == $post->post_status ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
				$caps[] = $post_type->cap->delete_private_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1138
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
		// edit_post breaks down to edit_posts, edit_published_posts, or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
		// edit_others_posts
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
	case 'edit_post':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
	case 'edit_page':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		$post = get_post( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
		if ( empty( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
		if ( 'revision' == $post->post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
			$post = get_post( $post->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
		$post_type = get_post_type_object( $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
		if ( ! $post_type->map_meta_cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
			$caps[] = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
			// Prior to 3.1 we would re-call map_meta_cap here.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
			if ( 'edit_post' == $cap )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
				$cap = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1163
		// If the post author is set and the user is the author...
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1164
		if ( $post->post_author && $user_id == $post->post_author ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
			// If the post is published...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
			if ( 'publish' == $post->post_status ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
				$caps[] = $post_type->cap->edit_published_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
			} elseif ( 'trash' == $post->post_status ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1169
				if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
					$caps[] = $post_type->cap->edit_published_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1171
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
				// If the post is draft...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
				$caps[] = $post_type->cap->edit_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
			// The user is trying to edit someone else's post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
			$caps[] = $post_type->cap->edit_others_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
			// The post is published, extra cap required.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1180
			if ( 'publish' == $post->post_status ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
				$caps[] = $post_type->cap->edit_published_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1182
			} elseif ( 'private' == $post->post_status ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
				$caps[] = $post_type->cap->edit_private_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1184
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
	case 'read_post':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
	case 'read_page':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
		$post = get_post( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
		if ( 'revision' == $post->post_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
			$post = get_post( $post->post_parent );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
		$post_type = get_post_type_object( $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
		if ( ! $post_type->map_meta_cap ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
			$caps[] = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
			// Prior to 3.1 we would re-call map_meta_cap here.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
			if ( 'read_post' == $cap )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
				$cap = $post_type->cap->$cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
		$status_obj = get_post_status_object( $post->post_status );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
		if ( $status_obj->public ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
			$caps[] = $post_type->cap->read;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
		if ( $post->post_author && $user_id == $post->post_author ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
			$caps[] = $post_type->cap->read;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
		} elseif ( $status_obj->private ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
			$caps[] = $post_type->cap->read_private_posts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
			$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
	case 'publish_post':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
		$post = get_post( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
		$post_type = get_post_type_object( $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
		$caps[] = $post_type->cap->publish_posts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
	case 'edit_post_meta':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
	case 'delete_post_meta':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
	case 'add_post_meta':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
		$post = get_post( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
		$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
		$meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
		if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1234
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1235
			 * Filter whether the user is allowed to add post meta to a post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1236
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1237
			 * The dynamic portion of the hook name, `$meta_key`, refers to the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1238
			 * meta key passed to {@see map_meta_cap()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1239
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1240
			 * @since 3.3.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1241
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1242
			 * @param bool   $allowed  Whether the user can add the post meta. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1243
			 * @param string $meta_key The meta key.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1244
			 * @param int    $post_id  Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1245
			 * @param int    $user_id  User ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1246
			 * @param string $cap      Capability name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1247
			 * @param array  $caps     User capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1248
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
			$allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
			if ( ! $allowed )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
				$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
		} elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
	case 'edit_comment':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
		$comment = get_comment( $args[0] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
		if ( empty( $comment ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
		$post = get_post( $comment->comment_post_ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
		$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
	case 'unfiltered_upload':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
		if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) )  )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
	case 'unfiltered_html' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
		// Disallow unfiltered_html for all users, even admins and super admins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
		if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
		elseif ( is_multisite() && ! is_super_admin( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
	case 'edit_files':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
	case 'edit_plugins':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
	case 'edit_themes':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
		// Disallow the file editors.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
		if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
		elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
		elseif ( is_multisite() && ! is_super_admin( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
	case 'update_plugins':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
	case 'delete_plugins':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
	case 'install_plugins':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1294
	case 'upload_plugins':
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
	case 'update_themes':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
	case 'delete_themes':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
	case 'install_themes':
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1298
	case 'upload_themes':
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
	case 'update_core':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
		// Disallow anything that creates, deletes, or updates core, plugin, or theme files.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
		// Files in uploads are excepted.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1302
		if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1303
			$caps[] = 'do_not_allow';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1304
		} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
			$caps[] = 'do_not_allow';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1306
		} elseif ( 'upload_themes' === $cap ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1307
			$caps[] = 'install_themes';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1308
		} elseif ( 'upload_plugins' === $cap ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1309
			$caps[] = 'install_plugins';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1310
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
			$caps[] = $cap;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1312
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
	case 'activate_plugins':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
		$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
		if ( is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
			// update_, install_, and delete_ are handled above with is_super_admin().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
			$menu_perms = get_site_option( 'menu_items', array() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
			if ( empty( $menu_perms['plugins'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
				$caps[] = 'manage_network_plugins';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
	case 'delete_user':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
	case 'delete_users':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
		// If multisite only super admins can delete users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
		if ( is_multisite() && ! is_super_admin( $user_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
			$caps[] = 'delete_users'; // delete_user maps to delete_users.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
	case 'create_users':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
		if ( !is_multisite() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
		elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
		break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
	case 'manage_links' :
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
		if ( get_option( 'link_manager_enabled' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
			$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
			$caps[] = 'do_not_allow';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
		break;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1345
	case 'customize' :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1346
		$caps[] = 'edit_theme_options';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1347
		break;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1348
	case 'delete_site':
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1349
		$caps[] = 'manage_options';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1350
		break;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
	default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
		// Handle meta capabilities for custom post types.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
		$post_type_meta_caps = _post_type_meta_capabilities();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
		if ( isset( $post_type_meta_caps[ $cap ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
			$args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
			return call_user_func_array( 'map_meta_cap', $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
		// If no meta caps match, return the original cap.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
		$caps[] = $cap;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1363
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1364
	 * Filter a user's capabilities depending on specific context and/or privilege.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1365
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1366
	 * @since 2.8.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1367
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1368
	 * @param array  $caps    Returns the user's actual capabilities.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1369
	 * @param string $cap     Capability name.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1370
	 * @param int    $user_id The user ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1371
	 * @param array  $args    Adds the context to the cap. Typically the object ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1372
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1373
	return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
 * Whether current user has capability or role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
 * @param string $capability Capability or role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
function current_user_can( $capability ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
	$current_user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
	if ( empty( $current_user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
	$args = array_slice( func_get_args(), 1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
	$args = array_merge( array( $capability ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
	return call_user_func_array( array( $current_user, 'has_cap' ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
 * Whether current user has a capability or role for a given blog.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
 * @param int $blog_id Blog ID
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
 * @param string $capability Capability or role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
function current_user_can_for_blog( $blog_id, $capability ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1406
	$switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
	$current_user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1410
	if ( empty( $current_user ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1411
		if ( $switched ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1412
			restore_current_blog();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1413
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
		return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1415
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
	$args = array_slice( func_get_args(), 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
	$args = array_merge( array( $capability ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
	$can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1422
	if ( $switched ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
		restore_current_blog();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1424
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
	return $can;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
 * Whether author of supplied post has capability or role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
 * @param int|object $post Post ID or post object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
 * @param string $capability Capability or role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
function author_can( $post, $capability ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
	if ( !$post = get_post($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
	$author = get_userdata( $post->post_author );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
	if ( ! $author )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
	$args = array_slice( func_get_args(), 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
	$args = array_merge( array( $capability ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
	return call_user_func_array( array( $author, 'has_cap' ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
 * Whether a particular user has capability or role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
 * @since 3.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
 * @param int|object $user User ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
 * @param string $capability Capability or role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
 * @return bool
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
function user_can( $user, $capability ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
	if ( ! is_object( $user ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
		$user = get_userdata( $user );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
	if ( ! $user || ! $user->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
	$args = array_slice( func_get_args(), 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
	$args = array_merge( array( $capability ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
	return call_user_func_array( array( $user, 'has_cap' ), $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
 * Retrieve role object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
 * @see WP_Roles::get_role() Uses method to retrieve role object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
function get_role( $role ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
	global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
	if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
		$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
	return $wp_roles->get_role( $role );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
 * Add role, if it does not exist.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
 * @see WP_Roles::add_role() Uses method to add role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
 * @param string $display_name Display name for role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
 * @return WP_Role|null WP_Role object if role is added, null if already exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
function add_role( $role, $display_name, $capabilities = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
	global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
	if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
		$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
	return $wp_roles->add_role( $role, $display_name, $capabilities );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
 * Remove role, if it exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
 * @see WP_Roles::remove_role() Uses method to remove role.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
 * @since 2.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
 * @param string $role Role name.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
function remove_role( $role ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
	global $wp_roles;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
	if ( ! isset( $wp_roles ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
		$wp_roles = new WP_Roles();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
	$wp_roles->remove_role( $role );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
 * Retrieve a list of super admins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
 * @uses $super_admins Super admins global variable, if set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
 * @return array List of super admin logins
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
function get_super_admins() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
	global $super_admins;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
	if ( isset($super_admins) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
		return $super_admins;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
		return get_site_option( 'site_admins', array('admin') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
 * Determine if user is a site admin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
 * @return bool True if the user is a site admin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
function is_super_admin( $user_id = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
	if ( ! $user_id || $user_id == get_current_user_id() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
		$user = wp_get_current_user();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
		$user = get_userdata( $user_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
	if ( ! $user || ! $user->exists() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
	if ( is_multisite() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
		$super_admins = get_super_admins();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
		if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
		if ( $user->has_cap('delete_users') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
}