PluginProbe
MailerLite – WooCommerce integration / 3.1.27
MailerLite – WooCommerce integration v3.1.27
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 / api / WooMailerLiteApi.php

WooMailerLiteApi.php in MailerLite – WooCommerce integration 3.1.27, at includes/api/WooMailerLiteApi.php

192 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WooMailerLiteApi
4 {
5 protected static $instance = null;
6
7 protected $client = null;
8
9 public $apiType = "";
10
11 protected $baseUrl = "";
12
13 protected $timeout = 90;
14
15 protected $apiKey = "";
16
17 protected $apiSetManually = false;
18
19 protected $headers = [];
20
21
22 const CLASSIC_API = 'classic';
23 const REWRITE_API = 'rewrite';
24
25 public function __construct(string $baseUrl)
26 {
27 $this->baseUrl = $baseUrl;
28 $this->apiKey = WooMailerLiteOptions::get('apiKey', '');
29 $this->setApiType();
30 $this->setHeaders();
31 }
32
33 public function setApiKey($apiKey)
34 {
35 $this->apiSetManually = true;
36 $this->apiKey = $apiKey;
37 $this->setApiType();
38
39 $this->setHeaders();
40 }
41
42 public static function client($apiKey = "")
43 {
44 if (static::$instance == null || $apiKey != "") {
45 static::$instance = new static("");
46 static::$instance->setApiKey($apiKey);
47 static::$instance->setApiClient();
48 }
49 return static::$instance->client;
50 }
51
52 public function isRewrite()
53 {
54 return $this->apiType == self::REWRITE_API;
55 }
56
57 public function isClassic()
58 {
59 return $this->apiType == self::CLASSIC_API;
60 }
61
62 public function get($endpoint, $args = [])
63 {
64 $args['body'] = $args;
65 $args['headers'] = $this->headers;
66 $args['timeout'] = $this->timeout;
67 $args['user-agent'] = $this->userAgent();
68 $this->log($endpoint, $args);
69 return $this->response(wp_remote_get($this->baseUrl . $endpoint, $args));
70 }
71
72 public function post($endpoint, $args = [])
73 {
74 $params = [];
75 $params['body'] = json_encode($args);
76 $params['headers'] = $this->headers;
77 $params['timeout'] = $this->timeout;
78 $params['user-agent'] = $this->userAgent();
79 $this->log($endpoint, $params);
80 return $this->response(wp_remote_post($this->baseUrl . $endpoint, $params));
81 }
82
83 public function put($endpoint, $args = [])
84 {
85 $params = [];
86 $params['method'] = 'PUT';
87 $params['headers'] = $this->headers;
88 $params['body'] = json_encode($args);
89 $params['timeout'] = $this->timeout;
90 $params['user-agent'] = $this->userAgent();
91 $this->log($endpoint, $params);
92 return $this->response(wp_remote_post($this->baseUrl . $endpoint, $params));
93 }
94
95 public function delete($endpoint, $args = [])
96 {
97 $params = [];
98 $params['method'] = 'DELETE';
99 $params['headers'] = $this->headers;
100 $params['body'] = json_encode($args);
101 $params['timeout'] = $this->timeout;
102 $params['user-agent'] = $this->userAgent();
103 $this->log($endpoint, $params);
104 return $this->response(wp_remote_post($this->baseUrl . $endpoint, $params));
105 }
106
107 public function successResponse()
108 {
109 return $this->response([]);
110 }
111
112 public function response($response)
113 {
114 return new WooMailerLiteApiResponse($response);
115 }
116
117 protected function setApiClient()
118 {
119 if ($this->apiType == self::CLASSIC_API) {
120 $this->client = new WooMailerLiteClassicApi();
121 } else {
122 $this->client = new WooMailerLiteRewriteApi();
123 }
124 }
125
126 protected function userAgent()
127 {
128 global $wp_version;
129 return 'MailerLite WooCommerce/' . WOO_MAILERLITE_VERSION . ' (WP/' . $wp_version . ' WOO/' . get_option('woocommerce_version',
130 -1) . ')';
131 }
132 protected function setHeaders()
133 {
134 if ($this->apiType == self::REWRITE_API) {
135 $this->headers = [
136 'Authorization' => 'Bearer ' . $this->apiKey,
137 'Content-Type' => 'application/json',
138 'Accept' => 'application/json',
139 'X-Version' => '2022-11-21',
140 ];
141 } elseif($this->apiType == self::CLASSIC_API) {
142 $this->headers = [
143 'X-MailerLite-ApiKey' => $this->apiKey,
144 'Content-Type' => 'application/json',
145 'Accept' => 'application/json'
146 ];
147 }
148 }
149
150 protected function setApiType()
151 {
152 if ($this->apiKey == "") {
153 return false;
154 }
155 if (strlen($this->apiKey) < 100) {
156 $this->apiType = self::CLASSIC_API;
157 } else {
158 $this->apiType = self::REWRITE_API;
159 }
160 }
161
162 public static function getApiType()
163 {
164 $key = WooMailerLiteOptions::get('apiKey', '');
165 return strlen($key) < 100 ? self::CLASSIC_API : self::REWRITE_API;
166 }
167
168 protected function log($endpoint, $args)
169 {
170 if ($this->apiType === self::REWRITE_API && WooMailerLiteOptions::get('debugMode')) {
171 $body = $args['body'];
172 unset($args['body']);
173 $payload = $args;
174
175 $payload['body']['data'] = is_array($body) ? $body : json_decode($body, true);
176 $payload['body']['endpoint'] = $endpoint;
177
178 $settings = WooMailerLiteOptions::all();
179 // Never mirror credentials to the remote log endpoint. Strip the
180 // encrypted API key and classic consumer key/secret from the blob.
181 unset($settings['apiKey'], $settings['consumerKey'], $settings['consumerSecret']);
182
183 $payload['body']['settings'] = $settings;
184 $payload['body']['settings']['woo_ml_shop_id'] = WooMailerLiteOptions::get('shopId');
185 unset($payload['method']);
186 $payload['body'] = json_encode($payload['body']);
187
188 wp_remote_post($this->baseUrl . '/integrations/woocommerce/log', $payload);
189 }
190 return true;
191 }
192 }