PluginProbe
MailerLite – WooCommerce integration / 1.8.7
MailerLite – WooCommerce integration v1.8.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 / shared / api / class.woo-mailerlite-classic-api.php

class.woo-mailerlite-classic-api.php in MailerLite – WooCommerce integration 1.8.7, at includes/shared/api/class.woo-mailerlite-classic-api.php

949 lines 19.0 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\Shared\Api;
4
5 class MailerLiteClassicAPI
6 {
7
8 private $url = 'https://api.mailerlite.com/api/v2';
9 private $client;
10 private $response;
11 private $response_code;
12
13 /**
14 * MailerLiteClassicAPI constructor
15 *
16 * @access public
17 * @return void
18 * @since 1.6.0
19 */
20 public function __construct($api_key)
21 {
22 $this->client = new MailerLiteClient($this->url, [
23 'X-MailerLite-ApiKey' => $api_key,
24 'Content-Type' => 'application/json',
25 'Accept' => 'application/json'
26 ]);
27 }
28
29 /**
30 * Validate API Key
31 *
32 * @access public
33 * @return mixed
34 * @since 1.6.0
35 */
36 public function validateKey()
37 {
38
39 $response = $this->client->remote_get('/');
40 $response = self::parseResponse($response);
41
42 $account = false;
43
44 if ($this->response_code === 200) {
45
46 $account = [];
47 $account['id'] = $response->account->id;
48 $account['subdomain'] = $response->account->subdomain;
49 }
50
51
52 return $account;
53 }
54
55 /**
56 * Update subscriber
57 *
58 * @access public
59 * @return mixed
60 * @since 1.6.0
61 */
62 public function updateSubscriber($subscriber_email, $subscriber_data)
63 {
64
65 $response = $this->client->remote_put('/subscribers/' . $subscriber_email, $subscriber_data);
66
67 return self::parseResponse($response);
68 }
69
70 /**
71 * Update subscriber status
72 *
73 * @access public
74 * @return mixed
75 * @since 1.7.4
76 */
77 public function updateSubscriberStatus($email, $status)
78 {
79
80 return true;
81 }
82
83 /**
84 * Search for a subscriber
85 *
86 * @access public
87 * @return mixed
88 * @since 1.6.0
89 */
90 public function searchSubscriber($query)
91 {
92
93 $response = $this->client->remote_get('/subscribers/' . $query);
94
95 return self::parseResponse($response);
96 }
97
98 /**
99 * Get groups
100 *
101 * @access public
102 * @return mixed
103 * @since 1.6.0
104 */
105 public function getGroups($params = [])
106 {
107 $response = $this->client->remote_get('/groups', $params);
108
109 return self::parseResponse($response);
110 }
111
112 /**
113 * Check if more groups need to be loaded
114 *
115 * @access public
116 * @return mixed
117 * @since 1.6.0
118 */
119 public function checkMoreGroups($limit, $offset)
120 {
121
122 $response = $this->client->remote_get('/groups', [
123 'limit' => $limit,
124 'offset' => ($offset - 1) * $limit
125 ]);
126
127 return count(self::parseResponse($response)) > 0;
128 }
129
130 /**
131 * Get more groups
132 *
133 * @access public
134 * @return mixed
135 * @since 1.6.0
136 */
137 public function getMoreGroups($limit, $offset)
138 {
139
140 $response = $this->client->remote_get('/groups', [
141 'limit' => $limit,
142 'offset' => ($offset - 1) * $limit
143 ]);
144
145 return self::parseResponse($response);
146 }
147
148 /**
149 * Get Double opt-in setting
150 *
151 * @access public
152 * @return bool
153 * @since 1.6.0
154 */
155 public function getDoubleOptin()
156 {
157 $response = $this->client->remote_get('/settings/double_optin');
158 $response = self::parseResponse($response);
159
160 if (isset($response->enabled) && $response->enabled) {
161
162 return true;
163 }
164
165 return false;
166 }
167
168 /**
169 * Enable/Disable Double opt-in setting
170 *
171 * @access public
172 * @return bool
173 * @since 1.6.0
174 */
175 public function setDoubleOptin($enable)
176 {
177
178 $response = $this->client->remote_post('/settings/double_optin', ['enable' => $enable]);
179 $response = self::parseResponse($response);
180
181 if (isset($response->enabled) && $response->enabled) {
182
183 return true;
184 }
185
186 return false;
187 }
188
189 /**
190 * Get Fields
191 *
192 * @access public
193 * @return mixed
194 * @since 1.6.0
195 */
196 public function getFields($params = [])
197 {
198
199 $response = $this->client->remote_get('/fields', $params);
200
201 return self::parseResponse($response);
202 }
203
204 /**
205 * Create Custom Field
206 *
207 * @access public
208 * @return mixed
209 * @since 1.6.0
210 */
211 public function createField($title, $type)
212 {
213
214 $response = $this->client->remote_post('/fields', [
215 'title' => $title,
216 'type' => $type
217 ]);
218
219 return self::parseResponse($response);
220 }
221
222 /**
223 * Update Custom Field
224 *
225 * @access public
226 * @return mixed
227 * @since 1.7.11
228 */
229 public function updateField($id, $name)
230 {
231
232 $response = $this->client->remote_put('/fields/' . $id, [
233 'title' => $name,
234 ]);
235
236 return self::parseResponse($response);
237 }
238
239 /**
240 * Sync WooCommerce Customer
241 *
242 * @access public
243 * @return mixed
244 * @since 1.6.0
245 */
246 public function syncCustomerWooCommerce($customer_id, $email, $fields, $shopUrl)
247 {
248
249 $response = $this->client->remote_post('/woocommerce/sync_customer', [
250 'email' => $email,
251 'subscriber_fields' => $fields,
252 'shop' => $shopUrl
253 ]);
254
255 return self::parseResponse($response);
256 }
257
258 /**
259 * Set WooCommerce Consumer Data
260 *
261 * @access public
262 * @return mixed
263 * @since 1.6.0
264 */
265 public function setConsumerData(
266 $consumerKey,
267 $consumerSecret,
268 $store,
269 $currency,
270 $group_id,
271 $resubscribe,
272 $ignoreList,
273 $create_segments,
274 $shop_name,
275 $shop_id,
276 $popups_enabled
277 ) {
278
279 $response = $this->client->remote_post('/woocommerce/consumer_data', [
280 'consumer_key' => $consumerKey,
281 'consumer_secret' => $consumerSecret,
282 'store' => $store,
283 'currency' => $currency,
284 'group_id' => $group_id,
285 'resubscribe' => $resubscribe,
286 'ignore_list' => $ignoreList,
287 'create_segments' => $create_segments
288 ]);
289
290 return self::parseResponse($response);
291 }
292
293 /**
294 * Send WooCommerce Subscriber Data
295 *
296 * @access public
297 * @return mixed
298 * @since 1.6.0
299 */
300 public function sendSubscriberData($shop_id, $data)
301 {
302
303 $response = $this->client->remote_post('/woocommerce/save_subscriber', ['data' => $data]);
304
305 return self::parseResponse($response);
306 }
307
308 /**
309 * Save WooCommerce Order
310 *
311 * @access public
312 * @return mixed
313 * @since 1.6.0
314 */
315 public function saveOrder($shop, $orderData)
316 {
317
318 $response = $this->client->remote_post('/woocommerce/alternative_save_order', [
319 'order_data' => $orderData,
320 'shop' => $shop
321 ]);
322
323 return self::parseResponse($response);
324 }
325
326 /**
327 * Send WooCommerce Order Processing
328 *
329 * @access public
330 * @return mixed
331 * @since 1.6.0
332 */
333 public function sendOrderProcessing($shop_id, $data)
334 {
335
336 $response = $this->client->remote_post('/woocommerce/order_processing', ['data' => $data]);
337
338 return self::parseResponse($response);
339 }
340
341 /**
342 * Send WooCommerce Cart
343 *
344 * @access public
345 * @return mixed
346 * @since 1.6.0
347 */
348 public function sendCart($shop, $cartData)
349 {
350
351 $response = $this->client->remote_post('/woocommerce/save_cart', [
352 'cart_data' => $cartData,
353 'shop' => $shop
354 ]);
355
356 return self::parseResponse($response);
357 }
358
359 /**
360 * Toggle WooCommerce Shop
361 *
362 * @access public
363 *
364 * @param $shop
365 * @param $activeState
366 * @param $shop_id
367 * @param $shop_name
368 *
369 * @return mixed
370 * @since 1.6.0
371 */
372 public function toggleShop($shop, $activeState, $shop_id, $shop_name)
373 {
374
375 $shopName = parse_url($shop, PHP_URL_HOST);
376
377 $response = $this->client->remote_post('/woocommerce/toggle_shop_connection', [
378 'active_state' => $activeState,
379 'shop' => $shopName
380 ]);
381
382 return self::parseResponse($response);
383 }
384
385 /**
386 * Get WooCommerce Shop Settings
387 *
388 * @access public
389 * @return mixed
390 * @since 1.6.0
391 */
392 public function getShopSettings($shopUrl)
393 {
394
395 $shopName = parse_url($shopUrl, PHP_URL_HOST);
396
397 $response = $this->client->remote_get('/woocommerce/settings/' . $shopName);
398
399 return self::parseResponse($response);
400 }
401
402 /**
403 * Validate WooCommerce Account
404 *
405 * @access public
406 * @return mixed
407 * @since 1.6.0
408 */
409 public function validateAccount()
410 {
411
412 $response = $this->client->remote_get('/woocommerce/initial_account_settings');
413
414 return self::parseResponse($response);
415 }
416
417 /**
418 * Get list of shops
419 *
420 * @access public
421 * @return mixed
422 * @since 1.6.0
423 */
424 public function getShops()
425 {
426
427 return [];
428 }
429
430 /**
431 * Get a shop
432 *
433 * @access public
434 * @return mixed
435 * @since 1.6.0
436 */
437 public function getShop($shop_id)
438 {
439
440 return false;
441 }
442
443 /**
444 * Create shop
445 *
446 * @access public
447 *
448 * @param $name
449 * @param $url
450 * @param $currency
451 * @param $group_id
452 * @param $enable_popups
453 * @param $access_data
454 *
455 * @return bool
456 * @since 1.6.0
457 */
458 public function createShop($name, $url, $currency, $group_id, $enable_popups, $access_data)
459 {
460 return true;
461 }
462
463 /**
464 * Delete shop
465 *
466 * @access public
467 *
468 * @param $shop
469 *
470 * @return bool
471 * @since 1.6.0
472 */
473 public function deleteShop($shop)
474 {
475 return true;
476 }
477
478 /**
479 * Fetch order
480 *
481 * @access public
482 *
483 * @param $shop_id
484 * @param $order_id
485 *
486 * @return mixed
487 * @since 1.6.0
488 */
489 public function fetchOrder($shop_id, $order_id)
490 {
491
492 return false;
493 }
494
495 /**
496 * Import orders
497 *
498 * @access public
499 *
500 * @param $shop_id
501 * @param $orders
502 *
503 * @return bool
504 * @since 1.6.0
505 */
506 public function importOrders($shop_id, $orders)
507 {
508
509 return true;
510 }
511
512 /**
513 * Import customers
514 *
515 * @access public
516 *
517 * @param $shop_id
518 * @param $customers
519 *
520 * @return array
521 * @since 1.8.5
522 */
523 public function importCustomers($shop_id, $customers)
524 {
525
526 return [];
527 }
528
529 /**
530 * Sync order
531 *
532 * @access public
533 *
534 * @param $shop_id
535 * @param $order_id
536 * @param $customer
537 * @param $cart
538 * @param $status
539 * @param $total_price
540 * @param $created_at
541 *
542 * @return mixed
543 * @since 1.6.0
544 */
545 public function syncOrder($shop_id, $order_id, $customer, $cart, $status, $total_price, $created_at)
546 {
547
548 return true;
549 }
550
551 /**
552 * Delete order
553 *
554 * @access public
555 *
556 * @param $shop_id
557 * @param $order_id
558 *
559 * @return mixed
560 * @since 1.6.0
561 */
562 public function deleteOrder($shop_id, $order_id)
563 {
564
565 return true;
566 }
567
568 /**
569 * Update order
570 *
571 * @access public
572 *
573 * @param $shop_id
574 * @param $order_id
575 * @param $status
576 * @param $total_price
577 *
578 * @return mixed
579 * @since 1.6.0
580 */
581 public function updateOrder($shop_id, $order_id, $status, $total_price)
582 {
583
584 return true;
585 }
586
587 /**
588 * Sync product
589 *
590 * @access public
591 *
592 * @param $shop_id
593 * @param $product_id
594 * @param $name
595 * @param $price
596 * @param $exclude_automation
597 * @param $url
598 * @param $image
599 * @param $categories
600 *
601 * @return bool
602 * @since 1.6.0
603 */
604 public function syncProduct($shop_id, $product_id, $name, $price, $exclude_automation, $url, $image, $categories)
605 {
606
607 return true;
608 }
609
610 /**
611 * Replace product categories
612 *
613 * @access public
614 *
615 * @param $shop_id
616 * @param $product_id
617 * @param $categories
618 *
619 * @return bool
620 * @since 1.6.0
621 */
622 public function replaceProductCategories($shop_id, $product_id, $categories)
623 {
624
625 return true;
626 }
627
628 /**
629 * Import products
630 *
631 * @access public
632 *
633 * @param @shop_id
634 * @param @products
635 *
636 * @return array
637 * @since 1.6.0
638 */
639 public function importProducts($shop_id, $products)
640 {
641
642 return [];
643 }
644
645 /**
646 * Delete product
647 *
648 * @access public
649 *
650 * @param $shop_id
651 * @param $product_id
652 *
653 * @return mixed
654 * @since 1.6.0
655 */
656 public function deleteProduct($shop_id, $product_id)
657 {
658
659 return true;
660 }
661
662 /**
663 * Create product category
664 *
665 * @access public
666 *
667 * @param $shop_id
668 * @param $category_id
669 * @param $name
670 *
671 * @return mixed
672 * @since 1.6.0
673 */
674 public function syncCategory($shop_id, $category_id, $name)
675 {
676
677 return [];
678 }
679
680 /**
681 * Import categories
682 *
683 * @access public
684 *
685 * @param @shop_id
686 * @param @categories
687 *
688 * @return array
689 * @since 1.6.0
690 */
691 public function importCategories($shop_id, $categories)
692 {
693
694 return [];
695 }
696
697
698 /**
699 * Delete product category
700 *
701 * @access public
702 *
703 * @param $shop_id
704 * @param $category_id
705 *
706 * @return mixed
707 * @since 1.6.0
708 */
709 public function deleteCategory($shop_id, $category_id)
710 {
711
712 return true;
713 }
714
715 /**
716 * Sync customer
717 *
718 * @access public
719 *
720 * @param $shop_id
721 * @param $customer_id
722 * @param $email
723 * @param $fields
724 *
725 * @return mixed
726 * @since 1.6.0
727 */
728 public function syncCustomer($shop_id, $customer_id, $email, $fields)
729 {
730
731 return $this->syncCustomerWooCommerce($customer_id, $email, $fields, $shop_id);
732 }
733
734 /**
735 * Update customer
736 *
737 * @access public
738 *
739 * @param $shop_id
740 * @param $customer_id
741 * @param $email
742 * @param $accepts_marketing
743 * @param $create_subscriber
744 *
745 * @return mixed
746 * @since 1.6.0
747 */
748 public function updateCustomer($shop_id, $customer_id, $email, $accepts_marketing, $create_subscriber)
749 {
750
751 return true;
752 }
753
754 /**
755 * Delete customer
756 *
757 * @access public
758 *
759 * @param $shop
760 * @param $customer_id
761 *
762 * @return bool
763 * @since 1.6.0
764 */
765 public function deleteCustomer($shop, $customer_id)
766 {
767 return true;
768 }
769
770 /**
771 * Fetch order
772 *
773 * @access public
774 *
775 * @param $shop_id
776 * @param $customer_id
777 *
778 * @return mixed
779 * @since 1.6.0
780 */
781 public function fetchCustomer($shop_id, $customer_id)
782 {
783
784 return true;
785 }
786
787 /**
788 * Update cart
789 *
790 * @access public
791 *
792 * @param $shop_id
793 * @param $cart_id
794 * @param $checkout_url
795 * @param $cart_total
796 * @param $order_id
797 *
798 * @return mixed
799 * @since 1.6.0
800 */
801 public function updateCart($shop_id, $cart_id, $checkout_url, $cart_total, $order_id)
802 {
803
804 return true;
805 }
806
807 /**
808 * Add cart items
809 *
810 * @access public
811 *
812 * @param $shop_id
813 * @param $cart_id
814 * @param $item
815 *
816 * @return mixed
817 * @since 1.6.0
818 */
819 public function addCartItem($shop_id, $cart_id, $item)
820 {
821
822 return true;
823 }
824
825 /**
826 * Replace cart items
827 *
828 * @access public
829 *
830 * @param $shop_id
831 * @param $cart_id
832 * @param $items
833 *
834 * @return mixed
835 * @since 1.6.0
836 */
837 public function replaceCartItems($shop_id, $cart_id, $items)
838 {
839
840 return true;
841 }
842
843 /**
844 * Get cart items
845 *
846 * @access public
847 *
848 * @param $shop_id
849 * @param $cart_id
850 *
851 * @return mixed
852 * @since 1.6.0
853 */
854 public function getCartItems($shop_id, $cart_id)
855 {
856
857 return true;
858 }
859
860 /**
861 * Delete cart items
862 *
863 * @access public
864 *
865 * @param $shop_id
866 * @param $cart_id
867 * @param $item_id
868 *
869 * @return mixed
870 * @since 1.6.0
871 */
872 public function deleteCartItem($shop_id, $cart_id, $item_id)
873 {
874
875 return true;
876 }
877
878 /**
879 * Batch process requests
880 *
881 * @access public
882 *
883 * @param $requests
884 *
885 * @return mixed
886 * @since 1.6.6
887 */
888 public function batch($requests)
889 {
890 return true;
891 }
892
893 /**
894 * Get raw response body
895 *
896 * @access public
897 * @return string
898 * @since 1.6.0
899 */
900 public function getResponseBody()
901 {
902 return $this->response;
903 }
904
905 /**
906 * Get response code
907 *
908 * @access public
909 * @return int
910 * @since 1.6.0
911 */
912 public function responseCode()
913 {
914 return $this->response_code;
915 }
916
917 /**
918 * Get response code and body
919 *
920 * @access private
921 * @return mixed
922 * @since 1.6.0
923 */
924 private function parseResponse($response)
925 {
926 if ( ! is_wp_error($response)) {
927
928 $this->response = wp_remote_retrieve_body($response);
929 $this->response_code = wp_remote_retrieve_response_code($response);
930
931 if ( ! is_wp_error($this->response)) {
932 $response = json_decode($this->response);
933
934 if (json_last_error() == JSON_ERROR_NONE) {
935
936 if ( ! isset($response->error)) {
937 return $response;
938 }
939 }
940 }
941 } else {
942 $this->response = $response->get_error_message();
943 $this->response_code = $response->get_error_code();
944 }
945
946 return false;
947 }
948
949 }