Nav apraksta

class-wp-user.php 22KB

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