Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * Administrative Tools
  5  *
  6  * @class    WC_POS_Admin_Settings_Tools
  7  * @package  WooCommerce POS
  8  * @author   Paul Kilmurray <paul@kilbot.com.au>
  9  * @link     http://www.woopos.com.au
 10  */
 11 
 12 namespace WC_POS\Admin\Settings;
 13 
 14 class Access extends Page {
 15 
 16   protected static $instance;
 17 
 18   /**
 19    * Each settings tab requires an id and label
 20    */
 21   public function __construct() {
 22     if(!current_user_can('promote_users')){
 23       $this->current_user_authorized = false;
 24     }
 25 
 26     $this->id    = 'access';
 27     $this->label = __( 'POS Access', 'woocommerce-pos' );
 28 
 29     // capabilities
 30     $this->caps = apply_filters('woocommerce_pos_capabilities', array(
 31       'pos' => array(
 32         'manage_woocommerce_pos', // pos admin
 33         'access_woocommerce_pos'  // pos frontend
 34       ),
 35       'woo' => array(
 36         'read_private_products',
 37         'read_private_shop_orders',
 38         'publish_shop_orders',
 39         'list_users'
 40       ),
 41     ));
 42   }
 43 
 44   /**
 45    * @param bool $key
 46    * @return array
 47    */
 48   public function get($key = false){
 49     return array(
 50       'id' => $this->id,
 51       'roles' => $this->get_role_caps()
 52     );
 53   }
 54 
 55   /**
 56    * Get: Loop through roles and capabilities
 57    *
 58    * @return array
 59    */
 60   private function get_role_caps(){
 61     global $wp_roles;
 62     $role_caps = array();
 63 
 64     $roles = $wp_roles->roles;
 65     if($roles): foreach($roles as $slug => $role):
 66       $role_caps[$slug] = array(
 67         'name' => $role['name'],
 68         'capabilities' => array(
 69           'pos' => array_intersect_key(
 70             $role['capabilities'],
 71             array_flip($this->caps['pos'])
 72           ),
 73           'woo' => array_intersect_key(
 74             $role['capabilities'],
 75             array_flip($this->caps['woo'])
 76           )
 77         )
 78       );
 79     endforeach; endif;
 80 
 81     return $role_caps;
 82   }
 83 
 84   /**
 85    * @param array $data
 86    * @return array
 87    */
 88   public function set( array $data ){
 89     if(isset($data['roles'])){
 90       $this->update_capabilities($data['roles']);
 91     }
 92     return $this->get();
 93   }
 94 
 95   /**
 96    * Set: Loop through roles and capabilities
 97    *
 98    * @param array $roles
 99    */
100   private function update_capabilities( array $roles ){
101     foreach($roles as $slug => $array):
102 
103       $role = get_role($slug);
104 
105       if( $array['capabilities'] ) : foreach( $array['capabilities'] as $key => $caps ):
106         if( $caps ): foreach( $caps as $cap => $grant ):
107           if( in_array( $cap, $this->caps[$key] ) ){
108             $grant ? $role->add_cap($cap) : $role->remove_cap($cap);
109           }
110         endforeach; endif;
111       endforeach; endif;
112 
113     endforeach;
114   }
115 
116   /**
117    * Delete: Loop through roles and capabilities
118    * - add for administrator/shop_manager
119    * - remove for everyone else
120    *
121    * @return bool|mixed|void
122    */
123   public function delete(){
124     global $wp_roles;
125 
126     $roles = $wp_roles->roles;
127     $caps = array_merge( $this->caps['pos'], $this->caps['woo'] );
128 
129     if( $roles ): foreach( $roles as $slug => $array ):
130       $role = get_role($slug);
131       $action = ( $slug == 'administrator' || $slug == 'shop_manager' ) ? 'add_cap' : 'remove_cap';
132       if( $caps ): foreach( $caps as $cap ):
133         $role->$action($cap);
134       endforeach; endif;
135     endforeach; endif;
136 
137     return $this->get();
138   }
139 
140 }
API documentation generated by ApiGen