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