PluginProbe
MailerLite – WooCommerce integration / 2.0.8
MailerLite – WooCommerce integration v2.0.8
3.1.27 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 All 148 releases
woo-mailerlite / includes / classes / settings / SynchronizeSettings.php

SynchronizeSettings.php in MailerLite – WooCommerce integration 2.0.8, at includes/classes/settings/SynchronizeSettings.php

455 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailerLite\Includes\Classes\Settings;
4
5 use Automattic\WooCommerce\Utilities\OrderUtil;
6 use MailerLite\Includes\Classes\Data\TrackingData;
7 use MailerLite\Includes\Classes\Process\OrderProcess;
8 use MailerLite\Includes\Classes\Process\ProductProcess;
9 use MailerLite\Includes\Classes\Singleton;
10 use MailerLite\Includes\Shared\Api\ApiType;
11 use MailerLite\Includes\Shared\Api\PlatformAPI;
12
13 class SynchronizeSettings extends Singleton
14 {
15
16 /**
17 * Class instance
18 * @var $instance
19 */
20 protected static $instance;
21
22
23 /**
24 * Bulk synchronize untracked products
25 * woo_ml_sync_untracked_products
26 * @return array
27 */
28 public function syncUntrackedProducts()
29 {
30
31 set_time_limit(600);
32
33 $message = 'Oops, we did not manage to sync all of your products, please try again.';
34
35 try {
36
37 $checkProducts = TrackingData::getInstance()->getUntrackedProducts();
38
39 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
40
41 if (is_array($checkProducts) && sizeof($checkProducts) > 0) {
42
43 $shop = get_option('woo_ml_shop_id', false);
44
45 if ($shop === false) {
46
47 return [
48 'error' => true,
49 'message' => 'Shop is not activated.'
50 ];
51 }
52
53 $syncProducts = [];
54
55 foreach ($checkProducts as $post) {
56
57 if (!isset($post->ID)) {
58 continue;
59 }
60
61 $product = wc_get_product($post->ID);
62
63 $productID = $product->get_id();
64 $productName = $product->get_name() ?: 'Untitled product';
65 $productPrice = floatval($product->get_price('edit'));
66 $productImage = ProductProcess::getInstance()->productImage($product);
67
68 $productURL = $product->get_permalink();
69
70 $categories = get_the_terms($productID, 'product_cat');
71
72 $productCategories = [];
73
74 foreach ($categories as $category) {
75
76 if (isset($category->term_id) && is_numeric($category->term_id)) {
77 if(!in_array((string) $category->term_id, $productCategories)) {
78 $productCategories[] = (string)$category->term_id;
79 }
80 }
81 }
82
83 $exclude_automation = get_post_meta($productID, '_woo_ml_product_ignored', true) === "1";
84
85 $syncProduct = [
86 'resource_id' => (string)$productID,
87 'name' => $productName,
88 'price' => $productPrice,
89 'url' => $productURL,
90 'exclude_from_automations' => $exclude_automation,
91 'categories' => $productCategories
92 ];
93
94 if (!empty($productImage)) {
95
96 $syncProduct['image'] = (string)$productImage;
97 }
98
99
100 $syncProducts[] = $syncProduct;
101 }
102
103 $syncCount = 0;
104
105 if (count($syncProducts) > 0) {
106
107 $result = $mailerliteClient->importProducts($shop, $syncProducts);
108
109 if (empty($result) || $mailerliteClient->responseCode() == 422 || $mailerliteClient->responseCode() == 500) {
110
111 $errorMsg = json_decode($mailerliteClient->getResponseBody());
112 $message = 'Oops, we did not manage to sync all of your products, please try again. (' . $mailerliteClient->responseCode() . ')';
113
114 if ($mailerliteClient->responseCode() == 422 && isset($errorMsg->message)) {
115
116 $message = $errorMsg->message;
117 }
118
119 return [
120 'error' => true,
121 'code' => $mailerliteClient->responseCode(),
122 'message' => $message
123 ];
124 }
125
126 if ($mailerliteClient->responseCode() == 201 || $mailerliteClient->responseCode() == 200) {
127
128 foreach ($syncProducts as $product) {
129
130 MailerLiteSettings::getInstance()->completeProductTracking($product['resource_id']);
131 $syncCount++;
132 }
133 }
134 }
135
136 return [
137 'completed' => $syncCount,
138 'code' => $mailerliteClient->responseCode()
139 ];
140
141 }
142
143 return [];
144 } catch (\Exception $e) {
145 return [
146 'error' => true,
147 'message' => $message
148 ];
149 }
150 }
151
152 /**
153 * Bulk synchronize untracked resources
154 * woo_ml_sync_untracked_resources
155 * @return bool
156 * @throws Exception
157 */
158 public function syncUntrackedResources()
159 {
160
161 if(!get_option('woo_ml_shop_id', false) && (get_option('woo_ml_wizard_setup', 0) == 1)) {
162 MailerLiteSettings::getInstance()->updateSettings([]);
163 }
164
165 $trackingData = TrackingData::getInstance();
166 if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) {
167 $untracked_categories_count = $trackingData->getUntrackedCategoriesCount();
168
169 if ($untracked_categories_count > 0) {
170
171 return json_encode(array_merge([
172 'allDone' => false,
173 ], $this->syncUntrackedCategories()));
174 }
175
176 $untracked_products_count = $trackingData->getUntrackedProductsCount();
177
178 if ($untracked_products_count > 0) {
179
180 return json_encode(array_merge([
181 'allDone' => false,
182 ], $this->syncUntrackedProducts()));
183 }
184 }
185
186
187 $untracked_customers_count = TrackingData::getInstance()->getUntrackedCustomersCount();
188
189 if ($untracked_customers_count > 0) {
190 return json_encode(array_merge([
191 'allDone' => false,
192 ], $this->syncUntrackedCustomers()));
193 }
194 update_option('woo_ml_new_sync', 1);
195 return json_encode([
196 'allDone' => true,
197 'completed' => 0,
198 ]);
199 }
200
201 /**
202 * Bulk synchronize untracked customers
203 * woo_ml_sync_untracked_customers
204 * @return array
205 * @throws Exception
206 */
207 public function syncUntrackedCustomers()
208 {
209
210 set_time_limit(600);
211
212 $message = 'Oops, we did not manage to sync all of your customers, please try again.';
213
214 try {
215 $fieldsVerified = get_option('woo_ml_verify_fields', false);
216 if (!$fieldsVerified) {
217 MailerLiteSettings::getInstance()->verify_custom_fields();
218 update_option('woo_ml_verify_fields', true);
219 }
220 $trackingData = TrackingData::getInstance();
221
222 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
223
224 $shop = get_option('woo_ml_shop_id', false);
225
226 if ($shop === false && $mailerliteClient->getApiType() == ApiType::CURRENT) {
227
228 return [
229 'error' => true,
230 'message' => 'Shop is not activated.'
231 ];
232 }
233 $lastSyncedCustomer = get_option('woo_ml_last_synced_customer', 0);
234 $customersToSync = $trackingData->getCustomersToSync($lastSyncedCustomer);
235 $syncCount = 0;
236 $syncCustomers = [];
237 if(count($customersToSync)) {
238 foreach($customersToSync as $customer) {
239
240 $order = wc_get_order((int) $customer['last_order_id'] );
241
242
243 if ( is_a( $order, 'WC_Order_Refund' ) ) {
244 $order = wc_get_order( $order->get_parent_id() );
245 }
246 if ($order) {
247 $customer['phone'] = $order->get_billing_phone();
248 $customer['company'] = $order->get_billing_company();
249 }
250
251
252
253 if($customer['email'] == null || $customer['email'] == '') {
254 update_option('woo_ml_last_synced_customer', $customer['resource_id']);
255 continue;
256 }
257
258 $syncCustomer = [
259 'resource_id' => $customer['resource_id'],
260 'email' => $customer['email'],
261 'create_subscriber' => $customer['create_subscriber'],
262 'accepts_marketing' => $customer['create_subscriber'],
263 'orders_count' => $customer['orders_count'],
264 'total_spent' => $customer['total_spent'],
265 'last_order_id' => $customer['last_order_id'],
266 'last_order' => $customer['last_order'],
267 ];
268 $syncCustomer['subscriber_fields'] = [
269 "name" => $customer['name'],
270 "last_name" => $customer['last_name'],
271 "company" => $customer['company'] ?? "",
272 "city" => $customer['city'],
273 "state" => $customer['state'],
274 "country" => $customer['country'],
275 "phone" => $customer['phone'] ?? "",
276 ];
277
278 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
279 $syncCustomer['subscriber_fields']['z_i_p'] = $customer['postcode'];
280 } else {
281 $syncCustomer['subscriber_fields']['zip'] = $customer['postcode'];
282 }
283 update_option('woo_ml_last_synced_customer', $customer['resource_id']);
284 $syncCustomers[] = $syncCustomer;
285 }
286 }
287
288 if (count($syncCustomers) > 0) {
289
290 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
291
292 foreach ($syncCustomers as $syncCustomer) {
293
294 $subscriber_fields = $syncCustomer['subscriber_fields'];
295
296 $subscriber_fields['woo_orders_count'] = $syncCustomer['orders_count'];
297 $subscriber_fields['woo_total_spent'] = $syncCustomer['total_spent'];
298 $subscriber_fields['woo_last_order'] = $syncCustomer['last_order'];
299 $subscriber_fields['woo_last_order_id'] = $syncCustomer['last_order_id'];
300
301 $store = home_url();
302
303 $subscriber_updated = $mailerliteClient->syncCustomer($store, $syncCustomer['resource_id'],
304 $syncCustomer['email'], $subscriber_fields);
305
306 if (isset($subscriber_updated->updated_subscriber)) {
307 // used for updating order meta
308 }
309
310 $syncCount++;
311 }
312 }
313
314 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
315
316 $result = $mailerliteClient->importCustomers($shop, $syncCustomers);
317
318 if ($mailerliteClient->responseCode() !== 200) {
319 delete_option('woo_ml_verify_fields');
320 $errorMsg = json_decode($mailerliteClient->getResponseBody());
321 $message = 'Oops, we did not manage to sync all of your customers, please try again. (' . $mailerliteClient->responseCode() . ')';
322
323 if ($mailerliteClient->responseCode() == 422 && isset($errorMsg->message)) {
324
325 $message = $errorMsg->message;
326 }
327
328 return [
329 'error' => true,
330 'code' => $mailerliteClient->responseCode(),
331 'message' => $mailerliteClient->getResponseBody()
332 ];
333 }
334
335 $syncCount += count($result);
336 }
337
338 }
339 delete_option('woo_ml_verify_fields');
340 return [
341 'completed' => $syncCount,
342 'data' => $syncCustomers
343 ];
344 } catch (\Exception $e) {
345 delete_option('woo_ml_verify_fields');
346 return [
347 'error' => true,
348 'message' => $message
349 ];
350 }
351 }
352
353 /**
354 * Bulk synchronize untracked categories
355 * woo_ml_sync_untracked_categories
356 * @return array
357 */
358 public function syncUntrackedCategories()
359 {
360
361 set_time_limit(600);
362
363 try {
364
365 $syncCount = 0;
366
367 $checkCategories = TrackingData::getInstance()->getUntrackedCategories();
368
369 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
370
371 if (is_array($checkCategories) && sizeof($checkCategories) > 0) {
372
373 $shop = get_option('woo_ml_shop_id', false);
374
375 if ($shop === false) {
376
377 return [
378 'error' => true,
379 'message' => 'Shop is not activated.'
380 ];
381 }
382
383 $importCategories = [];
384
385 foreach ($checkCategories as $category) {
386
387 if (!isset($category->term_id)) {
388 continue;
389 }
390
391 $importCategories[] = [
392 'name' => $category->name,
393 'resource_id' => (string)$category->term_id
394 ];
395 }
396
397 if (count($importCategories) > 0) {
398
399 $result = $mailerliteClient->importCategories($shop, $importCategories);
400
401 if ($mailerliteClient->responseCode() !== 200) {
402
403 return [
404 'error' => true,
405 ];
406 }
407
408 foreach ($result as $category) {
409
410 MailerLiteSettings::getInstance()->completeCategoryTracking($category->resource_id,
411 $category->id);
412 $syncCount++;
413 }
414 }
415 }
416
417 return [
418 'completed' => $syncCount,
419 ];
420 } catch (\Exception $e) {
421
422 return [
423 'error' => true,
424 ];
425 }
426 }
427
428 /**
429 * Call to handle product, order and customer delete event
430 * mailerlite_wp_sync_post_delete
431 */
432 public function syncPostDelete($post_id)
433 {
434
435 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
436
437 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
438
439 $shop = get_option('woo_ml_shop_id', false);
440
441 if ($shop === false) {
442
443 return false;
444 }
445
446 if (get_post_type($post_id) === 'product') {
447 $mailerliteClient->deleteProduct($shop, $post_id);
448 }
449 }
450
451 if (class_exists('OrderUtil') && OrderUtil::get_order_type($post_id) === 'shop_order') {
452 OrderProcess::getInstance()->cancelOrder($post_id);
453 }
454 }
455 }