PluginProbe
seQura / 4.1.0
seQura v4.1.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 +258 -296 3.2.14.1.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
@@ -87,13 +96,21 @@
87 96 self::PARAM_PASSWORD => $this->get_arg_string(),
88 97 self::PARAM_SEND_STATISTICAL_DATA => $this->get_arg_bool(),
89 98 )
90 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 + );
91 108
92 109 $validate_data_args = array_merge(
93 110 $data_args,
94 111 array(
95 - self::PARAM_MERCHANT_ID => $this->get_arg_string(),
112 + self::PARAM_MERCHANT_ID => $this->get_arg_string( false ),
96 113 )
97 114 );
98 115
99 116 $widget_args = array_merge(
@@ -98,35 +115,26 @@
98 115
99 116 $widget_args = array_merge(
100 117 $store_id_args,
101 118 array(
102 - self::PARAM_USE_WIDGETS => $this->get_arg_bool(),
103 - self::PARAM_ASSETS_KEY => $this->get_arg_string( true, '', array( $this, 'validate_assets_key' ) ),
104 119 self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE => $this->get_arg_bool(),
105 120 self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING => $this->get_arg_bool(),
106 121 self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE => $this->get_arg_bool(),
107 - self::PARAM_MINI_WIDGET_SELECTOR => $this->get_arg_string( true, '', '__return_true' ), // TODO: Add this field to form in the UI.
108 122 self::PARAM_WIDGET_STYLES => $this->get_arg_string(),
109 - self::PARAM_WIDGET_LABELS => array(
110 - 'default' => array(
111 - 'message' => '',
112 - 'messageBelowLimit' => '',
113 - ),
114 - 'required' => false,
115 - 'validate_callback' => array( $this, 'validate_widget_labels' ),
116 - 'sanitize_callback' => array( $this, 'sanitize_widget_labels' ),
117 - ),
118 - self::PARAM_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_widget_selector' ) ),
119 - self::PARAM_SEL_FOR_ALT_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_optional_widget_selector' ) ),
120 - self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER => $this->get_arg_string( true, null, array( $this, 'validate_optional_widget_selector' ) ),
121 - 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 +
122 128 self::PARAM_CUSTOM_LOCATIONS => $this->get_arg_widget_location_list(),
123 129
124 - self::PARAM_CART_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_cart_widget_selector' ) ),
125 - 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' ) ),
126 133
127 - self::PARAM_LISTING_SEL_FOR_PRICE => $this->get_arg_string( true, null, array( $this, 'validate_required_listing_widget_selector' ) ),
128 - 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' ) ),
129 137 )
130 138 );
131 139
132 140 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
@@ -131,12 +139,12 @@
131 139
132 140 $store_id = $this->url_param_pattern( self::PARAM_STORE_ID );
133 141
134 142 $this->register_get( "data/{$store_id}", 'get_connection_data', $store_id_args );
135 - $this->register_post( "data/{$store_id}", 'save_connection_data', $data_args );
136 143 $this->register_post( "data/validate/{$store_id}", 'validate_connection_data', $validate_data_args );
137 - $this->register_post( "data/disconnect/{$store_id}", 'disconnect', $store_id_args );
138 -
144 + $this->register_post( "data/disconnect/{$store_id}", 'disconnect', $disconnect_args );
145 + $this->register_post( "data/connect/{$store_id}", 'connect', $store_id_args );
146 +
139 147 $this->register_get( "widgets/{$store_id}", 'get_widgets', $store_id_args );
140 148 $this->register_post( "widgets/{$store_id}", 'save_widgets', $widget_args );
141 149
142 150 $this->register_get( "countries/selling/{$store_id}", 'get_selling_countries', $store_id_args );
@@ -141,8 +149,11 @@
141 149
142 150 $this->register_get( "countries/selling/{$store_id}", 'get_selling_countries', $store_id_args );
143 151 $this->register_get( "countries/{$store_id}", 'get_countries', $store_id_args );
144 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 );
145 156 }
146 157
147 158 /**
148 159 * Get connection data.
@@ -151,95 +162,101 @@
151 162 *
152 163 * @return WP_REST_Response|WP_Error
153 164 */
154 165 public function get_connection_data( WP_REST_Request $request ) {
155 - $response = null;
156 - try {
157 - $response = AdminAPI::get()
158 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
159 - ->getOnboardingData()
160 - ->toArray();
161 - } catch ( \Throwable $e ) {
162 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
163 - $response = new WP_Error( 'error', $e->getMessage() );
164 - }
165 - 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 );
166 174 }
167 175
168 176 /**
169 - * Validate connection data before saving.
177 + * Get deployments.
170 178 *
171 179 * @param WP_REST_Request $request The request.
172 180 *
173 181 * @return WP_REST_Response|WP_Error
174 182 */
175 - public function validate_connection_data( WP_REST_Request $request ) {
176 - $response = null;
177 - try {
178 - $response = AdminAPI::get()
179 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
180 - ->isConnectionDataValid(
181 - new ConnectionRequest(
182 - strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
183 - strval( $request->get_param( self::PARAM_MERCHANT_ID ) ),
184 - strval( $request->get_param( self::PARAM_USERNAME ) ),
185 - strval( $request->get_param( self::PARAM_PASSWORD ) )
186 - )
187 - );
188 - $response = $response->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 );
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 );
194 192 }
195 193
196 194 /**
197 - * Check if the assets key is valid.
195 + * Get deployments.
198 196 *
199 - * @param mixed $param The parameter.
200 197 * @param WP_REST_Request $request The request.
201 - * @param string $key The key.
198 + *
199 + * @return WP_REST_Response|WP_Error
202 200 */
203 - public function validate_assets_key( $param, $request, $key ): bool {
204 - return is_string( $param )
205 - && ( ! 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 );
206 210 }
207 211
208 212 /**
209 - * Save connection data.
213 + * Validate connection data before saving.
210 214 *
211 - * @throws \Exception
212 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.
213 240 *
241 + * @param WP_REST_Request $request The request.
242 + *
214 243 * @return WP_REST_Response|WP_Error
215 244 */
216 - public function save_connection_data( WP_REST_Request $request ) {
217 - $response = null;
218 - try {
219 - $response = AdminAPI::get()
220 - ->connection( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
221 - ->saveOnboardingData(
222 - new OnboardingRequest(
223 - strval( $request->get_param( self::PARAM_ENVIRONMENT ) ),
224 - strval( $request->get_param( self::PARAM_USERNAME ) ),
225 - strval( $request->get_param( self::PARAM_PASSWORD ) ),
226 - (bool) $request->get_param( self::PARAM_SEND_STATISTICAL_DATA ),
227 - null === $request->get_param( self::PARAM_MERCHANT_ID ) ? null : strval( $request->get_param( self::PARAM_MERCHANT_ID ) )
228 - )
229 - );
230 -
231 - $is_ok = $response->isSuccessful();
232 - $response = $response->toArray();
233 -
234 - if ( ! $is_ok ) {
235 - throw new \Exception( $response['errorMessage'] );
236 - }
237 - } catch ( \Throwable $e ) {
238 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
239 - $response = new WP_Error( 'error', $e->getMessage() );
240 - }
241 - 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 );
242 259 }
243 260
244 261 /**
245 262 * Disconnects integration from the shop.
@@ -247,20 +264,41 @@
247 264 * @param WP_REST_Request $request The request.
248 265 *
249 266 * @return WP_REST_Response|WP_Error
250 267 */
251 - public function disconnect( WP_REST_Request $request ) {
252 - $response = null;
253 - try {
254 - $response = AdminAPI::get()
255 - ->disconnect( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
256 - ->disconnect()
257 - ->toArray();
258 - } catch ( \Throwable $e ) {
259 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
260 - $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 + );
261 286 }
262 - 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 );
263 301 }
264 302
265 303 /**
266 304 * GET selling countries.
@@ -269,19 +307,16 @@
269 307 *
270 308 * @return WP_REST_Response|WP_Error
271 309 */
272 310 public function get_selling_countries( WP_REST_Request $request ) {
273 - $response = null;
274 - try {
275 - $response = AdminAPI::get()
276 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
277 - ->getSellingCountries()
278 - ->toArray();
279 - } catch ( \Throwable $e ) {
280 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
281 - $response = new WP_Error( 'error', $e->getMessage() );
282 - }
283 - 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 );
284 319 }
285 320
286 321 /**
287 322 * GET countries.
@@ -290,19 +325,16 @@
290 325 *
291 326 * @return WP_REST_Response|WP_Error
292 327 */
293 328 public function get_countries( WP_REST_Request $request ) {
294 - $response = null;
295 - try {
296 - $response = AdminAPI::get()
297 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
298 - ->getCountryConfigurations()
299 - ->toArray();
300 - } catch ( \Throwable $e ) {
301 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
302 - $response = new WP_Error( 'error', $e->getMessage() );
303 - }
304 - 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 );
305 337 }
306 338
307 339 /**
308 340 * Save countries.
@@ -315,27 +347,22 @@
315 347 public function save_countries( WP_REST_Request $request ) {
316 348 if ( ! $this->validate_countries( $request ) ) {
317 349 return new WP_REST_Response( 'Invalid data', 400 );
318 350 }
351 + /**
352 + * Data
353 + *
354 + * @var array<int, array<string, string>> $data */
355 + $data = (array) json_decode( $request->get_body(), true );
319 356
320 - $response = null;
321 - try {
322 - /**
323 - * Data
324 - *
325 - * @var array<int, array<string, string>> $data The data.
326 - */
327 - $data = (array) json_decode( $request->get_body(), true );
328 -
329 - $response = AdminAPI::get()
330 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
331 - ->saveCountryConfigurations( new CountryConfigurationRequest( $data ) )
332 - ->toArray();
333 - } catch ( \Throwable $e ) {
334 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
335 - $response = new WP_Error( 'error', $e->getMessage() );
336 - }
337 - 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 );
338 365 }
339 366
340 367 /**
341 368 * GET widgets.
@@ -344,29 +371,27 @@
344 371 *
345 372 * @return WP_REST_Response|WP_Error
346 373 */
347 374 public function get_widgets( WP_REST_Request $request ) {
348 - $response = null;
349 - try {
350 - $response = AdminAPI::get()
351 - ->widgetConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
352 - ->getWidgetSettings()
353 - ->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();
354 382
355 - if ( ! empty( $response ) ) {
356 - $response[ self::PARAM_WIDGET_LABELS ]['message'] = ! empty( $response[ self::PARAM_WIDGET_LABELS ]['messages'] ) ?
357 - reset( $response[ self::PARAM_WIDGET_LABELS ]['messages'] ) : '';
358 - $response[ self::PARAM_WIDGET_LABELS ]['messageBelowLimit'] = ! empty( $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] ) ?
359 - reset( $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] ) : '';
360 - $response[ self::PARAM_WIDGET_STYLES ] = $response['widgetConfiguration'];
361 - unset( $response[ self::PARAM_WIDGET_LABELS ]['messages'], $response[ self::PARAM_WIDGET_LABELS ]['messagesBelowLimit'] );
362 - unset( $response['widgetConfiguration'] );
363 - }
364 - } catch ( \Throwable $e ) {
365 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
366 - $response = new WP_Error( 'error', $e->getMessage() );
383 + $arr = $response->toArray();
384 + if ( ! $response->isSuccessful() ) {
385 + return $this->build_response_from_array( $arr, false );
367 386 }
368 - return \rest_ensure_response( $response );
387 + $widget_configuration = '';
388 + if ( isset( $arr['widgetConfiguration'] ) ) {
389 + $widget_configuration = $arr['widgetConfiguration'];
390 + unset( $arr['widgetConfiguration'] );
391 + }
392 + $arr[ self::PARAM_WIDGET_STYLES ] = $widget_configuration;
393 + return $this->build_response_from_array( $arr, true );
369 394 }
370 395
371 396 /**
372 397 * Save countries.
@@ -375,95 +400,35 @@
375 400 * @param WP_REST_Request $request The request.
376 401 *
377 402 * @return WP_REST_Response|WP_Error
378 403 */
379 - public function save_widgets( WP_REST_Request $request ) {
380 - $response = null;
381 - try {
382 - //phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar
383 - // $labels = $request->get_param( 'widgetLabels' );
384 - // $messages = $labels['message'] ? array( $storeConfig->getLocale() => $labels['message'] ) : array();
385 - // $messages_below_limit = $labels['messageBelowLimit'] ? array( $storeConfig->getLocale() => $labels['messageBelowLimit'] ) : array();
386 - //phpcs:enable
387 - $messages = array();
388 - $messages_below_limit = array();
389 -
390 - $store_id = strval( $request->get_param( self::PARAM_STORE_ID ) );
391 -
392 - /**
393 - * Filter the cart mini widgets.
394 - * This field is not used in the UI and is not documented.
395 - * The filter is intended for future use.
396 - *
397 - * @since 3.0.0
398 - * @var Mini_Widget[] $cart_mini_widgets The cart mini widgets.
399 - */
400 - $cart_mini_widgets = \apply_filters( 'sequra_widget_settings_cart_mini_widgets', array(), $store_id );
401 - if ( ! is_array( $cart_mini_widgets ) ) {
402 - $this->logger->log_debug( 'Invalid cart mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
403 - $cart_mini_widgets = array();
404 - }
405 - foreach ( $cart_mini_widgets as $mini_widget ) {
406 - if ( ! $mini_widget instanceof Mini_Widget ) {
407 - $this->logger->log_debug( 'Invalid cart mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
408 - $cart_mini_widgets = array();
409 - break;
410 - }
411 - }
412 -
413 - /**
414 - * Filter the product listing mini widgets.
415 - * This field is not used in the UI and is not documented.
416 - * The filter is intended for future use.
417 - *
418 - * @since 3.0.0
419 - * @var Mini_Widget[] $listing_mini_widgets The product listing mini widgets.
420 - */
421 - $listing_mini_widgets = \apply_filters( 'sequra_widget_settings_product_listing_mini_widgets', array(), $store_id );
422 - if ( ! is_array( $listing_mini_widgets ) ) {
423 - $this->logger->log_debug( 'Invalid product listing mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
424 - $listing_mini_widgets = array();
425 - }
426 - foreach ( $listing_mini_widgets as $mini_widget ) {
427 - if ( ! $mini_widget instanceof Mini_Widget ) {
428 - $this->logger->log_debug( 'Invalid product listing mini widgets. ' . Mini_Widget::class . '[] is expected', __FUNCTION__, __CLASS__ );
429 - $listing_mini_widgets = array();
430 - break;
431 - }
432 - }
433 -
434 - $response = AdminAPI::get()
435 - ->widgetConfiguration( $store_id )
436 - ->setWidgetSettings(
437 - new Widget_Settings_Request(
438 - (bool) $request->get_param( self::PARAM_USE_WIDGETS ),
439 - null === $request->get_param( self::PARAM_ASSETS_KEY ) ? null : strval( $request->get_param( self::PARAM_ASSETS_KEY ) ),
440 - (bool) $request->get_param( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE ),
441 - (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING ),
442 - (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE ),
443 - strval( $request->get_param( self::PARAM_MINI_WIDGET_SELECTOR ) ),
444 - strval( $request->get_param( self::PARAM_WIDGET_STYLES ) ),
445 - (array) $messages,
446 - (array) $messages_below_limit,
447 - strval( $request->get_param( self::PARAM_SEL_FOR_PRICE ) ),
448 - strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE ) ),
449 - strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER ) ),
450 - strval( $request->get_param( self::PARAM_SEL_FOR_DEFAULT_LOCATION ) ),
451 - (array) $request->get_param( self::PARAM_CUSTOM_LOCATIONS ),
452 - strval( $request->get_param( self::PARAM_CART_SEL_FOR_PRICE ) ),
453 - strval( $request->get_param( self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION ) ),
454 - $cart_mini_widgets,
455 - strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_PRICE ) ),
456 - strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_LOCATION ) ),
457 - $listing_mini_widgets
458 - )
404 + public function save_widgets( WP_REST_Request $request ) {
405 + /**
406 + * Response
407 + *
408 + * @var Response $response */
409 + $response = AdminAPI::get()
410 + ->widgetConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
411 + ->setWidgetSettings(
412 + new WidgetSettingsRequest(
413 + (bool) $request->get_param( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE ),
414 + (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_PRODUCT_LISTING ),
415 + (bool) $request->get_param( self::PARAM_SHOW_INSTALLMENT_AMOUNT_IN_CART_PAGE ),
416 + strval( $request->get_param( self::PARAM_WIDGET_STYLES ) ),
417 + strval( $request->get_param( self::PARAM_SEL_FOR_PRICE ) ),
418 + strval( $request->get_param( self::PARAM_SEL_FOR_DEFAULT_LOCATION ) ),
419 + strval( $request->get_param( self::PARAM_CART_SEL_FOR_PRICE ) ),
420 + strval( $request->get_param( self::PARAM_CART_SEL_FOR_DEFAULT_LOCATION ) ),
421 + strval( $request->get_param( self::PARAM_CART_WIDGET_ON_PAGE ) ),
422 + strval( $request->get_param( self::PARAM_LISTING_WIDGET_ON_PAGE ) ),
423 + strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_PRICE ) ),
424 + strval( $request->get_param( self::PARAM_LISTING_SEL_FOR_LOCATION ) ),
425 + strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE ) ),
426 + strval( $request->get_param( self::PARAM_SEL_FOR_ALT_PRICE_TRIGGER ) ),
427 + (array) $request->get_param( self::PARAM_CUSTOM_LOCATIONS )
459 428 )
460 - ->toArray();
461 - } catch ( \Throwable $e ) {
462 - $this->logger->log_throwable( $e, __FUNCTION__, __CLASS__ );
463 - $response = new WP_Error( 'error', $e->getMessage() );
464 - }
465 - return \rest_ensure_response( $response );
429 + );
430 + return $this->build_response( $response );
466 431 }
467 432
468 433 /**
469 434 * Validate if countries payload is valid.
@@ -470,32 +435,35 @@
470 435 *
471 436 * @param WP_REST_Request $request The request.
472 437 */
473 438 public function validate_countries( WP_REST_Request $request ): bool {
474 - try {
475 - $data = json_decode( $request->get_body(), true );
476 - if ( ! is_array( $data ) ) {
477 - return false;
478 - }
479 - $allowed_countries = AdminAPI::get()
480 - ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
481 - ->getSellingCountries()
482 - ->toArray();
439 + $data = json_decode( $request->get_body(), true );
440 + if ( ! is_array( $data ) ) {
441 + return false;
442 + }
443 + /**
444 + * Response
445 + *
446 + * @var Response $response */
447 + $response = AdminAPI::get()
448 + ->countryConfiguration( strval( $request->get_param( self::PARAM_STORE_ID ) ) )
449 + ->getSellingCountries();
483 450
484 - $allowed_countries = array_column( $allowed_countries, 'code' );
451 + if ( ! $response->isSuccessful() ) {
452 + return false;
453 + }
485 454
486 - foreach ( $data as $country ) {
487 - if ( ! isset( $country['countryCode'] )
488 - || ! isset( $country['merchantId'] )
489 - || ! is_string( $country['countryCode'] )
490 - || ! is_string( $country['merchantId'] )
491 - || ! in_array( $country['countryCode'], $allowed_countries, true )
492 - ) {
493 - return false;
494 - }
455 + $allowed_countries = array_column( $response->toArray(), 'code' );
456 +
457 + foreach ( $data as $country ) {
458 + if ( ! isset( $country['countryCode'] )
459 + || ! isset( $country['merchantId'] )
460 + || ! is_string( $country['countryCode'] )
461 + || ! is_string( $country['merchantId'] )
462 + || ! in_array( $country['countryCode'], $allowed_countries, true )
463 + ) {
464 + return false;
495 465 }
496 - } catch ( \Throwable $e ) {
497 - return false;
498 466 }
499 467 return true;
500 468 }
501 469
@@ -536,9 +504,9 @@
536 504 *
537 505 * @param mixed $param The param.
538 506 */
539 507 public function validate_required_widget_selector( $param, WP_REST_Request $request, string $key ): bool {
540 - return $this->validate_required_selector( self::PARAM_USE_WIDGETS, $param, $request, $key );
508 + return $this->validate_required_selector( self::PARAM_DISPLAY_WIDGET_ON_PRODUCT_PAGE, $param, $request, $key );
541 509 }
542 510
543 511 /**
544 512 * Validate if required cart widget selector is valid.
@@ -614,33 +582,28 @@
614 582 }
615 583 foreach ( $param as $location ) {
616 584 if ( ! isset(
617 585 $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ],
618 - $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ],
619 - $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ],
586 + $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ],
620 587 $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ],
621 588 $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ]
622 589 )
623 590 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ] )
624 591 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
625 - || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
626 592 || ! is_string( $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ] )
627 593 || ! is_bool( $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ] )
628 594 || empty( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
629 - || empty( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
630 595 ) {
631 596 return false;
632 597 }
633 598
634 - // check if exists another location with the same country title and product.
635 - $country = $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ];
599 + // check if exists another location with the same title and product.
636 600 $product = $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ];
637 601 $campaign = $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ?? null;
638 602 $found = \array_filter(
639 603 $param,
640 - function ( $loc ) use ( $country, $product, $campaign ) {
641 - return isset( $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ], $loc[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] )
642 - && $loc[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] === $country
604 + function ( $loc ) use ( $product, $campaign ) {
605 + return isset( $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] )
643 606 && $loc[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] === $product
644 607 && ( $loc[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ?? null ) === $campaign;
645 608 }
646 609 );
@@ -661,9 +624,8 @@
661 624 foreach ( $param as &$location ) {
662 625 $location = array(
663 626 self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_SEL_FOR_TARGET ] ) ),
664 627 self::PARAM_CUSTOM_LOCATION_PRODUCT => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_PRODUCT ] ) ),
665 - self::PARAM_CUSTOM_LOCATION_COUNTRY => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_COUNTRY ] ) ),
666 628 self::PARAM_CUSTOM_LOCATION_CAMPAIGN => isset( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ? \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_CAMPAIGN ] ) ) : null,
667 629 self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES => \sanitize_text_field( strval( $location[ self::PARAM_CUSTOM_LOCATION_WIDGET_STYLES ] ) ),
668 630 self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET => (bool) $location[ self::PARAM_CUSTOM_LOCATION_DISPLAY_WIDGET ],
669 631 );