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

/wp-includes/capabilities.php

https://gitlab.com/webkod3r/tripolis
PHP | 621 lines | 381 code | 56 blank | 184 comment | 118 complexity | b24de0dc62fb8743742d64b273eef034 MD5 | raw file
  1. <?php
  2. /**
  3. * Core User Role & Capabilities API
  4. *
  5. * @package WordPress
  6. * @subpackage Users
  7. */
  8. /**
  9. * Map meta capabilities to primitive capabilities.
  10. *
  11. * This does not actually compare whether the user ID has the actual capability,
  12. * just what the capability or capabilities are. Meta capability list value can
  13. * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
  14. * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
  15. *
  16. * @since 2.0.0
  17. *
  18. * @global array $post_type_meta_caps Used to get post type meta capabilities.
  19. *
  20. * @param string $cap Capability name.
  21. * @param int $user_id User ID.
  22. * @param int $object_id Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
  23. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  24. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  25. * 'edit_others_posts', etc. The parameter is accessed via func_get_args().
  26. * @return array Actual capabilities for meta capability.
  27. */
  28. function map_meta_cap( $cap, $user_id ) {
  29. $args = array_slice( func_get_args(), 2 );
  30. $caps = array();
  31. switch ( $cap ) {
  32. case 'remove_user':
  33. $caps[] = 'remove_users';
  34. break;
  35. case 'promote_user':
  36. case 'add_users':
  37. $caps[] = 'promote_users';
  38. break;
  39. case 'edit_user':
  40. case 'edit_users':
  41. // Allow user to edit itself
  42. if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
  43. break;
  44. // In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin.
  45. if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) {
  46. $caps[] = 'do_not_allow';
  47. } else {
  48. $caps[] = 'edit_users'; // edit_user maps to edit_users.
  49. }
  50. break;
  51. case 'delete_post':
  52. case 'delete_page':
  53. $post = get_post( $args[0] );
  54. if ( ! $post ) {
  55. $caps[] = 'do_not_allow';
  56. break;
  57. }
  58. if ( 'revision' == $post->post_type ) {
  59. $post = get_post( $post->post_parent );
  60. if ( ! $post ) {
  61. $caps[] = 'do_not_allow';
  62. break;
  63. }
  64. }
  65. $post_type = get_post_type_object( $post->post_type );
  66. if ( ! $post_type ) {
  67. /* translators: 1: post type, 2: capability name */
  68. _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' );
  69. $caps[] = 'edit_others_posts';
  70. break;
  71. }
  72. if ( ! $post_type->map_meta_cap ) {
  73. $caps[] = $post_type->cap->$cap;
  74. // Prior to 3.1 we would re-call map_meta_cap here.
  75. if ( 'delete_post' == $cap )
  76. $cap = $post_type->cap->$cap;
  77. break;
  78. }
  79. // If the post author is set and the user is the author...
  80. if ( $post->post_author && $user_id == $post->post_author ) {
  81. // If the post is published or scheduled...
  82. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  83. $caps[] = $post_type->cap->delete_published_posts;
  84. } elseif ( 'trash' == $post->post_status ) {
  85. $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
  86. if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
  87. $caps[] = $post_type->cap->delete_published_posts;
  88. } else {
  89. $caps[] = $post_type->cap->delete_posts;
  90. }
  91. } else {
  92. // If the post is draft...
  93. $caps[] = $post_type->cap->delete_posts;
  94. }
  95. } else {
  96. // The user is trying to edit someone else's post.
  97. $caps[] = $post_type->cap->delete_others_posts;
  98. // The post is published or scheduled, extra cap required.
  99. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  100. $caps[] = $post_type->cap->delete_published_posts;
  101. } elseif ( 'private' == $post->post_status ) {
  102. $caps[] = $post_type->cap->delete_private_posts;
  103. }
  104. }
  105. break;
  106. // edit_post breaks down to edit_posts, edit_published_posts, or
  107. // edit_others_posts
  108. case 'edit_post':
  109. case 'edit_page':
  110. $post = get_post( $args[0] );
  111. if ( ! $post ) {
  112. $caps[] = 'do_not_allow';
  113. break;
  114. }
  115. if ( 'revision' == $post->post_type ) {
  116. $post = get_post( $post->post_parent );
  117. if ( ! $post ) {
  118. $caps[] = 'do_not_allow';
  119. break;
  120. }
  121. }
  122. $post_type = get_post_type_object( $post->post_type );
  123. if ( ! $post_type ) {
  124. /* translators: 1: post type, 2: capability name */
  125. _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' );
  126. $caps[] = 'edit_others_posts';
  127. break;
  128. }
  129. if ( ! $post_type->map_meta_cap ) {
  130. $caps[] = $post_type->cap->$cap;
  131. // Prior to 3.1 we would re-call map_meta_cap here.
  132. if ( 'edit_post' == $cap )
  133. $cap = $post_type->cap->$cap;
  134. break;
  135. }
  136. // If the post author is set and the user is the author...
  137. if ( $post->post_author && $user_id == $post->post_author ) {
  138. // If the post is published or scheduled...
  139. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  140. $caps[] = $post_type->cap->edit_published_posts;
  141. } elseif ( 'trash' == $post->post_status ) {
  142. $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
  143. if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
  144. $caps[] = $post_type->cap->edit_published_posts;
  145. } else {
  146. $caps[] = $post_type->cap->edit_posts;
  147. }
  148. } else {
  149. // If the post is draft...
  150. $caps[] = $post_type->cap->edit_posts;
  151. }
  152. } else {
  153. // The user is trying to edit someone else's post.
  154. $caps[] = $post_type->cap->edit_others_posts;
  155. // The post is published or scheduled, extra cap required.
  156. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  157. $caps[] = $post_type->cap->edit_published_posts;
  158. } elseif ( 'private' == $post->post_status ) {
  159. $caps[] = $post_type->cap->edit_private_posts;
  160. }
  161. }
  162. break;
  163. case 'read_post':
  164. case 'read_page':
  165. $post = get_post( $args[0] );
  166. if ( ! $post ) {
  167. $caps[] = 'do_not_allow';
  168. break;
  169. }
  170. if ( 'revision' == $post->post_type ) {
  171. $post = get_post( $post->post_parent );
  172. if ( ! $post ) {
  173. $caps[] = 'do_not_allow';
  174. break;
  175. }
  176. }
  177. $post_type = get_post_type_object( $post->post_type );
  178. if ( ! $post_type ) {
  179. /* translators: 1: post type, 2: capability name */
  180. _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' );
  181. $caps[] = 'edit_others_posts';
  182. break;
  183. }
  184. if ( ! $post_type->map_meta_cap ) {
  185. $caps[] = $post_type->cap->$cap;
  186. // Prior to 3.1 we would re-call map_meta_cap here.
  187. if ( 'read_post' == $cap )
  188. $cap = $post_type->cap->$cap;
  189. break;
  190. }
  191. $status_obj = get_post_status_object( $post->post_status );
  192. if ( $status_obj->public ) {
  193. $caps[] = $post_type->cap->read;
  194. break;
  195. }
  196. if ( $post->post_author && $user_id == $post->post_author ) {
  197. $caps[] = $post_type->cap->read;
  198. } elseif ( $status_obj->private ) {
  199. $caps[] = $post_type->cap->read_private_posts;
  200. } else {
  201. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  202. }
  203. break;
  204. case 'publish_post':
  205. $post = get_post( $args[0] );
  206. if ( ! $post ) {
  207. $caps[] = 'do_not_allow';
  208. break;
  209. }
  210. $post_type = get_post_type_object( $post->post_type );
  211. if ( ! $post_type ) {
  212. /* translators: 1: post type, 2: capability name */
  213. _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post type %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post of that type.' ), $post->post_type, $cap ), '4.4.0' );
  214. $caps[] = 'edit_others_posts';
  215. break;
  216. }
  217. $caps[] = $post_type->cap->publish_posts;
  218. break;
  219. case 'edit_post_meta':
  220. case 'delete_post_meta':
  221. case 'add_post_meta':
  222. $post = get_post( $args[0] );
  223. if ( ! $post ) {
  224. $caps[] = 'do_not_allow';
  225. break;
  226. }
  227. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  228. $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
  229. if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
  230. /**
  231. * Filter whether the user is allowed to add post meta to a post.
  232. *
  233. * The dynamic portion of the hook name, `$meta_key`, refers to the
  234. * meta key passed to {@see map_meta_cap()}.
  235. *
  236. * @since 3.3.0
  237. *
  238. * @param bool $allowed Whether the user can add the post meta. Default false.
  239. * @param string $meta_key The meta key.
  240. * @param int $post_id Post ID.
  241. * @param int $user_id User ID.
  242. * @param string $cap Capability name.
  243. * @param array $caps User capabilities.
  244. */
  245. $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
  246. if ( ! $allowed )
  247. $caps[] = $cap;
  248. } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
  249. $caps[] = $cap;
  250. }
  251. break;
  252. case 'edit_comment':
  253. $comment = get_comment( $args[0] );
  254. if ( ! $comment ) {
  255. $caps[] = 'do_not_allow';
  256. break;
  257. }
  258. $post = get_post( $comment->comment_post_ID );
  259. /*
  260. * If the post doesn't exist, we have an orphaned comment.
  261. * Fall back to the edit_posts capability, instead.
  262. */
  263. if ( $post ) {
  264. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  265. } else {
  266. $caps = map_meta_cap( 'edit_posts', $user_id );
  267. }
  268. break;
  269. case 'unfiltered_upload':
  270. if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
  271. $caps[] = $cap;
  272. else
  273. $caps[] = 'do_not_allow';
  274. break;
  275. case 'unfiltered_html' :
  276. // Disallow unfiltered_html for all users, even admins and super admins.
  277. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
  278. $caps[] = 'do_not_allow';
  279. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  280. $caps[] = 'do_not_allow';
  281. else
  282. $caps[] = $cap;
  283. break;
  284. case 'edit_files':
  285. case 'edit_plugins':
  286. case 'edit_themes':
  287. // Disallow the file editors.
  288. if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
  289. $caps[] = 'do_not_allow';
  290. elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  291. $caps[] = 'do_not_allow';
  292. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  293. $caps[] = 'do_not_allow';
  294. else
  295. $caps[] = $cap;
  296. break;
  297. case 'update_plugins':
  298. case 'delete_plugins':
  299. case 'install_plugins':
  300. case 'upload_plugins':
  301. case 'update_themes':
  302. case 'delete_themes':
  303. case 'install_themes':
  304. case 'upload_themes':
  305. case 'update_core':
  306. // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
  307. // Files in uploads are excepted.
  308. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
  309. $caps[] = 'do_not_allow';
  310. } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
  311. $caps[] = 'do_not_allow';
  312. } elseif ( 'upload_themes' === $cap ) {
  313. $caps[] = 'install_themes';
  314. } elseif ( 'upload_plugins' === $cap ) {
  315. $caps[] = 'install_plugins';
  316. } else {
  317. $caps[] = $cap;
  318. }
  319. break;
  320. case 'activate_plugins':
  321. $caps[] = $cap;
  322. if ( is_multisite() ) {
  323. // update_, install_, and delete_ are handled above with is_super_admin().
  324. $menu_perms = get_site_option( 'menu_items', array() );
  325. if ( empty( $menu_perms['plugins'] ) )
  326. $caps[] = 'manage_network_plugins';
  327. }
  328. break;
  329. case 'delete_user':
  330. case 'delete_users':
  331. // If multisite only super admins can delete users.
  332. if ( is_multisite() && ! is_super_admin( $user_id ) )
  333. $caps[] = 'do_not_allow';
  334. else
  335. $caps[] = 'delete_users'; // delete_user maps to delete_users.
  336. break;
  337. case 'create_users':
  338. if ( !is_multisite() )
  339. $caps[] = $cap;
  340. elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
  341. $caps[] = $cap;
  342. else
  343. $caps[] = 'do_not_allow';
  344. break;
  345. case 'manage_links' :
  346. if ( get_option( 'link_manager_enabled' ) )
  347. $caps[] = $cap;
  348. else
  349. $caps[] = 'do_not_allow';
  350. break;
  351. case 'customize' :
  352. $caps[] = 'edit_theme_options';
  353. break;
  354. case 'delete_site':
  355. $caps[] = 'manage_options';
  356. break;
  357. default:
  358. // Handle meta capabilities for custom post types.
  359. global $post_type_meta_caps;
  360. if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  361. $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  362. return call_user_func_array( 'map_meta_cap', $args );
  363. }
  364. // If no meta caps match, return the original cap.
  365. $caps[] = $cap;
  366. }
  367. /**
  368. * Filter a user's capabilities depending on specific context and/or privilege.
  369. *
  370. * @since 2.8.0
  371. *
  372. * @param array $caps Returns the user's actual capabilities.
  373. * @param string $cap Capability name.
  374. * @param int $user_id The user ID.
  375. * @param array $args Adds the context to the cap. Typically the object ID.
  376. */
  377. return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
  378. }
  379. /**
  380. * Whether the current user has a specific capability.
  381. *
  382. * While checking against particular roles in place of a capability is supported
  383. * in part, this practice is discouraged as it may produce unreliable results.
  384. *
  385. * Note: Will always return true if the current user is a super admin, unless specifically denied.
  386. *
  387. * @since 2.0.0
  388. *
  389. * @see WP_User::has_cap()
  390. * @see map_meta_cap()
  391. *
  392. * @param string $capability Capability name.
  393. * @param int $object_id Optional. ID of the specific object to check against if `$capability` is a "meta" cap.
  394. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  395. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  396. * 'edit_others_posts', etc. Accessed via func_get_args() and passed to WP_User::has_cap(),
  397. * then map_meta_cap().
  398. * @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
  399. * passed, whether the current user has the given meta capability for the given object.
  400. */
  401. function current_user_can( $capability ) {
  402. $current_user = wp_get_current_user();
  403. if ( empty( $current_user ) )
  404. return false;
  405. $args = array_slice( func_get_args(), 1 );
  406. $args = array_merge( array( $capability ), $args );
  407. return call_user_func_array( array( $current_user, 'has_cap' ), $args );
  408. }
  409. /**
  410. * Whether current user has a capability or role for a given site.
  411. *
  412. * @since 3.0.0
  413. *
  414. * @param int $blog_id Site ID.
  415. * @param string $capability Capability or role name.
  416. * @return bool
  417. */
  418. function current_user_can_for_blog( $blog_id, $capability ) {
  419. $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
  420. $current_user = wp_get_current_user();
  421. if ( empty( $current_user ) ) {
  422. if ( $switched ) {
  423. restore_current_blog();
  424. }
  425. return false;
  426. }
  427. $args = array_slice( func_get_args(), 2 );
  428. $args = array_merge( array( $capability ), $args );
  429. $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
  430. if ( $switched ) {
  431. restore_current_blog();
  432. }
  433. return $can;
  434. }
  435. /**
  436. * Whether author of supplied post has capability or role.
  437. *
  438. * @since 2.9.0
  439. *
  440. * @param int|object $post Post ID or post object.
  441. * @param string $capability Capability or role name.
  442. * @return bool
  443. */
  444. function author_can( $post, $capability ) {
  445. if ( !$post = get_post($post) )
  446. return false;
  447. $author = get_userdata( $post->post_author );
  448. if ( ! $author )
  449. return false;
  450. $args = array_slice( func_get_args(), 2 );
  451. $args = array_merge( array( $capability ), $args );
  452. return call_user_func_array( array( $author, 'has_cap' ), $args );
  453. }
  454. /**
  455. * Whether a particular user has capability or role.
  456. *
  457. * @since 3.1.0
  458. *
  459. * @param int|object $user User ID or object.
  460. * @param string $capability Capability or role name.
  461. * @return bool
  462. */
  463. function user_can( $user, $capability ) {
  464. if ( ! is_object( $user ) )
  465. $user = get_userdata( $user );
  466. if ( ! $user || ! $user->exists() )
  467. return false;
  468. $args = array_slice( func_get_args(), 2 );
  469. $args = array_merge( array( $capability ), $args );
  470. return call_user_func_array( array( $user, 'has_cap' ), $args );
  471. }
  472. /**
  473. * Retrieves the global WP_Roles instance and instantiates it if necessary.
  474. *
  475. * @since 4.3.0
  476. *
  477. * @global WP_Roles $wp_roles WP_Roles global instance.
  478. *
  479. * @return WP_Roles WP_Roles global instance if not already instantiated.
  480. */
  481. function wp_roles() {
  482. global $wp_roles;
  483. if ( ! isset( $wp_roles ) ) {
  484. $wp_roles = new WP_Roles();
  485. }
  486. return $wp_roles;
  487. }
  488. /**
  489. * Retrieve role object.
  490. *
  491. * @since 2.0.0
  492. *
  493. * @param string $role Role name.
  494. * @return WP_Role|null WP_Role object if found, null if the role does not exist.
  495. */
  496. function get_role( $role ) {
  497. return wp_roles()->get_role( $role );
  498. }
  499. /**
  500. * Add role, if it does not exist.
  501. *
  502. * @since 2.0.0
  503. *
  504. * @param string $role Role name.
  505. * @param string $display_name Display name for role.
  506. * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
  507. * @return WP_Role|null WP_Role object if role is added, null if already exists.
  508. */
  509. function add_role( $role, $display_name, $capabilities = array() ) {
  510. if ( empty( $role ) ) {
  511. return;
  512. }
  513. return wp_roles()->add_role( $role, $display_name, $capabilities );
  514. }
  515. /**
  516. * Remove role, if it exists.
  517. *
  518. * @since 2.0.0
  519. *
  520. * @param string $role Role name.
  521. */
  522. function remove_role( $role ) {
  523. wp_roles()->remove_role( $role );
  524. }
  525. /**
  526. * Retrieve a list of super admins.
  527. *
  528. * @since 3.0.0
  529. *
  530. * @global array $super_admins
  531. *
  532. * @return array List of super admin logins
  533. */
  534. function get_super_admins() {
  535. global $super_admins;
  536. if ( isset($super_admins) )
  537. return $super_admins;
  538. else
  539. return get_site_option( 'site_admins', array('admin') );
  540. }
  541. /**
  542. * Determine if user is a site admin.
  543. *
  544. * @since 3.0.0
  545. *
  546. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  547. * @return bool True if the user is a site admin.
  548. */
  549. function is_super_admin( $user_id = false ) {
  550. if ( ! $user_id || $user_id == get_current_user_id() )
  551. $user = wp_get_current_user();
  552. else
  553. $user = get_userdata( $user_id );
  554. if ( ! $user || ! $user->exists() )
  555. return false;
  556. if ( is_multisite() ) {
  557. $super_admins = get_super_admins();
  558. if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
  559. return true;
  560. } else {
  561. if ( $user->has_cap('delete_users') )
  562. return true;
  563. }
  564. return false;
  565. }