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  * POS Settings
  5  *
  6  * @class    WC_POS_API_Settings
  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\API;
 13 
 14 use WC_API_Resource;
 15 use WC_API_Server;
 16 use WC_POS\Admin\Settings as Admin_Settings;
 17 use WC_POS\Admin\Settings\Gateways;
 18 use WC_POS\Admin\Status;
 19 
 20 class Settings extends WC_API_Resource {
 21 
 22   protected $base = '/pos/settings';
 23 
 24   /**
 25    * Register routes for POS Settings
 26    *
 27    * GET /pos
 28    *
 29    * @param array $routes
 30    * @return array
 31    */
 32   public function register_routes( array $routes ) {
 33 
 34     # GET /pos/settings
 35     $routes[ $this->base ] = array(
 36       array( array( $this, 'get_settings' ), WC_API_Server::READABLE )
 37     );
 38 
 39     # GET|PUT|DELETE /pos/settings/<id>
 40     $routes[ $this->base . '/(?P<id>\w+)' ] = array(
 41       array( array( $this, 'get_settings' ),  WC_API_Server::READABLE ),
 42       array( array( $this, 'edit_settings' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ),
 43       array( array( $this, 'delete_settings' ), WC_API_Server::DELETABLE ),
 44     );
 45 
 46     return $routes;
 47 
 48   }
 49 
 50   /**
 51    * @param string $id
 52    * @param null   $wc_pos_admin
 53    * @return array
 54    *
 55    * @todo refactor, get_settings returns payload, edit_settings returns data
 56    * @todo move business logic to WC_POS_Admin_Settings?
 57    */
 58   public function get_settings( $id = '', $wc_pos_admin = null, $defaults = false ){
 59 
 60     // @todo remove this special hack for 'restore default settings'
 61     if( $defaults ){
 62       return $this->delete_settings( $id );
 63     }
 64 
 65     $payload = $handlers = array();
 66 
 67     switch ($wc_pos_admin) {
 68       case 'admin_settings':
 69         $handlers = Admin_Settings::handlers();
 70         break;
 71       case 'admin_system_status':
 72         $handlers = Status::handlers();
 73         break;
 74     }
 75 
 76     // single settings
 77     if( $id && isset( $handlers[$id] ) ){
 78       $class = $handlers[$id];
 79       $handler = $class::get_instance();
 80       return $handler->get_payload();
 81     }
 82 
 83     foreach($handlers as $key => $class){
 84       $handler = $class::get_instance();
 85       $payload[] = $handler->get_payload();
 86     }
 87 
 88     return $payload;
 89   }
 90 
 91   /**
 92    * @param string $id
 93    * @param        $data
 94    * @return array|WP_Error
 95    * @throws WC_API_Exception
 96    */
 97   public function edit_settings( $id = '', $data ){
 98 
 99     if( !$data ){
100       throw new WC_API_Exception( 'woocommerce_pos_api_missing_settings_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'settings' ), 400 );
101     }
102 
103     $handler = $this->get_settings_handler( $id );
104 
105     if ( is_wp_error( $handler ) ) {
106       return $handler;
107     }
108 
109     if( $handler->flush_local_data($data) ){
110       Admin_Settings::bump_idb_version();
111     }
112 
113     return $handler->set( $data );
114   }
115 
116 
117   /**
118    * @param string $id
119    * @return array|WP_Error
120    */
121   public function delete_settings( $id = '' ){
122 
123     $handler = $this->get_settings_handler( $id );
124 
125     if ( is_wp_error( $handler ) ) {
126       return $handler;
127     }
128 
129     if( $handler->flush_local_data() ){
130       Admin_Settings::bump_idb_version();
131     }
132 
133     return $handler->delete();
134   }
135 
136   /**
137    * Delete all settings in WP options table
138    */
139   public function delete_all_settings() {
140     global $wpdb;
141     $wpdb->query(
142       $wpdb->prepare( "
143         DELETE FROM {$wpdb->options}
144         WHERE option_name
145         LIKE '%s'",
146         Admin_Settings::DB_PREFIX . '%'
147       )
148     );
149   }
150 
151   /**
152    * @param $id
153    * @return Gateways|\WP_Error
154    */
155   private function get_settings_handler( $id ){
156 
157     if( ! $id ){
158       return new \WP_Error(
159         'woocommerce_pos_settings_error',
160         __( 'There is no settings id', 'woocommerce-pos' ),
161         array( 'status' => 400 )
162       );
163     }
164 
165     // special case: gateway_
166     $gateway_id = preg_replace( '/^gateway_/', '', strtolower( $id ), 1, $count );
167     if($count) {
168       return new Gateways( $gateway_id );
169     }
170 
171     // special case: receipt_
172     $receipts_section = preg_replace( '/^receipt_/', '', strtolower( $id ), 1, $count );
173     if($count) {
174       $class = '\WC_POS\Admin\Settings\Receipt\\' . ucfirst( $receipts_section );
175       return new $class();
176     }
177 
178     // else, find handler by id
179     else {
180       $handlers = (array) Admin_Settings::handlers();
181       if( isset( $handlers[$id] ) )
182         return new $handlers[$id]();
183     }
184 
185     return new \WP_Error(
186       'woocommerce_pos_settings_error',
187       sprintf( __( 'No handler found for %s settings', 'woocommerce-pos' ), $id),
188       array( 'status' => 400 )
189     );
190 
191   }
192 
193 }
API documentation generated by ApiGen