PluginProbe
MailerLite – WooCommerce integration / 2.0.4
MailerLite – WooCommerce integration v2.0.4
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.4, at includes/classes/settings/SynchronizeSettings.php

486 lines 16.7 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 $total_customers_count = TrackingData::getInstance()->getAllCustomersCount();
187 $tracked_customers_count = TrackingData::getInstance()->getTrackedCustomersCount();
188 $tracked_guest_count = TrackingData::getInstance()->getTrackedGuestCustomersCount();
189
190 $untracked_customers_count = $total_customers_count - $tracked_customers_count - $tracked_guest_count;
191
192 if ($untracked_customers_count > 0) {
193 return json_encode(array_merge([
194 'allDone' => false,
195 ], $this->syncUntrackedCustomers()));
196 }
197
198 return json_encode([
199 'allDone' => true,
200 'completed' => 0,
201 ]);
202 }
203
204 /**
205 * Bulk synchronize untracked customers
206 * woo_ml_sync_untracked_customers
207 * @return array
208 * @throws Exception
209 */
210 public function syncUntrackedCustomers()
211 {
212
213 set_time_limit(600);
214
215 $message = 'Oops, we did not manage to sync all of your customers, please try again.';
216
217 try {
218
219 $trackingData = TrackingData::getInstance();
220
221 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
222
223 $shop = get_option('woo_ml_shop_id', false);
224
225 if ($shop === false && $mailerliteClient->getApiType() == ApiType::CURRENT) {
226
227 return [
228 'error' => true,
229 'message' => 'Shop is not activated.'
230 ];
231 }
232
233 $registeredCustomers = $trackingData->getRegisteredCustomersToSync();
234 $syncCount = 0;
235 $syncCustomers = [];
236 if(count($registeredCustomers)) {
237 foreach($registeredCustomers as $customer) {
238 if($customer['email'] == null || $customer['email'] == '') {
239 continue;
240 }
241 $wc_customer = new \WC_Customer($customer['user_id']);
242
243 $syncCustomer = [
244 'resource_id' => $customer['resource_id'],
245 'email' => $customer['email'],
246 'create_subscriber' => $customer['create_subscriber'],
247 'accepts_marketing' => $customer['create_subscriber'],
248 'orders_count' => $customer['orders_count'],
249 'total_spent' => $customer['total_spent'],
250 'last_order_id' => $customer['last_order_id'],
251 'last_order' => $customer['last_order'],
252 ];
253 $syncCustomer['subscriber_fields'] = [
254 "name" => $customer['name'],
255 "last_name" => $customer['last_name'],
256 "company" => $customer['company'],
257 "city" => $customer['city'],
258 "state" => $customer['state'],
259 "country" => $customer['country'],
260 "phone" => $customer['phone'],
261 ];
262
263 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
264 $syncCustomer['subscriber_fields']['z_i_p'] = $customer['postcode'];
265 } else {
266 $syncCustomer['subscriber_fields']['zip'] = $customer['postcode'];
267 }
268 $wc_customer->add_meta_data('_woo_ml_customer_tracked', true, true);
269 $wc_customer->save_meta_data();
270 $syncCustomers[] = $syncCustomer;
271 }
272 } else {
273 $lastSyncedGuest = get_option('woo_ml_last_synced_guest_id', 0);
274
275
276 $guestCustomers = $trackingData->getGuestCustomersToSync($lastSyncedGuest);
277
278 if(!count($guestCustomers)) {
279 return [
280 'allDone' => true,
281 'completed' => 0,
282 ];
283 }
284 foreach ($guestCustomers as $customer) {
285 if($customer['email'] == null || $customer['email'] == '') {
286 update_option('woo_ml_last_synced_guest_id', $customer['resource_id']);
287 continue;
288 }
289 $syncCustomer = [
290 'resource_id' => $customer['resource_id'],
291 'email' => $customer['email'],
292 'create_subscriber' => $customer['create_subscriber'],
293 'accepts_marketing' => $customer['create_subscriber'],
294 'orders_count' => $customer['orders_count'],
295 'total_spent' => $customer['total_spent'],
296 'last_order_id' => $customer['last_order_id'],
297 'last_order' => $customer['last_order'],
298 ];
299 $syncCustomer['subscriber_fields'] = [
300 "name" => $customer['name'],
301 "last_name" => $customer['last_name'],
302 "company" => $customer['company'],
303 "city" => $customer['city'],
304 "state" => $customer['state'],
305 "country" => $customer['country'],
306 "phone" => $customer['phone'],
307 ];
308
309 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
310 $syncCustomer['subscriber_fields']['z_i_p'] = $customer['postcode'];
311 } else {
312 $syncCustomer['subscriber_fields']['zip'] = $customer['postcode'];
313 }
314 $syncCustomers[] = $syncCustomer;
315
316 update_option('woo_ml_last_synced_guest_id', end($guestCustomers)['resource_id']);
317 }
318 }
319
320 if (count($syncCustomers) > 0) {
321
322 if ($mailerliteClient->getApiType() == ApiType::CLASSIC) {
323
324 foreach ($syncCustomers as $syncCustomer) {
325
326 $subscriber_fields = $syncCustomer['subscriber_fields'];
327
328 $subscriber_fields['woo_orders_count'] = $syncCustomer['orders_count'];
329 $subscriber_fields['woo_total_spent'] = $syncCustomer['total_spent'];
330 $subscriber_fields['woo_last_order'] = $syncCustomer['last_order'];
331 $subscriber_fields['woo_last_order_id'] = $syncCustomer['last_order_id'];
332
333 $store = home_url();
334
335 $subscriber_updated = $mailerliteClient->syncCustomer($store, $syncCustomer['resource_id'],
336 $syncCustomer['email'], $subscriber_fields);
337
338 if (isset($subscriber_updated->updated_subscriber)) {
339 // used for updating order meta
340 }
341
342 $syncCount++;
343 }
344 }
345
346 if ($mailerliteClient->getApiType() == ApiType::CURRENT) {
347
348 $result = $mailerliteClient->importCustomers($shop, $syncCustomers);
349
350 if ($mailerliteClient->responseCode() !== 200) {
351
352 $errorMsg = json_decode($mailerliteClient->getResponseBody());
353 $message = 'Oops, we did not manage to sync all of your customers, please try again. (' . $mailerliteClient->responseCode() . ')';
354
355 if ($mailerliteClient->responseCode() == 422 && isset($errorMsg->message)) {
356
357 $message = $errorMsg->message;
358 }
359
360 return [
361 'error' => true,
362 'code' => $mailerliteClient->responseCode(),
363 'message' => $mailerliteClient->getResponseBody()
364 ];
365 }
366
367 $syncCount += count($result);
368 }
369
370 }
371
372 return [
373 'completed' => $syncCount,
374 'data' => $syncCustomers
375 ];
376 } catch (\Exception $e) {
377 return [
378 'error' => true,
379 'message' => $message
380 ];
381 }
382 }
383
384 /**
385 * Bulk synchronize untracked categories
386 * woo_ml_sync_untracked_categories
387 * @return array
388 */
389 public function syncUntrackedCategories()
390 {
391
392 set_time_limit(600);
393
394 try {
395
396 $syncCount = 0;
397
398 $checkCategories = TrackingData::getInstance()->getUntrackedCategories();
399
400 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
401
402 if (is_array($checkCategories) && sizeof($checkCategories) > 0) {
403
404 $shop = get_option('woo_ml_shop_id', false);
405
406 if ($shop === false) {
407
408 return [
409 'error' => true,
410 'message' => 'Shop is not activated.'
411 ];
412 }
413
414 $importCategories = [];
415
416 foreach ($checkCategories as $category) {
417
418 if (!isset($category->term_id)) {
419 continue;
420 }
421
422 $importCategories[] = [
423 'name' => $category->name,
424 'resource_id' => (string)$category->term_id
425 ];
426 }
427
428 if (count($importCategories) > 0) {
429
430 $result = $mailerliteClient->importCategories($shop, $importCategories);
431
432 if ($mailerliteClient->responseCode() !== 200) {
433
434 return [
435 'error' => true,
436 ];
437 }
438
439 foreach ($result as $category) {
440
441 MailerLiteSettings::getInstance()->completeCategoryTracking($category->resource_id,
442 $category->id);
443 $syncCount++;
444 }
445 }
446 }
447
448 return [
449 'completed' => $syncCount,
450 ];
451 } catch (\Exception $e) {
452
453 return [
454 'error' => true,
455 ];
456 }
457 }
458
459 /**
460 * Call to handle product, order and customer delete event
461 * mailerlite_wp_sync_post_delete
462 */
463 public function syncPostDelete($post_id)
464 {
465
466 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
467
468 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
469
470 $shop = get_option('woo_ml_shop_id', false);
471
472 if ($shop === false) {
473
474 return false;
475 }
476
477 if (get_post_type($post_id) === 'product') {
478 $mailerliteClient->deleteProduct($shop, $post_id);
479 }
480
481 if (class_exists('OrderUtil') && OrderUtil::get_order_type($post_id) === 'shop_order') {
482 $mailerliteClient->deleteOrder($shop, $post_id);
483 }
484 }
485 }
486 }