暂无描述

class-wp-user.php 22KB

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