PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.0.9
Brevo – Email, SMS, Web Push, Chat, and more. v3.0.9
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
324 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
17 private $apiKey;
18 private $lastResponseCode;
19
20 /**
21 * SendinblueApiClient constructor.
22 */
23 public function __construct()
24 {
25 $this->apiKey = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME);
26 }
27
28 /**
29 * @return mixed
30 */
31 public function getAccount()
32 {
33 return $this->get('/account');
34 }
35
36 /**
37 * @return mixed
38 */
39 public function getAttributes()
40 {
41 return $this->get("/contacts/attributes");
42 }
43
44 /**
45 * @param $type ,$name,$data
46 * @return mixed
47 */
48 public function createAttribute($type, $name, $data)
49 {
50 return $this->post("/contacts/attributes/" . $type . "/" . $name, $data);
51 }
52
53 /**
54 * @param $id
55 * @return mixed
56 */
57 public function getEmailTemplate($id)
58 {
59 return $this->get("/smtp/templates/" . $id);
60 }
61
62 /**
63 * @param string $type
64 * @param array $data
65 * @return array
66 */
67 public function getAllCampaignsByType($type = self::CAMPAIGN_TYPE_EMAIL, $data = [])
68 {
69 $campaigns = [];
70
71 if (!isset($data['offset'])) {
72 $data['offset'] = 0;
73 }
74
75 do {
76 if ($type === self::CAMPAIGN_TYPE_SMS) {
77 $response = $this->getSmsCampaigns($data);
78 } else {
79 $response = $this->getEmailCampaigns($data);
80 }
81
82 if (isset($response['campaigns']) && is_array($response['campaigns'])) {
83 $campaigns = array_merge($campaigns, $response['campaigns']);
84 $data['offset']++;
85 }
86 } while (!empty($response['campaigns']));
87
88 return $campaigns;
89 }
90
91 /**
92 * @param $data
93 * @return mixed
94 */
95 public function getEmailCampaigns($data)
96 {
97 return $this->get("/emailCampaigns", $data);
98 }
99
100 /**
101 * @param $data
102 * @return mixed
103 */
104 public function getSmsCampaigns($data)
105 {
106 return $this->get("/smsCampaigns", $data);
107 }
108
109 /**
110 * @param $data
111 * @return mixed
112 */
113 public function getEmailTemplates($data)
114 {
115 return $this->get("/smtp/templates", $data);
116 }
117
118 /**
119 * @param $data
120 * @return mixed
121 */
122 public function sendEmail($data)
123 {
124 return $this->post("/smtp/email", $data);
125 }
126
127 /**
128 * @param $id ,$data
129 * @return mixed
130 */
131 public function sendTransactionalTemplate($id, $data)
132 {
133 return $this->post("/smtp/templates/" . $id . "/send", $data);
134 }
135
136 /**
137 * @param $email
138 * @return mixed
139 */
140 public function getUser($email)
141 {
142 return $this->get("/contacts/" . urlencode($email));
143 }
144
145 /**
146 * @param $data
147 * @return mixed
148 */
149 public function createUser($data)
150 {
151 return $this->post("/contacts", $data);
152 }
153
154 /**
155 * @return mixed
156 */
157 public function getSenders()
158 {
159 return $this->get("/senders");
160 }
161
162 /**
163 * @param $email ,$data
164 * @return mixed
165 */
166 public function updateUser($email, $data)
167 {
168 return $this->put("/contacts/" . $email, $data);
169 }
170
171 /**
172 * @param $data
173 * @return mixed
174 */
175 public function createList($data)
176 {
177 return $this->post("/contacts/lists", $data);
178 }
179
180 /**
181 * @param $data
182 * @return mixed
183 */
184 public function getLists($data)
185 {
186 return $this->get("/contacts/lists", $data);
187 }
188
189 /**
190 * @param $data
191 * @return mixed
192 */
193 public function getAllLists()
194 {
195 $lists = array("lists" => array(), "count" => 0);
196 $offset = 0;
197 $limit = 50;
198 do {
199 $list_data = $this->getLists(array('limit' => $limit, 'offset' => $offset));
200 if (isset($list_data["lists"]) && is_array($list_data["lists"])) {
201 $lists["lists"] = array_merge($lists["lists"], $list_data["lists"]);
202 $offset += 50;
203 $lists["count"] = $list_data["count"];
204 }
205 } while (!empty($lists['lists']) && count($lists["lists"]) < $list_data["count"]);
206
207 return $lists;
208 }
209
210 /**
211 * @param $data
212 * @return mixed
213 */
214 public function importContacts($data)
215 {
216 return $this->post('/contacts/import', $data);
217 }
218
219 /**
220 * @param $data
221 * @return mixed
222 */
223 public function setPartner($data)
224 {
225 return $this->post('/account/partner',$data);
226 }
227
228 /**
229 * @param $endpoint
230 * @param array $parameters
231 * @return mixed
232 */
233 public function get($endpoint, $parameters = [])
234 {
235 if ($parameters) {
236 foreach ($parameters as $key => $parameter) {
237 if (is_bool($parameter)) {
238 // http_build_query converts bool to int
239 $parameters[$key] = $parameter ? 'true' : 'false';
240 }
241 }
242 $endpoint .= '?' . http_build_query($parameters);
243 }
244 return $this->makeHttpRequest(self::HTTP_METHOD_GET, $endpoint);
245 }
246
247 /**
248 * @param $endpoint
249 * @param array $data
250 * @return mixed
251 */
252 public function post($endpoint, $data = [])
253 {
254 return $this->makeHttpRequest(self::HTTP_METHOD_POST, $endpoint, $data);
255 }
256
257 /**
258 * @param $endpoint
259 * @param array $data
260 * @return mixed
261 */
262 public function put($endpoint, $data = [])
263 {
264 return $this->makeHttpRequest(self::HTTP_METHOD_PUT, $endpoint, $data);
265 }
266
267 /**
268 * @param $method
269 * @param $endpoint
270 * @param array $body
271 * @return mixed
272 */
273 private function makeHttpRequest($method, $endpoint, $body = [])
274 {
275 $url = self::API_BASE_URL . $endpoint;
276
277 $args = [
278 'timeout' => 10000,
279 'method' => $method,
280 'headers' => [
281 'api-key' => $this->apiKey,
282 'Content-Type' => 'application/json'
283 ],
284 ];
285
286 if ($method != self::HTTP_METHOD_GET && $method != self::HTTP_METHOD_DELETE) {
287 if (isset($body['listIds'])) {
288 $body['listIds'] = $this->getListsIds($body['listIds']);
289 }
290 if (isset($body['unlinkListIds'])) {
291 $body['unlinkListIds'] = $this->getListsIds($body['unlinkListIds']);
292 }
293 $args['body'] = wp_json_encode($body);
294 }
295
296 $response = wp_remote_request($url, $args);
297 $this->lastResponseCode = wp_remote_retrieve_response_code($response);
298
299 if (is_wp_error($response)) {
300 $data = [
301 'code' => $response->get_error_code(),
302 'message' => $response->get_error_message()
303 ];
304 } else {
305 $data = json_decode(wp_remote_retrieve_body($response), true);
306 }
307
308 return $data;
309 }
310
311 private function getListsIds($listIds)
312 {
313 return array_unique(array_values(array_map('intval', (array)$listIds)));
314 }
315
316 /**
317 * @return int
318 */
319 public function getLastResponseCode()
320 {
321 return $this->lastResponseCode;
322 }
323 }
324