PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / controller / SettingPageAjaxController.php

SettingPageAjaxController.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/controller/SettingPageAjaxController.php

511 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of SettingPageAjaxController
5 *
6 * @author Ali2Woo Team
7 *
8 * @autoload: a2wl_admin_init
9 *
10 * @ajax: true
11 */
12
13 namespace AliNext_Lite;;
14
15 use Exception;
16 use Pages;
17 use Throwable;
18
19 class SettingPageAjaxController extends AbstractController
20 {
21 public const FREE_TARIFF_CODE = 'free';
22
23 protected ProductShippingDataRepository $ProductShippingDataRepository;
24 protected PurchaseCodeInfoService $PurchaseCodeInfoService;
25 protected BackgroundProcessFactory $BackgroundProcessFactory;
26
27 public function __construct(
28 ProductShippingDataRepository $ProductShippingDataRepository,
29 PurchaseCodeInfoService $PurchaseCodeInfoService,
30 BackgroundProcessFactory $BackgroundProcessFactory,
31 ) {
32 parent::__construct();
33
34 $this->ProductShippingDataRepository = $ProductShippingDataRepository;
35 $this->PurchaseCodeInfoService = $PurchaseCodeInfoService;
36 $this->BackgroundProcessFactory = $BackgroundProcessFactory;
37
38 add_action('wp_ajax_a2wl_update_price_rules', [$this, 'ajax_update_price_rules']);
39
40 add_action('wp_ajax_a2wl_apply_pricing_rules', [$this, 'ajax_apply_pricing_rules']);
41
42 add_action('wp_ajax_a2wl_update_phrase_rules', [$this, 'ajax_update_phrase_rules']);
43
44 add_action('wp_ajax_a2wl_apply_phrase_rules', [$this, 'ajax_apply_phrase_rules']);
45
46 add_action('wp_ajax_a2wl_reset_shipping_meta', [$this, 'ajax_reset_shipping_meta']);
47
48 add_action('wp_ajax_a2wl_calc_external_images_count', [$this, 'ajax_calc_external_images_count']);
49 add_action('wp_ajax_a2wl_calc_external_images', [$this, 'ajax_calc_external_images']);
50 add_action('wp_ajax_a2wl_load_external_image', [$this, 'ajax_load_external_image']);
51
52 add_action('wp_ajax_a2wl_purchase_code_info', [$this, 'ajax_purchase_code_info']);
53 add_action('wp_ajax_a2wl_import_cancel_process_action', [$this, 'ajax_import_cancel_process_action']);
54 add_action('wp_ajax_a2wl_push_process_action', [$this, 'ajax_push_process_action']);
55 }
56
57 public function ajax_update_phrase_rules(): void
58 {
59 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
60
61 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
62 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
63 echo wp_json_encode($result);
64 wp_die();
65 }
66
67 a2wl_init_error_handler();
68
69 $result = ResultBuilder::buildOk();
70 try {
71
72 PhraseFilter::deleteAll();
73
74 if (isset($_POST['phrases'])) {
75 foreach ($_POST['phrases'] as $phrase) {
76 $filter = new PhraseFilter($phrase);
77 $filter->save();
78 }
79 }
80
81 $result = ResultBuilder::buildOk(array('phrases' => PhraseFilter::load_phrases()));
82
83 restore_error_handler();
84 } catch (Throwable $e) {
85 a2wl_print_throwable($e);
86 $result = ResultBuilder::buildError($e->getMessage());
87 }
88
89 echo wp_json_encode($result);
90 wp_die();
91 }
92
93 public function ajax_import_cancel_process_action(): void
94 {
95 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
96
97 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
98 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
99 echo wp_json_encode($result);
100 wp_die();
101 }
102
103 a2wl_init_error_handler();
104 $result = ResultBuilder::buildWarn(
105 esc_html__('Process is successfully cancelled! Please wait for a few seconds.', 'ali2woo')
106 );
107 try {
108 if (!isset($_POST['process']) || !$_POST['process']) {
109 throw new Exception(esc_html__('Invalid process', 'ali2woo'));
110 }
111 $processCode = trim($_POST['process']);
112
113 $BackgroundProcess = $this->BackgroundProcessFactory->createProcessByCode($processCode);
114 if ($BackgroundProcess->isCancelled()) {
115 throw new Exception(esc_html__('The process is already cancelled.', 'ali2woo'));
116 }
117 $BackgroundProcess->cancel();
118
119 restore_error_handler();
120 } catch (Throwable $Exception) {
121 a2wl_print_throwable($Exception);
122 $result = ResultBuilder::buildError($Exception->getMessage());
123 }
124
125 echo wp_json_encode($result);
126 wp_die();
127 }
128
129 public function ajax_push_process_action(): void
130 {
131 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
132
133 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
134 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
135 echo wp_json_encode($result);
136 wp_die();
137 }
138
139 a2wl_init_error_handler();
140 $result = ResultBuilder::buildWarn(
141 esc_html__('Process is pushed manually! Please wait for a few seconds.', 'ali2woo')
142 );
143 try {
144 if (!isset($_POST['process']) || !$_POST['process']) {
145 throw new Exception(esc_html__('Invalid process', 'ali2woo'));
146 }
147 $processCode = trim($_POST['process']);
148
149 $BackgroundProcess = $this->BackgroundProcessFactory->createProcessByCode($processCode);
150 if ($BackgroundProcess->isCancelled()) {
151 throw new Exception(esc_html__('The process is already cancelled.', 'ali2woo'));
152 }
153 $BackgroundProcess->dispatch();
154
155 restore_error_handler();
156 } catch (Throwable $Exception) {
157 a2wl_print_throwable($Exception);
158 $result = ResultBuilder::buildError($Exception->getMessage());
159 }
160
161 echo wp_json_encode($result);
162 wp_die();
163 }
164
165 public function ajax_apply_phrase_rules(): void
166 {
167 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
168
169 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
170 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
171 echo wp_json_encode($result);
172 wp_die();
173 }
174
175 a2wl_init_error_handler();
176
177 $result = ResultBuilder::buildOk();
178 try {
179 $product_import_model = new ProductImport();
180
181 $type = isset($_POST['type']) ? $_POST['type'] : false;
182 $scope = isset($_POST['scope']) ? $_POST['scope'] : false;
183
184 if ($type === 'products' || $type === 'all_types') {
185 if ($scope === 'all' || $scope === 'import') {
186 $products = $product_import_model->get_product_list(false);
187
188 foreach ($products as $product) {
189
190 $product = PhraseFilter::apply_filter_to_product($product);
191 $product_import_model->upd_product($product);
192 }
193 }
194
195 if ($scope === 'all' || $scope === 'shop') {
196 //todo: update attributes as well
197 PhraseFilter::apply_filter_to_products();
198 }
199 }
200
201 if ($type === 'all_types' || $type === 'reviews') {
202
203 PhraseFilter::apply_filter_to_reviews();
204 }
205
206 if ($type === 'all_types' || $type === 'shippings') {
207
208 }
209 restore_error_handler();
210 } catch (Throwable $e) {
211 a2wl_print_throwable($e);
212 $result = ResultBuilder::buildError($e->getMessage());
213 }
214
215 echo wp_json_encode($result);
216 wp_die();
217 }
218
219 public function ajax_update_price_rules(): void
220 {
221 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
222
223 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
224 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
225 echo wp_json_encode($result);
226 wp_die();
227 }
228
229 a2wl_init_error_handler();
230
231 $result = ResultBuilder::buildOk();
232 try {
233 settings()->auto_commit(false);
234
235 $PriceFormulaRepository = A2WL()->getDI()->get('AliNext_Lite\PriceFormulaRepository');
236 $PriceFormulaFactory = A2WL()->getDI()->get('AliNext_Lite\PriceFormulaFactory');
237
238 $pricing_rules_types = array_keys(PriceFormula::pricing_rules_types());
239 set_setting('pricing_rules_type', $_POST['pricing_rules_type'] && in_array($_POST['pricing_rules_type'], $pricing_rules_types) ? $_POST['pricing_rules_type'] : $pricing_rules_types[0]);
240
241 $use_extended_price_markup = isset($_POST['use_extended_price_markup']) ? filter_var($_POST['use_extended_price_markup'], FILTER_VALIDATE_BOOLEAN) : false;
242 $use_compared_price_markup = isset($_POST['use_compared_price_markup']) ? filter_var($_POST['use_compared_price_markup'], FILTER_VALIDATE_BOOLEAN) : false;
243
244 set_setting('price_cents', isset($_POST['cents']) && intval($_POST['cents']) > -1 && intval($_POST['cents']) <= 99 ? intval(wp_unslash($_POST['cents'])) : -1);
245 if ($use_compared_price_markup) {
246 set_setting('price_compared_cents', isset($_POST['compared_cents']) && intval($_POST['compared_cents']) > -1 && intval($_POST['compared_cents']) <= 99 ? intval(wp_unslash($_POST['compared_cents'])) : -1);
247 } else {
248 set_setting('price_compared_cents', -1);
249 }
250
251 set_setting('use_extended_price_markup', $use_extended_price_markup);
252 set_setting('use_compared_price_markup', $use_compared_price_markup);
253
254 set_setting(
255 Settings::SETTING_ADD_SHIPPING_TO_PRICE,
256 !empty($_POST[Settings::SETTING_ADD_SHIPPING_TO_PRICE]) ? filter_var($_POST[Settings::SETTING_ADD_SHIPPING_TO_PRICE], FILTER_VALIDATE_BOOLEAN) : false
257 );
258 set_setting(
259 'apply_price_rules_after_shipping_cost',
260 !empty($_POST['apply_price_rules_after_shipping_cost']) ? filter_var($_POST['apply_price_rules_after_shipping_cost'], FILTER_VALIDATE_BOOLEAN) : false
261 );
262
263 settings()->commit();
264 settings()->auto_commit(true);
265
266 if (isset($_POST['rules'])) {
267 $PriceFormulaRepository->deleteAll();
268 foreach ($_POST['rules'] as $rule) {
269 $formula = $PriceFormulaFactory->createFormulaFromData($rule);
270 $PriceFormulaRepository->saveExtendedFormula($formula);
271 }
272 }
273
274 if (isset($_POST['default_rule'])) {
275 $PriceFormulaDefault = $PriceFormulaFactory->createFormulaFromData($_POST['default_rule']);
276 $PriceFormulaRepository->setDefaultFormula($PriceFormulaDefault);
277 }
278
279 $result = ResultBuilder::buildOk(
280 [
281 'rules' => $PriceFormulaRepository->getExtendedFormulas(),
282 'default_rule' => $PriceFormulaRepository->getDefaultFormula(),
283 'use_extended_price_markup' => $use_extended_price_markup,
284 'use_compared_price_markup' => $use_compared_price_markup
285 ]
286 );
287
288 restore_error_handler();
289 } catch (Throwable $e) {
290 a2wl_print_throwable($e);
291 $result = ResultBuilder::buildError($e->getMessage());
292 }
293
294 echo wp_json_encode($result);
295 wp_die();
296 }
297
298 public function ajax_apply_pricing_rules(): void
299 {
300 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
301
302 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
303 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
304 echo wp_json_encode($result);
305 wp_die();
306 }
307
308 a2wl_init_error_handler();
309
310 $result = ResultBuilder::buildOk(['done' => 1]);
311 try {
312 $type = $_POST['type'] ?? false;
313 $scope = $_POST['scope'] ?? false;
314 /* $page = $_POST['page'] ?? 0;
315 $import_page = $_POST['import_page'] ?? 0;*/
316
317 $ApplyPricingRulesProcess = new ApplyPricingRulesProcess();
318
319 if ($ApplyPricingRulesProcess->is_queued()) {
320 $message = _x('Please wait until previous update operation complete.', 'error text', 'ali2woo');
321 throw new Exception($message);
322 }
323
324 if ($scope === 'all' || $scope === 'import') {
325 $ProductImportModel = new ProductImport();
326 $products_count = $ProductImportModel->get_products_count();
327
328 $update_per_request = a2wl_check_defined('A2WL_UPDATE_PRODUCT_IN_IMPORTLIST_PER_REQUEST');
329 $update_per_request = $update_per_request ? A2WL_UPDATE_PRODUCT_IN_IMPORTLIST_PER_REQUEST : 30;
330
331 $import_page = -1;
332 do {
333 $import_page++;
334 $products_id_list = $ProductImportModel->get_product_id_list($update_per_request, $update_per_request * $import_page);
335 if (!empty($products_id_list)) {
336 $ApplyPricingRulesProcess->pushToQueue(
337 $products_id_list,
338 ApplyPricingRulesProcess::SCOPE_IMPORT,
339 $type
340 );
341 unset($products_id_list);
342 }
343 } while (($import_page * $update_per_request + $update_per_request) < $products_count);
344 }
345 if ($scope === 'all' || $scope === 'shop') {
346 /** @var $WoocommerceModel Woocommerce */
347 $WoocommerceModel = A2WL()->getDI()->get('AliNext_Lite\Woocommerce');
348 $update_per_request = a2wl_check_defined('A2WL_UPDATE_PRODUCT_PER_REQUEST');
349 $update_per_request = $update_per_request ? A2WL_UPDATE_PRODUCT_PER_REQUEST : 5;
350
351 $products_count = $WoocommerceModel->get_products_count();
352 $page = -1;
353 do {
354 $page++;
355 $product_ids = $WoocommerceModel->get_products_ids($page, $update_per_request);
356 if (!empty($product_ids)) {
357 $ApplyPricingRulesProcess->pushToQueue(
358 $product_ids,
359 ApplyPricingRulesProcess::SCOPE_SHOP,
360 $type
361 );
362 unset($product_ids);
363 }
364 } while (($page * $update_per_request + $update_per_request) < $products_count);
365 }
366
367 $result = ResultBuilder::buildOk([
368 'done' => 1,
369 'info' => 'Please wait until prices will be updated.',
370 ]);
371
372 $ApplyPricingRulesProcess->dispatch();
373
374 restore_error_handler();
375 } catch (Throwable $e) {
376 a2wl_print_throwable($e);
377 $result = ResultBuilder::buildError($e->getMessage());
378 }
379
380 echo wp_json_encode($result);
381 wp_die();
382 }
383
384 public function ajax_calc_external_images_count(): void
385 {
386 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
387
388 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
389 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
390 echo wp_json_encode($result);
391 wp_die();
392 }
393
394 $result = ResultBuilder::buildOk(array('total_images' => Attachment::calc_total_external_images()));
395
396 echo wp_json_encode($result);
397 wp_die();
398 }
399
400 public function ajax_calc_external_images(): void
401 {
402 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
403
404 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
405 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
406 echo wp_json_encode($result);
407 wp_die();
408 }
409
410 $page_size = isset($_POST['page_size']) && intval($_POST['page_size']) > 0 ? intval($_POST['page_size']) : 1000;
411 $result = ResultBuilder::buildOk(array('ids' => Attachment::find_external_images($page_size)));
412
413 echo wp_json_encode($result);
414 wp_die();
415 }
416
417 public function ajax_load_external_image(): void
418 {
419 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
420
421 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
422 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
423 echo wp_json_encode($result);
424 wp_die();
425 }
426
427 a2wl_init_error_handler();
428 $attachment_model = new Attachment('local');
429 $image_id = isset($_POST['id']) && intval($_POST['id']) > 0 ? intval($_POST['id']) : 0;
430
431 if ($image_id) {
432 try {
433 $attachment_model->load_external_image($image_id);
434
435 $result = ResultBuilder::buildOk();
436 } catch (Throwable $e) {
437 a2wl_print_throwable($e);
438 $result = ResultBuilder::buildError($e->getMessage());
439 }
440 } else {
441 $result = ResultBuilder::buildError("load_external_image: waiting for ID...");
442 }
443
444 echo wp_json_encode($result);
445 wp_die();
446 }
447
448 public function ajax_reset_shipping_meta(): void
449 {
450 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
451
452 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
453 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
454 echo wp_json_encode($result);
455 wp_die();
456 }
457
458 $result = ResultBuilder::buildOk();
459 $this->ProductShippingDataRepository->clear();
460
461 echo wp_json_encode($result);
462 wp_die();
463 }
464
465 public function ajax_purchase_code_info(): void
466 {
467 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
468
469 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
470 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
471 echo wp_json_encode($result);
472 wp_die();
473 }
474
475 try {
476 $PurchaseCodeInfo = $this->PurchaseCodeInfoService->getFromApi();
477 } catch (PlatformException $PlatformException) {
478 $result = ResultBuilder::buildError($PlatformException->getMessage());
479
480 echo wp_json_encode($result);
481 wp_die();
482 }
483
484 $isFreeTariff = $PurchaseCodeInfo->isTariffCodeFree();
485 $supported_until = $PurchaseCodeInfo->getSupportedUntilStamp();
486
487 $result = ResultBuilder::buildOk($PurchaseCodeInfo->toArray());
488
489 $result['tariff_name'] = $isFreeTariff ? 'Free' : ucfirst($PurchaseCodeInfo->getTariffCode());
490
491 if ($isFreeTariff){
492 //fix how we display limits in lite version
493 $result['limits']['reviews'] = 0;
494 $result['limits']['shipping'] = 0;
495 }
496
497 if ($supported_until && $supported_until < time()) {
498 $result['supported_until'] = "Support expired on " . gmdate("F j, Y", $supported_until);
499 } else if ($supported_until) {
500 $result['supported_until'] = gmdate("F j, Y", $supported_until);
501 } else {
502 $result['supported_until'] = "";
503 }
504
505
506 echo wp_json_encode($result);
507 wp_die();
508 }
509
510 }
511