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

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