class-wp-user.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. <?php
  2. /**
  3. * User API: WP_User class
  4. *
  5. * @package WordPress
  6. * @subpackage Users
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement the WP_User object.
  11. *
  12. * @since 2.0.0
  13. *
  14. * @property string $nickname
  15. * @property string $description
  16. * @property string $user_description
  17. * @property string $first_name
  18. * @property string $user_firstname
  19. * @property string $last_name
  20. * @property string $user_lastname
  21. * @property string $user_login
  22. * @property string $user_pass
  23. * @property string $user_nicename
  24. * @property string $user_email
  25. * @property string $user_url
  26. * @property string $user_registered
  27. * @property string $user_activation_key
  28. * @property string $user_status
  29. * @property int $user_level
  30. * @property string $display_name
  31. * @property string $spam
  32. * @property string $deleted
  33. * @property string $locale
  34. * @property string $rich_editing
  35. * @property string $syntax_highlighting
  36. */
  37. class WP_User {
  38. /**
  39. * User data container.
  40. *
  41. * @since 2.0.0
  42. * @var object
  43. */
  44. public $data;
  45. /**
  46. * The user's ID.
  47. *
  48. * @since 2.1.0
  49. * @var int
  50. */
  51. public $ID = 0;
  52. /**
  53. * The individual capabilities the user has been given.
  54. *
  55. * @since 2.0.0
  56. * @var array
  57. */
  58. public $caps = array();
  59. /**
  60. * User metadata option name.
  61. *
  62. * @since 2.0.0
  63. * @var string
  64. */
  65. public $cap_key;
  66. /**
  67. * The roles the user is part of.
  68. *
  69. * @since 2.0.0
  70. * @var array
  71. */
  72. public $roles = array();
  73. /**
  74. * All capabilities the user has, including individual and role based.
  75. *
  76. * @since 2.0.0
  77. * @var array
  78. */
  79. public $allcaps = array();
  80. /**
  81. * The filter context applied to user data fields.
  82. *
  83. * @since 2.9.0
  84. * @var string
  85. */
  86. public $filter = null;
  87. /**
  88. * The site ID the capabilities of this user are initialized for.
  89. *
  90. * @since 4.9.0
  91. * @var int
  92. */
  93. private $site_id = 0;
  94. /**
  95. * @static
  96. * @since 3.3.0
  97. * @var array
  98. */
  99. private static $back_compat_keys;
  100. /**
  101. * Constructor.
  102. *
  103. * Retrieves the userdata and passes it to WP_User::init().
  104. *
  105. * @since 2.0.0
  106. *
  107. * @global wpdb $wpdb WordPress database abstraction object.
  108. *
  109. * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
  110. * @param string $name Optional. User's username
  111. * @param int $site_id Optional Site ID, defaults to current site.
  112. */
  113. public function __construct( $id = 0, $name = '', $site_id = '' ) {
  114. if ( ! isset( self::$back_compat_keys ) ) {
  115. $prefix = $GLOBALS['wpdb']->prefix;
  116. self::$back_compat_keys = array(
  117. 'user_firstname' => 'first_name',
  118. 'user_lastname' => 'last_name',
  119. 'user_description' => 'description',
  120. 'user_level' => $prefix . 'user_level',
  121. $prefix . 'usersettings' => $prefix . 'user-settings',
  122. $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
  123. );
  124. }
  125. if ( $id instanceof WP_User ) {
  126. $this->init( $id->data, $site_id );
  127. return;
  128. } elseif ( is_object( $id ) ) {
  129. $this->init( $id, $site_id );
  130. return;
  131. }
  132. if ( ! empty( $id ) && ! is_numeric( $id ) ) {
  133. $name = $id;
  134. $id = 0;
  135. }
  136. if ( $id ) {
  137. $data = self::get_data_by( 'id', $id );
  138. } else {
  139. $data = self::get_data_by( 'login', $name );
  140. }
  141. if ( $data ) {
  142. $this->init( $data, $site_id );
  143. } else {
  144. $this->data = new stdClass;
  145. }
  146. }
  147. /**
  148. * Sets up object properties, including capabilities.
  149. *
  150. * @since 3.3.0
  151. *
  152. * @param object $data User DB row object.
  153. * @param int $site_id Optional. The site ID to initialize for.
  154. */
  155. public function init( $data, $site_id = '' ) {
  156. $this->data = $data;
  157. $this->ID = (int) $data->ID;
  158. $this->for_site( $site_id );
  159. }
  160. /**
  161. * Return only the main user fields
  162. *
  163. * @since 3.3.0
  164. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  165. *
  166. * @static
  167. *
  168. * @global wpdb $wpdb WordPress database abstraction object.
  169. *
  170. * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
  171. * @param string|int $value The field value
  172. * @return object|false Raw user object
  173. */
  174. public static function get_data_by( $field, $value ) {
  175. global $wpdb;
  176. // 'ID' is an alias of 'id'.
  177. if ( 'ID' === $field ) {
  178. $field = 'id';
  179. }
  180. if ( 'id' == $field ) {
  181. // Make sure the value is numeric to avoid casting objects, for example,
  182. // to int 1.
  183. if ( ! is_numeric( $value ) )
  184. return false;
  185. $value = intval( $value );
  186. if ( $value < 1 )
  187. return false;
  188. } else {
  189. $value = trim( $value );
  190. }
  191. if ( !$value )
  192. return false;
  193. switch ( $field ) {
  194. case 'id':
  195. $user_id = $value;
  196. $db_field = 'ID';
  197. break;
  198. case 'slug':
  199. $user_id = wp_cache_get($value, 'userslugs');
  200. $db_field = 'user_nicename';
  201. break;
  202. case 'email':
  203. $user_id = wp_cache_get($value, 'useremail');
  204. $db_field = 'user_email';
  205. break;
  206. case 'login':
  207. $value = sanitize_user( $value );
  208. $user_id = wp_cache_get($value, 'userlogins');
  209. $db_field = 'user_login';
  210. break;
  211. default:
  212. return false;
  213. }
  214. if ( false !== $user_id ) {
  215. if ( $user = wp_cache_get( $user_id, 'users' ) )
  216. return $user;
  217. }
  218. if ( !$user = $wpdb->get_row( $wpdb->prepare(
  219. "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
  220. ) ) )
  221. return false;
  222. update_user_caches( $user );
  223. return $user;
  224. }
  225. /**
  226. * Magic method for checking the existence of a certain custom field.
  227. *
  228. * @since 3.3.0
  229. *
  230. * @param string $key User meta key to check if set.
  231. * @return bool Whether the given user meta key is set.
  232. */
  233. public function __isset( $key ) {
  234. if ( 'id' == $key ) {
  235. _deprecated_argument( 'WP_User->id', '2.1.0',
  236. sprintf(
  237. /* translators: %s: WP_User->ID */
  238. __( 'Use %s instead.' ),
  239. '<code>WP_User->ID</code>'
  240. )
  241. );
  242. $key = 'ID';
  243. }
  244. if ( isset( $this->data->$key ) )
  245. return true;
  246. if ( isset( self::$back_compat_keys[ $key ] ) )
  247. $key = self::$back_compat_keys[ $key ];
  248. return metadata_exists( 'user', $this->ID, $key );
  249. }
  250. /**
  251. * Magic method for accessing custom fields.
  252. *
  253. * @since 3.3.0
  254. *
  255. * @param string $key User meta key to retrieve.
  256. * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
  257. */
  258. public function __get( $key ) {
  259. if ( 'id' == $key ) {
  260. _deprecated_argument( 'WP_User->id', '2.1.0',
  261. sprintf(
  262. /* translators: %s: WP_User->ID */
  263. __( 'Use %s instead.' ),
  264. '<code>WP_User->ID</code>'
  265. )
  266. );
  267. return $this->ID;
  268. }
  269. if ( isset( $this->data->$key ) ) {
  270. $value = $this->data->$key;
  271. } else {
  272. if ( isset( self::$back_compat_keys[ $key ] ) )
  273. $key = self::$back_compat_keys[ $key ];
  274. $value = get_user_meta( $this->ID, $key, true );
  275. }
  276. if ( $this->filter ) {
  277. $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
  278. }
  279. return $value;
  280. }
  281. /**
  282. * Magic method for setting custom user fields.
  283. *
  284. * This method does not update custom fields in the database. It only stores
  285. * the value on the WP_User instance.
  286. *
  287. * @since 3.3.0
  288. *
  289. * @param string $key User meta key.
  290. * @param mixed $value User meta value.
  291. */
  292. public function __set( $key, $value ) {
  293. if ( 'id' == $key ) {
  294. _deprecated_argument( 'WP_User->id', '2.1.0',
  295. sprintf(
  296. /* translators: %s: WP_User->ID */
  297. __( 'Use %s instead.' ),
  298. '<code>WP_User->ID</code>'
  299. )
  300. );
  301. $this->ID = $value;
  302. return;
  303. }
  304. $this->data->$key = $value;
  305. }
  306. /**
  307. * Magic method for unsetting a certain custom field.
  308. *
  309. * @since 4.4.0
  310. *
  311. * @param string $key User meta key to unset.
  312. */
  313. public function __unset( $key ) {
  314. if ( 'id' == $key ) {
  315. _deprecated_argument( 'WP_User->id', '2.1.0',
  316. sprintf(
  317. /* translators: %s: WP_User->ID */
  318. __( 'Use %s instead.' ),
  319. '<code>WP_User->ID</code>'
  320. )
  321. );
  322. }
  323. if ( isset( $this->data->$key ) ) {
  324. unset( $this->data->$key );
  325. }
  326. if ( isset( self::$back_compat_keys[ $key ] ) ) {
  327. unset( self::$back_compat_keys[ $key ] );
  328. }
  329. }
  330. /**
  331. * Determine whether the user exists in the database.
  332. *
  333. * @since 3.4.0
  334. *
  335. * @return bool True if user exists in the database, false if not.
  336. */
  337. public function exists() {
  338. return ! empty( $this->ID );
  339. }
  340. /**
  341. * Retrieve the value of a property or meta key.
  342. *
  343. * Retrieves from the users and usermeta table.
  344. *
  345. * @since 3.3.0
  346. *
  347. * @param string $key Property
  348. * @return mixed
  349. */
  350. public function get( $key ) {
  351. return $this->__get( $key );
  352. }
  353. /**
  354. * Determine whether a property or meta key is set
  355. *
  356. * Consults the users and usermeta tables.
  357. *
  358. * @since 3.3.0
  359. *
  360. * @param string $key Property
  361. * @return bool
  362. */
  363. public function has_prop( $key ) {
  364. return $this->__isset( $key );
  365. }
  366. /**
  367. * Return an array representation.
  368. *
  369. * @since 3.5.0
  370. *
  371. * @return array Array representation.
  372. */
  373. public function to_array() {
  374. return get_object_vars( $this->data );
  375. }
  376. /**
  377. * Makes private/protected methods readable for backward compatibility.
  378. *
  379. * @since 4.3.0
  380. *
  381. * @param callable $name Method to call.
  382. * @param array $arguments Arguments to pass when calling.
  383. * @return mixed|false Return value of the callback, false otherwise.
  384. */
  385. public function __call( $name, $arguments ) {
  386. if ( '_init_caps' === $name ) {
  387. return call_user_func_array( array( $this, $name ), $arguments );
  388. }
  389. return false;
  390. }
  391. /**
  392. * Set up capability object properties.
  393. *
  394. * Will set the value for the 'cap_key' property to current database table
  395. * prefix, followed by 'capabilities'. Will then check to see if the
  396. * property matching the 'cap_key' exists and is an array. If so, it will be
  397. * used.
  398. *
  399. * @since 2.1.0
  400. * @deprecated 4.9.0 Use WP_User::for_site()
  401. *
  402. * @global wpdb $wpdb WordPress database abstraction object.
  403. *
  404. * @param string $cap_key Optional capability key
  405. */
  406. protected function _init_caps( $cap_key = '' ) {
  407. global $wpdb;
  408. _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
  409. if ( empty( $cap_key ) ) {
  410. $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
  411. } else {
  412. $this->cap_key = $cap_key;
  413. }
  414. $this->caps = $this->get_caps_data();
  415. $this->get_role_caps();
  416. }
  417. /**
  418. * Retrieve all of the role capabilities and merge with individual capabilities.
  419. *
  420. * All of the capabilities of the roles the user belongs to are merged with
  421. * the users individual roles. This also means that the user can be denied
  422. * specific roles that their role might have, but the specific user isn't
  423. * granted permission to.
  424. *
  425. * @since 2.0.0
  426. *
  427. * @return array List of all capabilities for the user.
  428. */
  429. public function get_role_caps() {
  430. $switch_site = false;
  431. if ( is_multisite() && $this->site_id != get_current_blog_id() ) {
  432. $switch_site = true;
  433. switch_to_blog( $this->site_id );
  434. }
  435. $wp_roles = wp_roles();
  436. //Filter out caps that are not role names and assign to $this->roles
  437. if ( is_array( $this->caps ) )
  438. $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
  439. //Build $allcaps from role caps, overlay user's $caps
  440. $this->allcaps = array();
  441. foreach ( (array) $this->roles as $role ) {
  442. $the_role = $wp_roles->get_role( $role );
  443. $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
  444. }
  445. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  446. if ( $switch_site ) {
  447. restore_current_blog();
  448. }
  449. return $this->allcaps;
  450. }
  451. /**
  452. * Add role to user.
  453. *
  454. * Updates the user's meta data option with capabilities and roles.
  455. *
  456. * @since 2.0.0
  457. *
  458. * @param string $role Role name.
  459. */
  460. public function add_role( $role ) {
  461. if ( empty( $role ) ) {
  462. return;
  463. }
  464. $this->caps[$role] = true;
  465. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  466. $this->get_role_caps();
  467. $this->update_user_level_from_caps();
  468. /**
  469. * Fires immediately after the user has been given a new role.
  470. *
  471. * @since 4.3.0
  472. *
  473. * @param int $user_id The user ID.
  474. * @param string $role The new role.
  475. */
  476. do_action( 'add_user_role', $this->ID, $role );
  477. }
  478. /**
  479. * Remove role from user.
  480. *
  481. * @since 2.0.0
  482. *
  483. * @param string $role Role name.
  484. */
  485. public function remove_role( $role ) {
  486. if ( !in_array($role, $this->roles) )
  487. return;
  488. unset( $this->caps[$role] );
  489. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  490. $this->get_role_caps();
  491. $this->update_user_level_from_caps();
  492. /**
  493. * Fires immediately after a role as been removed from a user.
  494. *
  495. * @since 4.3.0
  496. *
  497. * @param int $user_id The user ID.
  498. * @param string $role The removed role.
  499. */
  500. do_action( 'remove_user_role', $this->ID, $role );
  501. }
  502. /**
  503. * Set the role of the user.
  504. *
  505. * This will remove the previous roles of the user and assign the user the
  506. * new one. You can set the role to an empty string and it will remove all
  507. * of the roles from the user.
  508. *
  509. * @since 2.0.0
  510. *
  511. * @param string $role Role name.
  512. */
  513. public function set_role( $role ) {
  514. if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
  515. return;
  516. foreach ( (array) $this->roles as $oldrole )
  517. unset( $this->caps[$oldrole] );
  518. $old_roles = $this->roles;
  519. if ( !empty( $role ) ) {
  520. $this->caps[$role] = true;
  521. $this->roles = array( $role => true );
  522. } else {
  523. $this->roles = false;
  524. }
  525. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  526. $this->get_role_caps();
  527. $this->update_user_level_from_caps();
  528. /**
  529. * Fires after the user's role has changed.
  530. *
  531. * @since 2.9.0
  532. * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
  533. *
  534. * @param int $user_id The user ID.
  535. * @param string $role The new role.
  536. * @param array $old_roles An array of the user's previous roles.
  537. */
  538. do_action( 'set_user_role', $this->ID, $role, $old_roles );
  539. }
  540. /**
  541. * Choose the maximum level the user has.
  542. *
  543. * Will compare the level from the $item parameter against the $max
  544. * parameter. If the item is incorrect, then just the $max parameter value
  545. * will be returned.
  546. *
  547. * Used to get the max level based on the capabilities the user has. This
  548. * is also based on roles, so if the user is assigned the Administrator role
  549. * then the capability 'level_10' will exist and the user will get that
  550. * value.
  551. *
  552. * @since 2.0.0
  553. *
  554. * @param int $max Max level of user.
  555. * @param string $item Level capability name.
  556. * @return int Max Level.
  557. */
  558. public function level_reduction( $max, $item ) {
  559. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  560. $level = intval( $matches[1] );
  561. return max( $max, $level );
  562. } else {
  563. return $max;
  564. }
  565. }
  566. /**
  567. * Update the maximum user level for the user.
  568. *
  569. * Updates the 'user_level' user metadata (includes prefix that is the
  570. * database table prefix) with the maximum user level. Gets the value from
  571. * the all of the capabilities that the user has.
  572. *
  573. * @since 2.0.0
  574. *
  575. * @global wpdb $wpdb WordPress database abstraction object.
  576. */
  577. public function update_user_level_from_caps() {
  578. global $wpdb;
  579. $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
  580. update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
  581. }
  582. /**
  583. * Add capability and grant or deny access to capability.
  584. *
  585. * @since 2.0.0
  586. *
  587. * @param string $cap Capability name.
  588. * @param bool $grant Whether to grant capability to user.
  589. */
  590. public function add_cap( $cap, $grant = true ) {
  591. $this->caps[$cap] = $grant;
  592. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  593. $this->get_role_caps();
  594. $this->update_user_level_from_caps();
  595. }
  596. /**
  597. * Remove capability from user.
  598. *
  599. * @since 2.0.0
  600. *
  601. * @param string $cap Capability name.
  602. */
  603. public function remove_cap( $cap ) {
  604. if ( ! isset( $this->caps[ $cap ] ) ) {
  605. return;
  606. }
  607. unset( $this->caps[ $cap ] );
  608. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  609. $this->get_role_caps();
  610. $this->update_user_level_from_caps();
  611. }
  612. /**
  613. * Remove all of the capabilities of the user.
  614. *
  615. * @since 2.1.0
  616. *
  617. * @global wpdb $wpdb WordPress database abstraction object.
  618. */
  619. public function remove_all_caps() {
  620. global $wpdb;
  621. $this->caps = array();
  622. delete_user_meta( $this->ID, $this->cap_key );
  623. delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
  624. $this->get_role_caps();
  625. }
  626. /**
  627. * Whether the user has a specific capability.
  628. *
  629. * While checking against a role in place of a capability is supported in part, this practice is discouraged as it
  630. * may produce unreliable results.
  631. *
  632. * @since 2.0.0
  633. *
  634. * @see map_meta_cap()
  635. *
  636. * @param string $cap Capability name.
  637. * @param int $object_id,... Optional. ID of a specific object to check against if `$cap` is a "meta" capability.
  638. * Meta capabilities such as `edit_post` and `edit_user` are capabilities used by
  639. * by the `map_meta_cap()` function to map to primitive capabilities that a user or
  640. * role has, such as `edit_posts` and `edit_others_posts`.
  641. * @return bool Whether the user has the given capability, or, if `$object_id` is passed, whether the user has
  642. * the given capability for that object.
  643. */
  644. public function has_cap( $cap ) {
  645. if ( is_numeric( $cap ) ) {
  646. _deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) );
  647. $cap = $this->translate_level_to_cap( $cap );
  648. }
  649. $args = array_slice( func_get_args(), 1 );
  650. $args = array_merge( array( $cap, $this->ID ), $args );
  651. $caps = call_user_func_array( 'map_meta_cap', $args );
  652. // Multisite super admin has all caps by definition, Unless specifically denied.
  653. if ( is_multisite() && is_super_admin( $this->ID ) ) {
  654. if ( in_array('do_not_allow', $caps) )
  655. return false;
  656. return true;
  657. }
  658. /**
  659. * Dynamically filter a user's capabilities.
  660. *
  661. * @since 2.0.0
  662. * @since 3.7.0 Added the user object.
  663. *
  664. * @param array $allcaps An array of all the user's capabilities.
  665. * @param array $caps Actual capabilities for meta capability.
  666. * @param array $args Optional parameters passed to has_cap(), typically object ID.
  667. * @param WP_User $user The user object.
  668. */
  669. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
  670. // Everyone is allowed to exist.
  671. $capabilities['exist'] = true;
  672. // Nobody is allowed to do things they are not allowed to do.
  673. unset( $capabilities['do_not_allow'] );
  674. // Must have ALL requested caps.
  675. foreach ( (array) $caps as $cap ) {
  676. if ( empty( $capabilities[ $cap ] ) )
  677. return false;
  678. }
  679. return true;
  680. }
  681. /**
  682. * Convert numeric level to level capability name.
  683. *
  684. * Prepends 'level_' to level number.
  685. *
  686. * @since 2.0.0
  687. *
  688. * @param int $level Level number, 1 to 10.
  689. * @return string
  690. */
  691. public function translate_level_to_cap( $level ) {
  692. return 'level_' . $level;
  693. }
  694. /**
  695. * Set the site to operate on. Defaults to the current site.
  696. *
  697. * @since 3.0.0
  698. * @deprecated 4.9.0 Use WP_User::for_site()
  699. *
  700. * @global wpdb $wpdb WordPress database abstraction object.
  701. *
  702. * @param int $blog_id Optional. Site ID, defaults to current site.
  703. */
  704. public function for_blog( $blog_id = '' ) {
  705. _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
  706. $this->for_site( $blog_id );
  707. }
  708. /**
  709. * Sets the site to operate on. Defaults to the current site.
  710. *
  711. * @since 4.9.0
  712. *
  713. * @global wpdb $wpdb WordPress database abstraction object.
  714. *
  715. * @param int $site_id Site ID to initialize user capabilities for. Default is the current site.
  716. */
  717. public function for_site( $site_id = '' ) {
  718. global $wpdb;
  719. if ( ! empty( $site_id ) ) {
  720. $this->site_id = absint( $site_id );
  721. } else {
  722. $this->site_id = get_current_blog_id();
  723. }
  724. $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
  725. $this->caps = $this->get_caps_data();
  726. $this->get_role_caps();
  727. }
  728. /**
  729. * Gets the ID of the site for which the user's capabilities are currently initialized.
  730. *
  731. * @since 4.9.0
  732. *
  733. * @return int Site ID.
  734. */
  735. public function get_site_id() {
  736. return $this->site_id;
  737. }
  738. /**
  739. * Gets the available user capabilities data.
  740. *
  741. * @since 4.9.0
  742. *
  743. * @return array User capabilities array.
  744. */
  745. private function get_caps_data() {
  746. $caps = get_user_meta( $this->ID, $this->cap_key, true );
  747. if ( ! is_array( $caps ) ) {
  748. return array();
  749. }
  750. return $caps;
  751. }
  752. }