PluginProbe
MailerLite – WooCommerce integration / 2.0.2
MailerLite – WooCommerce integration v2.0.2
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-mailerlite-api.php

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

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