PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.7
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.7
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / inc / SendinblueApiClient.php
mailin / inc Last commit date
templates 6 years ago SendinblueApiClient.php 5 years ago function.wp_mail.php 8 years ago index.php 8 years ago mailin.php 5 years ago sendinblue.php 5 years ago sib-api-manager.php 5 years ago sib-form-preview.php 5 years ago sib-sms-code.php 8 years ago table-forms.php 5 years ago
SendinblueApiClient.php
374 lines
1 <?php
2
3
4 class SendinblueApiClient
5 {
6 const API_BASE_URL = 'https://api.sendinblue.com/v3';
7 const HTTP_METHOD_GET = 'GET';
8 const HTTP_METHOD_POST = 'POST';
9 const HTTP_METHOD_PUT = 'PUT';
10 const HTTP_METHOD_DELETE = 'DELETE';
11 const CAMPAIGN_TYPE_EMAIL = 'email';
12 const CAMPAIGN_TYPE_SMS = 'sms';
13 const RESPONSE_CODE_OK = 200;
14 const RESPONSE_CODE_CREATED = 201;
15 const RESPONSE_CODE_ACCEPTED = 202;
16 const RESPONSE_CODE_UNAUTHORIZED = 401;
17 const PLUGIN_VERSION = '3.1.7';
18
19 private $apiKey;
20 private $lastResponseCode;
21
22 /**
23 * SendinblueApiClient constructor.
24 */
25 public function __construct()
26 {
27 $this->apiKey = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
28 }
29
30 /**
31 * @return mixed
32 */
33 public function getAccount()
34 {
35 return $this->get('/account');
36 }
37
38 /**
39 * @return mixed
40 */
41 public function getAttributes()
42 {
43 return $this->get("/contacts/attributes");
44 }
45
46 /**
47 * @param $type ,$name,$data
48 * @return mixed
49 */
50 public function createAttribute($type, $name, $data)
51 {
52 return $this->post("/contacts/attributes/" . $type . "/" . $name, $data);
53 }
54
55 /**
56 * @param $id
57 * @return mixed
58 */
59 public function getEmailTemplate($id)
60 {
61 return $this->get("/smtp/templates/" . $id);
62 }
63
64 /**
65 * @param string $type
66 * @param array $data
67 * @return array
68 */
69 public function getAllCampaignsByType($type = self::CAMPAIGN_TYPE_EMAIL, $data = [])
70 {
71 $campaigns = [];
72
73 if (!isset($data['offset'])) {
74 $data['offset'] = 0;
75 }
76
77 do {
78 if ($type === self::CAMPAIGN_TYPE_SMS) {
79 $response = $this->getSmsCampaigns($data);
80 } else {
81 $response = $this->getEmailCampaigns($data);
82 }
83
84 if (isset($response['campaigns']) && is_array($response['campaigns'])) {
85 $campaigns = array_merge($campaigns, $response['campaigns']);
86 $data['offset']++;
87 }
88 } while (!empty($response['campaigns']));
89
90 return $campaigns;
91 }
92
93 /**
94 * @param $data
95 * @return mixed
96 */
97 public function getEmailCampaigns($data)
98 {
99 return $this->get("/emailCampaigns", $data);
100 }
101
102 /**
103 * @param $data
104 * @return mixed
105 */
106 public function getSmsCampaigns($data)
107 {
108 return $this->get("/smsCampaigns", $data);
109 }
110
111 /**
112 * @param $data
113 * @return mixed
114 */
115 public function getEmailTemplates($data)
116 {
117 return $this->get("/smtp/templates", $data);
118 }
119
120 /**
121 * @param $data
122 * @return mixed
123 */
124 public function sendEmail($data)
125 {
126 return $this->post("/smtp/email", $data);
127 }
128
129 /**
130 * @param $email
131 * @return mixed
132 */
133 public function getUser($email)
134 {
135 return $this->get("/contacts/" . urlencode($email));
136 }
137
138 /**
139 * @param $data
140 * @return mixed
141 */
142 public function createUser($data)
143 {
144 return $this->post("/contacts", $data);
145 }
146
147 /**
148 * @return mixed
149 */
150 public function getSenders()
151 {
152 return $this->get("/senders");
153 }
154
155 /**
156 * @param $email ,$data
157 * @return mixed
158 */
159 public function updateUser($email, $data)
160 {
161 return $this->put("/contacts/" . $email, $data);
162 }
163
164 /**
165 * @param $data
166 * @return mixed
167 */
168 public function createInstallationInfo($data)
169 {
170 return $this->post("/account/partner/information", $data);
171 }
172
173 /**
174 * @param $installationId ,$data
175 * @return mixed
176 */
177 public function updateInstallationInfo($installationId, $data)
178 {
179 return $this->put("/account/partner/information/" . $installationId, $data);
180 }
181
182 /**
183 * @param $data
184 * @return mixed
185 */
186 public function createList($data)
187 {
188 return $this->post("/contacts/lists", $data);
189 }
190
191 /**
192 * @param $data
193 * @return mixed
194 */
195 public function getLists($data)
196 {
197 return $this->get("/contacts/lists", $data);
198 }
199
200 /**
201 * @param $data
202 * @return mixed
203 */
204 public function getAllLists()
205 {
206 $lists = array("lists" => array(), "count" => 0);
207 $offset = 0;
208 $limit = 50;
209 do {
210 $list_data = $this->getLists(array('limit' => $limit, 'offset' => $offset));
211 if (isset($list_data["lists"]) && is_array($list_data["lists"])) {
212 $lists["lists"] = array_merge($lists["lists"], $list_data["lists"]);
213 $offset += 50;
214 $lists["count"] = $list_data["count"];
215 }
216 } while (!empty($lists['lists']) && count($lists["lists"]) < $list_data["count"]);
217
218 return $lists;
219 }
220
221 /**
222 * @param $data
223 * @return mixed
224 */
225 public function createFolder($data)
226 {
227 return $this->post("/contacts/folders", $data);
228 }
229
230 /**
231 * @param $data
232 * @return mixed
233 */
234 public function getFolders($data)
235 {
236 return $this->get("/contacts/folders", $data);
237 }
238
239 /**
240 * @param $data
241 * @return mixed
242 */
243 public function getAllFolders()
244 {
245 $folders = array("folders" => array(), "count" => 0);
246 $offset = 0;
247 $limit = 50;
248 do {
249 $folder_data = $this->getFolders(array('limit' => $limit, 'offset' => $offset));
250 if (isset($folder_data["folders"]) && is_array($folder_data["folders"])) {
251 $folders["folders"] = array_merge($folders["folders"], $folder_data["folders"]);
252 $offset += 50;
253 $folders["count"] = $folder_data["count"];
254 }
255 } while (!empty($folders['folders']) && count($folders["folders"]) < $folder_data["count"]);
256
257 return $folders;
258 }
259
260 /**
261 * @param $data
262 * @return mixed
263 */
264 public function importContacts($data)
265 {
266 return $this->post('/contacts/import', $data);
267 }
268
269 /**
270 * @param $endpoint
271 * @param array $parameters
272 * @return mixed
273 */
274 public function get($endpoint, $parameters = [])
275 {
276 if ($parameters) {
277 foreach ($parameters as $key => $parameter) {
278 if (is_bool($parameter)) {
279 // http_build_query converts bool to int
280 $parameters[$key] = $parameter ? 'true' : 'false';
281 }
282 }
283 $endpoint .= '?' . http_build_query($parameters);
284 }
285 return $this->makeHttpRequest(self::HTTP_METHOD_GET, $endpoint);
286 }
287
288 /**
289 * @param $endpoint
290 * @param array $data
291 * @return mixed
292 */
293 public function post($endpoint, $data = [])
294 {
295 return $this->makeHttpRequest(self::HTTP_METHOD_POST, $endpoint, $data);
296 }
297
298 /**
299 * @param $endpoint
300 * @param array $data
301 * @return mixed
302 */
303 public function put($endpoint, $data = [])
304 {
305 return $this->makeHttpRequest(self::HTTP_METHOD_PUT, $endpoint, $data);
306 }
307
308 /**
309 * @param $method
310 * @param $endpoint
311 * @param array $body
312 * @return mixed
313 */
314 private function makeHttpRequest($method, $endpoint, $body = [])
315 {
316 $url = self::API_BASE_URL . $endpoint;
317
318 $args = [
319 'timeout' => 10000,
320 'method' => $method,
321 'headers' => [
322 'api-key' => $this->apiKey,
323 'sib-plugin' => 'wp-'.self::PLUGIN_VERSION,
324 'Content-Type' => 'application/json',
325 'User-Agent' => 'sendinblue_plugins/wordpress'
326 ],
327 ];
328
329 if ($method != self::HTTP_METHOD_GET && $method != self::HTTP_METHOD_DELETE) {
330 if (isset($body['listIds'])) {
331 $body['listIds'] = $this->getListsIds($body['listIds']);
332 }
333 if (isset($body['unlinkListIds'])) {
334 $body['unlinkListIds'] = $this->getListsIds($body['unlinkListIds']);
335 }
336 if(is_array($body)) {
337 foreach($body as $key => $val) {
338 if(empty($val) && $val!==false && $val!==0) {
339 unset($body[$key]);
340 }
341 }
342 }
343 $args['body'] = wp_json_encode($body);
344 }
345
346 $response = wp_remote_request($url, $args);
347 $this->lastResponseCode = wp_remote_retrieve_response_code($response);
348
349 if (is_wp_error($response)) {
350 $data = [
351 'code' => $response->get_error_code(),
352 'message' => $response->get_error_message()
353 ];
354 } else {
355 $data = json_decode(wp_remote_retrieve_body($response), true);
356 }
357
358 return $data;
359 }
360
361 private function getListsIds($listIds)
362 {
363 return array_unique(array_values(array_map('intval', (array)$listIds)));
364 }
365
366 /**
367 * @return int
368 */
369 public function getLastResponseCode()
370 {
371 return $this->lastResponseCode;
372 }
373 }
374