Pods_Roles

The Pods - Custom Content Types and Fields Pods Roles class.

Defined (1)

The class is defined in the following location(s).

/components/Roles/Roles.php  
  1. class Pods_Roles extends PodsComponent { 
  2.  
  3. /** 
  4. * Do things like register/enqueue scripts and stylesheets 
  5. * @since 2.0 
  6. */ 
  7. public function __construct () { 
  8. add_filter( 'pods_roles_get_capabilities', array( $this, 'remove_deprecated_capabilities' ) ); 
  9.  
  10. /** 
  11. * Enqueue styles 
  12. * @since 2.0 
  13. */ 
  14. public function admin_assets () { 
  15. wp_enqueue_style( 'pods-wizard' ); 
  16.  
  17. /** 
  18. * Build admin area 
  19. * @param $options 
  20. * @param $component 
  21. * @return void 
  22. * @since 2.0 
  23. */ 
  24. public function admin ( $options, $component ) { 
  25. global $wp_roles; 
  26.  
  27. // Hook into Gravity Forms roles (since it only adds filter if Members plugin itself is activated 
  28. if ( class_exists( 'RGForms' ) && !has_filter( 'members_get_capabilities', array( 'RGForms', 'members_get_capabilities' ) ) ) 
  29. add_filter( 'members_get_capabilities', array( 'RGForms', 'members_get_capabilities' ) ); 
  30.  
  31. $default_role = get_option( 'default_role' ); 
  32.  
  33. $roles = array(); 
  34.  
  35. foreach ( $wp_roles->role_objects as $key => $role ) { 
  36. $count = $this->count_users( $key ); 
  37.  
  38. $roles[ $key ] = array( 
  39. 'id' => $key,  
  40. 'label' => $wp_roles->role_names[ $key ],  
  41. 'name' => $key,  
  42. 'capabilities' => count( (array) $role->capabilities ),  
  43. 'users' => sprintf( _n( '%s User', '%s Users', $count, 'pods' ), $count ) 
  44. ); 
  45.  
  46. if ( $default_role == $key ) 
  47. $roles[ $key ][ 'label' ] .= ' (site default)'; 
  48.  
  49. if ( 0 < $count && pods_is_admin( array( 'list_users' ) ) ) { 
  50. $roles[ $key ][ 'users' ] .= '<br /><a href="' 
  51. . admin_url( esc_url( 'users.php?role=' . $key ) ) . '">' 
  52. . __( 'View Users', 'pods' ) . '</a>'; 
  53.  
  54. $ui = array( 
  55. 'component' => $component,  
  56. 'data' => $roles,  
  57. 'total' => count( $roles ),  
  58. 'total_found' => count( $roles ),  
  59. 'items' => 'Roles',  
  60. 'item' => 'Role',  
  61. 'fields' => array( 
  62. 'manage' => array( 
  63. 'label' => array( 'label' => __( 'Label', 'pods' ) ),  
  64. 'name' => array( 'label' => __( 'Name', 'pods' ) ),  
  65. 'capabilities' => array( 'label' => __( 'Capabilities', 'pods' ) ),  
  66. 'users' => array( 
  67. 'label' => __( 'Users', 'pods' ),  
  68. 'type' => 'text',  
  69. 'options' => array( 
  70. 'text_allow_html' => 1,  
  71. 'text_allowed_html_tags' => '' 
  72. ),  
  73. 'actions_disabled' => array( 'duplicate', 'view', 'export' ),  
  74. 'actions_custom' => array( 
  75. 'add' => array( $this, 'admin_add' ),  
  76. 'edit' => array( $this, 'admin_edit' ),  
  77. 'delete' => array( $this, 'admin_delete' ) 
  78. ),  
  79. 'search' => false,  
  80. 'searchable' => false,  
  81. 'sortable' => false,  
  82. 'pagination' => false 
  83. ); 
  84.  
  85. if ( isset( $roles[ pods_var( 'id', 'get', -1 ) ] ) ) 
  86. $ui[ 'row' ] = $roles[ pods_var( 'id', 'get', -1 ) ]; 
  87.  
  88. if ( !pods_is_admin( array( 'pods_roles_add' ) ) ) 
  89. $ui[ 'actions_disabled' ][] = 'add'; 
  90.  
  91. if ( !pods_is_admin( array( 'pods_roles_edit' ) ) ) 
  92. $ui[ 'actions_disabled' ][] = 'edit'; 
  93.  
  94. if ( count( $roles ) < 2 || !pods_is_admin( array( 'pods_roles_delete' ) ) ) 
  95. $ui[ 'actions_disabled' ][] = 'delete'; 
  96.  
  97. pods_ui( $ui ); 
  98.  
  99. function admin_add ( $obj ) { 
  100. global $wp_roles; 
  101.  
  102. $capabilities = $this->get_capabilities(); 
  103.  
  104. $defaults = $this->get_default_capabilities(); 
  105.  
  106. $component = $obj->x[ 'component' ]; 
  107.  
  108. $method = 'add'; // ajax_add 
  109.  
  110. pods_view( PODS_DIR . 'components/Roles/ui/add.php', compact( array_keys( get_defined_vars() ) ) ); 
  111.  
  112. function admin_edit ( $duplicate, $obj ) { 
  113. global $wp_roles; 
  114.  
  115. $id = $obj->id; 
  116.  
  117. $capabilities = $this->get_capabilities(); 
  118.  
  119. $role_name = $role_label = $role_capabilities = null; 
  120.  
  121. foreach ( $wp_roles->role_objects as $key => $role ) { 
  122. if ( $key != $id ) 
  123. continue; 
  124.  
  125. $role_name = $key; 
  126. $role_label = $wp_roles->role_names[ $key ]; 
  127. $role_capabilities = $role->capabilities; 
  128.  
  129. if ( empty( $role ) ) 
  130. return $obj->error( __( 'Role not found, cannot edit it.', 'pods' ) ); 
  131.  
  132. $component = $obj->x[ 'component' ]; 
  133.  
  134. $method = 'edit'; // ajax_edit 
  135.  
  136. pods_view( PODS_DIR . 'components/Roles/ui/edit.php', compact( array_keys( get_defined_vars() ) ) ); 
  137.  
  138. function admin_delete ( $id, $obj ) { 
  139. global $wp_roles; 
  140.  
  141. $id = $obj->id; 
  142.  
  143. if ( !isset( $obj->data[ $id ] ) ) 
  144. return $obj->error( __( 'Role not found, it cannot be deleted.', 'pods' ) ); 
  145.  
  146. $default_role = get_option( 'default_role' ); 
  147.  
  148. if ( $id == $default_role ) { 
  149. return $obj->error( sprintf( __( 'You cannot remove the <strong>%s</strong> role, you must set a new default role for the site first.', 'pods' ), $obj->data[ $id ][ 'name' ] ) ); 
  150.  
  151. $wp_user_search = new WP_User_Search( '', '', $id ); 
  152.  
  153. $users = $wp_user_search->get_results(); 
  154.  
  155. if ( !empty( $users ) && is_array( $users ) ) { 
  156. foreach ( $users as $user ) { 
  157. $user_object = new WP_User( $user ); 
  158.  
  159. if ( $user_object->has_cap( $id ) ) { 
  160. $user_object->remove_role( $id ); 
  161. $user_object->set_role( $default_role ); 
  162.  
  163. remove_role( $id ); 
  164.  
  165. $roles = array(); 
  166.  
  167. foreach ( $wp_roles->role_objects as $key => $role ) { 
  168. $count = $this->count_users( $key ); 
  169.  
  170. $roles[ $key ] = array( 
  171. 'id' => $key,  
  172. 'label' => $wp_roles->role_names[ $key ],  
  173. 'name' => $key,  
  174. 'capabilities' => count( (array) $role->capabilities ),  
  175. 'users' => sprintf( _n( '%s User', '%s Users', $count, 'pods' ), $count ) 
  176. ); 
  177.  
  178. if ( $default_role == $key ) 
  179. $roles[ $key ][ 'label' ] .= ' (site default)'; 
  180.  
  181. if ( 0 < $count && pods_is_admin( array( 'list_users' ) ) ) { 
  182. $roles[ $key ][ 'users' ] .= '<br /><a href="' 
  183. . admin_url( esc_url( 'users.php?role=' . $key ) ) . '">' 
  184. . __( 'View Users', 'pods' ) . '</a>'; 
  185.  
  186. $name = $obj->data[ $id ][ 'label' ] . ' (' . $obj->data[ $id ][ 'name' ] . ')'; 
  187.  
  188. $obj->data = $roles; 
  189. $obj->total = count( $roles ); 
  190. $obj->total_found = count( $roles ); 
  191.  
  192. $obj->message( '<strong>' . $name . '</strong> ' . __( 'role removed from site.', 'pods' ) ); 
  193.  
  194. /** 
  195. * Handle the Add Role AJAX 
  196. * @param $params 
  197. * @return mixed|void 
  198. */ 
  199. public function ajax_add ( $params ) { 
  200. global $wp_roles; 
  201.  
  202. $role_name = pods_var_raw( 'role_name', $params ); 
  203. $role_label = pods_var_raw( 'role_label', $params ); 
  204.  
  205. $params->capabilities = (array) pods_var_raw( 'capabilities', $params, array() ); 
  206.  
  207. $params->custom_capabilities = (array) pods_var_raw( 'custom_capabilities', $params, array() ); 
  208. $params->custom_capabilities = array_filter( array_unique( $params->custom_capabilities ) ); 
  209.  
  210. $capabilities = array(); 
  211.  
  212. foreach ( $params->capabilities as $capability => $x ) { 
  213. if ( empty( $capability ) || true !== (boolean) $x ) 
  214. continue; 
  215.  
  216. $capabilities[ esc_attr( $capability ) ] = true; 
  217.  
  218. foreach ( $params->custom_capabilities as $x => $capability ) { 
  219. if ( empty( $capability ) || '--1' == $x ) 
  220. continue; 
  221.  
  222. $capabilities[ esc_attr( $capability ) ] = true; 
  223.  
  224. if ( empty( $role_name ) ) 
  225. return pods_error( __( 'Role name is required', 'pods' ) ); 
  226.  
  227. if ( empty( $role_label ) ) 
  228. return pods_error( __( 'Role label is required', 'pods' ) ); 
  229.  
  230. return add_role( $role_name, $role_label, $capabilities ); 
  231.  
  232. /** 
  233. * Handle the Edit Role AJAX 
  234. * @todo allow rename role_label 
  235. * @param $params 
  236. * @return bool|mixed|void 
  237. */ 
  238. public function ajax_edit ( $params ) { 
  239. global $wp_roles; 
  240.  
  241. $capabilities = $this->get_capabilities(); 
  242.  
  243. $params->capabilities = (array) pods_var_raw( 'capabilities', $params, array() ); 
  244.  
  245. $params->custom_capabilities = (array) pods_var_raw( 'custom_capabilities', $params, array() ); 
  246. $params->custom_capabilities = array_filter( array_unique( $params->custom_capabilities ) ); 
  247.  
  248. if ( !isset( $params->id ) || empty( $params->id ) || !isset( $wp_roles->role_objects[ $params->id ] ) ) 
  249. return pods_error( __( 'Role not found, cannot edit it.', 'pods' ) ); 
  250.  
  251. /** 
  252. * @var $role WP_Role 
  253. */ 
  254. $role = $wp_roles->role_objects[ $params->id ]; 
  255. $role_name = $params->id; 
  256. $role_label = $wp_roles->role_names[ $params->id ]; 
  257. $role_capabilities = $role->capabilities; 
  258.  
  259. $new_capabilities = array(); 
  260.  
  261. foreach ( $params->capabilities as $capability => $x ) { 
  262. if ( empty( $capability ) || true !== (boolean) $x ) 
  263. continue; 
  264.  
  265. $new_capabilities[] = esc_attr( $capability ); 
  266.  
  267. if ( !$role->has_cap( $capability ) ) 
  268. $role->add_cap( $capability ); 
  269.  
  270. foreach ( $params->custom_capabilities as $x => $capability ) { 
  271. if ( empty( $capability ) ) 
  272. continue; 
  273.  
  274. if ( in_array( $capability, $new_capabilities ) ) 
  275. continue; 
  276.  
  277. $new_capabilities[] = esc_attr( $capability ); 
  278.  
  279. if ( !$role->has_cap( $capability ) ) 
  280. $role->add_cap( $capability ); 
  281.  
  282. foreach ( $role_capabilities as $capability => $x ) { 
  283. if ( !in_array( $capability, $new_capabilities ) && false === strpos( $capability, 'level_' ) ) 
  284. $role->remove_cap( $capability ); 
  285.  
  286. return true; 
  287.  
  288. /** 
  289. * Basic logic from Members plugin, it counts users of a specific role 
  290. * @param $role 
  291. * @return array 
  292. */ 
  293. function count_users ( $role ) { 
  294. $count_users = count_users(); 
  295.  
  296. $avail_roles = array(); 
  297.  
  298. foreach ( $count_users[ 'avail_roles' ] as $count_role => $count ) { 
  299. $avail_roles[ $count_role ] = $count; 
  300.  
  301. if ( empty( $role ) ) 
  302. return $avail_roles; 
  303.  
  304. if ( !isset( $avail_roles[ $role ] ) ) 
  305. $avail_roles[ $role ] = 0; 
  306.  
  307. return $avail_roles[ $role ]; 
  308.  
  309. function get_capabilities () { 
  310. global $wp_roles; 
  311.  
  312. $default_caps = $this->get_wp_capabilities(); 
  313.  
  314. $role_caps = array(); 
  315.  
  316. foreach ( $wp_roles->role_objects as $key => $role ) { 
  317. if ( is_array( $role->capabilities ) ) { 
  318. foreach ( $role->capabilities as $cap => $grant ) { 
  319. $role_caps[ $cap ] = $cap; 
  320.  
  321. $role_caps = array_unique( $role_caps ); 
  322.  
  323. $plugin_caps = array( 
  324. 'pods_roles_add',  
  325. 'pods_roles_delete',  
  326. 'pods_roles_edit' 
  327. ); 
  328.  
  329. $capabilities = array_merge( $default_caps, $role_caps, $plugin_caps ); 
  330.  
  331. // To support Members filters 
  332. $capabilities = apply_filters( 'members_get_capabilities', $capabilities ); 
  333.  
  334. $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities ); 
  335.  
  336. sort( $capabilities ); 
  337.  
  338. $capabilities = array_unique( $capabilities ); 
  339.  
  340. return $capabilities; 
  341.  
  342. function get_wp_capabilities () { 
  343. $defaults = array( 
  344. 'activate_plugins',  
  345. 'add_users',  
  346. 'create_users',  
  347. 'delete_others_pages',  
  348. 'delete_others_posts',  
  349. 'delete_pages',  
  350. 'delete_plugins',  
  351. 'delete_posts',  
  352. 'delete_private_pages',  
  353. 'delete_private_posts',  
  354. 'delete_published_pages',  
  355. 'delete_published_posts',  
  356. 'delete_users',  
  357. 'edit_dashboard',  
  358. 'edit_files',  
  359. 'edit_others_pages',  
  360. 'edit_others_posts',  
  361. 'edit_pages',  
  362. 'edit_plugins',  
  363. 'edit_posts',  
  364. 'edit_private_pages',  
  365. 'edit_private_posts',  
  366. 'edit_published_pages',  
  367. 'edit_published_posts',  
  368. 'edit_theme_options',  
  369. 'edit_themes',  
  370. 'edit_users',  
  371. 'import',  
  372. 'install_plugins',  
  373. 'install_themes',  
  374. 'list_users',  
  375. 'manage_categories',  
  376. 'manage_links',  
  377. 'manage_options',  
  378. 'moderate_comments',  
  379. 'promote_users',  
  380. 'publish_pages',  
  381. 'publish_posts',  
  382. 'read',  
  383. 'read_private_pages',  
  384. 'read_private_posts',  
  385. 'remove_users',  
  386. 'switch_themes',  
  387. 'unfiltered_html',  
  388. 'unfiltered_upload',  
  389. 'update_core',  
  390. 'update_plugins',  
  391. 'update_themes',  
  392. 'upload_files' 
  393. ); 
  394.  
  395. return $defaults; 
  396.  
  397. function get_default_capabilities () { 
  398. $capabilities = array( 
  399. 'read' 
  400. ); 
  401.  
  402. // To support Members filters 
  403. $capabilities = apply_filters( 'members_new_role_default_capabilities', $capabilities ); 
  404.  
  405. $capabilities = apply_filters( 'pods_roles_default_capabilities', $capabilities ); 
  406.  
  407. return $capabilities; 
  408.  
  409. function remove_deprecated_capabilities ( $capabilities ) { 
  410. $deprecated_capabilities = array( 
  411. 'level_0',  
  412. 'level_1',  
  413. 'level_2',  
  414. 'level_3',  
  415. 'level_4',  
  416. 'level_5',  
  417. 'level_6',  
  418. 'level_7',  
  419. 'level_8',  
  420. 'level_9',  
  421. 'level_10' 
  422. ); 
  423.  
  424. $capabilities = array_diff( $capabilities, $deprecated_capabilities ); 
  425.  
  426. return $capabilities;