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

812 lines
23 KiB

  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. if ( ( get_option( 'page_for_posts' ) == $post->ID ) || ( get_option( 'page_on_front' ) == $post->ID ) ) {
  66. $caps[] = 'manage_options';
  67. break;
  68. }
  69. $post_type = get_post_type_object( $post->post_type );
  70. if ( ! $post_type ) {
  71. /* translators: 1: post type, 2: capability name */
  72. _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' );
  73. $caps[] = 'edit_others_posts';
  74. break;
  75. }
  76. if ( ! $post_type->map_meta_cap ) {
  77. $caps[] = $post_type->cap->$cap;
  78. // Prior to 3.1 we would re-call map_meta_cap here.
  79. if ( 'delete_post' == $cap )
  80. $cap = $post_type->cap->$cap;
  81. break;
  82. }
  83. // If the post author is set and the user is the author...
  84. if ( $post->post_author && $user_id == $post->post_author ) {
  85. // If the post is published or scheduled...
  86. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  87. $caps[] = $post_type->cap->delete_published_posts;
  88. } elseif ( 'trash' == $post->post_status ) {
  89. $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
  90. if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
  91. $caps[] = $post_type->cap->delete_published_posts;
  92. } else {
  93. $caps[] = $post_type->cap->delete_posts;
  94. }
  95. } else {
  96. // If the post is draft...
  97. $caps[] = $post_type->cap->delete_posts;
  98. }
  99. } else {
  100. // The user is trying to edit someone else's post.
  101. $caps[] = $post_type->cap->delete_others_posts;
  102. // The post is published or scheduled, extra cap required.
  103. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  104. $caps[] = $post_type->cap->delete_published_posts;
  105. } elseif ( 'private' == $post->post_status ) {
  106. $caps[] = $post_type->cap->delete_private_posts;
  107. }
  108. }
  109. break;
  110. // edit_post breaks down to edit_posts, edit_published_posts, or
  111. // edit_others_posts
  112. case 'edit_post':
  113. case 'edit_page':
  114. $post = get_post( $args[0] );
  115. if ( ! $post ) {
  116. $caps[] = 'do_not_allow';
  117. break;
  118. }
  119. if ( 'revision' == $post->post_type ) {
  120. $post = get_post( $post->post_parent );
  121. if ( ! $post ) {
  122. $caps[] = 'do_not_allow';
  123. break;
  124. }
  125. }
  126. $post_type = get_post_type_object( $post->post_type );
  127. if ( ! $post_type ) {
  128. /* translators: 1: post type, 2: capability name */
  129. _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' );
  130. $caps[] = 'edit_others_posts';
  131. break;
  132. }
  133. if ( ! $post_type->map_meta_cap ) {
  134. $caps[] = $post_type->cap->$cap;
  135. // Prior to 3.1 we would re-call map_meta_cap here.
  136. if ( 'edit_post' == $cap )
  137. $cap = $post_type->cap->$cap;
  138. break;
  139. }
  140. // If the post author is set and the user is the author...
  141. if ( $post->post_author && $user_id == $post->post_author ) {
  142. // If the post is published or scheduled...
  143. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  144. $caps[] = $post_type->cap->edit_published_posts;
  145. } elseif ( 'trash' == $post->post_status ) {
  146. $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true );
  147. if ( in_array( $status, array( 'publish', 'future' ), true ) ) {
  148. $caps[] = $post_type->cap->edit_published_posts;
  149. } else {
  150. $caps[] = $post_type->cap->edit_posts;
  151. }
  152. } else {
  153. // If the post is draft...
  154. $caps[] = $post_type->cap->edit_posts;
  155. }
  156. } else {
  157. // The user is trying to edit someone else's post.
  158. $caps[] = $post_type->cap->edit_others_posts;
  159. // The post is published or scheduled, extra cap required.
  160. if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) {
  161. $caps[] = $post_type->cap->edit_published_posts;
  162. } elseif ( 'private' == $post->post_status ) {
  163. $caps[] = $post_type->cap->edit_private_posts;
  164. }
  165. }
  166. break;
  167. case 'read_post':
  168. case 'read_page':
  169. $post = get_post( $args[0] );
  170. if ( ! $post ) {
  171. $caps[] = 'do_not_allow';
  172. break;
  173. }
  174. if ( 'revision' == $post->post_type ) {
  175. $post = get_post( $post->post_parent );
  176. if ( ! $post ) {
  177. $caps[] = 'do_not_allow';
  178. break;
  179. }
  180. }
  181. $post_type = get_post_type_object( $post->post_type );
  182. if ( ! $post_type ) {
  183. /* translators: 1: post type, 2: capability name */
  184. _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' );
  185. $caps[] = 'edit_others_posts';
  186. break;
  187. }
  188. if ( ! $post_type->map_meta_cap ) {
  189. $caps[] = $post_type->cap->$cap;
  190. // Prior to 3.1 we would re-call map_meta_cap here.
  191. if ( 'read_post' == $cap )
  192. $cap = $post_type->cap->$cap;
  193. break;
  194. }
  195. $status_obj = get_post_status_object( $post->post_status );
  196. if ( $status_obj->public ) {
  197. $caps[] = $post_type->cap->read;
  198. break;
  199. }
  200. if ( $post->post_author && $user_id == $post->post_author ) {
  201. $caps[] = $post_type->cap->read;
  202. } elseif ( $status_obj->private ) {
  203. $caps[] = $post_type->cap->read_private_posts;
  204. } else {
  205. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  206. }
  207. break;
  208. case 'publish_post':
  209. $post = get_post( $args[0] );
  210. if ( ! $post ) {
  211. $caps[] = 'do_not_allow';
  212. break;
  213. }
  214. $post_type = get_post_type_object( $post->post_type );
  215. if ( ! $post_type ) {
  216. /* translators: 1: post type, 2: capability name */
  217. _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' );
  218. $caps[] = 'edit_others_posts';
  219. break;
  220. }
  221. $caps[] = $post_type->cap->publish_posts;
  222. break;
  223. case 'edit_post_meta':
  224. case 'delete_post_meta':
  225. case 'add_post_meta':
  226. case 'edit_comment_meta':
  227. case 'delete_comment_meta':
  228. case 'add_comment_meta':
  229. case 'edit_term_meta':
  230. case 'delete_term_meta':
  231. case 'add_term_meta':
  232. case 'edit_user_meta':
  233. case 'delete_user_meta':
  234. case 'add_user_meta':
  235. list( $_, $object_type, $_ ) = explode( '_', $cap );
  236. $object_id = (int) $args[0];
  237. switch ( $object_type ) {
  238. case 'post':
  239. $post = get_post( $object_id );
  240. if ( ! $post ) {
  241. break;
  242. }
  243. $sub_type = get_post_type( $post );
  244. break;
  245. case 'comment':
  246. $comment = get_comment( $object_id );
  247. if ( ! $comment ) {
  248. break;
  249. }
  250. $sub_type = empty( $comment->comment_type ) ? 'comment' : $comment->comment_type;
  251. break;
  252. case 'term':
  253. $term = get_term( $object_id );
  254. if ( ! $term ) {
  255. break;
  256. }
  257. $sub_type = $term->taxonomy;
  258. break;
  259. case 'user':
  260. $user = get_user_by( 'id', $object_id );
  261. if ( ! $user ) {
  262. break;
  263. }
  264. $sub_type = 'user';
  265. break;
  266. }
  267. if ( empty( $sub_type ) ) {
  268. $caps[] = 'do_not_allow';
  269. break;
  270. }
  271. $caps = map_meta_cap( "edit_{$object_type}", $user_id, $object_id );
  272. $meta_key = isset( $args[1] ) ? $args[1] : false;
  273. $has_filter = has_filter( "auth_{$object_type}_meta_{$meta_key}" ) || has_filter( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}" );
  274. if ( $meta_key && $has_filter ) {
  275. /** This filter is documented in wp-includes/meta.php */
  276. $allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", false, $meta_key, $object_id, $user_id, $cap, $caps );
  277. /** This filter is documented in wp-includes/meta.php */
  278. $allowed = apply_filters( "auth_{$object_type}_{$sub_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps );
  279. if ( ! $allowed ) {
  280. $caps[] = $cap;
  281. }
  282. } elseif ( $meta_key && is_protected_meta( $meta_key, $object_type ) ) {
  283. $caps[] = $cap;
  284. }
  285. break;
  286. case 'edit_comment':
  287. $comment = get_comment( $args[0] );
  288. if ( ! $comment ) {
  289. $caps[] = 'do_not_allow';
  290. break;
  291. }
  292. $post = get_post( $comment->comment_post_ID );
  293. /*
  294. * If the post doesn't exist, we have an orphaned comment.
  295. * Fall back to the edit_posts capability, instead.
  296. */
  297. if ( $post ) {
  298. $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  299. } else {
  300. $caps = map_meta_cap( 'edit_posts', $user_id );
  301. }
  302. break;
  303. case 'unfiltered_upload':
  304. if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
  305. $caps[] = $cap;
  306. else
  307. $caps[] = 'do_not_allow';
  308. break;
  309. case 'edit_css' :
  310. case 'unfiltered_html' :
  311. // Disallow unfiltered_html for all users, even admins and super admins.
  312. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
  313. $caps[] = 'do_not_allow';
  314. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  315. $caps[] = 'do_not_allow';
  316. else
  317. $caps[] = 'unfiltered_html';
  318. break;
  319. case 'edit_files':
  320. case 'edit_plugins':
  321. case 'edit_themes':
  322. // Disallow the file editors.
  323. if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
  324. $caps[] = 'do_not_allow';
  325. elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  326. $caps[] = 'do_not_allow';
  327. elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  328. $caps[] = 'do_not_allow';
  329. else
  330. $caps[] = $cap;
  331. break;
  332. case 'update_plugins':
  333. case 'delete_plugins':
  334. case 'install_plugins':
  335. case 'upload_plugins':
  336. case 'update_themes':
  337. case 'delete_themes':
  338. case 'install_themes':
  339. case 'upload_themes':
  340. case 'update_core':
  341. // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
  342. // Files in uploads are excepted.
  343. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
  344. $caps[] = 'do_not_allow';
  345. } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
  346. $caps[] = 'do_not_allow';
  347. } elseif ( 'upload_themes' === $cap ) {
  348. $caps[] = 'install_themes';
  349. } elseif ( 'upload_plugins' === $cap ) {
  350. $caps[] = 'install_plugins';
  351. } else {
  352. $caps[] = $cap;
  353. }
  354. break;
  355. case 'activate_plugins':
  356. $caps[] = $cap;
  357. if ( is_multisite() ) {
  358. // update_, install_, and delete_ are handled above with is_super_admin().
  359. $menu_perms = get_site_option( 'menu_items', array() );
  360. if ( empty( $menu_perms['plugins'] ) )
  361. $caps[] = 'manage_network_plugins';
  362. }
  363. break;
  364. case 'delete_user':
  365. case 'delete_users':
  366. // If multisite only super admins can delete users.
  367. if ( is_multisite() && ! is_super_admin( $user_id ) )
  368. $caps[] = 'do_not_allow';
  369. else
  370. $caps[] = 'delete_users'; // delete_user maps to delete_users.
  371. break;
  372. case 'create_users':
  373. if ( !is_multisite() )
  374. $caps[] = $cap;
  375. elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
  376. $caps[] = $cap;
  377. else
  378. $caps[] = 'do_not_allow';
  379. break;
  380. case 'manage_links' :
  381. if ( get_option( 'link_manager_enabled' ) )
  382. $caps[] = $cap;
  383. else
  384. $caps[] = 'do_not_allow';
  385. break;
  386. case 'customize' :
  387. $caps[] = 'edit_theme_options';
  388. break;
  389. case 'delete_site':
  390. $caps[] = 'manage_options';
  391. break;
  392. case 'edit_term':
  393. case 'delete_term':
  394. case 'assign_term':
  395. $term_id = (int) $args[0];
  396. $term = get_term( $term_id );
  397. if ( ! $term || is_wp_error( $term ) ) {
  398. $caps[] = 'do_not_allow';
  399. break;
  400. }
  401. $tax = get_taxonomy( $term->taxonomy );
  402. if ( ! $tax ) {
  403. $caps[] = 'do_not_allow';
  404. break;
  405. }
  406. if ( 'delete_term' === $cap && ( $term->term_id == get_option( 'default_' . $term->taxonomy ) ) ) {
  407. $caps[] = 'do_not_allow';
  408. break;
  409. }
  410. $taxo_cap = $cap . 's';
  411. $caps = map_meta_cap( $tax->cap->$taxo_cap, $user_id, $term_id );
  412. break;
  413. case 'manage_post_tags':
  414. case 'edit_categories':
  415. case 'edit_post_tags':
  416. case 'delete_categories':
  417. case 'delete_post_tags':
  418. $caps[] = 'manage_categories';
  419. break;
  420. case 'assign_categories':
  421. case 'assign_post_tags':
  422. $caps[] = 'edit_posts';
  423. break;
  424. case 'create_sites':
  425. case 'delete_sites':
  426. case 'manage_network':
  427. case 'manage_sites':
  428. case 'manage_network_users':
  429. case 'manage_network_plugins':
  430. case 'manage_network_themes':
  431. case 'manage_network_options':
  432. $caps[] = $cap;
  433. break;
  434. default:
  435. // Handle meta capabilities for custom post types.
  436. global $post_type_meta_caps;
  437. if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  438. $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  439. return call_user_func_array( 'map_meta_cap', $args );
  440. }
  441. // If no meta caps match, return the original cap.
  442. $caps[] = $cap;
  443. }
  444. /**
  445. * Filters a user's capabilities depending on specific context and/or privilege.
  446. *
  447. * @since 2.8.0
  448. *
  449. * @param array $caps Returns the user's actual capabilities.
  450. * @param string $cap Capability name.
  451. * @param int $user_id The user ID.
  452. * @param array $args Adds the context to the cap. Typically the object ID.
  453. */
  454. return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
  455. }
  456. /**
  457. * Whether the current user has a specific capability.
  458. *
  459. * While checking against particular roles in place of a capability is supported
  460. * in part, this practice is discouraged as it may produce unreliable results.
  461. *
  462. * Note: Will always return true if the current user is a super admin, unless specifically denied.
  463. *
  464. * @since 2.0.0
  465. *
  466. * @see WP_User::has_cap()
  467. * @see map_meta_cap()
  468. *
  469. * @param string $capability Capability name.
  470. * @param int $object_id Optional. ID of the specific object to check against if `$capability` is a "meta" cap.
  471. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  472. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  473. * 'edit_others_posts', etc. Accessed via func_get_args() and passed to WP_User::has_cap(),
  474. * then map_meta_cap().
  475. * @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is
  476. * passed, whether the current user has the given meta capability for the given object.
  477. */
  478. function current_user_can( $capability ) {
  479. $current_user = wp_get_current_user();
  480. if ( empty( $current_user ) )
  481. return false;
  482. $args = array_slice( func_get_args(), 1 );
  483. $args = array_merge( array( $capability ), $args );
  484. return call_user_func_array( array( $current_user, 'has_cap' ), $args );
  485. }
  486. /**
  487. * Whether current user has a capability or role for a given site.
  488. *
  489. * @since 3.0.0
  490. *
  491. * @param int $blog_id Site ID.
  492. * @param string $capability Capability or role name.
  493. * @return bool
  494. */
  495. function current_user_can_for_blog( $blog_id, $capability ) {
  496. $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
  497. $current_user = wp_get_current_user();
  498. if ( empty( $current_user ) ) {
  499. if ( $switched ) {
  500. restore_current_blog();
  501. }
  502. return false;
  503. }
  504. $args = array_slice( func_get_args(), 2 );
  505. $args = array_merge( array( $capability ), $args );
  506. $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
  507. if ( $switched ) {
  508. restore_current_blog();
  509. }
  510. return $can;
  511. }
  512. /**
  513. * Whether author of supplied post has capability or role.
  514. *
  515. * @since 2.9.0
  516. *
  517. * @param int|object $post Post ID or post object.
  518. * @param string $capability Capability or role name.
  519. * @return bool
  520. */
  521. function author_can( $post, $capability ) {
  522. if ( !$post = get_post($post) )
  523. return false;
  524. $author = get_userdata( $post->post_author );
  525. if ( ! $author )
  526. return false;
  527. $args = array_slice( func_get_args(), 2 );
  528. $args = array_merge( array( $capability ), $args );
  529. return call_user_func_array( array( $author, 'has_cap' ), $args );
  530. }
  531. /**
  532. * Whether a particular user has capability or role.
  533. *
  534. * @since 3.1.0
  535. *
  536. * @param int|object $user User ID or object.
  537. * @param string $capability Capability or role name.
  538. * @return bool
  539. */
  540. function user_can( $user, $capability ) {
  541. if ( ! is_object( $user ) )
  542. $user = get_userdata( $user );
  543. if ( ! $user || ! $user->exists() )
  544. return false;
  545. $args = array_slice( func_get_args(), 2 );
  546. $args = array_merge( array( $capability ), $args );
  547. return call_user_func_array( array( $user, 'has_cap' ), $args );
  548. }
  549. /**
  550. * Retrieves the global WP_Roles instance and instantiates it if necessary.
  551. *
  552. * @since 4.3.0
  553. *
  554. * @global WP_Roles $wp_roles WP_Roles global instance.
  555. *
  556. * @return WP_Roles WP_Roles global instance if not already instantiated.
  557. */
  558. function wp_roles() {
  559. global $wp_roles;
  560. if ( ! isset( $wp_roles ) ) {
  561. $wp_roles = new WP_Roles();
  562. }
  563. return $wp_roles;
  564. }
  565. /**
  566. * Retrieve role object.
  567. *
  568. * @since 2.0.0
  569. *
  570. * @param string $role Role name.
  571. * @return WP_Role|null WP_Role object if found, null if the role does not exist.
  572. */
  573. function get_role( $role ) {
  574. return wp_roles()->get_role( $role );
  575. }
  576. /**
  577. * Add role, if it does not exist.
  578. *
  579. * @since 2.0.0
  580. *
  581. * @param string $role Role name.
  582. * @param string $display_name Display name for role.
  583. * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
  584. * @return WP_Role|null WP_Role object if role is added, null if already exists.
  585. */
  586. function add_role( $role, $display_name, $capabilities = array() ) {
  587. if ( empty( $role ) ) {
  588. return;
  589. }
  590. return wp_roles()->add_role( $role, $display_name, $capabilities );
  591. }
  592. /**
  593. * Remove role, if it exists.
  594. *
  595. * @since 2.0.0
  596. *
  597. * @param string $role Role name.
  598. */
  599. function remove_role( $role ) {
  600. wp_roles()->remove_role( $role );
  601. }
  602. /**
  603. * Retrieve a list of super admins.
  604. *
  605. * @since 3.0.0
  606. *
  607. * @global array $super_admins
  608. *
  609. * @return array List of super admin logins
  610. */
  611. function get_super_admins() {
  612. global $super_admins;
  613. if ( isset($super_admins) )
  614. return $super_admins;
  615. else
  616. return get_site_option( 'site_admins', array('admin') );
  617. }
  618. /**
  619. * Determine if user is a site admin.
  620. *
  621. * @since 3.0.0
  622. *
  623. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  624. * @return bool True if the user is a site admin.
  625. */
  626. function is_super_admin( $user_id = false ) {
  627. if ( ! $user_id || $user_id == get_current_user_id() )
  628. $user = wp_get_current_user();
  629. else
  630. $user = get_userdata( $user_id );
  631. if ( ! $user || ! $user->exists() )
  632. return false;
  633. if ( is_multisite() ) {
  634. $super_admins = get_super_admins();
  635. if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
  636. return true;
  637. } else {
  638. if ( $user->has_cap('delete_users') )
  639. return true;
  640. }
  641. return false;
  642. }
  643. /**
  644. * Grants Super Admin privileges.
  645. *
  646. * @since 3.0.0
  647. *
  648. * @global array $super_admins
  649. *
  650. * @param int $user_id ID of the user to be granted Super Admin privileges.
  651. * @return bool True on success, false on failure. This can fail when the user is
  652. * already a super admin or when the `$super_admins` global is defined.
  653. */
  654. function grant_super_admin( $user_id ) {
  655. // If global super_admins override is defined, there is nothing to do here.
  656. if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
  657. return false;
  658. }
  659. /**
  660. * Fires before the user is granted Super Admin privileges.
  661. *
  662. * @since 3.0.0
  663. *
  664. * @param int $user_id ID of the user that is about to be granted Super Admin privileges.
  665. */
  666. do_action( 'grant_super_admin', $user_id );
  667. // Directly fetch site_admins instead of using get_super_admins()
  668. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  669. $user = get_userdata( $user_id );
  670. if ( $user && ! in_array( $user->user_login, $super_admins ) ) {
  671. $super_admins[] = $user->user_login;
  672. update_site_option( 'site_admins' , $super_admins );
  673. /**
  674. * Fires after the user is granted Super Admin privileges.
  675. *
  676. * @since 3.0.0
  677. *
  678. * @param int $user_id ID of the user that was granted Super Admin privileges.
  679. */
  680. do_action( 'granted_super_admin', $user_id );
  681. return true;
  682. }
  683. return false;
  684. }
  685. /**
  686. * Revokes Super Admin privileges.
  687. *
  688. * @since 3.0.0
  689. *
  690. * @global array $super_admins
  691. *
  692. * @param int $user_id ID of the user Super Admin privileges to be revoked from.
  693. * @return bool True on success, false on failure. This can fail when the user's email
  694. * is the network admin email or when the `$super_admins` global is defined.
  695. */
  696. function revoke_super_admin( $user_id ) {
  697. // If global super_admins override is defined, there is nothing to do here.
  698. if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) {
  699. return false;
  700. }
  701. /**
  702. * Fires before the user's Super Admin privileges are revoked.
  703. *
  704. * @since 3.0.0
  705. *
  706. * @param int $user_id ID of the user Super Admin privileges are being revoked from.
  707. */
  708. do_action( 'revoke_super_admin', $user_id );
  709. // Directly fetch site_admins instead of using get_super_admins()
  710. $super_admins = get_site_option( 'site_admins', array( 'admin' ) );
  711. $user = get_userdata( $user_id );
  712. if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) {
  713. if ( false !== ( $key = array_search( $user->user_login, $super_admins ) ) ) {
  714. unset( $super_admins[$key] );
  715. update_site_option( 'site_admins', $super_admins );
  716. /**
  717. * Fires after the user's Super Admin privileges are revoked.
  718. *
  719. * @since 3.0.0
  720. *
  721. * @param int $user_id ID of the user Super Admin privileges were revoked from.
  722. */
  723. do_action( 'revoked_super_admin', $user_id );
  724. return true;
  725. }
  726. }
  727. return false;
  728. }