WC_POS_Admin_Settings_Access

Administrative Tools.

Defined (1)

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

/includes/admin/settings/class-wc-pos-access.php  
  1. class WC_POS_Admin_Settings_Access extends WC_POS_Admin_Settings_Abstract { 
  2.  
  3. protected static $instance; 
  4.  
  5. /** 
  6. * Each settings tab requires an id and label 
  7. */ 
  8. public function __construct() { 
  9. if(!current_user_can('promote_users')) { 
  10. $this->current_user_authorized = false; 
  11.  
  12. $this->id = 'access'; 
  13. $this->label = __( 'POS Access', 'woocommerce-pos' ); 
  14.  
  15. // capabilities 
  16. $this->caps = apply_filters('woocommerce_pos_capabilities', array( 
  17. 'pos' => array( 
  18. 'manage_woocommerce_pos', // pos admin 
  19. 'access_woocommerce_pos' // pos frontend 
  20. ),  
  21. 'woo' => array( 
  22. 'read_private_products',  
  23. 'read_private_shop_orders',  
  24. 'publish_shop_orders',  
  25. 'list_users' 
  26. ),  
  27. )); 
  28.  
  29. /** 
  30. * @param bool $key 
  31. * @return array 
  32. */ 
  33. public function get($key = false) { 
  34. return array( 
  35. 'roles' => $this->get_role_caps() 
  36. ); 
  37.  
  38. /** 
  39. * Get: Loop through roles and capabilities 
  40. * @return array 
  41. */ 
  42. private function get_role_caps() { 
  43. global $wp_roles; 
  44. $role_caps = array(); 
  45.  
  46. $roles = $wp_roles->roles; 
  47. if($roles): foreach($roles as $slug => $role): 
  48. $role_caps[$slug] = array( 
  49. 'name' => $role['name'],  
  50. 'capabilities' => array( 
  51. 'pos' => array_intersect_key( 
  52. $role['capabilities'],  
  53. array_flip($this->caps['pos']) 
  54. ),  
  55. 'woo' => array_intersect_key( 
  56. $role['capabilities'],  
  57. array_flip($this->caps['woo']) 
  58. ); 
  59. endforeach; endif; 
  60.  
  61. return $role_caps; 
  62.  
  63. /** 
  64. * @param array $data 
  65. * @return array 
  66. */ 
  67. public function set( array $data ) { 
  68. if(isset($data['roles'])) { 
  69. $this->update_capabilities($data['roles']); 
  70. return $this->get(); 
  71.  
  72. /** 
  73. * Set: Loop through roles and capabilities 
  74. * @param array $roles 
  75. */ 
  76. private function update_capabilities( array $roles ) { 
  77. foreach($roles as $slug => $array): 
  78.  
  79. $role = get_role($slug); 
  80.  
  81. if( $array['capabilities'] ) : foreach( $array['capabilities'] as $key => $caps ): 
  82. if( $caps ): foreach( $caps as $cap => $grant ): 
  83. if( in_array( $cap, $this->caps[$key] ) ) { 
  84. $grant ? $role->add_cap($cap) : $role->remove_cap($cap); 
  85. endforeach; endif; 
  86. endforeach; endif; 
  87.  
  88. endforeach; 
  89.  
  90. /** 
  91. * Delete: Loop through roles and capabilities 
  92. * - add for administrator/shop_manager 
  93. * - remove for everyone else 
  94. * @return bool|mixed|void 
  95. */ 
  96. public function delete() { 
  97. global $wp_roles; 
  98.  
  99. $roles = $wp_roles->roles; 
  100. $caps = array_merge( $this->caps['pos'], $this->caps['woo'] ); 
  101.  
  102. if( $roles ): foreach( $roles as $slug => $array ): 
  103. $role = get_role($slug); 
  104. $action = ( $slug == 'administrator' || $slug == 'shop_manager' ) ? 'add_cap' : 'remove_cap'; 
  105. if( $caps ): foreach( $caps as $cap ): 
  106. $role->$action($cap); 
  107. endforeach; endif; 
  108. endforeach; endif; 
  109.  
  110. return $this->get(); 
  111.