PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/capabilities.php

https://bitbucket.org/abnopanda/wordpress
PHP | 1474 lines | 679 code | 167 blank | 628 comment | 170 complexity | d956f39935931ac6fa5cf151a4431b26 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Roles and Capabilities.
  4. *
  5. * @package WordPress
  6. * @subpackage User
  7. */
  8. /**
  9. * WordPress User Roles.
  10. *
  11. * The role option is simple, the structure is organized by role name that store
  12. * the name in value of the 'name' key. The capabilities are stored as an array
  13. * in the value of the 'capability' key.
  14. *
  15. * <code>
  16. * array (
  17. * 'rolename' => array (
  18. * 'name' => 'rolename',
  19. * 'capabilities' => array()
  20. * )
  21. * )
  22. * </code>
  23. *
  24. * @since 2.0.0
  25. * @package WordPress
  26. * @subpackage User
  27. */
  28. class WP_Roles {
  29. /**
  30. * List of roles and capabilities.
  31. *
  32. * @since 2.0.0
  33. * @access public
  34. * @var array
  35. */
  36. var $roles;
  37. /**
  38. * List of the role objects.
  39. *
  40. * @since 2.0.0
  41. * @access public
  42. * @var array
  43. */
  44. var $role_objects = array();
  45. /**
  46. * List of role names.
  47. *
  48. * @since 2.0.0
  49. * @access public
  50. * @var array
  51. */
  52. var $role_names = array();
  53. /**
  54. * Option name for storing role list.
  55. *
  56. * @since 2.0.0
  57. * @access public
  58. * @var string
  59. */
  60. var $role_key;
  61. /**
  62. * Whether to use the database for retrieval and storage.
  63. *
  64. * @since 2.1.0
  65. * @access public
  66. * @var bool
  67. */
  68. var $use_db = true;
  69. /**
  70. * Constructor
  71. *
  72. * @since 2.0.0
  73. */
  74. function __construct() {
  75. $this->_init();
  76. }
  77. /**
  78. * Set up the object properties.
  79. *
  80. * The role key is set to the current prefix for the $wpdb object with
  81. * 'user_roles' appended. If the $wp_user_roles global is set, then it will
  82. * be used and the role option will not be updated or used.
  83. *
  84. * @since 2.1.0
  85. * @access protected
  86. * @uses $wpdb Used to get the database prefix.
  87. * @global array $wp_user_roles Used to set the 'roles' property value.
  88. */
  89. function _init () {
  90. global $wpdb, $wp_user_roles;
  91. $this->role_key = $wpdb->prefix . 'user_roles';
  92. if ( ! empty( $wp_user_roles ) ) {
  93. $this->roles = $wp_user_roles;
  94. $this->use_db = false;
  95. } else {
  96. $this->roles = get_option( $this->role_key );
  97. }
  98. if ( empty( $this->roles ) )
  99. return;
  100. $this->role_objects = array();
  101. $this->role_names = array();
  102. foreach ( array_keys( $this->roles ) as $role ) {
  103. $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
  104. $this->role_names[$role] = $this->roles[$role]['name'];
  105. }
  106. }
  107. /**
  108. * Reinitialize the object
  109. *
  110. * Recreates the role objects. This is typically called only by switch_to_blog()
  111. * after switching wpdb to a new blog ID.
  112. *
  113. * @since 3.5.0
  114. * @access public
  115. */
  116. function reinit() {
  117. // There is no need to reinit if using the wp_user_roles global.
  118. if ( ! $this->use_db )
  119. return;
  120. global $wpdb, $wp_user_roles;
  121. // Duplicated from _init() to avoid an extra function call.
  122. $this->role_key = $wpdb->prefix . 'user_roles';
  123. $this->roles = get_option( $this->role_key );
  124. if ( empty( $this->roles ) )
  125. return;
  126. $this->role_objects = array();
  127. $this->role_names = array();
  128. foreach ( array_keys( $this->roles ) as $role ) {
  129. $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
  130. $this->role_names[$role] = $this->roles[$role]['name'];
  131. }
  132. }
  133. /**
  134. * Add role name with capabilities to list.
  135. *
  136. * Updates the list of roles, if the role doesn't already exist.
  137. *
  138. * The capabilities are defined in the following format `array( 'read' => true );`
  139. * To explicitly deny a role a capability you set the value for that capability to false.
  140. *
  141. * @since 2.0.0
  142. * @access public
  143. *
  144. * @param string $role Role name.
  145. * @param string $display_name Role display name.
  146. * @param array $capabilities List of role capabilities in the above format.
  147. * @return null|WP_Role WP_Role object if role is added, null if already exists.
  148. */
  149. function add_role( $role, $display_name, $capabilities = array() ) {
  150. if ( isset( $this->roles[$role] ) )
  151. return;
  152. $this->roles[$role] = array(
  153. 'name' => $display_name,
  154. 'capabilities' => $capabilities
  155. );
  156. if ( $this->use_db )
  157. update_option( $this->role_key, $this->roles );
  158. $this->role_objects[$role] = new WP_Role( $role, $capabilities );
  159. $this->role_names[$role] = $display_name;
  160. return $this->role_objects[$role];
  161. }
  162. /**
  163. * Remove role by name.
  164. *
  165. * @since 2.0.0
  166. * @access public
  167. *
  168. * @param string $role Role name.
  169. */
  170. function remove_role( $role ) {
  171. if ( ! isset( $this->role_objects[$role] ) )
  172. return;
  173. unset( $this->role_objects[$role] );
  174. unset( $this->role_names[$role] );
  175. unset( $this->roles[$role] );
  176. if ( $this->use_db )
  177. update_option( $this->role_key, $this->roles );
  178. }
  179. /**
  180. * Add capability to role.
  181. *
  182. * @since 2.0.0
  183. * @access public
  184. *
  185. * @param string $role Role name.
  186. * @param string $cap Capability name.
  187. * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
  188. */
  189. function add_cap( $role, $cap, $grant = true ) {
  190. if ( ! isset( $this->roles[$role] ) )
  191. return;
  192. $this->roles[$role]['capabilities'][$cap] = $grant;
  193. if ( $this->use_db )
  194. update_option( $this->role_key, $this->roles );
  195. }
  196. /**
  197. * Remove capability from role.
  198. *
  199. * @since 2.0.0
  200. * @access public
  201. *
  202. * @param string $role Role name.
  203. * @param string $cap Capability name.
  204. */
  205. function remove_cap( $role, $cap ) {
  206. if ( ! isset( $this->roles[$role] ) )
  207. return;
  208. unset( $this->roles[$role]['capabilities'][$cap] );
  209. if ( $this->use_db )
  210. update_option( $this->role_key, $this->roles );
  211. }
  212. /**
  213. * Retrieve role object by name.
  214. *
  215. * @since 2.0.0
  216. * @access public
  217. *
  218. * @param string $role Role name.
  219. * @return object|null Null, if role does not exist. WP_Role object, if found.
  220. */
  221. function get_role( $role ) {
  222. if ( isset( $this->role_objects[$role] ) )
  223. return $this->role_objects[$role];
  224. else
  225. return null;
  226. }
  227. /**
  228. * Retrieve list of role names.
  229. *
  230. * @since 2.0.0
  231. * @access public
  232. *
  233. * @return array List of role names.
  234. */
  235. function get_names() {
  236. return $this->role_names;
  237. }
  238. /**
  239. * Whether role name is currently in the list of available roles.
  240. *
  241. * @since 2.0.0
  242. * @access public
  243. *
  244. * @param string $role Role name to look up.
  245. * @return bool
  246. */
  247. function is_role( $role ) {
  248. return isset( $this->role_names[$role] );
  249. }
  250. }
  251. /**
  252. * WordPress Role class.
  253. *
  254. * @since 2.0.0
  255. * @package WordPress
  256. * @subpackage User
  257. */
  258. class WP_Role {
  259. /**
  260. * Role name.
  261. *
  262. * @since 2.0.0
  263. * @access public
  264. * @var string
  265. */
  266. var $name;
  267. /**
  268. * List of capabilities the role contains.
  269. *
  270. * @since 2.0.0
  271. * @access public
  272. * @var array
  273. */
  274. var $capabilities;
  275. /**
  276. * Constructor - Set up object properties.
  277. *
  278. * The list of capabilities, must have the key as the name of the capability
  279. * and the value a boolean of whether it is granted to the role.
  280. *
  281. * @since 2.0.0
  282. * @access public
  283. *
  284. * @param string $role Role name.
  285. * @param array $capabilities List of capabilities.
  286. */
  287. function __construct( $role, $capabilities ) {
  288. $this->name = $role;
  289. $this->capabilities = $capabilities;
  290. }
  291. /**
  292. * Assign role a capability.
  293. *
  294. * @see WP_Roles::add_cap() Method uses implementation for role.
  295. * @since 2.0.0
  296. * @access public
  297. *
  298. * @param string $cap Capability name.
  299. * @param bool $grant Whether role has capability privilege.
  300. */
  301. function add_cap( $cap, $grant = true ) {
  302. global $wp_roles;
  303. if ( ! isset( $wp_roles ) )
  304. $wp_roles = new WP_Roles();
  305. $this->capabilities[$cap] = $grant;
  306. $wp_roles->add_cap( $this->name, $cap, $grant );
  307. }
  308. /**
  309. * Remove capability from role.
  310. *
  311. * This is a container for {@link WP_Roles::remove_cap()} to remove the
  312. * capability from the role. That is to say, that {@link
  313. * WP_Roles::remove_cap()} implements the functionality, but it also makes
  314. * sense to use this class, because you don't need to enter the role name.
  315. *
  316. * @since 2.0.0
  317. * @access public
  318. *
  319. * @param string $cap Capability name.
  320. */
  321. function remove_cap( $cap ) {
  322. global $wp_roles;
  323. if ( ! isset( $wp_roles ) )
  324. $wp_roles = new WP_Roles();
  325. unset( $this->capabilities[$cap] );
  326. $wp_roles->remove_cap( $this->name, $cap );
  327. }
  328. /**
  329. * Whether role has capability.
  330. *
  331. * The capabilities is passed through the 'role_has_cap' filter. The first
  332. * parameter for the hook is the list of capabilities the class has
  333. * assigned. The second parameter is the capability name to look for. The
  334. * third and final parameter for the hook is the role name.
  335. *
  336. * @since 2.0.0
  337. * @access public
  338. *
  339. * @param string $cap Capability name.
  340. * @return bool True, if user has capability. False, if doesn't have capability.
  341. */
  342. function has_cap( $cap ) {
  343. $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
  344. if ( !empty( $capabilities[$cap] ) )
  345. return $capabilities[$cap];
  346. else
  347. return false;
  348. }
  349. }
  350. /**
  351. * WordPress User class.
  352. *
  353. * @since 2.0.0
  354. * @package WordPress
  355. * @subpackage User
  356. */
  357. class WP_User {
  358. /**
  359. * User data container.
  360. *
  361. * @since 2.0.0
  362. * @access private
  363. * @var array
  364. */
  365. var $data;
  366. /**
  367. * The user's ID.
  368. *
  369. * @since 2.1.0
  370. * @access public
  371. * @var int
  372. */
  373. var $ID = 0;
  374. /**
  375. * The individual capabilities the user has been given.
  376. *
  377. * @since 2.0.0
  378. * @access public
  379. * @var array
  380. */
  381. var $caps = array();
  382. /**
  383. * User metadata option name.
  384. *
  385. * @since 2.0.0
  386. * @access public
  387. * @var string
  388. */
  389. var $cap_key;
  390. /**
  391. * The roles the user is part of.
  392. *
  393. * @since 2.0.0
  394. * @access public
  395. * @var array
  396. */
  397. var $roles = array();
  398. /**
  399. * All capabilities the user has, including individual and role based.
  400. *
  401. * @since 2.0.0
  402. * @access public
  403. * @var array
  404. */
  405. var $allcaps = array();
  406. /**
  407. * The filter context applied to user data fields.
  408. *
  409. * @since 2.9.0
  410. * @access private
  411. * @var string
  412. */
  413. var $filter = null;
  414. private static $back_compat_keys;
  415. /**
  416. * Constructor
  417. *
  418. * Retrieves the userdata and passes it to {@link WP_User::init()}.
  419. *
  420. * @since 2.0.0
  421. * @access public
  422. *
  423. * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
  424. * @param string $name Optional. User's username
  425. * @param int $blog_id Optional Blog ID, defaults to current blog.
  426. * @return WP_User
  427. */
  428. function __construct( $id = 0, $name = '', $blog_id = '' ) {
  429. if ( ! isset( self::$back_compat_keys ) ) {
  430. $prefix = $GLOBALS['wpdb']->prefix;
  431. self::$back_compat_keys = array(
  432. 'user_firstname' => 'first_name',
  433. 'user_lastname' => 'last_name',
  434. 'user_description' => 'description',
  435. 'user_level' => $prefix . 'user_level',
  436. $prefix . 'usersettings' => $prefix . 'user-settings',
  437. $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
  438. );
  439. }
  440. if ( is_a( $id, 'WP_User' ) ) {
  441. $this->init( $id->data, $blog_id );
  442. return;
  443. } elseif ( is_object( $id ) ) {
  444. $this->init( $id, $blog_id );
  445. return;
  446. }
  447. if ( ! empty( $id ) && ! is_numeric( $id ) ) {
  448. $name = $id;
  449. $id = 0;
  450. }
  451. if ( $id )
  452. $data = self::get_data_by( 'id', $id );
  453. else
  454. $data = self::get_data_by( 'login', $name );
  455. if ( $data )
  456. $this->init( $data, $blog_id );
  457. }
  458. /**
  459. * Sets up object properties, including capabilities.
  460. *
  461. * @param object $data User DB row object
  462. * @param int $blog_id Optional. The blog id to initialize for
  463. */
  464. function init( $data, $blog_id = '' ) {
  465. $this->data = $data;
  466. $this->ID = (int) $data->ID;
  467. $this->for_blog( $blog_id );
  468. }
  469. /**
  470. * Return only the main user fields
  471. *
  472. * @since 3.3.0
  473. *
  474. * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
  475. * @param string|int $value The field value
  476. * @return object Raw user object
  477. */
  478. static function get_data_by( $field, $value ) {
  479. global $wpdb;
  480. if ( 'id' == $field ) {
  481. // Make sure the value is numeric to avoid casting objects, for example,
  482. // to int 1.
  483. if ( ! is_numeric( $value ) )
  484. return false;
  485. $value = absint( $value );
  486. } else {
  487. $value = trim( $value );
  488. }
  489. if ( !$value )
  490. return false;
  491. switch ( $field ) {
  492. case 'id':
  493. $user_id = $value;
  494. $db_field = 'ID';
  495. break;
  496. case 'slug':
  497. $user_id = wp_cache_get($value, 'userslugs');
  498. $db_field = 'user_nicename';
  499. break;
  500. case 'email':
  501. $user_id = wp_cache_get($value, 'useremail');
  502. $db_field = 'user_email';
  503. break;
  504. case 'login':
  505. $value = sanitize_user( $value );
  506. $user_id = wp_cache_get($value, 'userlogins');
  507. $db_field = 'user_login';
  508. break;
  509. default:
  510. return false;
  511. }
  512. if ( false !== $user_id ) {
  513. if ( $user = wp_cache_get( $user_id, 'users' ) )
  514. return $user;
  515. }
  516. if ( !$user = $wpdb->get_row( $wpdb->prepare(
  517. "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
  518. ) ) )
  519. return false;
  520. update_user_caches( $user );
  521. return $user;
  522. }
  523. /**
  524. * Magic method for checking the existence of a certain custom field
  525. *
  526. * @since 3.3.0
  527. */
  528. function __isset( $key ) {
  529. if ( 'id' == $key ) {
  530. _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
  531. $key = 'ID';
  532. }
  533. if ( isset( $this->data->$key ) )
  534. return true;
  535. if ( isset( self::$back_compat_keys[ $key ] ) )
  536. $key = self::$back_compat_keys[ $key ];
  537. return metadata_exists( 'user', $this->ID, $key );
  538. }
  539. /**
  540. * Magic method for accessing custom fields
  541. *
  542. * @since 3.3.0
  543. */
  544. function __get( $key ) {
  545. if ( 'id' == $key ) {
  546. _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
  547. return $this->ID;
  548. }
  549. if ( isset( $this->data->$key ) ) {
  550. $value = $this->data->$key;
  551. } else {
  552. if ( isset( self::$back_compat_keys[ $key ] ) )
  553. $key = self::$back_compat_keys[ $key ];
  554. $value = get_user_meta( $this->ID, $key, true );
  555. }
  556. if ( $this->filter ) {
  557. $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
  558. }
  559. return $value;
  560. }
  561. /**
  562. * Magic method for setting custom fields
  563. *
  564. * @since 3.3.0
  565. */
  566. function __set( $key, $value ) {
  567. if ( 'id' == $key ) {
  568. _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
  569. $this->ID = $value;
  570. return;
  571. }
  572. $this->data->$key = $value;
  573. }
  574. /**
  575. * Determine whether the user exists in the database.
  576. *
  577. * @since 3.4.0
  578. * @access public
  579. *
  580. * @return bool True if user exists in the database, false if not.
  581. */
  582. function exists() {
  583. return ! empty( $this->ID );
  584. }
  585. /**
  586. * Retrieve the value of a property or meta key.
  587. *
  588. * Retrieves from the users and usermeta table.
  589. *
  590. * @since 3.3.0
  591. *
  592. * @param string $key Property
  593. */
  594. function get( $key ) {
  595. return $this->__get( $key );
  596. }
  597. /**
  598. * Determine whether a property or meta key is set
  599. *
  600. * Consults the users and usermeta tables.
  601. *
  602. * @since 3.3.0
  603. *
  604. * @param string $key Property
  605. */
  606. function has_prop( $key ) {
  607. return $this->__isset( $key );
  608. }
  609. /*
  610. * Return an array representation.
  611. *
  612. * @since 3.5.0
  613. *
  614. * @return array Array representation.
  615. */
  616. function to_array() {
  617. return get_object_vars( $this->data );
  618. }
  619. /**
  620. * Set up capability object properties.
  621. *
  622. * Will set the value for the 'cap_key' property to current database table
  623. * prefix, followed by 'capabilities'. Will then check to see if the
  624. * property matching the 'cap_key' exists and is an array. If so, it will be
  625. * used.
  626. *
  627. * @access protected
  628. * @since 2.1.0
  629. *
  630. * @param string $cap_key Optional capability key
  631. */
  632. function _init_caps( $cap_key = '' ) {
  633. global $wpdb;
  634. if ( empty($cap_key) )
  635. $this->cap_key = $wpdb->prefix . 'capabilities';
  636. else
  637. $this->cap_key = $cap_key;
  638. $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
  639. if ( ! is_array( $this->caps ) )
  640. $this->caps = array();
  641. $this->get_role_caps();
  642. }
  643. /**
  644. * Retrieve all of the role capabilities and merge with individual capabilities.
  645. *
  646. * All of the capabilities of the roles the user belongs to are merged with
  647. * the users individual roles. This also means that the user can be denied
  648. * specific roles that their role might have, but the specific user isn't
  649. * granted permission to.
  650. *
  651. * @since 2.0.0
  652. * @uses $wp_roles
  653. * @access public
  654. */
  655. function get_role_caps() {
  656. global $wp_roles;
  657. if ( ! isset( $wp_roles ) )
  658. $wp_roles = new WP_Roles();
  659. //Filter out caps that are not role names and assign to $this->roles
  660. if ( is_array( $this->caps ) )
  661. $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
  662. //Build $allcaps from role caps, overlay user's $caps
  663. $this->allcaps = array();
  664. foreach ( (array) $this->roles as $role ) {
  665. $the_role = $wp_roles->get_role( $role );
  666. $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
  667. }
  668. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  669. }
  670. /**
  671. * Add role to user.
  672. *
  673. * Updates the user's meta data option with capabilities and roles.
  674. *
  675. * @since 2.0.0
  676. * @access public
  677. *
  678. * @param string $role Role name.
  679. */
  680. function add_role( $role ) {
  681. $this->caps[$role] = true;
  682. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  683. $this->get_role_caps();
  684. $this->update_user_level_from_caps();
  685. }
  686. /**
  687. * Remove role from user.
  688. *
  689. * @since 2.0.0
  690. * @access public
  691. *
  692. * @param string $role Role name.
  693. */
  694. function remove_role( $role ) {
  695. if ( !in_array($role, $this->roles) )
  696. return;
  697. unset( $this->caps[$role] );
  698. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  699. $this->get_role_caps();
  700. $this->update_user_level_from_caps();
  701. }
  702. /**
  703. * Set the role of the user.
  704. *
  705. * This will remove the previous roles of the user and assign the user the
  706. * new one. You can set the role to an empty string and it will remove all
  707. * of the roles from the user.
  708. *
  709. * @since 2.0.0
  710. * @access public
  711. *
  712. * @param string $role Role name.
  713. */
  714. function set_role( $role ) {
  715. if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
  716. return;
  717. foreach ( (array) $this->roles as $oldrole )
  718. unset( $this->caps[$oldrole] );
  719. if ( !empty( $role ) ) {
  720. $this->caps[$role] = true;
  721. $this->roles = array( $role => true );
  722. } else {
  723. $this->roles = false;
  724. }
  725. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  726. $this->get_role_caps();
  727. $this->update_user_level_from_caps();
  728. do_action( 'set_user_role', $this->ID, $role );
  729. }
  730. /**
  731. * Choose the maximum level the user has.
  732. *
  733. * Will compare the level from the $item parameter against the $max
  734. * parameter. If the item is incorrect, then just the $max parameter value
  735. * will be returned.
  736. *
  737. * Used to get the max level based on the capabilities the user has. This
  738. * is also based on roles, so if the user is assigned the Administrator role
  739. * then the capability 'level_10' will exist and the user will get that
  740. * value.
  741. *
  742. * @since 2.0.0
  743. * @access public
  744. *
  745. * @param int $max Max level of user.
  746. * @param string $item Level capability name.
  747. * @return int Max Level.
  748. */
  749. function level_reduction( $max, $item ) {
  750. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  751. $level = intval( $matches[1] );
  752. return max( $max, $level );
  753. } else {
  754. return $max;
  755. }
  756. }
  757. /**
  758. * Update the maximum user level for the user.
  759. *
  760. * Updates the 'user_level' user metadata (includes prefix that is the
  761. * database table prefix) with the maximum user level. Gets the value from
  762. * the all of the capabilities that the user has.
  763. *
  764. * @since 2.0.0
  765. * @access public
  766. */
  767. function update_user_level_from_caps() {
  768. global $wpdb;
  769. $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
  770. update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
  771. }
  772. /**
  773. * Add capability and grant or deny access to capability.
  774. *
  775. * @since 2.0.0
  776. * @access public
  777. *
  778. * @param string $cap Capability name.
  779. * @param bool $grant Whether to grant capability to user.
  780. */
  781. function add_cap( $cap, $grant = true ) {
  782. $this->caps[$cap] = $grant;
  783. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  784. }
  785. /**
  786. * Remove capability from user.
  787. *
  788. * @since 2.0.0
  789. * @access public
  790. *
  791. * @param string $cap Capability name.
  792. */
  793. function remove_cap( $cap ) {
  794. if ( ! isset( $this->caps[$cap] ) )
  795. return;
  796. unset( $this->caps[$cap] );
  797. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  798. }
  799. /**
  800. * Remove all of the capabilities of the user.
  801. *
  802. * @since 2.1.0
  803. * @access public
  804. */
  805. function remove_all_caps() {
  806. global $wpdb;
  807. $this->caps = array();
  808. delete_user_meta( $this->ID, $this->cap_key );
  809. delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
  810. $this->get_role_caps();
  811. }
  812. /**
  813. * Whether user has capability or role name.
  814. *
  815. * This is useful for looking up whether the user has a specific role
  816. * assigned to the user. The second optional parameter can also be used to
  817. * check for capabilities against a specific object, such as a post or user.
  818. *
  819. * @since 2.0.0
  820. * @access public
  821. *
  822. * @param string|int $cap Capability or role name to search.
  823. * @return bool True, if user has capability; false, if user does not have capability.
  824. */
  825. function has_cap( $cap ) {
  826. if ( is_numeric( $cap ) ) {
  827. _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
  828. $cap = $this->translate_level_to_cap( $cap );
  829. }
  830. $args = array_slice( func_get_args(), 1 );
  831. $args = array_merge( array( $cap, $this->ID ), $args );
  832. $caps = call_user_func_array( 'map_meta_cap', $args );
  833. // Multisite super admin has all caps by definition, Unless specifically denied.
  834. if ( is_multisite() && is_super_admin( $this->ID ) ) {
  835. if ( in_array('do_not_allow', $caps) )
  836. return false;
  837. return true;
  838. }
  839. // Must have ALL requested caps
  840. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
  841. $capabilities['exist'] = true; // Everyone is allowed to exist
  842. foreach ( (array) $caps as $cap ) {
  843. if ( empty( $capabilities[ $cap ] ) )
  844. return false;
  845. }
  846. return true;
  847. }
  848. /**
  849. * Convert numeric level to level capability name.
  850. *
  851. * Prepends 'level_' to level number.
  852. *
  853. * @since 2.0.0
  854. * @access public
  855. *
  856. * @param int $level Level number, 1 to 10.
  857. * @return string
  858. */
  859. function translate_level_to_cap( $level ) {
  860. return 'level_' . $level;
  861. }
  862. /**
  863. * Set the blog to operate on. Defaults to the current blog.
  864. *
  865. * @since 3.0.0
  866. *
  867. * @param int $blog_id Optional Blog ID, defaults to current blog.
  868. */
  869. function for_blog( $blog_id = '' ) {
  870. global $wpdb;
  871. if ( ! empty( $blog_id ) )
  872. $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
  873. else
  874. $cap_key = '';
  875. $this->_init_caps( $cap_key );
  876. }
  877. }
  878. /**
  879. * Map meta capabilities to primitive capabilities.
  880. *
  881. * This does not actually compare whether the user ID has the actual capability,
  882. * just what the capability or capabilities are. Meta capability list value can
  883. * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
  884. * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
  885. *
  886. * @since 2.0.0
  887. *
  888. * @param string $cap Capability name.
  889. * @param int $user_id User ID.
  890. * @return array Actual capabilities for meta capability.
  891. */
  892. function map_meta_cap( $cap, $user_id ) {
  893. $args = array_slice( func_get_args(), 2 );
  894. $caps = array();
  895. switch ( $cap ) {
  896. case 'remove_user':
  897. $caps[] = 'remove_users';
  898. break;
  899. case 'promote_user':
  900. $caps[] = 'promote_users';
  901. break;
  902. case 'edit_user':
  903. case 'edit_users':
  904. // Allow user to edit itself
  905. if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
  906. break;
  907. // If multisite these caps are allowed only for super admins.
  908. if ( is_multisite() && !is_super_admin( $user_id ) )
  909. $caps[] = 'do_not_allow';
  910. else
  911. $caps[] = 'edit_users'; // edit_user maps to edit_users.
  912. break;
  913. case 'delete_post':
  914. case 'delete_page':
  915. $post = get_post( $args[0] );
  916. if ( 'revision' == $post->post_type ) {
  917. $post = get_post( $post->post_parent );
  918. }
  919. $post_type = get_post_type_object( $post->post_type );
  920. if ( ! $post_type->map_meta_cap ) {
  921. $caps[] = $post_type->cap->$cap;
  922. // Prior to 3.1 we would re-call map_meta_cap here.
  923. if ( 'delete_post' == $cap )
  924. $cap = $post_type->cap->$cap;
  925. break;
  926. }
  927. $post_author_id = $post->post_author;
  928. // If no author set yet, default to current user for cap checks.
  929. if ( ! $post_author_id )
  930. $post_author_id = $user_id;
  931. $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
  932. // If the user is the author...
  933. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  934. // If the post is published...
  935. if ( 'publish' == $post->post_status ) {
  936. $caps[] = $post_type->cap->delete_published_posts;
  937. } elseif ( 'trash' == $post->post_status ) {
  938. if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
  939. $caps[] = $post_type->cap->delete_published_posts;
  940. } else {
  941. // If the post is draft...
  942. $caps[] = $post_type->cap->delete_posts;
  943. }
  944. } else {
  945. // The user is trying to edit someone else's post.
  946. $caps[] = $post_type->cap->delete_others_posts;
  947. // The post is published, extra cap required.
  948. if ( 'publish' == $post->post_status )
  949. $caps[] = $post_type->cap->delete_published_posts;
  950. elseif ( 'private' == $post->post_status )
  951. $caps[] = $post_type->cap->delete_private_posts;
  952. }
  953. break;
  954. // current_user_can( 'create_posts', $post_type )
  955. case 'create_posts':
  956. $post_type = isset( $args[0] ) ? $args[0] : 'post';
  957. $post_type_object = get_post_type_object( $post_type );
  958. $caps[] = $post_type_object->cap->create_posts;
  959. break;
  960. // edit_post breaks down to edit_posts, edit_published_posts, or
  961. // edit_others_posts
  962. case 'edit_post':
  963. case 'edit_page':
  964. $post = get_post( $args[0] );
  965. if ( 'revision' == $post->post_type ) {
  966. $post = get_post( $post->post_parent );
  967. }
  968. $post_type = get_post_type_object( $post->post_type );
  969. if ( ! $post_type->map_meta_cap ) {
  970. $caps[] = $post_type->cap->$cap;
  971. // Prior to 3.1 we would re-call map_meta_cap here.
  972. if ( 'edit_post' == $cap )
  973. $cap = $post_type->cap->$cap;
  974. break;
  975. }
  976. $post_author_id = $post->post_author;
  977. // If no author set yet, default to current user for cap checks.
  978. if ( ! $post_author_id )
  979. $post_author_id = $user_id;
  980. $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
  981. // If the user is the author...
  982. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  983. // If the post is published...
  984. if ( 'publish' == $post->post_status ) {
  985. $caps[] = $post_type->cap->edit_published_posts;
  986. } elseif ( 'trash' == $post->post_status ) {
  987. if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
  988. $caps[] = $post_type->cap->edit_published_posts;
  989. } else {
  990. // If the post is draft...
  991. $caps[] = $post_type->cap->edit_posts;
  992. }
  993. } else {
  994. // The user is trying to edit someone else's post.
  995. $caps[] = $post_type->cap->edit_others_posts;
  996. // The post is published, extra cap required.
  997. if ( 'publish' == $post->post_status )
  998. $caps[] = $post_type->cap->edit_published_posts;
  999. elseif ( 'private' == $post->post_status )
  1000. $caps[] = $post_type->cap->edit_private_posts;
  1001. }
  1002. break;
  1003. case 'read_post':
  1004. case 'read_page':
  1005. $post = get_post( $args[0] );
  1006. if ( 'revision' == $post->post_type ) {
  1007. $post = get_post( $post->post_parent );
  1008. }
  1009. $post_type = get_post_type_object( $post->post_type );
  1010. if ( ! $post_type->map_meta_cap ) {
  1011. $caps[] = $post_type->cap->$cap;
  1012. // Prior to 3.1 we would re-call map_meta_cap here.
  1013. if ( 'read_post' == $cap )
  1014. $cap = $post_type->cap->$cap;
  1015. break;
  1016. }
  1017. $status_obj = get_post_status_object( $post->post_status );
  1018. if ( $status_obj->public ) {
  1019. $caps[] = $post_type->cap->read;
  1020. break;
  1021. }
  1022. $post_author_id = $post->post_author;
  1023. // If no author set yet, default to current user for cap checks.
  1024. if ( ! $post_author_id )
  1025. $post_author_id = $user_id;
  1026. $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
  1027. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
  1028. $caps[] = $post_type->cap->read;
  1029. elseif ( $status_obj->private )
  1030. $caps[] = $post_type->cap->read_private_posts;
  1031. else
  1032. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  1033. break;
  1034. case 'publish_post':
  1035. $post = get_post( $args[0] );
  1036. $post_type = get_post_type_object( $post->post_type );
  1037. $caps[] = $post_type->cap->publish_posts;
  1038. break;
  1039. case 'edit_post_meta':
  1040. case 'delete_post_meta':
  1041. case 'add_post_meta':
  1042. $post = get_post( $args[0] );
  1043. $post_type_object = get_post_type_object( $post->post_type );
  1044. $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
  1045. $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
  1046. if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
  1047. $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
  1048. if ( ! $allowed )
  1049. $caps[] = $cap;
  1050. } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
  1051. $caps[] = $cap;
  1052. }
  1053. break;
  1054. case 'edit_comment':
  1055. $comment = get_comment( $args[0] );
  1056. $post = get_post( $comment->comment_post_ID );
  1057. $post_type_object = get_post_type_object( $post->post_type );
  1058. $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
  1059. break;
  1060. case 'unfiltered_upload':
  1061. if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
  1062. $caps[] = $cap;
  1063. else
  1064. $caps[] = 'do_not_allow';
  1065. break;
  1066. case 'unfiltered_html' :
  1067. // Disallow unfiltered_html for all users, even admins and super admins.
  1068. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
  1069. $caps[] = 'do_not_allow';
  1070. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  1071. $caps[] = 'do_not_allow';
  1072. else
  1073. $caps[] = $cap;
  1074. break;
  1075. case 'edit_files':
  1076. case 'edit_plugins':
  1077. case 'edit_themes':
  1078. // Disallow the file editors.
  1079. if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
  1080. $caps[] = 'do_not_allow';
  1081. elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  1082. $caps[] = 'do_not_allow';
  1083. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  1084. $caps[] = 'do_not_allow';
  1085. else
  1086. $caps[] = $cap;
  1087. break;
  1088. case 'update_plugins':
  1089. case 'delete_plugins':
  1090. case 'install_plugins':
  1091. case 'update_themes':
  1092. case 'delete_themes':
  1093. case 'install_themes':
  1094. case 'update_core':
  1095. // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
  1096. // Files in uploads are excepted.
  1097. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  1098. $caps[] = 'do_not_allow';
  1099. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  1100. $caps[] = 'do_not_allow';
  1101. else
  1102. $caps[] = $cap;
  1103. break;
  1104. case 'activate_plugins':
  1105. $caps[] = $cap;
  1106. if ( is_multisite() ) {
  1107. // update_, install_, and delete_ are handled above with is_super_admin().
  1108. $menu_perms = get_site_option( 'menu_items', array() );
  1109. if ( empty( $menu_perms['plugins'] ) )
  1110. $caps[] = 'manage_network_plugins';
  1111. }
  1112. break;
  1113. case 'delete_user':
  1114. case 'delete_users':
  1115. // If multisite only super admins can delete users.
  1116. if ( is_multisite() && ! is_super_admin( $user_id ) )
  1117. $caps[] = 'do_not_allow';
  1118. else
  1119. $caps[] = 'delete_users'; // delete_user maps to delete_users.
  1120. break;
  1121. case 'create_users':
  1122. if ( !is_multisite() )
  1123. $caps[] = $cap;
  1124. elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
  1125. $caps[] = $cap;
  1126. else
  1127. $caps[] = 'do_not_allow';
  1128. break;
  1129. case 'manage_links' :
  1130. if ( get_option( 'link_manager_enabled' ) )
  1131. $caps[] = $cap;
  1132. else
  1133. $caps[] = 'do_not_allow';
  1134. break;
  1135. default:
  1136. // Handle meta capabilities for custom post types.
  1137. $post_type_meta_caps = _post_type_meta_capabilities();
  1138. if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  1139. $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  1140. return call_user_func_array( 'map_meta_cap', $args );
  1141. }
  1142. // If no meta caps match, return the original cap.
  1143. $caps[] = $cap;
  1144. }
  1145. return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
  1146. }
  1147. /**
  1148. * Whether current user has capability or role.
  1149. *
  1150. * @since 2.0.0
  1151. *
  1152. * @param string $capability Capability or role name.
  1153. * @return bool
  1154. */
  1155. function current_user_can( $capability ) {
  1156. $current_user = wp_get_current_user();
  1157. if ( empty( $current_user ) )
  1158. return false;
  1159. $args = array_slice( func_get_args(), 1 );
  1160. $args = array_merge( array( $capability ), $args );
  1161. return call_user_func_array( array( $current_user, 'has_cap' ), $args );
  1162. }
  1163. /**
  1164. * Whether current user has a capability or role for a given blog.
  1165. *
  1166. * @since 3.0.0
  1167. *
  1168. * @param int $blog_id Blog ID
  1169. * @param string $capability Capability or role name.
  1170. * @return bool
  1171. */
  1172. function current_user_can_for_blog( $blog_id, $capability ) {
  1173. switch_to_blog( $blog_id );
  1174. $current_user = wp_get_current_user();
  1175. if ( empty( $current_user ) )
  1176. return false;
  1177. $args = array_slice( func_get_args(), 2 );
  1178. $args = array_merge( array( $capability ), $args );
  1179. $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
  1180. restore_current_blog();
  1181. return $can;
  1182. }
  1183. /**
  1184. * Whether author of supplied post has capability or role.
  1185. *
  1186. * @since 2.9.0
  1187. *
  1188. * @param int|object $post Post ID or post object.
  1189. * @param string $capability Capability or role name.
  1190. * @return bool
  1191. */
  1192. function author_can( $post, $capability ) {
  1193. if ( !$post = get_post($post) )
  1194. return false;
  1195. $author = get_userdata( $post->post_author );
  1196. if ( ! $author )
  1197. return false;
  1198. $args = array_slice( func_get_args(), 2 );
  1199. $args = array_merge( array( $capability ), $args );
  1200. return call_user_func_array( array( $author, 'has_cap' ), $args );
  1201. }
  1202. /**
  1203. * Whether a particular user has capability or role.
  1204. *
  1205. * @since 3.1.0
  1206. *
  1207. * @param int|object $user User ID or object.
  1208. * @param string $capability Capability or role name.
  1209. * @return bool
  1210. */
  1211. function user_can( $user, $capability ) {
  1212. if ( ! is_object( $user ) )
  1213. $user = get_userdata( $user );
  1214. if ( ! $user || ! $user->exists() )
  1215. return false;
  1216. $args = array_slice( func_get_args(), 2 );
  1217. $args = array_merge( array( $capability ), $args );
  1218. return call_user_func_array( array( $user, 'has_cap' ), $args );
  1219. }
  1220. /**
  1221. * Retrieve role object.
  1222. *
  1223. * @see WP_Roles::get_role() Uses method to retrieve role object.
  1224. * @since 2.0.0
  1225. *
  1226. * @param string $role Role name.
  1227. * @return object
  1228. */
  1229. function get_role( $role ) {
  1230. global $wp_roles;
  1231. if ( ! isset( $wp_roles ) )
  1232. $wp_roles = new WP_Roles();
  1233. return $wp_roles->get_role( $role );
  1234. }
  1235. /**
  1236. * Add role, if it does not exist.
  1237. *
  1238. * @see WP_Roles::add_role() Uses method to add role.
  1239. * @since 2.0.0
  1240. *
  1241. * @param string $role Role name.
  1242. * @param string $display_name Display name for role.
  1243. * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
  1244. * @return null|WP_Role WP_Role object if role is added, null if already exists.
  1245. */
  1246. function add_role( $role, $display_name, $capabilities = array() ) {
  1247. global $wp_roles;
  1248. if ( ! isset( $wp_roles ) )
  1249. $wp_roles = new WP_Roles();
  1250. return $wp_roles->add_role( $role, $display_name, $capabilities );
  1251. }
  1252. /**
  1253. * Remove role, if it exists.
  1254. *
  1255. * @see WP_Roles::remove_role() Uses method to remove role.
  1256. * @since 2.0.0
  1257. *
  1258. * @param string $role Role name.
  1259. * @return null
  1260. */
  1261. function remove_role( $role ) {
  1262. global $wp_roles;
  1263. if ( ! isset( $wp_roles ) )
  1264. $wp_roles = new WP_Roles();
  1265. return $wp_roles->remove_role( $role );
  1266. }
  1267. /**
  1268. * Retrieve a list of super admins.
  1269. *
  1270. * @since 3.0.0
  1271. *
  1272. * @uses $super_admins Super admins global variable, if set.
  1273. *
  1274. * @return array List of super admin logins
  1275. */
  1276. function get_super_admins() {
  1277. global $super_admins;
  1278. if ( isset($super_admins) )
  1279. return $super_admins;
  1280. else
  1281. return get_site_option( 'site_admins', array('admin') );
  1282. }
  1283. /**
  1284. * Determine if user is a site admin.
  1285. *
  1286. * @since 3.0.0
  1287. *
  1288. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  1289. * @return bool True if the user is a site admin.
  1290. */
  1291. function is_super_admin( $user_id = false ) {
  1292. if ( ! $user_id || $user_id == get_current_user_id() )
  1293. $user = wp_get_current_user();
  1294. else
  1295. $user = get_userdata( $user_id );
  1296. if ( ! $user || ! $user->exists() )
  1297. return false;
  1298. if ( is_multisite() ) {
  1299. $super_admins = get_super_admins();
  1300. if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
  1301. return true;
  1302. } else {
  1303. if ( $user->has_cap('delete_users') )
  1304. return true;
  1305. }
  1306. return false;
  1307. }