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