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

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

152 lines 4.8 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 PriceFormulaSetAjaxController
5 *
6 * @author Ali2Woo Team
7 *
8 * @autoload: a2wl_admin_init
9 *
10 * @ajax: true
11 */
12 // phpcs:ignoreFile WordPress.Security.EscapeOutput.OutputNotEscaped
13 namespace AliNext_Lite;;
14
15 use Exception;
16 use Pages;
17
18 class PriceFormulaSetAjaxController extends AbstractController
19 {
20 private PriceFormulaSetRepository $PriceFormulaSetRepository;
21 private PriceFormulaSetService $PriceFormulaSetService;
22
23 public function __construct(
24 PriceFormulaSetRepository $PriceFormulaSetRepository,
25 PriceFormulaSetService $PriceFormulaSetService
26 ) {
27 parent::__construct();
28
29 $this->PriceFormulaSetRepository = $PriceFormulaSetRepository;
30 $this->PriceFormulaSetService = $PriceFormulaSetService;
31
32 add_action('wp_ajax_a2wl_save_set', [$this, 'ajaxSaveSet']);
33 add_action('wp_ajax_a2wl_choose_set', [$this, 'ajaxChooseSet']);
34 add_action('wp_ajax_a2wl_remove_set', [$this, 'ajaxRemoveSet']);
35 }
36
37 public function ajaxSaveSet(): void
38 {
39 $this->verifyNonceAjax();
40
41 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
42 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
43 echo wp_json_encode($result);
44 wp_die();
45 }
46
47 $setName = $_POST['name'];
48
49 a2wl_init_error_handler();
50
51 $result = ResultBuilder::buildOk();
52
53 try {
54 if ($this->PriceFormulaSetRepository->checkExists($setName)) {
55 throw new RepositoryException(
56 _x( "A price formula set with that name already exists. Please choose a different name.",
57 'error text', 'ali2woo')
58 );
59 }
60 $PriceFormulaSet = $this->PriceFormulaSetService->buildFromSettings($setName);
61 $this->PriceFormulaSetRepository->add($PriceFormulaSet);
62
63 restore_error_handler();
64 } catch (RepositoryException $RepositoryException) {
65 $result = ResultBuilder::buildError($RepositoryException->getMessage());
66 } catch (Exception $Exception) {
67 a2wl_print_throwable($Exception);
68 $result = ResultBuilder::buildError($Exception->getMessage());
69 }
70
71 echo wp_json_encode($result);
72 wp_die();
73 }
74
75 public function ajaxChooseSet(): void
76 {
77 $this->verifyNonceAjax();
78
79 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
80 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
81 echo wp_json_encode($result);
82 wp_die();
83 }
84
85 $setName = $_POST['name'];
86
87 a2wl_init_error_handler();
88
89 $result = ResultBuilder::buildOk();
90
91 try {
92 if (!$this->PriceFormulaSetRepository->checkExists($setName)) {
93 throw new RepositoryException(
94 _x("A price formula set with that name does`t exists. Please choose a different name.",
95 'error text', 'ali2woo')
96 );
97 }
98
99 $PriceFormulaSet = $this->PriceFormulaSetRepository->getOneByName($setName);
100 $this->PriceFormulaSetService->choose($PriceFormulaSet);
101
102 restore_error_handler();
103 } catch (RepositoryException|ServiceException $Exception) {
104 $result = ResultBuilder::buildError($Exception->getMessage());
105 } catch (Exception $Exception) {
106 a2wl_print_throwable($Exception);
107 $result = ResultBuilder::buildError($Exception->getMessage());
108 }
109
110 echo wp_json_encode($result);
111 wp_die();
112 }
113
114 public function ajaxRemoveSet(): void
115 {
116 $this->verifyNonceAjax();
117
118 if (!PageGuardHelper::canAccessPage(Pages::SETTINGS)) {
119 $result = ResultBuilder::buildError($this->getErrorTextNoPermissions());
120 echo wp_json_encode($result);
121 wp_die();
122 }
123
124 $setName = $_POST['name'];
125
126 a2wl_init_error_handler();
127
128 $result = ResultBuilder::buildOk();
129
130 try {
131 if (!$this->PriceFormulaSetRepository->checkExists($setName)) {
132 throw new RepositoryException(
133 _x("A price formula set with that name does`t exists. Please choose a different name.",
134 'error text', 'ali2woo')
135 );
136 }
137
138 $this->PriceFormulaSetRepository->removeOneByName($setName);
139
140 restore_error_handler();
141 } catch (RepositoryException $RepositoryException) {
142 $result = ResultBuilder::buildError($RepositoryException->getMessage());
143 } catch (Exception $Exception) {
144 a2wl_print_throwable($Exception);
145 $result = ResultBuilder::buildError($Exception->getMessage());
146 }
147
148 echo wp_json_encode($result);
149 wp_die();
150 }
151 }
152