class-wp-user.php 21 KB

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