PluginProbe
MailerLite – WooCommerce integration / 2.0.3
MailerLite – WooCommerce integration v2.0.3
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.3, at includes/classes/settings/SynchronizeSettings.php

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