PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/capabilities.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1226 lines | 512 code | 125 blank | 589 comment | 136 complexity | fb11d3de3fe214afce2076cae30db1d9 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  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. * This will be set as properties of the object.
  333. *
  334. * @since 2.0.0
  335. * @access private
  336. * @var array
  337. */
  338. var $data;
  339. /**
  340. * The user's ID.
  341. *
  342. * @since 2.1.0
  343. * @access public
  344. * @var int
  345. */
  346. var $ID = 0;
  347. /**
  348. * The deprecated user's ID.
  349. *
  350. * @since 2.0.0
  351. * @access public
  352. * @deprecated Use WP_User::$ID
  353. * @see WP_User::$ID
  354. * @var int
  355. */
  356. var $id = 0;
  357. /**
  358. * The individual capabilities the user has been given.
  359. *
  360. * @since 2.0.0
  361. * @access public
  362. * @var array
  363. */
  364. var $caps = array();
  365. /**
  366. * User metadata option name.
  367. *
  368. * @since 2.0.0
  369. * @access public
  370. * @var string
  371. */
  372. var $cap_key;
  373. /**
  374. * The roles the user is part of.
  375. *
  376. * @since 2.0.0
  377. * @access public
  378. * @var array
  379. */
  380. var $roles = array();
  381. /**
  382. * All capabilities the user has, including individual and role based.
  383. *
  384. * @since 2.0.0
  385. * @access public
  386. * @var array
  387. */
  388. var $allcaps = array();
  389. /**
  390. * First name of the user.
  391. *
  392. * Created to prevent notices.
  393. *
  394. * @since 2.7.0
  395. * @access public
  396. * @var string
  397. */
  398. var $first_name = '';
  399. /**
  400. * Last name of the user.
  401. *
  402. * Created to prevent notices.
  403. *
  404. * @since 2.7.0
  405. * @access public
  406. * @var string
  407. */
  408. var $last_name = '';
  409. /**
  410. * The filter context applied to user data fields.
  411. *
  412. * @since 2.9.0
  413. * @access private
  414. * @var string
  415. */
  416. var $filter = null;
  417. /**
  418. * Constructor - Sets up the object properties.
  419. *
  420. * Retrieves the userdata and then assigns all of the data keys to direct
  421. * properties of the object. Calls {@link WP_User::_init_caps()} after
  422. * setting up the object's user data properties.
  423. *
  424. * @since 2.0.0
  425. * @access public
  426. *
  427. * @param int|string $id User's ID or username
  428. * @param int $name Optional. User's username
  429. * @param int $blog_id Optional Blog ID, defaults to current blog.
  430. * @return WP_User
  431. */
  432. function __construct( $id, $name = '', $blog_id = '' ) {
  433. if ( empty( $id ) && empty( $name ) )
  434. return;
  435. if ( ! is_numeric( $id ) ) {
  436. $name = $id;
  437. $id = 0;
  438. }
  439. if ( ! empty( $id ) )
  440. $this->data = get_userdata( $id );
  441. else
  442. $this->data = get_userdatabylogin( $name );
  443. if ( empty( $this->data->ID ) )
  444. return;
  445. foreach ( get_object_vars( $this->data ) as $key => $value ) {
  446. $this->{$key} = $value;
  447. }
  448. $this->id = $this->ID;
  449. $this->for_blog( $blog_id );
  450. }
  451. /**
  452. * Set up capability object properties.
  453. *
  454. * Will set the value for the 'cap_key' property to current database table
  455. * prefix, followed by 'capabilities'. Will then check to see if the
  456. * property matching the 'cap_key' exists and is an array. If so, it will be
  457. * used.
  458. *
  459. * @since 2.1.0
  460. *
  461. * @param string $cap_key Optional capability key
  462. * @access protected
  463. */
  464. function _init_caps( $cap_key = '' ) {
  465. global $wpdb;
  466. if ( empty($cap_key) )
  467. $this->cap_key = $wpdb->prefix . 'capabilities';
  468. else
  469. $this->cap_key = $cap_key;
  470. $this->caps = &$this->{$this->cap_key};
  471. if ( ! is_array( $this->caps ) )
  472. $this->caps = array();
  473. $this->get_role_caps();
  474. }
  475. /**
  476. * Retrieve all of the role capabilities and merge with individual capabilities.
  477. *
  478. * All of the capabilities of the roles the user belongs to are merged with
  479. * the users individual roles. This also means that the user can be denied
  480. * specific roles that their role might have, but the specific user isn't
  481. * granted permission to.
  482. *
  483. * @since 2.0.0
  484. * @uses $wp_roles
  485. * @access public
  486. */
  487. function get_role_caps() {
  488. global $wp_roles;
  489. if ( ! isset( $wp_roles ) )
  490. $wp_roles = new WP_Roles();
  491. //Filter out caps that are not role names and assign to $this->roles
  492. if ( is_array( $this->caps ) )
  493. $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
  494. //Build $allcaps from role caps, overlay user's $caps
  495. $this->allcaps = array();
  496. foreach ( (array) $this->roles as $role ) {
  497. $the_role =& $wp_roles->get_role( $role );
  498. $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
  499. }
  500. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  501. }
  502. /**
  503. * Add role to user.
  504. *
  505. * Updates the user's meta data option with capabilities and roles.
  506. *
  507. * @since 2.0.0
  508. * @access public
  509. *
  510. * @param string $role Role name.
  511. */
  512. function add_role( $role ) {
  513. $this->caps[$role] = true;
  514. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  515. $this->get_role_caps();
  516. $this->update_user_level_from_caps();
  517. }
  518. /**
  519. * Remove role from user.
  520. *
  521. * @since 2.0.0
  522. * @access public
  523. *
  524. * @param string $role Role name.
  525. */
  526. function remove_role( $role ) {
  527. if ( !in_array($role, $this->roles) )
  528. return;
  529. unset( $this->caps[$role] );
  530. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  531. $this->get_role_caps();
  532. $this->update_user_level_from_caps();
  533. }
  534. /**
  535. * Set the role of the user.
  536. *
  537. * This will remove the previous roles of the user and assign the user the
  538. * new one. You can set the role to an empty string and it will remove all
  539. * of the roles from the user.
  540. *
  541. * @since 2.0.0
  542. * @access public
  543. *
  544. * @param string $role Role name.
  545. */
  546. function set_role( $role ) {
  547. foreach ( (array) $this->roles as $oldrole )
  548. unset( $this->caps[$oldrole] );
  549. if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
  550. return;
  551. if ( !empty( $role ) ) {
  552. $this->caps[$role] = true;
  553. $this->roles = array( $role => true );
  554. } else {
  555. $this->roles = false;
  556. }
  557. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  558. $this->get_role_caps();
  559. $this->update_user_level_from_caps();
  560. do_action( 'set_user_role', $this->ID, $role );
  561. }
  562. /**
  563. * Choose the maximum level the user has.
  564. *
  565. * Will compare the level from the $item parameter against the $max
  566. * parameter. If the item is incorrect, then just the $max parameter value
  567. * will be returned.
  568. *
  569. * Used to get the max level based on the capabilities the user has. This
  570. * is also based on roles, so if the user is assigned the Administrator role
  571. * then the capability 'level_10' will exist and the user will get that
  572. * value.
  573. *
  574. * @since 2.0.0
  575. * @access public
  576. *
  577. * @param int $max Max level of user.
  578. * @param string $item Level capability name.
  579. * @return int Max Level.
  580. */
  581. function level_reduction( $max, $item ) {
  582. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  583. $level = intval( $matches[1] );
  584. return max( $max, $level );
  585. } else {
  586. return $max;
  587. }
  588. }
  589. /**
  590. * Update the maximum user level for the user.
  591. *
  592. * Updates the 'user_level' user metadata (includes prefix that is the
  593. * database table prefix) with the maximum user level. Gets the value from
  594. * the all of the capabilities that the user has.
  595. *
  596. * @since 2.0.0
  597. * @access public
  598. */
  599. function update_user_level_from_caps() {
  600. global $wpdb;
  601. $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
  602. update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
  603. }
  604. /**
  605. * Add capability and grant or deny access to capability.
  606. *
  607. * @since 2.0.0
  608. * @access public
  609. *
  610. * @param string $cap Capability name.
  611. * @param bool $grant Whether to grant capability to user.
  612. */
  613. function add_cap( $cap, $grant = true ) {
  614. $this->caps[$cap] = $grant;
  615. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  616. }
  617. /**
  618. * Remove capability from user.
  619. *
  620. * @since 2.0.0
  621. * @access public
  622. *
  623. * @param string $cap Capability name.
  624. */
  625. function remove_cap( $cap ) {
  626. if ( empty( $this->caps[$cap] ) )
  627. return;
  628. unset( $this->caps[$cap] );
  629. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  630. }
  631. /**
  632. * Remove all of the capabilities of the user.
  633. *
  634. * @since 2.1.0
  635. * @access public
  636. */
  637. function remove_all_caps() {
  638. global $wpdb;
  639. $this->caps = array();
  640. delete_user_meta( $this->ID, $this->cap_key );
  641. delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
  642. $this->get_role_caps();
  643. }
  644. /**
  645. * Whether user has capability or role name.
  646. *
  647. * This is useful for looking up whether the user has a specific role
  648. * assigned to the user. The second optional parameter can also be used to
  649. * check for capabilities against a specfic post.
  650. *
  651. * @since 2.0.0
  652. * @access public
  653. *
  654. * @param string|int $cap Capability or role name to search.
  655. * @param int $post_id Optional. Post ID to check capability against specific post.
  656. * @return bool True, if user has capability; false, if user does not have capability.
  657. */
  658. function has_cap( $cap ) {
  659. if ( is_numeric( $cap ) ) {
  660. _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
  661. $cap = $this->translate_level_to_cap( $cap );
  662. }
  663. $args = array_slice( func_get_args(), 1 );
  664. $args = array_merge( array( $cap, $this->ID ), $args );
  665. $caps = call_user_func_array( 'map_meta_cap', $args );
  666. // Multisite super admin has all caps by definition, Unless specifically denied.
  667. if ( is_multisite() && is_super_admin( $this->ID ) ) {
  668. if ( in_array('do_not_allow', $caps) )
  669. return false;
  670. return true;
  671. }
  672. // Must have ALL requested caps
  673. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
  674. $capabilities['exist'] = true; // Everyone is allowed to exist
  675. foreach ( (array) $caps as $cap ) {
  676. //echo "Checking cap $cap<br />";
  677. if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
  678. return false;
  679. }
  680. return true;
  681. }
  682. /**
  683. * Convert numeric level to level capability name.
  684. *
  685. * Prepends 'level_' to level number.
  686. *
  687. * @since 2.0.0
  688. * @access public
  689. *
  690. * @param int $level Level number, 1 to 10.
  691. * @return string
  692. */
  693. function translate_level_to_cap( $level ) {
  694. return 'level_' . $level;
  695. }
  696. /**
  697. * Set the blog to operate on. Defaults to the current blog.
  698. *
  699. * @since 3.0.0
  700. *
  701. * @param int $blog_id Optional Blog ID, defaults to current blog.
  702. */
  703. function for_blog( $blog_id = '' ) {
  704. global $wpdb;
  705. if ( ! empty( $blog_id ) )
  706. $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
  707. else
  708. $cap_key = '';
  709. $this->_init_caps( $cap_key );
  710. }
  711. }
  712. /**
  713. * Map meta capabilities to primitive capabilities.
  714. *
  715. * This does not actually compare whether the user ID has the actual capability,
  716. * just what the capability or capabilities are. Meta capability list value can
  717. * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
  718. * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
  719. *
  720. * @since 2.0.0
  721. *
  722. * @param string $cap Capability name.
  723. * @param int $user_id User ID.
  724. * @return array Actual capabilities for meta capability.
  725. */
  726. function map_meta_cap( $cap, $user_id ) {
  727. $args = array_slice( func_get_args(), 2 );
  728. $caps = array();
  729. switch ( $cap ) {
  730. case 'remove_user':
  731. $caps[] = 'remove_users';
  732. break;
  733. case 'promote_user':
  734. $caps[] = 'promote_users';
  735. break;
  736. case 'edit_user':
  737. // Allow user to edit itself
  738. if ( isset( $args[0] ) && $user_id == $args[0] )
  739. break;
  740. // Fall through
  741. case 'edit_users':
  742. // If multisite these caps are allowed only for super admins.
  743. if ( is_multisite() && !is_super_admin( $user_id ) )
  744. $caps[] = 'do_not_allow';
  745. else
  746. $caps[] = 'edit_users'; // Explicit due to primitive fall through
  747. break;
  748. case 'delete_post':
  749. case 'delete_page':
  750. $author_data = get_userdata( $user_id );
  751. $post = get_post( $args[0] );
  752. if ( 'revision' == $post->post_type ) {
  753. $post = get_post( $post->post_parent );
  754. }
  755. $post_type = get_post_type_object( $post->post_type );
  756. if ( ! $post_type->map_meta_cap ) {
  757. $caps[] = $post_type->cap->$cap;
  758. // Prior to 3.1 we would re-call map_meta_cap here.
  759. if ( 'delete_post' == $cap )
  760. $cap = $post_type->cap->$cap;
  761. break;
  762. }
  763. if ( '' != $post->post_author ) {
  764. $post_author_data = get_userdata( $post->post_author );
  765. } else {
  766. // No author set yet, so default to current user for cap checks.
  767. $post_author_data = $author_data;
  768. }
  769. // If the user is the author...
  770. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  771. // If the post is published...
  772. if ( 'publish' == $post->post_status ) {
  773. $caps[] = $post_type->cap->delete_published_posts;
  774. } elseif ( 'trash' == $post->post_status ) {
  775. if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
  776. $caps[] = $post_type->cap->delete_published_posts;
  777. } else {
  778. // If the post is draft...
  779. $caps[] = $post_type->cap->delete_posts;
  780. }
  781. } else {
  782. // The user is trying to edit someone else's post.
  783. $caps[] = $post_type->cap->delete_others_posts;
  784. // The post is published, extra cap required.
  785. if ( 'publish' == $post->post_status )
  786. $caps[] = $post_type->cap->delete_published_posts;
  787. elseif ( 'private' == $post->post_status )
  788. $caps[] = $post_type->cap->delete_private_posts;
  789. }
  790. break;
  791. // edit_post breaks down to edit_posts, edit_published_posts, or
  792. // edit_others_posts
  793. case 'edit_post':
  794. case 'edit_page':
  795. $author_data = get_userdata( $user_id );
  796. $post = get_post( $args[0] );
  797. if ( 'revision' == $post->post_type ) {
  798. $post = get_post( $post->post_parent );
  799. }
  800. $post_type = get_post_type_object( $post->post_type );
  801. if ( ! $post_type->map_meta_cap ) {
  802. $caps[] = $post_type->cap->$cap;
  803. // Prior to 3.1 we would re-call map_meta_cap here.
  804. if ( 'edit_post' == $cap )
  805. $cap = $post_type->cap->$cap;
  806. break;
  807. }
  808. if ( '' != $post->post_author ) {
  809. $post_author_data = get_userdata( $post->post_author );
  810. } else {
  811. // No author set yet, so default to current user for cap checks.
  812. $post_author_data = $author_data;
  813. }
  814. //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
  815. // If the user is the author...
  816. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  817. // If the post is published...
  818. if ( 'publish' == $post->post_status ) {
  819. $caps[] = $post_type->cap->edit_published_posts;
  820. } elseif ( 'trash' == $post->post_status ) {
  821. if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
  822. $caps[] = $post_type->cap->edit_published_posts;
  823. } else {
  824. // If the post is draft...
  825. $caps[] = $post_type->cap->edit_posts;
  826. }
  827. } else {
  828. // The user is trying to edit someone else's post.
  829. $caps[] = $post_type->cap->edit_others_posts;
  830. // The post is published, extra cap required.
  831. if ( 'publish' == $post->post_status )
  832. $caps[] = $post_type->cap->edit_published_posts;
  833. elseif ( 'private' == $post->post_status )
  834. $caps[] = $post_type->cap->edit_private_posts;
  835. }
  836. break;
  837. case 'read_post':
  838. case 'read_page':
  839. $author_data = get_userdata( $user_id );
  840. $post = get_post( $args[0] );
  841. if ( 'revision' == $post->post_type ) {
  842. $post = get_post( $post->post_parent );
  843. }
  844. $post_type = get_post_type_object( $post->post_type );
  845. if ( ! $post_type->map_meta_cap ) {
  846. $caps[] = $post_type->cap->$cap;
  847. // Prior to 3.1 we would re-call map_meta_cap here.
  848. if ( 'read_post' == $cap )
  849. $cap = $post_type->cap->$cap;
  850. break;
  851. }
  852. if ( 'private' != $post->post_status ) {
  853. $caps[] = $post_type->cap->read;
  854. break;
  855. }
  856. if ( '' != $post->post_author ) {
  857. $post_author_data = get_userdata( $post->post_author );
  858. } else {
  859. // No author set yet, so default to current user for cap checks.
  860. $post_author_data = $author_data;
  861. }
  862. if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
  863. $caps[] = $post_type->cap->read;
  864. else
  865. $caps[] = $post_type->cap->read_private_posts;
  866. break;
  867. case 'edit_comment':
  868. $comment = get_comment( $args[0] );
  869. $post = get_post( $comment->comment_post_ID );
  870. $post_type_object = get_post_type_object( $post->post_type );
  871. $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
  872. break;
  873. case 'unfiltered_upload':
  874. if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
  875. $caps[] = $cap;
  876. else
  877. $caps[] = 'do_not_allow';
  878. break;
  879. case 'edit_files':
  880. case 'edit_plugins':
  881. case 'edit_themes':
  882. if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
  883. $caps[] = 'do_not_allow';
  884. break;
  885. }
  886. // Fall through if not DISALLOW_FILE_EDIT.
  887. case 'update_plugins':
  888. case 'delete_plugins':
  889. case 'install_plugins':
  890. case 'update_themes':
  891. case 'delete_themes':
  892. case 'install_themes':
  893. case 'update_core':
  894. // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
  895. // Files in uploads are excepted.
  896. if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
  897. $caps[] = 'do_not_allow';
  898. break;
  899. }
  900. // Fall through if not DISALLOW_FILE_MODS.
  901. case 'unfiltered_html':
  902. // Disallow unfiltered_html for all users, even admins and super admins.
  903. if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
  904. $caps[] = 'do_not_allow';
  905. break;
  906. }
  907. // Fall through if not DISALLOW_UNFILTERED_HTML
  908. case 'delete_user':
  909. case 'delete_users':
  910. // If multisite these caps are allowed only for super admins.
  911. if ( is_multisite() && !is_super_admin( $user_id ) ) {
  912. $caps[] = 'do_not_allow';
  913. } else {
  914. if ( 'delete_user' == $cap )
  915. $cap = 'delete_users';
  916. $caps[] = $cap;
  917. }
  918. break;
  919. case 'create_users':
  920. if ( !is_multisite() )
  921. $caps[] = $cap;
  922. elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
  923. $caps[] = $cap;
  924. else
  925. $caps[] = 'do_not_allow';
  926. break;
  927. default:
  928. // Handle meta capabilities for custom post types.
  929. $post_type_meta_caps = _post_type_meta_capabilities();
  930. if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  931. $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  932. return call_user_func_array( 'map_meta_cap', $args );
  933. }
  934. // If no meta caps match, return the original cap.
  935. $caps[] = $cap;
  936. }
  937. return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
  938. }
  939. /**
  940. * Whether current user has capability or role.
  941. *
  942. * @since 2.0.0
  943. *
  944. * @param string $capability Capability or role name.
  945. * @return bool
  946. */
  947. function current_user_can( $capability ) {
  948. $current_user = wp_get_current_user();
  949. if ( empty( $current_user ) )
  950. return false;
  951. $args = array_slice( func_get_args(), 1 );
  952. $args = array_merge( array( $capability ), $args );
  953. return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
  954. }
  955. /**
  956. * Whether current user has a capability or role for a given blog.
  957. *
  958. * @since 3.0.0
  959. *
  960. * @param int $blog_id Blog ID
  961. * @param string $capability Capability or role name.
  962. * @return bool
  963. */
  964. function current_user_can_for_blog( $blog_id, $capability ) {
  965. $current_user = wp_get_current_user();
  966. if ( empty( $current_user ) )
  967. return false;
  968. // Create new object to avoid stomping the global current_user.
  969. $user = new WP_User( $current_user->id) ;
  970. // Set the blog id. @todo add blog id arg to WP_User constructor?
  971. $user->for_blog( $blog_id );
  972. $args = array_slice( func_get_args(), 2 );
  973. $args = array_merge( array( $capability ), $args );
  974. return call_user_func_array( array( &$user, 'has_cap' ), $args );
  975. }
  976. /**
  977. * Whether author of supplied post has capability or role.
  978. *
  979. * @since 2.9.0
  980. *
  981. * @param int|object $post Post ID or post object.
  982. * @param string $capability Capability or role name.
  983. * @return bool
  984. */
  985. function author_can( $post, $capability ) {
  986. if ( !$post = get_post($post) )
  987. return false;
  988. $author = new WP_User( $post->post_author );
  989. if ( empty( $author->ID ) )
  990. return false;
  991. $args = array_slice( func_get_args(), 2 );
  992. $args = array_merge( array( $capability ), $args );
  993. return call_user_func_array( array( &$author, 'has_cap' ), $args );
  994. }
  995. /**
  996. * Whether a particular user has capability or role.
  997. *
  998. * @since 3.1.0
  999. *
  1000. * @param int|object $user User ID or object.
  1001. * @param string $capability Capability or role name.
  1002. * @return bool
  1003. */
  1004. function user_can( $user, $capability ) {
  1005. if ( ! is_object( $user ) )
  1006. $user = new WP_User( $user );
  1007. if ( ! $user || ! $user->ID )
  1008. return false;
  1009. $args = array_slice( func_get_args(), 2 );
  1010. $args = array_merge( array( $capability ), $args );
  1011. return call_user_func_array( array( &$user, 'has_cap' ), $args );
  1012. }
  1013. /**
  1014. * Retrieve role object.
  1015. *
  1016. * @see WP_Roles::get_role() Uses method to retrieve role object.
  1017. * @since 2.0.0
  1018. *
  1019. * @param string $role Role name.
  1020. * @return object
  1021. */
  1022. function get_role( $role ) {
  1023. global $wp_roles;
  1024. if ( ! isset( $wp_roles ) )
  1025. $wp_roles = new WP_Roles();
  1026. return $wp_roles->get_role( $role );
  1027. }
  1028. /**
  1029. * Add role, if it does not exist.
  1030. *
  1031. * @see WP_Roles::add_role() Uses method to add role.
  1032. * @since 2.0.0
  1033. *
  1034. * @param string $role Role name.
  1035. * @param string $display_name Display name for role.
  1036. * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
  1037. * @return null|WP_Role WP_Role object if role is added, null if already exists.
  1038. */
  1039. function add_role( $role, $display_name, $capabilities = array() ) {
  1040. global $wp_roles;
  1041. if ( ! isset( $wp_roles ) )
  1042. $wp_roles = new WP_Roles();
  1043. return $wp_roles->add_role( $role, $display_name, $capabilities );
  1044. }
  1045. /**
  1046. * Remove role, if it exists.
  1047. *
  1048. * @see WP_Roles::remove_role() Uses method to remove role.
  1049. * @since 2.0.0
  1050. *
  1051. * @param string $role Role name.
  1052. * @return null
  1053. */
  1054. function remove_role( $role ) {
  1055. global $wp_roles;
  1056. if ( ! isset( $wp_roles ) )
  1057. $wp_roles = new WP_Roles();
  1058. return $wp_roles->remove_role( $role );
  1059. }
  1060. /**
  1061. * Retrieve a list of super admins.
  1062. *
  1063. * @since 3.0.0
  1064. *
  1065. * @uses $super_admins Super admins global variable, if set.
  1066. *
  1067. * @return array List of super admin logins
  1068. */
  1069. function get_super_admins() {
  1070. global $super_admins;
  1071. if ( isset($super_admins) )
  1072. return $super_admins;
  1073. else
  1074. return get_site_option( 'site_admins', array('admin') );
  1075. }
  1076. /**
  1077. * Determine if user is a site admin.
  1078. *
  1079. * @since 3.0.0
  1080. *
  1081. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  1082. * @return bool True if the user is a site admin.
  1083. */
  1084. function is_super_admin( $user_id = false ) {
  1085. if ( $user_id )
  1086. $user = new WP_User( $user_id );
  1087. else
  1088. $user = wp_get_current_user();
  1089. if ( empty( $user->id ) )
  1090. return false;
  1091. if ( is_multisite() ) {
  1092. $super_admins = get_super_admins();
  1093. if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
  1094. return true;
  1095. } else {
  1096. if ( $user->has_cap('delete_users') )
  1097. return true;
  1098. }
  1099. return false;
  1100. }
  1101. ?>