PluginProbe
seQura / 3.0.2
seQura v3.0.2
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Controllers / Rest / class-general-settings-rest-controller.php

class-general-settings-rest-controller.php in seQura 3.0.2, at src/Controllers/Rest/class-general-settings-rest-controller.php

429 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST Settings Controller
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Controllers/Rest
7 */
8
9 namespace SeQura\WC\Controllers\Rest;
10
11 use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI;
12 use SeQura\Core\BusinessLogic\AdminAPI\OrderStatusSettings\Requests\OrderStatusSettingsRequest;
13 use SeQura\Core\BusinessLogic\Domain\Order\OrderStates;
14 use SeQura\WC\Core\Extension\BusinessLogic\AdminAPI\GeneralSettings\Requests\General_Settings_Request;
15 use SeQura\WC\Services\Interface_Logger_Service;
16 use WP_Error;
17 use WP_REST_Request;
18 use WP_REST_Response;
19
20 /**
21 * REST Settings Controller
22 */
23 class General_Settings_REST_Controller extends REST_Controller {
24
25 const PARAM_SEND_ORDER_REPORTS_PERIODICALLY_TO_SEQURA = 'sendOrderReportsPeriodicallyToSeQura';
26 const PARAM_SHOW_SEQURA_CHECKOUT_AS_HOSTED_PAGE = 'showSeQuraCheckoutAsHostedPage';
27 const PARAM_ALLOWED_IP_ADDRESSES = 'allowedIPAddresses';
28 const PARAM_EXCLUDED_PRODUCTS = 'excludedProducts';
29 const PARAM_EXCLUDED_CATEGORIES = 'excludedCategories';
30 const PARAM_ENABLED_FOR_SERVICES = 'enabledForServices';
31 const PARAM_ALLOW_FIRST_SERVICE_PAYMENT_DELAY = 'allowFirstServicePaymentDelay';
32 const PARAM_ALLOW_SERVICE_REG_ITEMS = 'allowServiceRegItems';
33 const PARAM_DEFAULT_SERVICES_END_DATE = 'defaultServicesEndDate';
34
35 /**
36 * Constructor.
37 *
38 * @param string $rest_namespace The namespace.
39 * @param Interface_Logger_Service $logger The logger service.
40 */
41 public function __construct( $rest_namespace, Interface_Logger_Service $logger ) {
42 parent::__construct( $logger );
43 $this->namespace = $rest_namespace;
44 $this->rest_base = '/settings';
45 }
46
47 /**
48 * Register the API endpoints.
49 */
50 public function register_routes(): void {
51 $this->logger->log_info( 'Hook executed', __FUNCTION__, __CLASS__ );
52 $store_id_args = array( self::PARAM_STORE_ID => $this->get_arg_string() );
53 $general_args = array_merge(
54 $store_id_args,
55 array(
56 self::PARAM_SEND_ORDER_REPORTS_PERIODICALLY_TO_SEQURA => $this->get_arg_bool(),
57 self::PARAM_SHOW_SEQURA_CHECKOUT_AS_HOSTED_PAGE => $this->get_arg_bool( false, false ),
58 self::PARAM_ALLOWED_IP_ADDRESSES => $this->get_arg_ip_list( true, array() ),
59 self::PARAM_EXCLUDED_PRODUCTS => array(
60 'required' => true,
61 'validate_callback' => array( $this, 'validate_array_of_product_sku' ),
62 'sanitize_callback' => array( $this, 'sanitize_array_sanitize_text_field' ),
63 ),
64 self::PARAM_EXCLUDED_CATEGORIES => array(
65 'required' => false,
66 'validate_callback' => array( $this, 'validate_array_of_product_cat' ),
67 'sanitize_callback' => array( $this, 'sanitize_array_of_ids' ),
68 ),
69 self::PARAM_ENABLED_FOR_SERVICES => $this->get_arg_bool( false, false ),
70 self::PARAM_ALLOW_FIRST_SERVICE_PAYMENT_DELAY => $this->get_arg_bool( false, true ),
71 self::PARAM_ALLOW_SERVICE_REG_ITEMS => $this->get_arg_bool( false, true ),
72 self::PARAM_DEFAULT_SERVICES_END_DATE => $this->get_arg_string( false, 'P1Y', array( $this, 'validate_time_duration' ) ),
73 )
74 );
75
76 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
77
78 $this->register_get( 'current-store', 'get_current_store' );
79 $this->register_get( "version/{$store_id}", 'get_version', $store_id_args );
80 $this->register_get( "stores/{$store_id}", 'get_stores', $store_id_args );
81 $this->register_get( "state/{$store_id}", 'get_state', $store_id_args );
82 $this->register_get( "shop-categories/{$store_id}", 'get_shop_categories', $store_id_args );
83 $this->register_get( "shop-name/{$store_id}", 'get_shop_name', $store_id_args );
84
85 $this->register_get( "general/{$store_id}", 'get_general', $store_id_args );
86 $this->register_post( "general/{$store_id}", 'save_general', $general_args );
87
88 $this->register_get( "order-status/list/{$store_id}", 'get_list_order_status', $store_id_args );
89 $this->register_get( "order-status/{$store_id}", 'get_order_status', $store_id_args );
90 $this->register_post( "order-status/{$store_id}", 'save_order_status', $store_id_args );
91 }
92
93 /**
94 * GET current store.
95 *
96 * @return WP_REST_Response|WP_Error
97 */
98 public function get_current_store() {
99 $response = null;
100 try {
101 $response = AdminAPI::get()
102 ->store( (string) get_current_blog_id() )
103 ->getCurrentStore()
104 ->toArray();
105 } catch ( \Throwable $e ) {
106 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
107 $response = new WP_Error( 'error', $e->getMessage() );
108 }
109 return rest_ensure_response( $response );
110 }
111
112 /**
113 * GET version.
114 *
115 * @param WP_REST_Request $request The request.
116 *
117 * @return WP_REST_Response|WP_Error
118 */
119 public function get_version( WP_REST_Request $request ) {
120 $response = null;
121 try {
122 $response = AdminAPI::get()
123 ->integration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
124 ->getVersion()
125 ->toArray();
126 } catch ( \Throwable $e ) {
127 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
128 $response = new WP_Error( 'error', $e->getMessage() );
129 }
130 return rest_ensure_response( $response );
131 }
132
133 /**
134 * GET stores.
135 *
136 * @param WP_REST_Request $request The request.
137 *
138 * @return WP_REST_Response|WP_Error
139 */
140 public function get_stores( WP_REST_Request $request ) {
141 $response = null;
142 try {
143 $response = AdminAPI::get()
144 ->store( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
145 ->getStores()
146 ->toArray();
147 } catch ( \Throwable $e ) {
148 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
149 $response = new WP_Error( 'error', $e->getMessage() );
150 }
151 return rest_ensure_response( $response );
152 }
153
154 /**
155 * GET state.
156 *
157 * @param WP_REST_Request $request The request.
158 *
159 * @return WP_REST_Response|WP_Error
160 */
161 public function get_state( WP_REST_Request $request ) {
162 $response = null;
163 try {
164 $response = AdminAPI::get()
165 ->integration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
166 ->getUIState( true ) // Pass false if the Onboarding does not configure widgets.
167 ->toArray();
168 } catch ( \Throwable $e ) {
169 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
170 $response = new WP_Error( 'error', $e->getMessage() );
171 }
172 return rest_ensure_response( $response );
173 }
174
175 /**
176 * GET general settings.
177 *
178 * @param WP_REST_Request $request The request.
179 *
180 * @return WP_REST_Response|WP_Error
181 */
182 public function get_general( WP_REST_Request $request ) {
183 $response = null;
184 try {
185 $response = AdminAPI::get()
186 ->generalSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
187 ->getGeneralSettings()
188 ->toArray();
189 } catch ( \Throwable $e ) {
190 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
191 $response = new WP_Error( 'error', $e->getMessage() );
192 }
193 return rest_ensure_response( $response );
194 }
195
196 /**
197 * GET general settings.
198 *
199 * @param WP_REST_Request $request The request.
200 *
201 * @return WP_REST_Response|WP_Error
202 */
203 public function get_shop_name( WP_REST_Request $request ) {
204 $response = null;
205 try {
206 $response = AdminAPI::get()
207 ->integration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
208 ->getShopName()
209 ->toArray();
210 } catch ( \Throwable $e ) {
211 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
212 $response = new WP_Error( 'error', $e->getMessage() );
213 }
214 return rest_ensure_response( $response );
215 }
216
217 /**
218 * Save general settings.
219 *
220 * @throws \Exception
221 * @param WP_REST_Request $request The request.
222 *
223 * @return WP_REST_Response|WP_Error
224 */
225 public function save_general( WP_REST_Request $request ) {
226 $response = null;
227 try {
228 $response = AdminAPI::get()
229 ->generalSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
230 ->saveGeneralSettings(
231 new General_Settings_Request(
232 (bool) $request->get_param( self::PARAM_SEND_ORDER_REPORTS_PERIODICALLY_TO_SEQURA ),
233 (bool) $request->get_param( self::PARAM_SHOW_SEQURA_CHECKOUT_AS_HOSTED_PAGE ),
234 (array) $request->get_param( self::PARAM_ALLOWED_IP_ADDRESSES ),
235 (array) $request->get_param( self::PARAM_EXCLUDED_PRODUCTS ),
236 (array) $request->get_param( self::PARAM_EXCLUDED_CATEGORIES ),
237 (bool) $request->get_param( self::PARAM_ENABLED_FOR_SERVICES ),
238 (bool) $request->get_param( self::PARAM_ALLOW_FIRST_SERVICE_PAYMENT_DELAY ),
239 (bool) $request->get_param( self::PARAM_ALLOW_SERVICE_REG_ITEMS ),
240 strval( $request->get_param( self::PARAM_DEFAULT_SERVICES_END_DATE ) )
241 )
242 )
243 ->toArray();
244 } catch ( \Throwable $e ) {
245 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
246 $response = new WP_Error( 'error', $e->getMessage() );
247 }
248 return rest_ensure_response( $response );
249 }
250
251 /**
252 * GET shop categories.
253 *
254 * @param WP_REST_Request $request The request.
255 *
256 * @return WP_REST_Response|WP_Error
257 */
258 public function get_shop_categories( WP_REST_Request $request ) {
259 $response = null;
260 try {
261 $response = AdminAPI::get()
262 ->generalSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
263 ->getShopCategories()
264 ->toArray();
265 } catch ( \Throwable $e ) {
266 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
267 $response = new WP_Error( 'error', $e->getMessage() );
268 }
269 return rest_ensure_response( $response );
270 }
271
272 /**
273 * GET Order Status List.
274 *
275 * @param WP_REST_Request $request The request.
276 *
277 * @return WP_REST_Response|WP_Error
278 */
279 public function get_list_order_status( WP_REST_Request $request ) {
280 $response = null;
281 try {
282 $response = AdminAPI::get()
283 ->orderStatusSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
284 ->getShopOrderStatuses()
285 ->toArray();
286 } catch ( \Throwable $e ) {
287 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
288 $response = new WP_Error( 'error', $e->getMessage() );
289 }
290 return rest_ensure_response( $response );
291 }
292
293 /**
294 * GET Order Status settings.
295 *
296 * @param WP_REST_Request $request The request.
297 *
298 * @return WP_REST_Response|WP_Error
299 */
300 public function get_order_status( WP_REST_Request $request ) {
301 $response = null;
302 try {
303 $response = AdminAPI::get()
304 ->orderStatusSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
305 ->getOrderStatusSettings()
306 ->toArray();
307 } catch ( \Throwable $e ) {
308 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
309 $response = new WP_Error( 'error', $e->getMessage() );
310 }
311 return rest_ensure_response( $response );
312 }
313
314 /**
315 * POST Order Status settings.
316 *
317 * @param WP_REST_Request $request The request.
318 *
319 * @return WP_REST_Response|WP_Error
320 */
321 public function save_order_status( WP_REST_Request $request ) {
322 if ( ! $this->validate_order_status( $request ) ) {
323 return new WP_REST_Response( 'Invalid data', 400 );
324 }
325
326 $response = null;
327 try {
328 $response = AdminAPI::get()
329 ->orderStatusSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
330 ->saveOrderStatusSettings(
331 new OrderStatusSettingsRequest( (array) json_decode( $request->get_body(), true ) )
332 )
333 ->toArray();
334 } catch ( \Throwable $e ) {
335 $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
336 $response = new WP_Error( 'error', $e->getMessage() );
337 }
338 return rest_ensure_response( $response );
339 }
340
341 /**
342 * Validate if the parameter is a boolean.
343 *
344 * @param mixed $param The parameter.
345 * @param WP_REST_Request $request The request.
346 * @param string $key The key.
347 */
348 public function validate_array_of_product_sku( $param, $request, $key ): bool {
349 if ( ! is_array( $param ) ) {
350 return false;
351 }
352
353 foreach ( $param as $sku ) {
354 if ( ! is_string( $sku ) || '' === trim( $sku ) ) {
355 return false;
356 }
357 }
358 return true;
359 }
360
361 /**
362 * Validate if the parameter is a boolean.
363 *
364 * @param mixed $param The parameter.
365 * @param WP_REST_Request $request The request.
366 * @param string $key The key.
367 */
368 public function validate_array_of_product_cat( $param, $request, $key ): bool {
369 if ( ! is_array( $param ) ) {
370 return false;
371 }
372
373 foreach ( $param as $term_id ) {
374 if ( ! is_numeric( $term_id ) || empty( $term_id ) ) {
375 return false;
376 }
377 }
378 return true;
379 }
380
381 /**
382 * Validate if order status payload is valid.
383 *
384 * @param WP_REST_Request $request The request.
385 */
386 public function validate_order_status( WP_REST_Request $request ): bool {
387 try {
388 $data = json_decode( $request->get_body(), true );
389 if ( ! is_array( $data ) ) {
390 return false;
391 }
392 $allowed_shop_statuses = AdminAPI::get()
393 ->orderStatusSettings( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
394 ->getShopOrderStatuses()
395 ->toArray();
396 $allowed_shop_statuses = array_column( $allowed_shop_statuses, 'id' );
397 $allowed_sequra_statuses = OrderStates::toArray();
398
399 foreach ( $data as $status_map ) {
400 if ( ! isset( $status_map['sequraStatus'] )
401 || ! isset( $status_map['shopStatus'] )
402 || ! is_string( $status_map['sequraStatus'] )
403 || ! is_string( $status_map['shopStatus'] )
404 || ! in_array( $status_map['sequraStatus'], $allowed_sequra_statuses, true )
405 || ! in_array( $status_map['shopStatus'], $allowed_shop_statuses, true )
406 ) {
407 return false;
408 }
409 }
410 } catch ( \Throwable $e ) {
411 return false;
412 }
413 return true;
414 }
415
416 /**
417 * Sanitize an array of ids.
418 *
419 * @param mixed[] $param The parameter.
420 * @return mixed[]
421 */
422 public function sanitize_array_of_ids( array $param ): array {
423 foreach ( $param as &$val ) {
424 $val = (string) abs( intval( $val ) );
425 }
426 return $param;
427 }
428 }
429