PluginProbe
seQura / 3.2.2
seQura v3.2.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.2.2, at src/Controllers/Rest/class-general-settings-rest-controller.php

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