Edit File by line
/home/barbar84/www/wp-inclu...
File: class-wp-user.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* User API: WP_User 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 the WP_User object.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 2.0.0
[12] Fix | Delete
*
[13] Fix | Delete
* @property string $nickname
[14] Fix | Delete
* @property string $description
[15] Fix | Delete
* @property string $user_description
[16] Fix | Delete
* @property string $first_name
[17] Fix | Delete
* @property string $user_firstname
[18] Fix | Delete
* @property string $last_name
[19] Fix | Delete
* @property string $user_lastname
[20] Fix | Delete
* @property string $user_login
[21] Fix | Delete
* @property string $user_pass
[22] Fix | Delete
* @property string $user_nicename
[23] Fix | Delete
* @property string $user_email
[24] Fix | Delete
* @property string $user_url
[25] Fix | Delete
* @property string $user_registered
[26] Fix | Delete
* @property string $user_activation_key
[27] Fix | Delete
* @property string $user_status
[28] Fix | Delete
* @property int $user_level
[29] Fix | Delete
* @property string $display_name
[30] Fix | Delete
* @property string $spam
[31] Fix | Delete
* @property string $deleted
[32] Fix | Delete
* @property string $locale
[33] Fix | Delete
* @property string $rich_editing
[34] Fix | Delete
* @property string $syntax_highlighting
[35] Fix | Delete
*/
[36] Fix | Delete
class WP_User {
[37] Fix | Delete
/**
[38] Fix | Delete
* User data container.
[39] Fix | Delete
*
[40] Fix | Delete
* @since 2.0.0
[41] Fix | Delete
* @var stdClass
[42] Fix | Delete
*/
[43] Fix | Delete
public $data;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* The user's ID.
[47] Fix | Delete
*
[48] Fix | Delete
* @since 2.1.0
[49] Fix | Delete
* @var int
[50] Fix | Delete
*/
[51] Fix | Delete
public $ID = 0;
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Capabilities that the individual user has been granted outside of those inherited from their role.
[55] Fix | Delete
*
[56] Fix | Delete
* @since 2.0.0
[57] Fix | Delete
* @var bool[] Array of key/value pairs where keys represent a capability name
[58] Fix | Delete
* and boolean values represent whether the user has that capability.
[59] Fix | Delete
*/
[60] Fix | Delete
public $caps = array();
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* User metadata option name.
[64] Fix | Delete
*
[65] Fix | Delete
* @since 2.0.0
[66] Fix | Delete
* @var string
[67] Fix | Delete
*/
[68] Fix | Delete
public $cap_key;
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* The roles the user is part of.
[72] Fix | Delete
*
[73] Fix | Delete
* @since 2.0.0
[74] Fix | Delete
* @var string[]
[75] Fix | Delete
*/
[76] Fix | Delete
public $roles = array();
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* All capabilities the user has, including individual and role based.
[80] Fix | Delete
*
[81] Fix | Delete
* @since 2.0.0
[82] Fix | Delete
* @var bool[] Array of key/value pairs where keys represent a capability name
[83] Fix | Delete
* and boolean values represent whether the user has that capability.
[84] Fix | Delete
*/
[85] Fix | Delete
public $allcaps = array();
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* The filter context applied to user data fields.
[89] Fix | Delete
*
[90] Fix | Delete
* @since 2.9.0
[91] Fix | Delete
* @var string
[92] Fix | Delete
*/
[93] Fix | Delete
public $filter = null;
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* The site ID the capabilities of this user are initialized for.
[97] Fix | Delete
*
[98] Fix | Delete
* @since 4.9.0
[99] Fix | Delete
* @var int
[100] Fix | Delete
*/
[101] Fix | Delete
private $site_id = 0;
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* @since 3.3.0
[105] Fix | Delete
* @var array
[106] Fix | Delete
*/
[107] Fix | Delete
private static $back_compat_keys;
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Constructor.
[111] Fix | Delete
*
[112] Fix | Delete
* Retrieves the userdata and passes it to WP_User::init().
[113] Fix | Delete
*
[114] Fix | Delete
* @since 2.0.0
[115] Fix | Delete
*
[116] Fix | Delete
* @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
[117] Fix | Delete
* @param string $name Optional. User's username
[118] Fix | Delete
* @param int $site_id Optional Site ID, defaults to current site.
[119] Fix | Delete
*/
[120] Fix | Delete
public function __construct( $id = 0, $name = '', $site_id = '' ) {
[121] Fix | Delete
if ( ! isset( self::$back_compat_keys ) ) {
[122] Fix | Delete
$prefix = $GLOBALS['wpdb']->prefix;
[123] Fix | Delete
self::$back_compat_keys = array(
[124] Fix | Delete
'user_firstname' => 'first_name',
[125] Fix | Delete
'user_lastname' => 'last_name',
[126] Fix | Delete
'user_description' => 'description',
[127] Fix | Delete
'user_level' => $prefix . 'user_level',
[128] Fix | Delete
$prefix . 'usersettings' => $prefix . 'user-settings',
[129] Fix | Delete
$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
[130] Fix | Delete
);
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
if ( $id instanceof WP_User ) {
[134] Fix | Delete
$this->init( $id->data, $site_id );
[135] Fix | Delete
return;
[136] Fix | Delete
} elseif ( is_object( $id ) ) {
[137] Fix | Delete
$this->init( $id, $site_id );
[138] Fix | Delete
return;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
if ( ! empty( $id ) && ! is_numeric( $id ) ) {
[142] Fix | Delete
$name = $id;
[143] Fix | Delete
$id = 0;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
if ( $id ) {
[147] Fix | Delete
$data = self::get_data_by( 'id', $id );
[148] Fix | Delete
} else {
[149] Fix | Delete
$data = self::get_data_by( 'login', $name );
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
if ( $data ) {
[153] Fix | Delete
$this->init( $data, $site_id );
[154] Fix | Delete
} else {
[155] Fix | Delete
$this->data = new stdClass;
[156] Fix | Delete
}
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Sets up object properties, including capabilities.
[161] Fix | Delete
*
[162] Fix | Delete
* @since 3.3.0
[163] Fix | Delete
*
[164] Fix | Delete
* @param object $data User DB row object.
[165] Fix | Delete
* @param int $site_id Optional. The site ID to initialize for.
[166] Fix | Delete
*/
[167] Fix | Delete
public function init( $data, $site_id = '' ) {
[168] Fix | Delete
$this->data = $data;
[169] Fix | Delete
$this->ID = (int) $data->ID;
[170] Fix | Delete
[171] Fix | Delete
$this->for_site( $site_id );
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Return only the main user fields
[176] Fix | Delete
*
[177] Fix | Delete
* @since 3.3.0
[178] Fix | Delete
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
[179] Fix | Delete
*
[180] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[181] Fix | Delete
*
[182] Fix | Delete
* @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
[183] Fix | Delete
* @param string|int $value The field value
[184] Fix | Delete
* @return object|false Raw user object
[185] Fix | Delete
*/
[186] Fix | Delete
public static function get_data_by( $field, $value ) {
[187] Fix | Delete
global $wpdb;
[188] Fix | Delete
[189] Fix | Delete
// 'ID' is an alias of 'id'.
[190] Fix | Delete
if ( 'ID' === $field ) {
[191] Fix | Delete
$field = 'id';
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
if ( 'id' === $field ) {
[195] Fix | Delete
// Make sure the value is numeric to avoid casting objects, for example,
[196] Fix | Delete
// to int 1.
[197] Fix | Delete
if ( ! is_numeric( $value ) ) {
[198] Fix | Delete
return false;
[199] Fix | Delete
}
[200] Fix | Delete
$value = (int) $value;
[201] Fix | Delete
if ( $value < 1 ) {
[202] Fix | Delete
return false;
[203] Fix | Delete
}
[204] Fix | Delete
} else {
[205] Fix | Delete
$value = trim( $value );
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
if ( ! $value ) {
[209] Fix | Delete
return false;
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
switch ( $field ) {
[213] Fix | Delete
case 'id':
[214] Fix | Delete
$user_id = $value;
[215] Fix | Delete
$db_field = 'ID';
[216] Fix | Delete
break;
[217] Fix | Delete
case 'slug':
[218] Fix | Delete
$user_id = wp_cache_get( $value, 'userslugs' );
[219] Fix | Delete
$db_field = 'user_nicename';
[220] Fix | Delete
break;
[221] Fix | Delete
case 'email':
[222] Fix | Delete
$user_id = wp_cache_get( $value, 'useremail' );
[223] Fix | Delete
$db_field = 'user_email';
[224] Fix | Delete
break;
[225] Fix | Delete
case 'login':
[226] Fix | Delete
$value = sanitize_user( $value );
[227] Fix | Delete
$user_id = wp_cache_get( $value, 'userlogins' );
[228] Fix | Delete
$db_field = 'user_login';
[229] Fix | Delete
break;
[230] Fix | Delete
default:
[231] Fix | Delete
return false;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
if ( false !== $user_id ) {
[235] Fix | Delete
$user = wp_cache_get( $user_id, 'users' );
[236] Fix | Delete
if ( $user ) {
[237] Fix | Delete
return $user;
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
$user = $wpdb->get_row(
[242] Fix | Delete
$wpdb->prepare(
[243] Fix | Delete
"SELECT * FROM $wpdb->users WHERE $db_field = %s LIMIT 1",
[244] Fix | Delete
$value
[245] Fix | Delete
)
[246] Fix | Delete
);
[247] Fix | Delete
if ( ! $user ) {
[248] Fix | Delete
return false;
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
update_user_caches( $user );
[252] Fix | Delete
[253] Fix | Delete
return $user;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Magic method for checking the existence of a certain custom field.
[258] Fix | Delete
*
[259] Fix | Delete
* @since 3.3.0
[260] Fix | Delete
*
[261] Fix | Delete
* @param string $key User meta key to check if set.
[262] Fix | Delete
* @return bool Whether the given user meta key is set.
[263] Fix | Delete
*/
[264] Fix | Delete
public function __isset( $key ) {
[265] Fix | Delete
if ( 'id' === $key ) {
[266] Fix | Delete
_deprecated_argument(
[267] Fix | Delete
'WP_User->id',
[268] Fix | Delete
'2.1.0',
[269] Fix | Delete
sprintf(
[270] Fix | Delete
/* translators: %s: WP_User->ID */
[271] Fix | Delete
__( 'Use %s instead.' ),
[272] Fix | Delete
'<code>WP_User->ID</code>'
[273] Fix | Delete
)
[274] Fix | Delete
);
[275] Fix | Delete
$key = 'ID';
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
if ( isset( $this->data->$key ) ) {
[279] Fix | Delete
return true;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
if ( isset( self::$back_compat_keys[ $key ] ) ) {
[283] Fix | Delete
$key = self::$back_compat_keys[ $key ];
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
return metadata_exists( 'user', $this->ID, $key );
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
/**
[290] Fix | Delete
* Magic method for accessing custom fields.
[291] Fix | Delete
*
[292] Fix | Delete
* @since 3.3.0
[293] Fix | Delete
*
[294] Fix | Delete
* @param string $key User meta key to retrieve.
[295] Fix | Delete
* @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
[296] Fix | Delete
*/
[297] Fix | Delete
public function __get( $key ) {
[298] Fix | Delete
if ( 'id' === $key ) {
[299] Fix | Delete
_deprecated_argument(
[300] Fix | Delete
'WP_User->id',
[301] Fix | Delete
'2.1.0',
[302] Fix | Delete
sprintf(
[303] Fix | Delete
/* translators: %s: WP_User->ID */
[304] Fix | Delete
__( 'Use %s instead.' ),
[305] Fix | Delete
'<code>WP_User->ID</code>'
[306] Fix | Delete
)
[307] Fix | Delete
);
[308] Fix | Delete
return $this->ID;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
if ( isset( $this->data->$key ) ) {
[312] Fix | Delete
$value = $this->data->$key;
[313] Fix | Delete
} else {
[314] Fix | Delete
if ( isset( self::$back_compat_keys[ $key ] ) ) {
[315] Fix | Delete
$key = self::$back_compat_keys[ $key ];
[316] Fix | Delete
}
[317] Fix | Delete
$value = get_user_meta( $this->ID, $key, true );
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
if ( $this->filter ) {
[321] Fix | Delete
$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
return $value;
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
/**
[328] Fix | Delete
* Magic method for setting custom user fields.
[329] Fix | Delete
*
[330] Fix | Delete
* This method does not update custom fields in the database. It only stores
[331] Fix | Delete
* the value on the WP_User instance.
[332] Fix | Delete
*
[333] Fix | Delete
* @since 3.3.0
[334] Fix | Delete
*
[335] Fix | Delete
* @param string $key User meta key.
[336] Fix | Delete
* @param mixed $value User meta value.
[337] Fix | Delete
*/
[338] Fix | Delete
public function __set( $key, $value ) {
[339] Fix | Delete
if ( 'id' === $key ) {
[340] Fix | Delete
_deprecated_argument(
[341] Fix | Delete
'WP_User->id',
[342] Fix | Delete
'2.1.0',
[343] Fix | Delete
sprintf(
[344] Fix | Delete
/* translators: %s: WP_User->ID */
[345] Fix | Delete
__( 'Use %s instead.' ),
[346] Fix | Delete
'<code>WP_User->ID</code>'
[347] Fix | Delete
)
[348] Fix | Delete
);
[349] Fix | Delete
$this->ID = $value;
[350] Fix | Delete
return;
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
$this->data->$key = $value;
[354] Fix | Delete
}
[355] Fix | Delete
[356] Fix | Delete
/**
[357] Fix | Delete
* Magic method for unsetting a certain custom field.
[358] Fix | Delete
*
[359] Fix | Delete
* @since 4.4.0
[360] Fix | Delete
*
[361] Fix | Delete
* @param string $key User meta key to unset.
[362] Fix | Delete
*/
[363] Fix | Delete
public function __unset( $key ) {
[364] Fix | Delete
if ( 'id' === $key ) {
[365] Fix | Delete
_deprecated_argument(
[366] Fix | Delete
'WP_User->id',
[367] Fix | Delete
'2.1.0',
[368] Fix | Delete
sprintf(
[369] Fix | Delete
/* translators: %s: WP_User->ID */
[370] Fix | Delete
__( 'Use %s instead.' ),
[371] Fix | Delete
'<code>WP_User->ID</code>'
[372] Fix | Delete
)
[373] Fix | Delete
);
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
if ( isset( $this->data->$key ) ) {
[377] Fix | Delete
unset( $this->data->$key );
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
if ( isset( self::$back_compat_keys[ $key ] ) ) {
[381] Fix | Delete
unset( self::$back_compat_keys[ $key ] );
[382] Fix | Delete
}
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
/**
[386] Fix | Delete
* Determine whether the user exists in the database.
[387] Fix | Delete
*
[388] Fix | Delete
* @since 3.4.0
[389] Fix | Delete
*
[390] Fix | Delete
* @return bool True if user exists in the database, false if not.
[391] Fix | Delete
*/
[392] Fix | Delete
public function exists() {
[393] Fix | Delete
return ! empty( $this->ID );
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Retrieve the value of a property or meta key.
[398] Fix | Delete
*
[399] Fix | Delete
* Retrieves from the users and usermeta table.
[400] Fix | Delete
*
[401] Fix | Delete
* @since 3.3.0
[402] Fix | Delete
*
[403] Fix | Delete
* @param string $key Property
[404] Fix | Delete
* @return mixed
[405] Fix | Delete
*/
[406] Fix | Delete
public function get( $key ) {
[407] Fix | Delete
return $this->__get( $key );
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
/**
[411] Fix | Delete
* Determine whether a property or meta key is set
[412] Fix | Delete
*
[413] Fix | Delete
* Consults the users and usermeta tables.
[414] Fix | Delete
*
[415] Fix | Delete
* @since 3.3.0
[416] Fix | Delete
*
[417] Fix | Delete
* @param string $key Property
[418] Fix | Delete
* @return bool
[419] Fix | Delete
*/
[420] Fix | Delete
public function has_prop( $key ) {
[421] Fix | Delete
return $this->__isset( $key );
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
/**
[425] Fix | Delete
* Return an array representation.
[426] Fix | Delete
*
[427] Fix | Delete
* @since 3.5.0
[428] Fix | Delete
*
[429] Fix | Delete
* @return array Array representation.
[430] Fix | Delete
*/
[431] Fix | Delete
public function to_array() {
[432] Fix | Delete
return get_object_vars( $this->data );
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
/**
[436] Fix | Delete
* Makes private/protected methods readable for backward compatibility.
[437] Fix | Delete
*
[438] Fix | Delete
* @since 4.3.0
[439] Fix | Delete
*
[440] Fix | Delete
* @param string $name Method to call.
[441] Fix | Delete
* @param array $arguments Arguments to pass when calling.
[442] Fix | Delete
* @return mixed|false Return value of the callback, false otherwise.
[443] Fix | Delete
*/
[444] Fix | Delete
public function __call( $name, $arguments ) {
[445] Fix | Delete
if ( '_init_caps' === $name ) {
[446] Fix | Delete
return $this->_init_caps( ...$arguments );
[447] Fix | Delete
}
[448] Fix | Delete
return false;
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
/**
[452] Fix | Delete
* Set up capability object properties.
[453] Fix | Delete
*
[454] Fix | Delete
* Will set the value for the 'cap_key' property to current database table
[455] Fix | Delete
* prefix, followed by 'capabilities'. Will then check to see if the
[456] Fix | Delete
* property matching the 'cap_key' exists and is an array. If so, it will be
[457] Fix | Delete
* used.
[458] Fix | Delete
*
[459] Fix | Delete
* @since 2.1.0
[460] Fix | Delete
* @deprecated 4.9.0 Use WP_User::for_site()
[461] Fix | Delete
*
[462] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[463] Fix | Delete
*
[464] Fix | Delete
* @param string $cap_key Optional capability key
[465] Fix | Delete
*/
[466] Fix | Delete
protected function _init_caps( $cap_key = '' ) {
[467] Fix | Delete
global $wpdb;
[468] Fix | Delete
[469] Fix | Delete
_deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
[470] Fix | Delete
[471] Fix | Delete
if ( empty( $cap_key ) ) {
[472] Fix | Delete
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
[473] Fix | Delete
} else {
[474] Fix | Delete
$this->cap_key = $cap_key;
[475] Fix | Delete
}
[476] Fix | Delete
[477] Fix | Delete
$this->caps = $this->get_caps_data();
[478] Fix | Delete
[479] Fix | Delete
$this->get_role_caps();
[480] Fix | Delete
}
[481] Fix | Delete
[482] Fix | Delete
/**
[483] Fix | Delete
* Retrieves all of the capabilities of the user's roles, and merges them with
[484] Fix | Delete
* individual user capabilities.
[485] Fix | Delete
*
[486] Fix | Delete
* All of the capabilities of the user's roles are merged with the user's individual
[487] Fix | Delete
* capabilities. This means that the user can be denied specific capabilities that
[488] Fix | Delete
* their role might have, but the user is specifically denied.
[489] Fix | Delete
*
[490] Fix | Delete
* @since 2.0.0
[491] Fix | Delete
*
[492] Fix | Delete
* @return bool[] Array of key/value pairs where keys represent a capability name
[493] Fix | Delete
* and boolean values represent whether the user has that capability.
[494] Fix | Delete
*/
[495] Fix | Delete
public function get_role_caps() {
[496] Fix | Delete
$switch_site = false;
[497] Fix | Delete
if ( is_multisite() && get_current_blog_id() != $this->site_id ) {
[498] Fix | Delete
$switch_site = true;
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function