PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.1.5
Brevo – Email, SMS, Web Push, Chat, and more. v3.1.5
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
365 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.5';
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 createList($data)
169 {
170 return $this->post("/contacts/lists", $data);
171 }
172
173 /**
174 * @param $data
175 * @return mixed
176 */
177 public function getLists($data)
178 {
179 return $this->get("/contacts/lists", $data);
180 }
181
182 /**
183 * @param $data
184 * @return mixed
185 */
186 public function getAllLists()
187 {
188 $lists = array("lists" => array(), "count" => 0);
189 $offset = 0;
190 $limit = 50;
191 do {
192 $list_data = $this->getLists(array('limit' => $limit, 'offset' => $offset));
193 if (isset($list_data["lists"]) && is_array($list_data["lists"])) {
194 $lists["lists"] = array_merge($lists["lists"], $list_data["lists"]);
195 $offset += 50;
196 $lists["count"] = $list_data["count"];
197 }
198 } while (!empty($lists['lists']) && count($lists["lists"]) < $list_data["count"]);
199
200 return $lists;
201 }
202
203 /**
204 * @param $data
205 * @return mixed
206 */
207 public function createFolder($data)
208 {
209 return $this->post("/contacts/folders", $data);
210 }
211
212 /**
213 * @param $data
214 * @return mixed
215 */
216 public function getFolders($data)
217 {
218 return $this->get("/contacts/folders", $data);
219 }
220
221 /**
222 * @param $data
223 * @return mixed
224 */
225 public function getAllFolders()
226 {
227 $folders = array("folders" => array(), "count" => 0);
228 $offset = 0;
229 $limit = 50;
230 do {
231 $folder_data = $this->getFolders(array('limit' => $limit, 'offset' => $offset));
232 if (isset($folder_data["folders"]) && is_array($folder_data["folders"])) {
233 $folders["folders"] = array_merge($folders["folders"], $folder_data["folders"]);
234 $offset += 50;
235 $folders["count"] = $folder_data["count"];
236 }
237 } while (!empty($folders['folders']) && count($folders["folders"]) < $folder_data["count"]);
238
239 return $folders;
240 }
241
242 /**
243 * @param $data
244 * @return mixed
245 */
246 public function importContacts($data)
247 {
248 return $this->post('/contacts/import', $data);
249 }
250
251 /**
252 * @param $data
253 * @return mixed
254 */
255 public function setPartner($data)
256 {
257 return $this->post('/account/partner',$data);
258 }
259
260 /**
261 * @param $endpoint
262 * @param array $parameters
263 * @return mixed
264 */
265 public function get($endpoint, $parameters = [])
266 {
267 if ($parameters) {
268 foreach ($parameters as $key => $parameter) {
269 if (is_bool($parameter)) {
270 // http_build_query converts bool to int
271 $parameters[$key] = $parameter ? 'true' : 'false';
272 }
273 }
274 $endpoint .= '?' . http_build_query($parameters);
275 }
276 return $this->makeHttpRequest(self::HTTP_METHOD_GET, $endpoint);
277 }
278
279 /**
280 * @param $endpoint
281 * @param array $data
282 * @return mixed
283 */
284 public function post($endpoint, $data = [])
285 {
286 return $this->makeHttpRequest(self::HTTP_METHOD_POST, $endpoint, $data);
287 }
288
289 /**
290 * @param $endpoint
291 * @param array $data
292 * @return mixed
293 */
294 public function put($endpoint, $data = [])
295 {
296 return $this->makeHttpRequest(self::HTTP_METHOD_PUT, $endpoint, $data);
297 }
298
299 /**
300 * @param $method
301 * @param $endpoint
302 * @param array $body
303 * @return mixed
304 */
305 private function makeHttpRequest($method, $endpoint, $body = [])
306 {
307 $url = self::API_BASE_URL . $endpoint;
308
309 $args = [
310 'timeout' => 10000,
311 'method' => $method,
312 'headers' => [
313 'api-key' => $this->apiKey,
314 'sib-plugin' => 'wp-'.self::PLUGIN_VERSION,
315 'Content-Type' => 'application/json',
316 'User-Agent' => 'sendinblue_plugins/wordpress'
317 ],
318 ];
319
320 if ($method != self::HTTP_METHOD_GET && $method != self::HTTP_METHOD_DELETE) {
321 if (isset($body['listIds'])) {
322 $body['listIds'] = $this->getListsIds($body['listIds']);
323 }
324 if (isset($body['unlinkListIds'])) {
325 $body['unlinkListIds'] = $this->getListsIds($body['unlinkListIds']);
326 }
327 if(is_array($body)) {
328 foreach($body as $key => $val) {
329 if(empty($val)) {
330 unset($body[$key]);
331 }
332 }
333 }
334 $args['body'] = wp_json_encode($body);
335 }
336
337 $response = wp_remote_request($url, $args);
338 $this->lastResponseCode = wp_remote_retrieve_response_code($response);
339
340 if (is_wp_error($response)) {
341 $data = [
342 'code' => $response->get_error_code(),
343 'message' => $response->get_error_message()
344 ];
345 } else {
346 $data = json_decode(wp_remote_retrieve_body($response), true);
347 }
348
349 return $data;
350 }
351
352 private function getListsIds($listIds)
353 {
354 return array_unique(array_values(array_map('intval', (array)$listIds)));
355 }
356
357 /**
358 * @return int
359 */
360 public function getLastResponseCode()
361 {
362 return $this->lastResponseCode;
363 }
364 }
365