WC_POS_Settings

The WooCommerce POS WC POS Settings class.

Defined (1)

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

/includes/class-wc-pos-settings.php  
  1. class WC_POS_Settings { 
  2.  
  3. /** @var string The db prefix for WP Options table */ 
  4. const DB_PREFIX = 'woocommerce_pos_'; 
  5.  
  6. /** @var WC_POS_Params instance */ 
  7. private $params; 
  8.  
  9. /** @var array */ 
  10. private $settings; 
  11.  
  12. /** 
  13. * @var array settings handlers 
  14. */ 
  15. static public $handlers = array( 
  16. 'general' => 'WC_POS_Admin_Settings_General',  
  17. 'checkout' => 'WC_POS_Admin_Settings_Checkout',  
  18. 'hotkeys' => 'WC_POS_Admin_Settings_HotKeys',  
  19. 'access' => 'WC_POS_Admin_Settings_Access',  
  20. 'tools' => 'WC_POS_Admin_Settings_Tools',  
  21. 'status' => 'WC_POS_Admin_Settings_Status' 
  22. ); 
  23.  
  24. /** 
  25. */ 
  26. public function __construct() { 
  27. add_action( 'wp_ajax_wc_pos_admin_settings_payload', array( $this, 'payload') ); 
  28. add_action( 'wp_ajax_wc_pos_admin_settings', array( $this, 'admin_settings') ); 
  29.  
  30. /** 
  31. * Returns array of settings classes 
  32. * @return mixed|void 
  33. */ 
  34. static public function handlers() { 
  35. return apply_filters( 'woocommerce_pos_settings_handlers', self::$handlers); 
  36.  
  37. /** 
  38. * AJAX payload 
  39. */ 
  40. public function payload() { 
  41. WC_POS_Server::check_ajax_referer(); 
  42.  
  43. $this->params = new WC_POS_Params(); 
  44.  
  45. $payload = array( 
  46. 'templates' => $this->templates_payload(),  
  47. 'settings' => $this->settings,  
  48. 'params' => $this->params->payload(),  
  49. 'i18n' => WC_POS_i18n::payload() 
  50. ); 
  51.  
  52. WC_POS_Server::response( $payload ); 
  53.  
  54. /** 
  55. * Templates payload 
  56. * @return array 
  57. */ 
  58. public function templates_payload() { 
  59. $templates = array(); 
  60. foreach(self::handlers() as $key => $handler) { 
  61. $settings = $handler::get_instance(); 
  62. ob_start(); 
  63. $settings->output(); 
  64. $template = ob_get_clean(); 
  65. $templates[ $key ] = preg_replace('/^\s+|\n|\r|\s+$/m', '', $template); 
  66. $this->settings[] = array( 
  67. 'id' => $settings->id,  
  68. 'label' => $settings->label,  
  69. 'data' => $settings->get() 
  70. ); 
  71. return apply_filters( 'woocommerce_pos_admin_settings_templates', $templates ); 
  72.  
  73. /** 
  74. * @return int 
  75. */ 
  76. static public function get_db_version() { 
  77. return get_option( self::DB_PREFIX . 'db_version', 0 ); 
  78.  
  79. /** 
  80. * updates db to new version number 
  81. * bumps the idb version number 
  82. */ 
  83. static public function bump_versions() { 
  84. self::update_option( self::DB_PREFIX . 'db_version', WC_POS_VERSION ); 
  85. self::bump_idb_version(); 
  86.  
  87. /** 
  88. */ 
  89. static public function get_idb_version() { 
  90. $version = (int) get_option( self::DB_PREFIX . 'idb_version', 0 ); 
  91. return $version ? $version : self::bump_idb_version(); 
  92.  
  93. /** 
  94. */ 
  95. static public function bump_idb_version() { 
  96. $version = (int) get_option( self::DB_PREFIX . 'idb_version', 0 ) + 1; 
  97. if( self::update_option( self::DB_PREFIX . 'idb_version', $version ) ) { 
  98. return $version; 
  99.  
  100. /** 
  101. * @param $id 
  102. * @param $key 
  103. * @return bool 
  104. */ 
  105. static public function get_option( $id, $key = false ) { 
  106. $handlers = (array) self::handlers(); 
  107. if( !array_key_exists( $id, $handlers ) ) 
  108. return false; 
  109.  
  110. $settings = $handlers[$id]::get_instance(); 
  111. return $settings->get( $key ); 
  112.  
  113. /** 
  114. * Add or update a WordPress option. 
  115. * The option will _not_ auto-load by default. 
  116. * @param string $name 
  117. * @param mixed $value 
  118. * @param string $autoload 
  119. * @return bool 
  120. */ 
  121. static public function update_option( $name, $value, $autoload = 'no' ) { 
  122. $success = add_option( $name, $value, '', $autoload ); 
  123. return $success ? $success : update_option( $name, $value ); 
  124.  
  125. /** 
  126. * POS Settings stored in options table 
  127. */ 
  128. public function admin_settings() { 
  129. WC_POS_Server::check_ajax_referer(); 
  130. $result = $this->process_admin_settings(); 
  131. WC_POS_Server::response($result); 
  132.  
  133. /** 
  134. * @return array|bool|mixed|void|WP_Error 
  135. */ 
  136. private function process_admin_settings() { 
  137. // validate 
  138. if(!isset($_GET['id'])) 
  139. return new WP_Error( 
  140. 'woocommerce_pos_settings_error',  
  141. __( 'There is no settings id', 'woocommerce-pos' ),  
  142. array( 'status' => 400 ) 
  143. ); 
  144.  
  145. $id = $_GET['id']; 
  146. $data = WC_POS_Server::get_raw_data(); 
  147.  
  148. // special case: gateway_ 
  149. $gateway_id = preg_replace( '/^gateway_/', '', strtolower( $id ), 1, $count ); 
  150. if($count) { 
  151. $handler = new WC_POS_Admin_Settings_Gateways($gateway_id); 
  152.  
  153. // else, find handler by id 
  154. } else { 
  155. $handlers = (array) self::handlers(); 
  156. if(!isset($handlers[$id])) 
  157. return new WP_Error( 
  158. 'woocommerce_pos_settings_error',  
  159. sprintf( __( 'No handler found for %s settings', 'woocommerce-pos' ), $_GET['id']),  
  160. array( 'status' => 400 ) 
  161. ); 
  162. $handler = new $handlers[$id](); 
  163.  
  164. // Compatibility for clients that can't use PUT/PATCH/DELETE 
  165. $method = strtoupper($_SERVER['REQUEST_METHOD']); 
  166. if ( isset( $_GET['_method'] ) ) { 
  167. $method = strtoupper( $_GET['_method'] ); 
  168. } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) { 
  169. $method = strtoupper( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ); 
  170.  
  171. // bump IDB version 
  172. if( isset( $data['bump_idb_version'] ) && $data['bump_idb_version'] ) { 
  173. self::bump_idb_version(); 
  174. unset( $data['bump_idb_version'] ); 
  175.  
  176. // get 
  177. if( $method === 'GET' ) { 
  178. return $handler->get(); 
  179.  
  180. // set 
  181. if( $method === 'POST' || $method === 'PUT' ) { 
  182. return $handler->set($data); 
  183.  
  184. // delete 
  185. if( $method === 'DELETE' ) { 
  186. return $handler->delete($data); 
  187.  
  188. return new WP_Error( 
  189. 'woocommerce_pos_cannot_'.$method.'_'.$id,  
  190. __( 'Settings error', 'woocommerce-pos' ),  
  191. array( 'status' => 405 ) 
  192. ); 
  193.