PluginProbe
seQura / 4.3.0
seQura v4.3.0
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
← All changes | src/Controllers/Rest/class-onboarding-rest-controller.php +260 -297 3.0.24.3.0 View file →
@@ -11,11 +11,13 @@
11 11 use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI;
12 12 use SeQura\Core\BusinessLogic\AdminAPI\Connection\Requests\ConnectionRequest;
13 13 use SeQura\Core\BusinessLogic\AdminAPI\Connection\Requests\OnboardingRequest;
14 14 use SeQura\Core\BusinessLogic\AdminAPI\CountryConfiguration\Requests\CountryConfigurationRequest;
15 -use SeQura\WC\Core\Extension\BusinessLogic\AdminAPI\PromotionalWidgets\Requests\Widget_Settings_Request;
16 -use SeQura\WC\Core\Extension\BusinessLogic\Domain\PromotionalWidgets\Models\Mini_Widget;
17 -use SeQura\WC\Services\Interface_Logger_Service;
15 +use SeQura\Core\BusinessLogic\AdminAPI\Disconnect\Requests\DisconnectRequest;
16 +use SeQura\Core\BusinessLogic\AdminAPI\PromotionalWidgets\Requests\WidgetSettingsRequest;
17 +use SeQura\Core\BusinessLogic\AdminAPI\Response\Response;
18 +use SeQura\WC\Services\Log\Interface_Logger_Service;
19 +use SeQura\Core\Infrastructure\Utility\RegexProvider;
18 20 use WP_Error;
19 21 use WP_REST_Request;
20 22 use WP_REST_Response;
21 23
@@ -26,44 +28,51 @@
26 28
27 29 private const PARAM_ENVIRONMENT = 'environment';
28 30 private const PARAM_USERNAME = 'username';
29 31 private const PARAM_PASSWORD = 'password';
32 + private const PARAM_DEPLOYMENT_ID = 'deploymentId';
33 + private const PARAM_DEPLOYMENT = 'deployment';
34 + private const PARAM_CONNECTION_DATA = 'connectionData';
30 35 private const PARAM_SEND_STATISTICAL_DATA = 'sendStatisticalData';
31 - private const PARAM_USE_WIDGETS = 'useWidgets';
32 - private const PARAM_ASSETS_KEY = 'assetsKey';
33 36 private const PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE = 'displayWidgetOnProductPage';
34 37 private const PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING = 'showInstallmentAmountInProductListing';
35 38 private const PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE = 'showInstallmentAmountInCartPage';
36 - private const PARAM_MINI_WIDGET_SELECTOR = 'miniWidgetSelector';
37 39 private const PARAM_WIDGET_STYLES = 'widgetStyles';
38 - private const PARAM_WIDGET_LABELS = 'widgetLabels';
39 - private const PARAM_SEL_FOR_PRICE = 'selForPrice';
40 - private const PARAM_SEL_FOR_ALT_PRICE = 'selForAltPrice';
41 - private const PARAM_SEL_FOR_ALT_PRICE_TRIGGER = 'selForAltPriceTrigger';
42 - private const PARAM_SEL_FOR_DEFAULT_LOCATION = 'selForDefaultLocation';
43 40 private const PARAM_CUSTOM_LOCATIONS = 'customLocations';
44 41 private const PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET = 'selForTarget';
45 - private const PARAM_CUSTOM_LOCATION_WIDGET_STYLES = 'widgetStyles';
42 + private const PARAM_CUSTOM_LOCATION_WIDGET_STYLES = self::PARAM_WIDGET_STYLES;
46 43 private const PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET = 'displayWidget';
47 - private const PARAM_CUSTOM_LOCATION_COUNTRY = 'country';
48 44 private const PARAM_CUSTOM_LOCATION_PRODUCT = 'product';
49 45 private const PARAM_CUSTOM_LOCATION_CAMPAIGN = 'campaign';
50 46
47 + private const PARAM_SEL_FOR_PRICE = 'productPriceSelector';
48 + private const PARAM_SEL_FOR_ALT_PRICE = 'altProductPriceSelector';
49 + private const PARAM_SEL_FOR_ALT_PRICE_TRIGGER = 'altProductPriceTriggerSelector';
50 + private const PARAM_SEL_FOR_DEFAULT_LOCATION = 'defaultProductLocationSelector';
51 51 // Cart widget config.
52 - private const PARAM_CART_SEL_FOR_PRICE = 'selForCartPrice';
53 - private const PARAM_CART_SEL_FOR_DEFAULT_LOCATION = 'selForCartLocation';
52 + private const PARAM_CART_SEL_FOR_PRICE = 'cartPriceSelector';
53 + private const PARAM_CART_SEL_FOR_DEFAULT_LOCATION = 'cartLocationSelector';
54 + private const PARAM_CART_WIDGET_ON_PAGE = 'widgetOnCartPage';
54 55 // Product listing widget config.
55 - private const PARAM_LISTING_SEL_FOR_PRICE = 'selForListingPrice';
56 - private const PARAM_LISTING_SEL_FOR_LOCATION = 'selForListingLocation';
56 + private const PARAM_LISTING_SEL_FOR_PRICE = 'listingPriceSelector';
57 + private const PARAM_LISTING_SEL_FOR_LOCATION = 'listingLocationSelector';
58 + private const PARAM_LISTING_WIDGET_ON_PAGE = 'widgetOnListingPage';
57 59
60 + private const PARAM_IS_FULL_DISCONNECT = 'isFullDisconnect';
61 +
58 62 /**
59 63 * Constructor.
60 64 *
61 65 * @param string $rest_namespace The namespace.
62 66 * @param Interface_Logger_Service $logger The logger service.
67 + * @param RegexProvider $regex The regex provider.
63 68 */
64 - public function __construct( $rest_namespace, Interface_Logger_Service $logger ) {
65 - parent::__construct( $logger );
69 + public function __construct(
70 + $rest_namespace,
71 + Interface_Logger_Service $logger,
72 + RegexProvider $regex
73 + ) {
74 + parent::__construct( $logger, $regex );
66 75 $this->namespace = $rest_namespace;
67 76 $this->rest_base = '/onboarding';
68 77 }
69 78
@@ -68,11 +77,13 @@
68 77 }
69 78
70 79 /**
71 80 * Register the API endpoints.
81 + *
82 + * @return void
72 83 */
73 - public function register_routes(): void {
74 - $this->logger->log_info( 'Hook executed', __FUNCTION__, __CLASS__ );
84 + public function register_routes() {
85 + $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ );
75 86 $store_id_args = array( self::PARAM_STORE_ID => $this->get_arg_string() );
76 87
77 88 $data_args = array_merge(
78 89 $store_id_args,
@@ -85,13 +96,21 @@
85 96 self::PARAM_PASSWORD => $this->get_arg_string(),
86 97 self::PARAM_SEND_STATISTICAL_DATA => $this->get_arg_bool(),
87 98 )
88 99 );
100 +
101 + $disconnect_args = array_merge(
102 + $store_id_args,
103 + array(
104 + self::PARAM_IS_FULL_DISCONNECT => $this->get_arg_bool(),
105 + self::PARAM_DEPLOYMENT_ID => $this->get_arg_string(),
106 + )
107 + );
89 108
90 109 $validate_data_args = array_merge(
91 110 $data_args,
92 111 array(
93 - self::PARAM_MERCHANT_ID => $this->get_arg_string(),
112 + self::PARAM_MERCHANT_ID => $this->get_arg_string( false ),
94 113 )
95 114 );
96 115
97 116 $widget_args = array_merge(
@@ -96,35 +115,26 @@
96 115
97 116 $widget_args = array_merge(
98 117 $store_id_args,
99 118 array(
100 - self::PARAM_USE_WIDGETS => $this->get_arg_bool(),
101 - self::PARAM_ASSETS_KEY => $this->get_arg_string( true, '', array( $this, 'validate_assets_key' ) ),
102 119 self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE => $this->get_arg_bool(),
103 120 self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING => $this->get_arg_bool(),
104 121 self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE => $this->get_arg_bool(),
105 - self::PARAM_MINI_WIDGET_SELECTOR => $this->get_arg_string( true, '', '__return_true' ), // TODO: Add this field to form in the UI.
106 122 self::PARAM_WIDGET_STYLES => $this->get_arg_string(),
107 - self::PARAM_WIDGET_LABELS => array(
108 - 'default' => array(
109 - 'message' => '',
110 - 'messageBelowLimit' => '',
111 - ),
112 - 'required' => false,
113 - 'validate_callback' => array( $this, 'validate_widget_labels' ),
114 - 'sanitize_callback' => array( $this, 'sanitize_widget_labels' ),
115 - ),
116 - self::PARAM_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_widget_selector' ) ),
117 - self::PARAM_SEL_FOR_ALT_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_optional_widget_selector' ) ),
118 - self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER => $this->get_arg_string( true, null, array( $this, 'validate_optional_widget_selector' ) ),
119 - self::PARAM_SEL_FOR_DEFAULT_LOCATION => $this->get_arg_string( true, null, array( $this, 'validate_required_widget_selector' ) ),
123 + self::PARAM_SEL_FOR_PRICE => $this->get_arg_string( true, '', array( $this, 'validate_required_widget_selector' ) ),
124 + self::PARAM_SEL_FOR_ALT_PRICE => $this->get_arg_string( true, '', array( $this, 'validate_optional_widget_selector' ) ),
125 + self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER => $this->get_arg_string( true, '', array( $this, 'validate_optional_widget_selector' ) ),
126 + self::PARAM_SEL_FOR_DEFAULT_LOCATION => $this->get_arg_string( true, '', array( $this, 'validate_required_widget_selector' ) ),
127 +
120 128 self::PARAM_CUSTOM_LOCATIONS => $this->get_arg_widget_location_list(),
121 129
122 - self::PARAM_CART_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_cart_widget_selector' ) ),
123 - self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION => $this->get_arg_string( true, null, array( $this, 'validate_required_cart_widget_selector' ) ),
130 + self::PARAM_CART_SEL_FOR_PRICE => $this->get_arg_string( true, '', array( $this, 'validate_required_cart_widget_selector' ) ),
131 + self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION => $this->get_arg_string( true, '', array( $this, 'validate_required_cart_widget_selector' ) ),
132 + self::PARAM_CART_WIDGET_ON_PAGE => $this->get_arg_string( true, '', array( $this, 'validate_required_cart_widget_selector' ) ),
124 133
125 - self::PARAM_LISTING_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_listing_widget_selector' ) ),
126 - self::PARAM_LISTING_SEL_FOR_LOCATION => $this->get_arg_string( true, null, array( $this, 'validate_required_listing_widget_selector' ) ),
134 + self::PARAM_LISTING_SEL_FOR_PRICE => $this->get_arg_string( true, '', array( $this, 'validate_required_listing_widget_selector' ) ),
135 + self::PARAM_LISTING_SEL_FOR_LOCATION => $this->get_arg_string( true, '', array( $this, 'validate_required_listing_widget_selector' ) ),
136 + self::PARAM_LISTING_WIDGET_ON_PAGE => $this->get_arg_string( true, '', array( $this, 'validate_required_listing_widget_selector' ) ),
127 137 )
128 138 );
129 139
130 140 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
@@ -129,12 +139,12 @@
129 139
130 140 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
131 141
132 142 $this->register_get( "data/{$store_id}", 'get_connection_data', $store_id_args );
133 - $this->register_post( "data/{$store_id}", 'save_connection_data', $data_args );
134 143 $this->register_post( "data/validate/{$store_id}", 'validate_connection_data', $validate_data_args );
135 - $this->register_post( "data/disconnect/{$store_id}", 'disconnect', $store_id_args );
136 -
144 + $this->register_post( "data/disconnect/{$store_id}", 'disconnect', $disconnect_args );
145 + $this->register_post( "data/connect/{$store_id}", 'connect', $store_id_args );
146 +
137 147 $this->register_get( "widgets/{$store_id}", 'get_widgets', $store_id_args );
138 148 $this->register_post( "widgets/{$store_id}", 'save_widgets', $widget_args );
139 149
140 150 $this->register_get( "countries/selling/{$store_id}", 'get_selling_countries', $store_id_args );
@@ -139,8 +149,11 @@
139 149
140 150 $this->register_get( "countries/selling/{$store_id}", 'get_selling_countries', $store_id_args );
141 151 $this->register_get( "countries/{$store_id}", 'get_countries', $store_id_args );
142 152 $this->register_post( "countries/{$store_id}", 'save_countries', $store_id_args );
153 +
154 + $this->register_get( "deployments/{$store_id}", 'get_deployments', $store_id_args );
155 + $this->register_get( "deployments/not-connected/{$store_id}", 'get_not_connected_deployments', $store_id_args );
143 156 }
144 157
145 158 /**
146 159 * Get connection data.
@@ -149,95 +162,101 @@
149 162 *
150 163 * @return WP_REST_Response|WP_Error
151 164 */
152 165 public function get_connection_data( WP_REST_Request $request ) {
153 - $response = null;
154 - try {
155 - $response = AdminAPI::get()
156 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
157 - ->getOnboardingData()
158 - ->toArray();
159 - } catch ( \Throwable $e ) {
160 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
161 - $response = new WP_Error( 'error', $e->getMessage() );
162 - }
163 - return rest_ensure_response( $response );
166 + /**
167 + * Response
168 + *
169 + * @var Response $response */
170 + $response = AdminAPI::get()
171 + ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
172 + ->getOnboardingData();
173 + return $this->build_response( $response );
164 174 }
165 175
166 176 /**
167 - * Validate connection data before saving.
177 + * Get deployments.
168 178 *
169 179 * @param WP_REST_Request $request The request.
170 180 *
171 181 * @return WP_REST_Response|WP_Error
172 182 */
173 - public function validate_connection_data( WP_REST_Request $request ) {
174 - $response = null;
175 - try {
176 - $response = AdminAPI::get()
177 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
178 - ->isConnectionDataValid(
179 - new ConnectionRequest(
180 - strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
181 - strval( $request->get_param( self::PARAM_MERCHANT_ID ) ),
182 - strval( $request->get_param( self::PARAM_USERNAME ) ),
183 - strval( $request->get_param( self::PARAM_PASSWORD ) )
184 - )
185 - );
186 - $response = $response->toArray();
187 - } catch ( \Throwable $e ) {
188 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
189 - $response = new WP_Error( 'error', $e->getMessage() );
190 - }
191 - return rest_ensure_response( $response );
183 + public function get_deployments( WP_REST_Request $request ) {
184 + /**
185 + * Response
186 + *
187 + * @var Response $response */
188 + $response = AdminAPI::get()
189 + ->deployments( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
190 + ->getAllDeployments();
191 + return $this->build_response( $response );
192 192 }
193 193
194 194 /**
195 - * Check if the assets key is valid.
195 + * Get deployments.
196 196 *
197 - * @param mixed $param The parameter.
198 197 * @param WP_REST_Request $request The request.
199 - * @param string $key The key.
198 + *
199 + * @return WP_REST_Response|WP_Error
200 200 */
201 - public function validate_assets_key( $param, $request, $key ): bool {
202 - return is_string( $param )
203 - && ( ! boolval( $request->get_param( self::PARAM_USE_WIDGETS ) ) || '' !== trim( $param ) );
201 + public function get_not_connected_deployments( WP_REST_Request $request ) {
202 + /**
203 + * Response
204 + *
205 + * @var Response $response */
206 + $response = AdminAPI::get()
207 + ->deployments( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
208 + ->getNotConnectedDeployments();
209 + return $this->build_response( $response );
204 210 }
205 211
206 212 /**
207 - * Save connection data.
213 + * Validate connection data before saving.
208 214 *
209 - * @throws \Exception
210 215 * @param WP_REST_Request $request The request.
216 + *
217 + * @return WP_REST_Response|WP_Error
218 + */
219 + public function validate_connection_data( WP_REST_Request $request ) {
220 + /**
221 + * Response
222 + *
223 + * @var Response $response */
224 + $response = AdminAPI::get()
225 + ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
226 + ->isConnectionDataValid(
227 + new ConnectionRequest(
228 + strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
229 + strval( $request->get_param( self::PARAM_MERCHANT_ID ) ),
230 + strval( $request->get_param( self::PARAM_USERNAME ) ),
231 + strval( $request->get_param( self::PARAM_PASSWORD ) ),
232 + strval( $request->get_param( self::PARAM_DEPLOYMENT_ID ) )
233 + )
234 + );
235 + return $this->build_response( $response );
236 + }
237 +
238 + /**
239 + * Disconnects integration from the shop.
211 240 *
241 + * @param WP_REST_Request $request The request.
242 + *
212 243 * @return WP_REST_Response|WP_Error
213 244 */
214 - public function save_connection_data( WP_REST_Request $request ) {
215 - $response = null;
216 - try {
217 - $response = AdminAPI::get()
218 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
219 - ->saveOnboardingData(
220 - new OnboardingRequest(
221 - strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
222 - strval( $request->get_param( self::PARAM_USERNAME ) ),
223 - strval( $request->get_param( self::PARAM_PASSWORD ) ),
224 - (bool) $request->get_param( self::PARAM_SEND_STATISTICAL_DATA ),
225 - null === $request->get_param( self::PARAM_MERCHANT_ID ) ? null : strval( $request->get_param( self::PARAM_MERCHANT_ID ) )
226 - )
227 - );
228 -
229 - $is_ok = $response->isSuccessful();
230 - $response = $response->toArray();
231 -
232 - if ( ! $is_ok ) {
233 - throw new \Exception( $response['errorMessage'] );
234 - }
235 - } catch ( \Throwable $e ) {
236 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
237 - $response = new WP_Error( 'error', $e->getMessage() );
238 - }
239 - return rest_ensure_response( $response );
245 + public function disconnect( WP_REST_Request $request ) {
246 + /**
247 + * Response
248 + *
249 + * @var Response $response */
250 + $response = AdminAPI::get()
251 + ->disconnect( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
252 + ->disconnect(
253 + new DisconnectRequest(
254 + strval( $request->get_param( self::PARAM_DEPLOYMENT_ID ) ),
255 + (bool) $request->get_param( self::PARAM_IS_FULL_DISCONNECT )
256 + )
257 + );
258 + return $this->build_response( $response );
240 259 }
241 260
242 261 /**
243 262 * Disconnects integration from the shop.
@@ -245,20 +264,41 @@
245 264 * @param WP_REST_Request $request The request.
246 265 *
247 266 * @return WP_REST_Response|WP_Error
248 267 */
249 - public function disconnect( WP_REST_Request $request ) {
250 - $response = null;
251 - try {
252 - $response = AdminAPI::get()
253 - ->disconnect( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
254 - ->disconnect()
255 - ->toArray();
256 - } catch ( \Throwable $e ) {
257 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
258 - $response = new WP_Error( 'error', $e->getMessage() );
268 + public function connect( WP_REST_Request $request ) {
269 + /**
270 + * Connection Data
271 + *
272 + * @var array<array{merchantId?: string,
273 + * username?: string,
274 + * password?: string,
275 + * deployment?: string}> $connection_data_array */
276 + $connection_data_array = $request->get_param( self::PARAM_CONNECTION_DATA ) ?? array();
277 + $connection_requests = array();
278 + foreach ( $connection_data_array as $conn_data ) {
279 + $connection_requests[] = new ConnectionRequest(
280 + strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
281 + strval( $conn_data[ self::PARAM_MERCHANT_ID ] ?? '' ),
282 + strval( $conn_data[ self::PARAM_USERNAME ] ?? '' ),
283 + strval( $conn_data[ self::PARAM_PASSWORD ] ?? '' ),
284 + strval( $conn_data[ self::PARAM_DEPLOYMENT ] ?? '' )
285 + );
259 286 }
260 - return rest_ensure_response( $response );
287 +
288 + /**
289 + * Response
290 + *
291 + * @var Response $response */
292 + $response = AdminAPI::get()
293 + ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
294 + ->connect(
295 + new OnboardingRequest(
296 + $connection_requests,
297 + (bool) $request->get_param( self::PARAM_SEND_STATISTICAL_DATA ),
298 + )
299 + );
300 + return $this->build_response( $response );
261 301 }
262 302
263 303 /**
264 304 * GET selling countries.
@@ -267,19 +307,16 @@
267 307 *
268 308 * @return WP_REST_Response|WP_Error
269 309 */
270 310 public function get_selling_countries( WP_REST_Request $request ) {
271 - $response = null;
272 - try {
273 - $response = AdminAPI::get()
274 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
275 - ->getSellingCountries()
276 - ->toArray();
277 - } catch ( \Throwable $e ) {
278 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
279 - $response = new WP_Error( 'error', $e->getMessage() );
280 - }
281 - return rest_ensure_response( $response );
311 + /**
312 + * Response
313 + *
314 + * @var Response $response */
315 + $response = AdminAPI::get()
316 + ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
317 + ->getSellingCountries();
318 + return $this->build_response( $response );
282 319 }
283 320
284 321 /**
285 322 * GET countries.
@@ -288,19 +325,16 @@
288 325 *
289 326 * @return WP_REST_Response|WP_Error
290 327 */
291 328 public function get_countries( WP_REST_Request $request ) {
292 - $response = null;
293 - try {
294 - $response = AdminAPI::get()
295 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
296 - ->getCountryConfigurations()
297 - ->toArray();
298 - } catch ( \Throwable $e ) {
299 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
300 - $response = new WP_Error( 'error', $e->getMessage() );
301 - }
302 - return rest_ensure_response( $response );
329 + /**
330 + * Response
331 + *
332 + * @var Response $response */
333 + $response = AdminAPI::get()
334 + ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
335 + ->getCountryConfigurations();
336 + return $this->build_response( $response );
303 337 }
304 338
305 339 /**
306 340 * Save countries.
@@ -313,22 +347,22 @@
313 347 public function save_countries( WP_REST_Request $request ) {
314 348 if ( ! $this->validate_countries( $request ) ) {
315 349 return new WP_REST_Response( 'Invalid data', 400 );
316 350 }
351 + /**
352 + * Data
353 + *
354 + * @var array<int, array<string, string>> $data */
355 + $data = (array) json_decode( $request->get_body(), true );
317 356
318 - $response = null;
319 - try {
320 - $data = (array) json_decode( $request->get_body(), true );
321 -
322 - $response = AdminAPI::get()
323 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
324 - ->saveCountryConfigurations( new CountryConfigurationRequest( $data ) )
325 - ->toArray();
326 - } catch ( \Throwable $e ) {
327 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
328 - $response = new WP_Error( 'error', $e->getMessage() );
329 - }
330 - return rest_ensure_response( $response );
357 + /**
358 + * Response
359 + *
360 + * @var Response $response */
361 + $response = AdminAPI::get()
362 + ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
363 + ->saveCountryConfigurations( new CountryConfigurationRequest( $data ) );
364 + return $this->build_response( $response );
331 365 }
332 366
333 367 /**
334 368 * GET widgets.
@@ -337,29 +371,17 @@
337 371 *
338 372 * @return WP_REST_Response|WP_Error
339 373 */
340 374 public function get_widgets( WP_REST_Request $request ) {
341 - $response = null;
342 - try {
343 - $response = AdminAPI::get()
344 - ->widgetConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
345 - ->getWidgetSettings()
346 - ->toArray();
375 + /**
376 + * Response
377 + *
378 + * @var Response $response */
379 + $response = AdminAPI::get()
380 + ->widgetConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
381 + ->getWidgetSettings();
347 382
348 - if ( ! empty( $response ) ) {
349 - $response[ self::PARAM_WIDGET_LABELS ]['message'] = ! empty( $response[ self::PARAM_WIDGET_LABELS ]['messages'] ) ?
350 - reset( $response[ self::PARAM_WIDGET_LABELS ]['messages'] ) : '';
351 - $response[ self::PARAM_WIDGET_LABELS ]['messageBelowLimit'] = ! empty( $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] ) ?
352 - reset( $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] ) : '';
353 - $response[ self::PARAM_WIDGET_STYLES ] = $response['widgetConfiguration'];
354 - unset( $response[ self::PARAM_WIDGET_LABELS ]['messages'], $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] );
355 - unset( $response['widgetConfiguration'] );
356 - }
357 - } catch ( \Throwable $e ) {
358 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
359 - $response = new WP_Error( 'error', $e->getMessage() );
360 - }
361 - return rest_ensure_response( $response );
383 + return $this->build_response( $response );
362 384 }
363 385
364 386 /**
365 387 * Save countries.
@@ -368,91 +390,35 @@
368 390 * @param WP_REST_Request $request The request.
369 391 *
370 392 * @return WP_REST_Response|WP_Error
371 393 */
372 - public function save_widgets( WP_REST_Request $request ) {
373 - $response = null;
374 - try {
375 - //phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar
376 - // $labels = $request->get_param( 'widgetLabels' );
377 - // $messages = $labels['message'] ? array( $storeConfig->getLocale() => $labels['message'] ) : array();
378 - // $messages_below_limit = $labels['messageBelowLimit'] ? array( $storeConfig->getLocale() => $labels['messageBelowLimit'] ) : array();
379 - //phpcs:enable
380 - $messages = array();
381 - $messages_below_limit = array();
382 -
383 - $store_id = strval( $request->get_param( self::PARAM_STORE_ID ) );
384 -
385 - /**
386 - * Filter the cart mini widgets.
387 - *
388 - * @since 3.0.0
389 - * @var Mini_Widget[] $cart_mini_widgets The cart mini widgets.
390 - */
391 - $cart_mini_widgets = apply_filters( 'sequra_widget_settings_cart_mini_widgets', array(), $store_id );
392 - if ( ! is_array( $cart_mini_widgets ) ) {
393 - $this->logger->log_debug( 'Invalid cart mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
394 - $cart_mini_widgets = array();
395 - }
396 - foreach ( $cart_mini_widgets as $mini_widget ) {
397 - if ( ! $mini_widget instanceof Mini_Widget ) {
398 - $this->logger->log_debug( 'Invalid cart mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
399 - $cart_mini_widgets = array();
400 - break;
401 - }
402 - }
403 -
404 - /**
405 - * Filter the product listing mini widgets.
406 - *
407 - * @since 3.0.0
408 - * @var Mini_Widget[] $listing_mini_widgets The product listing mini widgets.
409 - */
410 - $listing_mini_widgets = apply_filters( 'sequra_widget_settings_product_listing_mini_widgets', array(), $store_id );
411 - if ( ! is_array( $listing_mini_widgets ) ) {
412 - $this->logger->log_debug( 'Invalid product listing mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
413 - $listing_mini_widgets = array();
414 - }
415 - foreach ( $listing_mini_widgets as $mini_widget ) {
416 - if ( ! $mini_widget instanceof Mini_Widget ) {
417 - $this->logger->log_debug( 'Invalid product listing mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
418 - $listing_mini_widgets = array();
419 - break;
420 - }
421 - }
422 -
423 - $response = AdminAPI::get()
424 - ->widgetConfiguration( $store_id )
425 - ->setWidgetSettings(
426 - new Widget_Settings_Request(
427 - (bool) $request->get_param( self::PARAM_USE_WIDGETS ),
428 - null === $request->get_param( self::PARAM_ASSETS_KEY ) ? null : strval( $request->get_param( self::PARAM_ASSETS_KEY ) ),
429 - (bool) $request->get_param( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE ),
430 - (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING ),
431 - (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE ),
432 - strval( $request->get_param( self::PARAM_MINI_WIDGET_SELECTOR ) ),
433 - strval( $request->get_param( self::PARAM_WIDGET_STYLES ) ),
434 - (array) $messages,
435 - (array) $messages_below_limit,
436 - strval( $request->get_param( self::PARAM_SEL_FOR_PRICE ) ),
437 - strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE ) ),
438 - strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER ) ),
439 - strval( $request->get_param( self::PARAM_SEL_FOR_DEFAULT_LOCATION ) ),
440 - (array) $request->get_param( self::PARAM_CUSTOM_LOCATIONS ),
441 - strval( $request->get_param( self::PARAM_CART_SEL_FOR_PRICE ) ),
442 - strval( $request->get_param( self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION ) ),
443 - $cart_mini_widgets,
444 - strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_PRICE ) ),
445 - strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_LOCATION ) ),
446 - $listing_mini_widgets
447 - )
394 + public function save_widgets( WP_REST_Request $request ) {
395 + /**
396 + * Response
397 + *
398 + * @var Response $response */
399 + $response = AdminAPI::get()
400 + ->widgetConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
401 + ->setWidgetSettings(
402 + new WidgetSettingsRequest(
403 + (bool) $request->get_param( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE ),
404 + (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING ),
405 + (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE ),
406 + strval( $request->get_param( self::PARAM_WIDGET_STYLES ) ),
407 + strval( $request->get_param( self::PARAM_SEL_FOR_PRICE ) ),
408 + strval( $request->get_param( self::PARAM_SEL_FOR_DEFAULT_LOCATION ) ),
409 + strval( $request->get_param( self::PARAM_CART_SEL_FOR_PRICE ) ),
410 + strval( $request->get_param( self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION ) ),
411 + strval( $request->get_param( self::PARAM_CART_WIDGET_ON_PAGE ) ),
412 + strval( $request->get_param( self::PARAM_LISTING_WIDGET_ON_PAGE ) ),
413 + strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_PRICE ) ),
414 + strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_LOCATION ) ),
415 + strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE ) ),
416 + strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER ) ),
417 + (array) $request->get_param( self::PARAM_CUSTOM_LOCATIONS )
448 418 )
449 - ->toArray();
450 - } catch ( \Throwable $e ) {
451 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
452 - $response = new WP_Error( 'error', $e->getMessage() );
453 - }
454 - return rest_ensure_response( $response );
419 + );
420 + return $this->build_response( $response );
455 421 }
456 422
457 423 /**
458 424 * Validate if countries payload is valid.
@@ -459,32 +425,35 @@
459 425 *
460 426 * @param WP_REST_Request $request The request.
461 427 */
462 428 public function validate_countries( WP_REST_Request $request ): bool {
463 - try {
464 - $data = json_decode( $request->get_body(), true );
465 - if ( ! is_array( $data ) ) {
466 - return false;
467 - }
468 - $allowed_countries = AdminAPI::get()
469 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
470 - ->getSellingCountries()
471 - ->toArray();
429 + $data = json_decode( $request->get_body(), true );
430 + if ( ! is_array( $data ) ) {
431 + return false;
432 + }
433 + /**
434 + * Response
435 + *
436 + * @var Response $response */
437 + $response = AdminAPI::get()
438 + ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
439 + ->getSellingCountries();
472 440
473 - $allowed_countries = array_column( $allowed_countries, 'code' );
441 + if ( ! $response->isSuccessful() ) {
442 + return false;
443 + }
474 444
475 - foreach ( $data as $country ) {
476 - if ( ! isset( $country['countryCode'] )
477 - || ! isset( $country['merchantId'] )
478 - || ! is_string( $country['countryCode'] )
479 - || ! is_string( $country['merchantId'] )
480 - || ! in_array( $country['countryCode'], $allowed_countries, true )
481 - ) {
482 - return false;
483 - }
445 + $allowed_countries = array_column( $response->toArray(), 'code' );
446 +
447 + foreach ( $data as $country ) {
448 + if ( ! isset( $country['countryCode'] )
449 + || ! isset( $country['merchantId'] )
450 + || ! is_string( $country['countryCode'] )
451 + || ! is_string( $country['merchantId'] )
452 + || ! in_array( $country['countryCode'], $allowed_countries, true )
453 + ) {
454 + return false;
484 455 }
485 - } catch ( \Throwable $e ) {
486 - return false;
487 456 }
488 457 return true;
489 458 }
490 459
@@ -525,9 +494,9 @@
525 494 *
526 495 * @param mixed $param The param.
527 496 */
528 497 public function validate_required_widget_selector( $param, WP_REST_Request $request, string $key ): bool {
529 - return $this->validate_required_selector( self::PARAM_USE_WIDGETS, $param, $request, $key );
498 + return $this->validate_required_selector( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE, $param, $request, $key );
530 499 }
531 500
532 501 /**
533 502 * Validate if required cart widget selector is valid.
@@ -565,10 +534,10 @@
565 534 * @return mixed[]
566 535 */
567 536 public function sanitize_widget_labels( $param ): array {
568 537 return array(
569 - 'message' => sanitize_text_field( strval( $param['message'] ) ),
570 - 'messageBelowLimit' => sanitize_text_field( strval( $param['messageBelowLimit'] ) ),
538 + 'message' => \sanitize_text_field( strval( $param['message'] ) ),
539 + 'messageBelowLimit' => \sanitize_text_field( strval( $param['messageBelowLimit'] ) ),
571 540 );
572 541 }
573 542
574 543 /**
@@ -603,33 +572,28 @@
603 572 }
604 573 foreach ( $param as $location ) {
605 574 if ( ! isset(
606 575 $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ],
607 - $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ],
608 - $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ],
576 + $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ],
609 577 $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ],
610 578 $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ]
611 579 )
612 580 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ] )
613 581 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
614 - || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
615 582 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ] )
616 583 || ! is_bool( $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ] )
617 584 || empty( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
618 - || empty( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
619 585 ) {
620 586 return false;
621 587 }
622 588
623 - // check if exists another location with the same country title and product.
624 - $country = $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ];
589 + // check if exists another location with the same title and product.
625 590 $product = $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ];
626 591 $campaign = $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ?? null;
627 - $found = array_filter(
592 + $found = \array_filter(
628 593 $param,
629 - function ( $loc ) use ( $country, $product, $campaign ) {
630 - return isset( $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ], $loc[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
631 - && $loc[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] === $country
594 + function ( $loc ) use ( $product, $campaign ) {
595 + return isset( $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
632 596 && $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] === $product
633 597 && ( $loc[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ?? null ) === $campaign;
634 598 }
635 599 );
@@ -648,13 +612,12 @@
648 612 */
649 613 public function sanitize_widget_location_list( $param ): array {
650 614 foreach ( $param as &$location ) {
651 615 $location = array(
652 - self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET => sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ] ) ),
653 - self::PARAM_CUSTOM_LOCATION_PRODUCT => sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] ) ),
654 - self::PARAM_CUSTOM_LOCATION_COUNTRY => sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] ) ),
655 - self::PARAM_CUSTOM_LOCATION_CAMPAIGN => isset( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ? sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ) : null,
656 - self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES => sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ] ) ),
616 + self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ] ) ),
617 + self::PARAM_CUSTOM_LOCATION_PRODUCT => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] ) ),
618 + self::PARAM_CUSTOM_LOCATION_CAMPAIGN => isset( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ? \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ) : null,
619 + self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ] ) ),
657 620 self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET => (bool) $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ],
658 621 );
659 622 }
660 623 return $param;