Edit File by line
/home/barbar84/www/wp-inclu...
File: class-wp-roles.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* User API: WP_Roles class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Users
[5] Fix | Delete
* @since 4.4.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class used to implement a user roles API.
[10] Fix | Delete
*
[11] Fix | Delete
* The role option is simple, the structure is organized by role name that store
[12] Fix | Delete
* the name in value of the 'name' key. The capabilities are stored as an array
[13] Fix | Delete
* in the value of the 'capability' key.
[14] Fix | Delete
*
[15] Fix | Delete
* array (
[16] Fix | Delete
* 'rolename' => array (
[17] Fix | Delete
* 'name' => 'rolename',
[18] Fix | Delete
* 'capabilities' => array()
[19] Fix | Delete
* )
[20] Fix | Delete
* )
[21] Fix | Delete
*
[22] Fix | Delete
* @since 2.0.0
[23] Fix | Delete
*/
[24] Fix | Delete
class WP_Roles {
[25] Fix | Delete
/**
[26] Fix | Delete
* List of roles and capabilities.
[27] Fix | Delete
*
[28] Fix | Delete
* @since 2.0.0
[29] Fix | Delete
* @var array[]
[30] Fix | Delete
*/
[31] Fix | Delete
public $roles;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* List of the role objects.
[35] Fix | Delete
*
[36] Fix | Delete
* @since 2.0.0
[37] Fix | Delete
* @var WP_Role[]
[38] Fix | Delete
*/
[39] Fix | Delete
public $role_objects = array();
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* List of role names.
[43] Fix | Delete
*
[44] Fix | Delete
* @since 2.0.0
[45] Fix | Delete
* @var string[]
[46] Fix | Delete
*/
[47] Fix | Delete
public $role_names = array();
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* Option name for storing role list.
[51] Fix | Delete
*
[52] Fix | Delete
* @since 2.0.0
[53] Fix | Delete
* @var string
[54] Fix | Delete
*/
[55] Fix | Delete
public $role_key;
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Whether to use the database for retrieval and storage.
[59] Fix | Delete
*
[60] Fix | Delete
* @since 2.1.0
[61] Fix | Delete
* @var bool
[62] Fix | Delete
*/
[63] Fix | Delete
public $use_db = true;
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* The site ID the roles are initialized for.
[67] Fix | Delete
*
[68] Fix | Delete
* @since 4.9.0
[69] Fix | Delete
* @var int
[70] Fix | Delete
*/
[71] Fix | Delete
protected $site_id = 0;
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Constructor
[75] Fix | Delete
*
[76] Fix | Delete
* @since 2.0.0
[77] Fix | Delete
* @since 4.9.0 The `$site_id` argument was added.
[78] Fix | Delete
*
[79] Fix | Delete
* @global array $wp_user_roles Used to set the 'roles' property value.
[80] Fix | Delete
*
[81] Fix | Delete
* @param int $site_id Site ID to initialize roles for. Default is the current site.
[82] Fix | Delete
*/
[83] Fix | Delete
public function __construct( $site_id = null ) {
[84] Fix | Delete
global $wp_user_roles;
[85] Fix | Delete
[86] Fix | Delete
$this->use_db = empty( $wp_user_roles );
[87] Fix | Delete
[88] Fix | Delete
$this->for_site( $site_id );
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Make private/protected methods readable for backward compatibility.
[93] Fix | Delete
*
[94] Fix | Delete
* @since 4.0.0
[95] Fix | Delete
*
[96] Fix | Delete
* @param string $name Method to call.
[97] Fix | Delete
* @param array $arguments Arguments to pass when calling.
[98] Fix | Delete
* @return mixed|false Return value of the callback, false otherwise.
[99] Fix | Delete
*/
[100] Fix | Delete
public function __call( $name, $arguments ) {
[101] Fix | Delete
if ( '_init' === $name ) {
[102] Fix | Delete
return $this->_init( ...$arguments );
[103] Fix | Delete
}
[104] Fix | Delete
return false;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Set up the object properties.
[109] Fix | Delete
*
[110] Fix | Delete
* The role key is set to the current prefix for the $wpdb object with
[111] Fix | Delete
* 'user_roles' appended. If the $wp_user_roles global is set, then it will
[112] Fix | Delete
* be used and the role option will not be updated or used.
[113] Fix | Delete
*
[114] Fix | Delete
* @since 2.1.0
[115] Fix | Delete
* @deprecated 4.9.0 Use WP_Roles::for_site()
[116] Fix | Delete
*/
[117] Fix | Delete
protected function _init() {
[118] Fix | Delete
_deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' );
[119] Fix | Delete
[120] Fix | Delete
$this->for_site();
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Reinitialize the object
[125] Fix | Delete
*
[126] Fix | Delete
* Recreates the role objects. This is typically called only by switch_to_blog()
[127] Fix | Delete
* after switching wpdb to a new site ID.
[128] Fix | Delete
*
[129] Fix | Delete
* @since 3.5.0
[130] Fix | Delete
* @deprecated 4.7.0 Use WP_Roles::for_site()
[131] Fix | Delete
*/
[132] Fix | Delete
public function reinit() {
[133] Fix | Delete
_deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' );
[134] Fix | Delete
[135] Fix | Delete
$this->for_site();
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Add role name with capabilities to list.
[140] Fix | Delete
*
[141] Fix | Delete
* Updates the list of roles, if the role doesn't already exist.
[142] Fix | Delete
*
[143] Fix | Delete
* The capabilities are defined in the following format `array( 'read' => true );`
[144] Fix | Delete
* To explicitly deny a role a capability you set the value for that capability to false.
[145] Fix | Delete
*
[146] Fix | Delete
* @since 2.0.0
[147] Fix | Delete
*
[148] Fix | Delete
* @param string $role Role name.
[149] Fix | Delete
* @param string $display_name Role display name.
[150] Fix | Delete
* @param bool[] $capabilities List of capabilities keyed by the capability name,
[151] Fix | Delete
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
[152] Fix | Delete
* @return WP_Role|void WP_Role object, if role is added.
[153] Fix | Delete
*/
[154] Fix | Delete
public function add_role( $role, $display_name, $capabilities = array() ) {
[155] Fix | Delete
if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
[156] Fix | Delete
return;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
$this->roles[ $role ] = array(
[160] Fix | Delete
'name' => $display_name,
[161] Fix | Delete
'capabilities' => $capabilities,
[162] Fix | Delete
);
[163] Fix | Delete
if ( $this->use_db ) {
[164] Fix | Delete
update_option( $this->role_key, $this->roles );
[165] Fix | Delete
}
[166] Fix | Delete
$this->role_objects[ $role ] = new WP_Role( $role, $capabilities );
[167] Fix | Delete
$this->role_names[ $role ] = $display_name;
[168] Fix | Delete
return $this->role_objects[ $role ];
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
/**
[172] Fix | Delete
* Remove role by name.
[173] Fix | Delete
*
[174] Fix | Delete
* @since 2.0.0
[175] Fix | Delete
*
[176] Fix | Delete
* @param string $role Role name.
[177] Fix | Delete
*/
[178] Fix | Delete
public function remove_role( $role ) {
[179] Fix | Delete
if ( ! isset( $this->role_objects[ $role ] ) ) {
[180] Fix | Delete
return;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
unset( $this->role_objects[ $role ] );
[184] Fix | Delete
unset( $this->role_names[ $role ] );
[185] Fix | Delete
unset( $this->roles[ $role ] );
[186] Fix | Delete
[187] Fix | Delete
if ( $this->use_db ) {
[188] Fix | Delete
update_option( $this->role_key, $this->roles );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
if ( get_option( 'default_role' ) == $role ) {
[192] Fix | Delete
update_option( 'default_role', 'subscriber' );
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
/**
[197] Fix | Delete
* Add capability to role.
[198] Fix | Delete
*
[199] Fix | Delete
* @since 2.0.0
[200] Fix | Delete
*
[201] Fix | Delete
* @param string $role Role name.
[202] Fix | Delete
* @param string $cap Capability name.
[203] Fix | Delete
* @param bool $grant Optional. Whether role is capable of performing capability.
[204] Fix | Delete
* Default true.
[205] Fix | Delete
*/
[206] Fix | Delete
public function add_cap( $role, $cap, $grant = true ) {
[207] Fix | Delete
if ( ! isset( $this->roles[ $role ] ) ) {
[208] Fix | Delete
return;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
$this->roles[ $role ]['capabilities'][ $cap ] = $grant;
[212] Fix | Delete
if ( $this->use_db ) {
[213] Fix | Delete
update_option( $this->role_key, $this->roles );
[214] Fix | Delete
}
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Remove capability from role.
[219] Fix | Delete
*
[220] Fix | Delete
* @since 2.0.0
[221] Fix | Delete
*
[222] Fix | Delete
* @param string $role Role name.
[223] Fix | Delete
* @param string $cap Capability name.
[224] Fix | Delete
*/
[225] Fix | Delete
public function remove_cap( $role, $cap ) {
[226] Fix | Delete
if ( ! isset( $this->roles[ $role ] ) ) {
[227] Fix | Delete
return;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
unset( $this->roles[ $role ]['capabilities'][ $cap ] );
[231] Fix | Delete
if ( $this->use_db ) {
[232] Fix | Delete
update_option( $this->role_key, $this->roles );
[233] Fix | Delete
}
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Retrieve role object by name.
[238] Fix | Delete
*
[239] Fix | Delete
* @since 2.0.0
[240] Fix | Delete
*
[241] Fix | Delete
* @param string $role Role name.
[242] Fix | Delete
* @return WP_Role|null WP_Role object if found, null if the role does not exist.
[243] Fix | Delete
*/
[244] Fix | Delete
public function get_role( $role ) {
[245] Fix | Delete
if ( isset( $this->role_objects[ $role ] ) ) {
[246] Fix | Delete
return $this->role_objects[ $role ];
[247] Fix | Delete
} else {
[248] Fix | Delete
return null;
[249] Fix | Delete
}
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
/**
[253] Fix | Delete
* Retrieve list of role names.
[254] Fix | Delete
*
[255] Fix | Delete
* @since 2.0.0
[256] Fix | Delete
*
[257] Fix | Delete
* @return string[] List of role names.
[258] Fix | Delete
*/
[259] Fix | Delete
public function get_names() {
[260] Fix | Delete
return $this->role_names;
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
/**
[264] Fix | Delete
* Whether role name is currently in the list of available roles.
[265] Fix | Delete
*
[266] Fix | Delete
* @since 2.0.0
[267] Fix | Delete
*
[268] Fix | Delete
* @param string $role Role name to look up.
[269] Fix | Delete
* @return bool
[270] Fix | Delete
*/
[271] Fix | Delete
public function is_role( $role ) {
[272] Fix | Delete
return isset( $this->role_names[ $role ] );
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Initializes all of the available roles.
[277] Fix | Delete
*
[278] Fix | Delete
* @since 4.9.0
[279] Fix | Delete
*/
[280] Fix | Delete
public function init_roles() {
[281] Fix | Delete
if ( empty( $this->roles ) ) {
[282] Fix | Delete
return;
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
$this->role_objects = array();
[286] Fix | Delete
$this->role_names = array();
[287] Fix | Delete
foreach ( array_keys( $this->roles ) as $role ) {
[288] Fix | Delete
$this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
[289] Fix | Delete
$this->role_names[ $role ] = $this->roles[ $role ]['name'];
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
/**
[293] Fix | Delete
* After the roles have been initialized, allow plugins to add their own roles.
[294] Fix | Delete
*
[295] Fix | Delete
* @since 4.7.0
[296] Fix | Delete
*
[297] Fix | Delete
* @param WP_Roles $this A reference to the WP_Roles object.
[298] Fix | Delete
*/
[299] Fix | Delete
do_action( 'wp_roles_init', $this );
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* Sets the site to operate on. Defaults to the current site.
[304] Fix | Delete
*
[305] Fix | Delete
* @since 4.9.0
[306] Fix | Delete
*
[307] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[308] Fix | Delete
*
[309] Fix | Delete
* @param int $site_id Site ID to initialize roles for. Default is the current site.
[310] Fix | Delete
*/
[311] Fix | Delete
public function for_site( $site_id = null ) {
[312] Fix | Delete
global $wpdb;
[313] Fix | Delete
[314] Fix | Delete
if ( ! empty( $site_id ) ) {
[315] Fix | Delete
$this->site_id = absint( $site_id );
[316] Fix | Delete
} else {
[317] Fix | Delete
$this->site_id = get_current_blog_id();
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
$this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles';
[321] Fix | Delete
[322] Fix | Delete
if ( ! empty( $this->roles ) && ! $this->use_db ) {
[323] Fix | Delete
return;
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
$this->roles = $this->get_roles_data();
[327] Fix | Delete
[328] Fix | Delete
$this->init_roles();
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
/**
[332] Fix | Delete
* Gets the ID of the site for which roles are currently initialized.
[333] Fix | Delete
*
[334] Fix | Delete
* @since 4.9.0
[335] Fix | Delete
*
[336] Fix | Delete
* @return int Site ID.
[337] Fix | Delete
*/
[338] Fix | Delete
public function get_site_id() {
[339] Fix | Delete
return $this->site_id;
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
/**
[343] Fix | Delete
* Gets the available roles data.
[344] Fix | Delete
*
[345] Fix | Delete
* @since 4.9.0
[346] Fix | Delete
*
[347] Fix | Delete
* @global array $wp_user_roles Used to set the 'roles' property value.
[348] Fix | Delete
*
[349] Fix | Delete
* @return array Roles array.
[350] Fix | Delete
*/
[351] Fix | Delete
protected function get_roles_data() {
[352] Fix | Delete
global $wp_user_roles;
[353] Fix | Delete
[354] Fix | Delete
if ( ! empty( $wp_user_roles ) ) {
[355] Fix | Delete
return $wp_user_roles;
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
if ( is_multisite() && get_current_blog_id() != $this->site_id ) {
[359] Fix | Delete
remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
[360] Fix | Delete
[361] Fix | Delete
$roles = get_blog_option( $this->site_id, $this->role_key, array() );
[362] Fix | Delete
[363] Fix | Delete
add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
[364] Fix | Delete
[365] Fix | Delete
return $roles;
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
return get_option( $this->role_key, array() );
[369] Fix | Delete
}
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function