PluginProbe
MailerLite – WooCommerce integration / 3.1.25
MailerLite – WooCommerce integration v3.1.25
3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 147 releases
woo-mailerlite / admin / controllers / WooMailerLiteAdminSettingsController.php

WooMailerLiteAdminSettingsController.php in MailerLite – WooCommerce integration 3.1.25, at admin/controllers/WooMailerLiteAdminSettingsController.php

316 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WooMailerLiteAdminSettingsController extends WooMailerLiteController
4 {
5 public function updateIgnoreProductsBulkAndQuickEdit($productId, $post)
6 {
7 $product = $this->resolveResource(WooMailerLiteProduct::class, $productId);
8 if (!$product) {
9 return $productId;
10 }
11 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
12 return $productId;
13 }
14
15 if ('product' !== $post->post_type) {
16 return $productId;
17 }
18
19 $ignoredProducts = WooMailerLiteOptions::get('ignored_products', []);
20
21 $wasIgnored = $product->ignored;
22
23 if ($this->request('ml_ignore_product')) {
24 $product->ignored = true;
25 $ignoredProducts[$product->resource_id] = $post->post_title;
26 } else {
27 $product->ignored = false;
28
29 unset($ignoredProducts[$product->resource_id]);
30 }
31 $product->save();
32
33 WooMailerLiteOptions::update('ignored_products', $ignoredProducts);
34 if ($product->isDeleted()) {
35 return $this->deleteProduct($product);
36 }
37
38 if ($this->apiClient()->isClassic()) {
39 if ((bool)$wasIgnored !== (bool)$product->ignored) {
40 $this->apiClient()->setConsumerData([
41 'store' => home_url(),
42 'currency' => get_option('woocommerce_currency'),
43 'ignore_list' => array_map('strval', array_keys(WooMailerLiteOptions::get('ignored_products', []))),
44 'consumer_key' => WooMailerLiteOptions::get('consumerKey', null),
45 'consumer_secret' => WooMailerLiteOptions::get('consumerSecret', null),
46 'group_id' => WooMailerLiteOptions::get('group.id'),
47 'resubscribe' => WooMailerLiteOptions::get('settings.resubscribe'),
48 'create_segments' => false
49 ]);
50 }
51 } else {
52 $product->exclude_from_automations = $product->ignored;
53 $this->apiClient()->syncProduct(WooMailerLiteOptions::get('shopId'), $product->toArrayWithVariants(), true);
54 }
55 return true;
56 }
57
58 public function updateIgnoreProductList($productId)
59 {
60 $title = $this->request['post_title'];
61 $product = $this->resolveResource(WooMailerLiteProduct::class, $productId);
62 $ignoredProducts = WooMailerLiteOptions::get('ignored_products', []);
63
64 if ($this->requestHas('ml_ignore_product')) {
65
66 $ignoredProducts[$productId] = $title;
67
68 } else {
69 unset($ignoredProducts[$productId]);
70 }
71 WooMailerLiteOptions::update('ignored_products', $ignoredProducts);
72 if ($product) {
73 $product->ignored = $this->requestHas('ml_ignore_product');
74 $product->save();
75 }
76 return true;
77 }
78
79 public function updateProduct($productId)
80 {
81 if (did_action('woocommerce_update_product') === 1) {
82
83 if (defined('ICL_SITEPRESS_VERSION') && isset($_POST['icl_translation_of']) && !empty($_POST['icl_translation_of'])) {
84 return true;
85 }
86
87 $product = $this->resolveResource(WooMailerLiteProduct::class, $productId);
88 $shopId = WooMailerLiteOptions::get('shopId');
89 if ($product) {
90 $product->exclude_from_automations = $product->ignored ? 1 : 0;
91 $product->categories = $product->category_ids;
92 $response = $this->apiClient()->syncProduct($shopId, $product->toArrayWithVariants(), true);
93 if ($response->success) {
94 $product->tracked = true;
95 $product->save();
96 } else {
97 WooMailerLiteLog()->error('product:sync:failed', [
98 'product_id' => $product->resource_id,
99 'response' => $response
100 ]);
101 }
102
103 $ignoredProducts = WooMailerLiteOptions::get('ignored_products', []);
104
105 $wasIgnored = $product->ignored;
106
107 if ($this->request('ml_ignore_product')) {
108 $product->ignored = true;
109 $ignoredProducts[$product->resource_id] = $product->name;
110 } else {
111 $product->ignored = false;
112
113 unset($ignoredProducts[$product->resource_id]);
114 }
115 $product->save();
116
117 WooMailerLiteOptions::update('ignored_products', $ignoredProducts);
118 if ($this->apiClient()->isClassic()) {
119 if ((bool)$wasIgnored !== (bool)$product->ignored) {
120 $this->apiClient()->setConsumerData([
121 'store' => home_url(),
122 'currency' => get_option('woocommerce_currency'),
123 'ignore_list' => array_map('strval', array_keys(WooMailerLiteOptions::get('ignored_products', []))),
124 'consumer_key' => WooMailerLiteOptions::get('consumerKey', null),
125 'consumer_secret' => WooMailerLiteOptions::get('consumerSecret', null),
126 'group_id' => WooMailerLiteOptions::get('group.id'),
127 'resubscribe' => WooMailerLiteOptions::get('settings.resubscribe'),
128 'create_segments' => false
129 ]);
130 }
131 } else {
132 $product->exclude_from_automations = $product->ignored;
133 $this->apiClient()->syncProduct(WooMailerLiteOptions::get('shopId'), $product->toArrayWithVariants(), true);
134 }
135 } else {
136 WooMailerLiteLog()->error('product:update:not_found', [
137 'product_id' => $productId
138 ]);
139 }
140
141 return true;
142 }
143 }
144
145 public function updateCategory($categoryId)
146 {
147 if ((did_action('created_product_cat') === 1) || (did_action('edited_product_cat') === 1)) {
148 $category = $this->resolveResource(WooMailerLiteCategory::class, $categoryId);
149 if ($category) {
150 $response = $this->apiClient()->syncCategory($category->toArray());
151 if ($response->success) {
152 $category->tracked = true;
153 $category->save();
154 }
155 }
156 }
157 return true;
158 }
159
160 public function deleteCategory($categoryId)
161 {
162 if ((did_action('delete_product_cat') === 1)) {
163 $this->apiClient()->deleteCategory($categoryId);
164 }
165 return true;
166 }
167
168 private function deleteProduct($product)
169 {
170 $response = $this->apiClient()->deleteProduct($product->resource_id);
171 if ($response->success) {
172 return true;
173 }
174
175 WooMailerLiteLog()->error('product:delete:failed', [
176 'product_id' => $product->resource_id,
177 'error' => $response->message ?? 'Unknown error'
178 ]);
179
180 return false;
181 }
182
183 public function saveSettings()
184 {
185
186 $this->authorize()->validate([
187 'settings.group' => ['required', 'int'],
188 'settings.popUps' => ['sometimes', 'int'],
189 'settings.syncFields' => ['required', 'array']
190 ]);
191
192 $currentStatus = $this->apiClient()->getDoubleOptin();
193 $currentStatus->data->double_optin = $currentStatus->data->double_optin ?? $currentStatus->data->enabled ?? false;
194
195 if ($this->request('settings.doubleOptIn')) {
196 if ($currentStatus->success) {
197 if ($currentStatus->data->double_optin != $this->request('settings.doubleOptIn')) {
198 $this->apiClient()->setDoubleOptin($this->request('settings.doubleOptIn'));
199 }
200 }
201 } else {
202 if ($currentStatus->success) {
203 if ($currentStatus->data->double_optin) {
204 $this->apiClient()->setDoubleOptin(false);
205 }
206 }
207 }
208 if (!WooMailerLiteOptions::get('group') || (!empty(WooMailerLiteOptions::get('group', [])) && WooMailerLiteOptions::get('group')['id'] != $this->validated['settings.group'])) {
209 $response = $this->apiClient()->getGroupById($this->validated['settings.group']);
210 if (!$response->success) {
211 return $this->response($response, $response->status);
212 }
213 WooMailerLiteOptions::update('group', ['id' => $response->data->id, 'name' => $response->data->name]);
214 }
215 $shopName = get_bloginfo('name');
216 $shopName = !empty($shopName) ? $shopName : home_url();
217 $shopId = WooMailerLiteOptions::get('shopId');
218 $currency = get_option('woocommerce_currency');
219 $store = home_url();
220 if ($this->apiClient()->isReWrite()) {
221 if ($shopId === false) {
222 $shops = $this->apiClient()->getShops();
223 if ($shops->success) {
224 foreach ($shops->data as $shop) {
225 if ($shop->url == home_url()) {
226 $shopId = $shop->id;
227 WooMailerLiteOptions::update('shopId', $shopId);
228 break;
229 }
230 }
231 }
232 }
233 $data = [
234 'name' => $shopName,
235 'url' => $store,
236 'currency' => $currency,
237 'platform' => 'woocommerce',
238 'group_id' => $this->validated['settings.group'],
239 'enable_popups' => (bool)$this->request('settings.popUps'),
240 'enable_resubscribe' => (bool)$this->request('settings.resubscribe'),
241 'enabled' => true,
242 'access_data' => '-'
243 ];
244 } else {
245 $data = [
246 'consumer_key' => WooMailerLiteOptions::get('consumerKey', null),
247 'consumer_secret' => WooMailerLiteOptions::get('consumerSecret', null),
248 'store' => $store,
249 'currency' => $currency,
250 'group_id' => $this->validated['settings.group'],
251 'resubscribe' => $this->request('settings.resubscribe') ?? 0,
252 'ignore_list' => $this->request('settings.ignoreList') ?? '',
253 'create_segments' => $this->request('settings.createSegments') ?? ''
254 ];
255 }
256 $response = $this->apiClient()->setConsumerData($data);
257 if ($response->success) {
258 WooMailerLiteOptions::updateMultiple([
259 'shopId' => $response->data->id,
260 'popupsEnabled' => $response->data->enable_popups ?? ($this->request('settings.popUps') ?? false),
261 'syncFields' => $this->validated['settings.syncFields'],
262 'settings' => $this->request('settings'),
263 ]);
264 if (!$response->data->group) {
265 $response->data->group = WooMailerLiteOptions::get('group');
266 }
267 }
268 $message = 'Settings saved successfully.';
269 if (!$response->success) {
270 $message = $response->message ?? 'Unable to set up the shop. Please try again.';
271 }
272 return $this->response($response, $response->status, $message);
273 }
274
275 public function resetIntegration()
276 {
277 $this->authorize();
278 $this->apiClient()->toggleShop(home_url(), 0);
279 WooMailerLiteProductSyncResetJob::dispatchSync();
280 WooMailerLiteOptions::deleteAll();
281 WooMailerLiteMigration::rollback();
282 WooMailerLiteCache::delete('table_check');
283 return $this->response(['success' => true, 'message' => 'Integration settings reset'], 200);
284 }
285
286 public function downgradePlugin()
287 {
288 $this->authorize();
289
290 $slug = 'woo-mailerlite';
291 $version = '2.1.29';
292 $zip_url = "https://downloads.wordpress.org/plugin/{$slug}.{$version}.zip";
293
294 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
295 require_once ABSPATH . 'wp-admin/includes/plugin.php';
296
297 // Overwrite plugin files (no delete needed)
298 $upgrader = new Plugin_Upgrader(new Automatic_Upgrader_Skin());
299 $result = $upgrader->install($zip_url);
300
301 if (is_wp_error($result)) {
302 wp_die('Failed to install old version: ' . $result->get_error_message());
303 }
304
305 wp_redirect(admin_url('plugins.php?downgrade=success'));
306 exit;
307 }
308
309 public function enableDebugMode()
310 {
311 $this->authorize();
312 WooMailerLiteOptions::update('debugMode', !WooMailerLiteOptions::get('debugMode'));
313 return $this->response(['success' => true, 'message' => 'Debug mode enabled'], WooMailerLiteOptions::get('debugMode') ? 200 : 201);
314 }
315 }
316