1 <?php
2
3 4 5 6 7 8 9 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 26 27 28 29 30 31
32 public function register_routes( array $routes ) {
33
34
35 $routes[ $this->base ] = array(
36 array( array( $this, 'get_settings' ), WC_API_Server::READABLE )
37 );
38
39
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 52 53 54 55 56 57
58 public function get_settings( $id = '', $wc_pos_admin = null, $defaults = false ){
59
60
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
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 93 94 95 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 119 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 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 153 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
166 $gateway_id = preg_replace( '/^gateway_/', '', strtolower( $id ), 1, $count );
167 if($count) {
168 return new Gateways( $gateway_id );
169 }
170
171
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
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 }