You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

790 line
19 KiB

  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. */
  35. class WP_User {
  36. /**
  37. * User data container.
  38. *
  39. * @since 2.0.0
  40. * @var object
  41. */
  42. public $data;
  43. /**
  44. * The user's ID.
  45. *
  46. * @since 2.1.0
  47. * @access public
  48. * @var int
  49. */
  50. public $ID = 0;
  51. /**
  52. * The individual capabilities the user has been given.
  53. *
  54. * @since 2.0.0
  55. * @access public
  56. * @var array
  57. */
  58. public $caps = array();
  59. /**
  60. * User metadata option name.
  61. *
  62. * @since 2.0.0
  63. * @access public
  64. * @var string
  65. */
  66. public $cap_key;
  67. /**
  68. * The roles the user is part of.
  69. *
  70. * @since 2.0.0
  71. * @access public
  72. * @var array
  73. */
  74. public $roles = array();
  75. /**
  76. * All capabilities the user has, including individual and role based.
  77. *
  78. * @since 2.0.0
  79. * @access public
  80. * @var array
  81. */
  82. public $allcaps = array();
  83. /**
  84. * The filter context applied to user data fields.
  85. *
  86. * @since 2.9.0
  87. * @access private
  88. * @var string
  89. */
  90. var $filter = null;
  91. /**
  92. * @static
  93. * @access private
  94. * @var array
  95. */
  96. private static $back_compat_keys;
  97. /**
  98. * Constructor.
  99. *
  100. * Retrieves the userdata and passes it to WP_User::init().
  101. *
  102. * @since 2.0.0
  103. * @access public
  104. *
  105. * @global wpdb $wpdb WordPress database abstraction object.
  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 $blog_id Optional Site ID, defaults to current site.
  110. */
  111. public function __construct( $id = 0, $name = '', $blog_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, $blog_id );
  125. return;
  126. } elseif ( is_object( $id ) ) {
  127. $this->init( $id, $blog_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, $blog_id );
  141. } else {
  142. $this->data = new stdClass;
  143. }
  144. }
  145. /**
  146. * Sets up object properties, including capabilities.
  147. *
  148. * @param object $data User DB row object.
  149. * @param int $blog_id Optional. The site ID to initialize for.
  150. */
  151. public function init( $data, $blog_id = '' ) {
  152. $this->data = $data;
  153. $this->ID = (int) $data->ID;
  154. $this->for_blog( $blog_id );
  155. }
  156. /**
  157. * Return only the main user fields
  158. *
  159. * @since 3.3.0
  160. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  161. *
  162. * @static
  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. $value = intval( $value );
  182. if ( $value < 1 )
  183. return false;
  184. } else {
  185. $value = trim( $value );
  186. }
  187. if ( !$value )
  188. return false;
  189. switch ( $field ) {
  190. case 'id':
  191. $user_id = $value;
  192. $db_field = 'ID';
  193. break;
  194. case 'slug':
  195. $user_id = wp_cache_get($value, 'userslugs');
  196. $db_field = 'user_nicename';
  197. break;
  198. case 'email':
  199. $user_id = wp_cache_get($value, 'useremail');
  200. $db_field = 'user_email';
  201. break;
  202. case 'login':
  203. $value = sanitize_user( $value );
  204. $user_id = wp_cache_get($value, 'userlogins');
  205. $db_field = 'user_login';
  206. break;
  207. default:
  208. return false;
  209. }
  210. if ( false !== $user_id ) {
  211. if ( $user = wp_cache_get( $user_id, 'users' ) )
  212. return $user;
  213. }
  214. if ( !$user = $wpdb->get_row( $wpdb->prepare(
  215. "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
  216. ) ) )
  217. return false;
  218. update_user_caches( $user );
  219. return $user;
  220. }
  221. /**
  222. * Makes private/protected methods readable for backward compatibility.
  223. *
  224. * @since 4.3.0
  225. * @access public
  226. *
  227. * @param callable $name Method to call.
  228. * @param array $arguments Arguments to pass when calling.
  229. * @return mixed|false Return value of the callback, false otherwise.
  230. */
  231. public function __call( $name, $arguments ) {
  232. if ( '_init_caps' === $name ) {
  233. return call_user_func_array( array( $this, $name ), $arguments );
  234. }
  235. return false;
  236. }
  237. /**
  238. * Magic method for checking the existence of a certain custom field.
  239. *
  240. * @since 3.3.0
  241. * @access public
  242. *
  243. * @param string $key User meta key to check if set.
  244. * @return bool Whether the given user meta key is set.
  245. */
  246. public function __isset( $key ) {
  247. if ( 'id' == $key ) {
  248. _deprecated_argument( 'WP_User->id', '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. if ( isset( self::$back_compat_keys[ $key ] ) )
  260. $key = self::$back_compat_keys[ $key ];
  261. return metadata_exists( 'user', $this->ID, $key );
  262. }
  263. /**
  264. * Magic method for accessing custom fields.
  265. *
  266. * @since 3.3.0
  267. * @access public
  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( 'WP_User->id', '2.1.0',
  275. sprintf(
  276. /* translators: %s: WP_User->ID */
  277. __( 'Use %s instead.' ),
  278. '<code>WP_User->ID</code>'
  279. )
  280. );
  281. return $this->ID;
  282. }
  283. if ( isset( $this->data->$key ) ) {
  284. $value = $this->data->$key;
  285. } else {
  286. if ( isset( self::$back_compat_keys[ $key ] ) )
  287. $key = self::$back_compat_keys[ $key ];
  288. $value = get_user_meta( $this->ID, $key, true );
  289. }
  290. if ( $this->filter ) {
  291. $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
  292. }
  293. return $value;
  294. }
  295. /**
  296. * Magic method for setting custom user fields.
  297. *
  298. * This method does not update custom fields in the database. It only stores
  299. * the value on the WP_User instance.
  300. *
  301. * @since 3.3.0
  302. * @access public
  303. *
  304. * @param string $key User meta key.
  305. * @param mixed $value User meta value.
  306. */
  307. public function __set( $key, $value ) {
  308. if ( 'id' == $key ) {
  309. _deprecated_argument( 'WP_User->id', '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. * @access public
  326. *
  327. * @param string $key User meta key to unset.
  328. */
  329. public function __unset( $key ) {
  330. if ( 'id' == $key ) {
  331. _deprecated_argument( 'WP_User->id', '2.1.0',
  332. sprintf(
  333. /* translators: %s: WP_User->ID */
  334. __( 'Use %s instead.' ),
  335. '<code>WP_User->ID</code>'
  336. )
  337. );
  338. }
  339. if ( isset( $this->data->$key ) ) {
  340. unset( $this->data->$key );
  341. }
  342. if ( isset( self::$back_compat_keys[ $key ] ) ) {
  343. unset( self::$back_compat_keys[ $key ] );
  344. }
  345. }
  346. /**
  347. * Determine whether the user exists in the database.
  348. *
  349. * @since 3.4.0
  350. * @access public
  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. * Set up capability object properties.
  395. *
  396. * Will set the value for the 'cap_key' property to current database table
  397. * prefix, followed by 'capabilities'. Will then check to see if the
  398. * property matching the 'cap_key' exists and is an array. If so, it will be
  399. * used.
  400. *
  401. * @access protected
  402. * @since 2.1.0
  403. *
  404. * @global wpdb $wpdb WordPress database abstraction object.
  405. *
  406. * @param string $cap_key Optional capability key
  407. */
  408. protected function _init_caps( $cap_key = '' ) {
  409. global $wpdb;
  410. if ( empty($cap_key) )
  411. $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
  412. else
  413. $this->cap_key = $cap_key;
  414. $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
  415. if ( ! is_array( $this->caps ) )
  416. $this->caps = array();
  417. $this->get_role_caps();
  418. }
  419. /**
  420. * Retrieve all of the role capabilities and merge with individual capabilities.
  421. *
  422. * All of the capabilities of the roles the user belongs to are merged with
  423. * the users individual roles. This also means that the user can be denied
  424. * specific roles that their role might have, but the specific user isn't
  425. * granted permission to.
  426. *
  427. * @since 2.0.0
  428. * @access public
  429. *
  430. * @return array List of all capabilities for the user.
  431. */
  432. public function get_role_caps() {
  433. $wp_roles = wp_roles();
  434. //Filter out caps that are not role names and assign to $this->roles
  435. if ( is_array( $this->caps ) )
  436. $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
  437. //Build $allcaps from role caps, overlay user's $caps
  438. $this->allcaps = array();
  439. foreach ( (array) $this->roles as $role ) {
  440. $the_role = $wp_roles->get_role( $role );
  441. $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
  442. }
  443. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  444. return $this->allcaps;
  445. }
  446. /**
  447. * Add role to user.
  448. *
  449. * Updates the user's meta data option with capabilities and roles.
  450. *
  451. * @since 2.0.0
  452. * @access public
  453. *
  454. * @param string $role Role name.
  455. */
  456. public function add_role( $role ) {
  457. if ( empty( $role ) ) {
  458. return;
  459. }
  460. $this->caps[$role] = true;
  461. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  462. $this->get_role_caps();
  463. $this->update_user_level_from_caps();
  464. /**
  465. * Fires immediately after the user has been given a new role.
  466. *
  467. * @since 4.3.0
  468. *
  469. * @param int $user_id The user ID.
  470. * @param string $role The new role.
  471. */
  472. do_action( 'add_user_role', $this->ID, $role );
  473. }
  474. /**
  475. * Remove role from user.
  476. *
  477. * @since 2.0.0
  478. * @access public
  479. *
  480. * @param string $role Role name.
  481. */
  482. public function remove_role( $role ) {
  483. if ( !in_array($role, $this->roles) )
  484. return;
  485. unset( $this->caps[$role] );
  486. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  487. $this->get_role_caps();
  488. $this->update_user_level_from_caps();
  489. /**
  490. * Fires immediately after a role as been removed from a user.
  491. *
  492. * @since 4.3.0
  493. *
  494. * @param int $user_id The user ID.
  495. * @param string $role The removed role.
  496. */
  497. do_action( 'remove_user_role', $this->ID, $role );
  498. }
  499. /**
  500. * Set the role of the user.
  501. *
  502. * This will remove the previous roles of the user and assign the user the
  503. * new one. You can set the role to an empty string and it will remove all
  504. * of the roles from the user.
  505. *
  506. * @since 2.0.0
  507. * @access public
  508. *
  509. * @param string $role Role name.
  510. */
  511. public function set_role( $role ) {
  512. if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
  513. return;
  514. foreach ( (array) $this->roles as $oldrole )
  515. unset( $this->caps[$oldrole] );
  516. $old_roles = $this->roles;
  517. if ( !empty( $role ) ) {
  518. $this->caps[$role] = true;
  519. $this->roles = array( $role => true );
  520. } else {
  521. $this->roles = false;
  522. }
  523. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  524. $this->get_role_caps();
  525. $this->update_user_level_from_caps();
  526. /**
  527. * Fires after the user's role has changed.
  528. *
  529. * @since 2.9.0
  530. * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
  531. *
  532. * @param int $user_id The user ID.
  533. * @param string $role The new role.
  534. * @param array $old_roles An array of the user's previous roles.
  535. */
  536. do_action( 'set_user_role', $this->ID, $role, $old_roles );
  537. }
  538. /**
  539. * Choose the maximum level the user has.
  540. *
  541. * Will compare the level from the $item parameter against the $max
  542. * parameter. If the item is incorrect, then just the $max parameter value
  543. * will be returned.
  544. *
  545. * Used to get the max level based on the capabilities the user has. This
  546. * is also based on roles, so if the user is assigned the Administrator role
  547. * then the capability 'level_10' will exist and the user will get that
  548. * value.
  549. *
  550. * @since 2.0.0
  551. * @access public
  552. *
  553. * @param int $max Max level of user.
  554. * @param string $item Level capability name.
  555. * @return int Max Level.
  556. */
  557. public function level_reduction( $max, $item ) {
  558. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  559. $level = intval( $matches[1] );
  560. return max( $max, $level );
  561. } else {
  562. return $max;
  563. }
  564. }
  565. /**
  566. * Update the maximum user level for the user.
  567. *
  568. * Updates the 'user_level' user metadata (includes prefix that is the
  569. * database table prefix) with the maximum user level. Gets the value from
  570. * the all of the capabilities that the user has.
  571. *
  572. * @since 2.0.0
  573. * @access public
  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. * @access public
  587. *
  588. * @param string $cap Capability name.
  589. * @param bool $grant Whether to grant capability to user.
  590. */
  591. public function add_cap( $cap, $grant = true ) {
  592. $this->caps[$cap] = $grant;
  593. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  594. $this->get_role_caps();
  595. $this->update_user_level_from_caps();
  596. }
  597. /**
  598. * Remove capability from user.
  599. *
  600. * @since 2.0.0
  601. * @access public
  602. *
  603. * @param string $cap Capability name.
  604. */
  605. public function remove_cap( $cap ) {
  606. if ( ! isset( $this->caps[ $cap ] ) ) {
  607. return;
  608. }
  609. unset( $this->caps[ $cap ] );
  610. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  611. $this->get_role_caps();
  612. $this->update_user_level_from_caps();
  613. }
  614. /**
  615. * Remove all of the capabilities of the user.
  616. *
  617. * @since 2.1.0
  618. * @access public
  619. *
  620. * @global wpdb $wpdb WordPress database abstraction object.
  621. */
  622. public function remove_all_caps() {
  623. global $wpdb;
  624. $this->caps = array();
  625. delete_user_meta( $this->ID, $this->cap_key );
  626. delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
  627. $this->get_role_caps();
  628. }
  629. /**
  630. * Whether user has capability or role name.
  631. *
  632. * While checking against particular roles in place of a capability is supported
  633. * in part, this practice is discouraged as it may produce unreliable results.
  634. *
  635. * @since 2.0.0
  636. * @access public
  637. *
  638. * @see map_meta_cap()
  639. *
  640. * @param string $cap Capability name.
  641. * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
  642. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  643. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  644. * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed
  645. * to map_meta_cap().
  646. * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is
  647. * passed, whether the current user has the given meta capability for the given object.
  648. */
  649. public function has_cap( $cap ) {
  650. if ( is_numeric( $cap ) ) {
  651. _deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
  652. $cap = $this->translate_level_to_cap( $cap );
  653. }
  654. $args = array_slice( func_get_args(), 1 );
  655. $args = array_merge( array( $cap, $this->ID ), $args );
  656. $caps = call_user_func_array( 'map_meta_cap', $args );
  657. // Multisite super admin has all caps by definition, Unless specifically denied.
  658. if ( is_multisite() && is_super_admin( $this->ID ) ) {
  659. if ( in_array('do_not_allow', $caps) )
  660. return false;
  661. return true;
  662. }
  663. /**
  664. * Dynamically filter a user's capabilities.
  665. *
  666. * @since 2.0.0
  667. * @since 3.7.0 Added the user object.
  668. *
  669. * @param array $allcaps An array of all the user's capabilities.
  670. * @param array $caps Actual capabilities for meta capability.
  671. * @param array $args Optional parameters passed to has_cap(), typically object ID.
  672. * @param WP_User $user The user object.
  673. */
  674. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
  675. // Everyone is allowed to exist.
  676. $capabilities['exist'] = true;
  677. // Must have ALL requested caps.
  678. foreach ( (array) $caps as $cap ) {
  679. if ( empty( $capabilities[ $cap ] ) )
  680. return false;
  681. }
  682. return true;
  683. }
  684. /**
  685. * Convert numeric level to level capability name.
  686. *
  687. * Prepends 'level_' to level number.
  688. *
  689. * @since 2.0.0
  690. * @access public
  691. *
  692. * @param int $level Level number, 1 to 10.
  693. * @return string
  694. */
  695. public function translate_level_to_cap( $level ) {
  696. return 'level_' . $level;
  697. }
  698. /**
  699. * Set the site to operate on. Defaults to the current site.
  700. *
  701. * @since 3.0.0
  702. *
  703. * @global wpdb $wpdb WordPress database abstraction object.
  704. *
  705. * @param int $blog_id Optional. Site ID, defaults to current site.
  706. */
  707. public function for_blog( $blog_id = '' ) {
  708. global $wpdb;
  709. if ( ! empty( $blog_id ) )
  710. $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
  711. else
  712. $cap_key = '';
  713. $this->_init_caps( $cap_key );
  714. }
  715. }