wp/wp-includes/class-wp-roles.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    13  * The role option is simple, the structure is organized by role name that store
    13  * The role option is simple, the structure is organized by role name that store
    14  * the name in value of the 'name' key. The capabilities are stored as an array
    14  * the name in value of the 'name' key. The capabilities are stored as an array
    15  * in the value of the 'capability' key.
    15  * in the value of the 'capability' key.
    16  *
    16  *
    17  *     array (
    17  *     array (
    18  *    		'rolename' => array (
    18  *          'rolename' => array (
    19  *    			'name' => 'rolename',
    19  *              'name' => 'rolename',
    20  *    			'capabilities' => array()
    20  *              'capabilities' => array()
    21  *    		)
    21  *          )
    22  *     )
    22  *     )
    23  *
    23  *
    24  * @since 2.0.0
    24  * @since 2.0.0
    25  */
    25  */
    26 class WP_Roles {
    26 class WP_Roles {
    27 	/**
    27 	/**
    28 	 * List of roles and capabilities.
    28 	 * List of roles and capabilities.
    29 	 *
    29 	 *
    30 	 * @since 2.0.0
    30 	 * @since 2.0.0
    31 	 * @var array
    31 	 * @var array[]
    32 	 */
    32 	 */
    33 	public $roles;
    33 	public $roles;
    34 
    34 
    35 	/**
    35 	/**
    36 	 * List of the role objects.
    36 	 * List of the role objects.
    37 	 *
    37 	 *
    38 	 * @since 2.0.0
    38 	 * @since 2.0.0
    39 	 * @var array
    39 	 * @var WP_Role[]
    40 	 */
    40 	 */
    41 	public $role_objects = array();
    41 	public $role_objects = array();
    42 
    42 
    43 	/**
    43 	/**
    44 	 * List of role names.
    44 	 * List of role names.
    45 	 *
    45 	 *
    46 	 * @since 2.0.0
    46 	 * @since 2.0.0
    47 	 * @var array
    47 	 * @var string[]
    48 	 */
    48 	 */
    49 	public $role_names = array();
    49 	public $role_names = array();
    50 
    50 
    51 	/**
    51 	/**
    52 	 * Option name for storing role list.
    52 	 * Option name for storing role list.
    74 
    74 
    75 	/**
    75 	/**
    76 	 * Constructor
    76 	 * Constructor
    77 	 *
    77 	 *
    78 	 * @since 2.0.0
    78 	 * @since 2.0.0
    79 	 * @since 4.9.0 The $site_id argument was added.
    79 	 * @since 4.9.0 The `$site_id` argument was added.
    80 	 *
    80 	 *
    81 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    81 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    82 	 *
    82 	 *
    83 	 * @param int $site_id Site ID to initialize roles for. Default is the current site.
    83 	 * @param int $site_id Site ID to initialize roles for. Default is the current site.
    84 	 */
    84 	 */
    93 	/**
    93 	/**
    94 	 * Make private/protected methods readable for backward compatibility.
    94 	 * Make private/protected methods readable for backward compatibility.
    95 	 *
    95 	 *
    96 	 * @since 4.0.0
    96 	 * @since 4.0.0
    97 	 *
    97 	 *
    98 	 * @param callable $name      Method to call.
    98 	 * @param string   $name      Method to call.
    99 	 * @param array    $arguments Arguments to pass when calling.
    99 	 * @param array    $arguments Arguments to pass when calling.
   100 	 * @return mixed|false Return value of the callback, false otherwise.
   100 	 * @return mixed|false Return value of the callback, false otherwise.
   101 	 */
   101 	 */
   102 	public function __call( $name, $arguments ) {
   102 	public function __call( $name, $arguments ) {
   103 		if ( '_init' === $name ) {
   103 		if ( '_init' === $name ) {
   155 	public function add_role( $role, $display_name, $capabilities = array() ) {
   155 	public function add_role( $role, $display_name, $capabilities = array() ) {
   156 		if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
   156 		if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
   157 			return;
   157 			return;
   158 		}
   158 		}
   159 
   159 
   160 		$this->roles[$role] = array(
   160 		$this->roles[ $role ] = array(
   161 			'name' => $display_name,
   161 			'name'         => $display_name,
   162 			'capabilities' => $capabilities
   162 			'capabilities' => $capabilities,
   163 			);
   163 		);
   164 		if ( $this->use_db )
   164 		if ( $this->use_db ) {
   165 			update_option( $this->role_key, $this->roles );
   165 			update_option( $this->role_key, $this->roles );
   166 		$this->role_objects[$role] = new WP_Role( $role, $capabilities );
   166 		}
   167 		$this->role_names[$role] = $display_name;
   167 		$this->role_objects[ $role ] = new WP_Role( $role, $capabilities );
   168 		return $this->role_objects[$role];
   168 		$this->role_names[ $role ]   = $display_name;
       
   169 		return $this->role_objects[ $role ];
   169 	}
   170 	}
   170 
   171 
   171 	/**
   172 	/**
   172 	 * Remove role by name.
   173 	 * Remove role by name.
   173 	 *
   174 	 *
   174 	 * @since 2.0.0
   175 	 * @since 2.0.0
   175 	 *
   176 	 *
   176 	 * @param string $role Role name.
   177 	 * @param string $role Role name.
   177 	 */
   178 	 */
   178 	public function remove_role( $role ) {
   179 	public function remove_role( $role ) {
   179 		if ( ! isset( $this->role_objects[$role] ) )
   180 		if ( ! isset( $this->role_objects[ $role ] ) ) {
   180 			return;
   181 			return;
   181 
   182 		}
   182 		unset( $this->role_objects[$role] );
   183 
   183 		unset( $this->role_names[$role] );
   184 		unset( $this->role_objects[ $role ] );
   184 		unset( $this->roles[$role] );
   185 		unset( $this->role_names[ $role ] );
   185 
   186 		unset( $this->roles[ $role ] );
   186 		if ( $this->use_db )
   187 
       
   188 		if ( $this->use_db ) {
   187 			update_option( $this->role_key, $this->roles );
   189 			update_option( $this->role_key, $this->roles );
   188 
   190 		}
   189 		if ( get_option( 'default_role' ) == $role )
   191 
       
   192 		if ( get_option( 'default_role' ) == $role ) {
   190 			update_option( 'default_role', 'subscriber' );
   193 			update_option( 'default_role', 'subscriber' );
       
   194 		}
   191 	}
   195 	}
   192 
   196 
   193 	/**
   197 	/**
   194 	 * Add capability to role.
   198 	 * Add capability to role.
   195 	 *
   199 	 *
   198 	 * @param string $role Role name.
   202 	 * @param string $role Role name.
   199 	 * @param string $cap Capability name.
   203 	 * @param string $cap Capability name.
   200 	 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
   204 	 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
   201 	 */
   205 	 */
   202 	public function add_cap( $role, $cap, $grant = true ) {
   206 	public function add_cap( $role, $cap, $grant = true ) {
   203 		if ( ! isset( $this->roles[$role] ) )
   207 		if ( ! isset( $this->roles[ $role ] ) ) {
   204 			return;
   208 			return;
   205 
   209 		}
   206 		$this->roles[$role]['capabilities'][$cap] = $grant;
   210 
   207 		if ( $this->use_db )
   211 		$this->roles[ $role ]['capabilities'][ $cap ] = $grant;
       
   212 		if ( $this->use_db ) {
   208 			update_option( $this->role_key, $this->roles );
   213 			update_option( $this->role_key, $this->roles );
       
   214 		}
   209 	}
   215 	}
   210 
   216 
   211 	/**
   217 	/**
   212 	 * Remove capability from role.
   218 	 * Remove capability from role.
   213 	 *
   219 	 *
   215 	 *
   221 	 *
   216 	 * @param string $role Role name.
   222 	 * @param string $role Role name.
   217 	 * @param string $cap Capability name.
   223 	 * @param string $cap Capability name.
   218 	 */
   224 	 */
   219 	public function remove_cap( $role, $cap ) {
   225 	public function remove_cap( $role, $cap ) {
   220 		if ( ! isset( $this->roles[$role] ) )
   226 		if ( ! isset( $this->roles[ $role ] ) ) {
   221 			return;
   227 			return;
   222 
   228 		}
   223 		unset( $this->roles[$role]['capabilities'][$cap] );
   229 
   224 		if ( $this->use_db )
   230 		unset( $this->roles[ $role ]['capabilities'][ $cap ] );
       
   231 		if ( $this->use_db ) {
   225 			update_option( $this->role_key, $this->roles );
   232 			update_option( $this->role_key, $this->roles );
       
   233 		}
   226 	}
   234 	}
   227 
   235 
   228 	/**
   236 	/**
   229 	 * Retrieve role object by name.
   237 	 * Retrieve role object by name.
   230 	 *
   238 	 *
   232 	 *
   240 	 *
   233 	 * @param string $role Role name.
   241 	 * @param string $role Role name.
   234 	 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
   242 	 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
   235 	 */
   243 	 */
   236 	public function get_role( $role ) {
   244 	public function get_role( $role ) {
   237 		if ( isset( $this->role_objects[$role] ) )
   245 		if ( isset( $this->role_objects[ $role ] ) ) {
   238 			return $this->role_objects[$role];
   246 			return $this->role_objects[ $role ];
   239 		else
   247 		} else {
   240 			return null;
   248 			return null;
       
   249 		}
   241 	}
   250 	}
   242 
   251 
   243 	/**
   252 	/**
   244 	 * Retrieve list of role names.
   253 	 * Retrieve list of role names.
   245 	 *
   254 	 *
   246 	 * @since 2.0.0
   255 	 * @since 2.0.0
   247 	 *
   256 	 *
   248 	 * @return array List of role names.
   257 	 * @return string[] List of role names.
   249 	 */
   258 	 */
   250 	public function get_names() {
   259 	public function get_names() {
   251 		return $this->role_names;
   260 		return $this->role_names;
   252 	}
   261 	}
   253 
   262 
   258 	 *
   267 	 *
   259 	 * @param string $role Role name to look up.
   268 	 * @param string $role Role name to look up.
   260 	 * @return bool
   269 	 * @return bool
   261 	 */
   270 	 */
   262 	public function is_role( $role ) {
   271 	public function is_role( $role ) {
   263 		return isset( $this->role_names[$role] );
   272 		return isset( $this->role_names[ $role ] );
   264 	}
   273 	}
   265 
   274 
   266 	/**
   275 	/**
   267 	 * Initializes all of the available roles.
   276 	 * Initializes all of the available roles.
   268 	 *
   277 	 *
   272 		if ( empty( $this->roles ) ) {
   281 		if ( empty( $this->roles ) ) {
   273 			return;
   282 			return;
   274 		}
   283 		}
   275 
   284 
   276 		$this->role_objects = array();
   285 		$this->role_objects = array();
   277 		$this->role_names =  array();
   286 		$this->role_names   = array();
   278 		foreach ( array_keys( $this->roles ) as $role ) {
   287 		foreach ( array_keys( $this->roles ) as $role ) {
   279 			$this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
   288 			$this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
   280 			$this->role_names[ $role ] = $this->roles[ $role ]['name'];
   289 			$this->role_names[ $role ]   = $this->roles[ $role ]['name'];
   281 		}
   290 		}
   282 
   291 
   283 		/**
   292 		/**
   284 		 * After the roles have been initialized, allow plugins to add their own roles.
   293 		 * After the roles have been initialized, allow plugins to add their own roles.
   285 		 *
   294 		 *