PluginProbe
MailerLite – WooCommerce integration / 2.0
MailerLite – WooCommerce integration v2.0
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 / includes / classes / settings / SynchronizeSettings.php

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

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