PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/capabilities.php

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