templates
2 years ago
SendinblueAccount.php
5 years ago
SendinblueApiClient.php
1 year ago
function.wp_mail.php
8 years ago
index.php
8 years ago
mailin.php
3 years ago
sendinblue.php
3 years ago
sib-api-manager.php
2 years ago
sib-form-preview.php
2 years ago
sib-sms-code.php
3 years ago
table-forms.php
1 year ago
SendinblueApiClient.php
420 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | class SendinblueApiClient |
| 5 | { |
| 6 | const API_BASE_URL = 'https://api.brevo.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.83'; |
| 18 | const USER_AGENT = 'sendinblue_plugins/wordpress'; |
| 19 | |
| 20 | private $apiKey; |
| 21 | private $lastResponseCode; |
| 22 | |
| 23 | /** |
| 24 | * SendinblueApiClient constructor. |
| 25 | */ |
| 26 | public function __construct() |
| 27 | { |
| 28 | $this->apiKey = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return mixed |
| 33 | */ |
| 34 | public function getAccount() |
| 35 | { |
| 36 | $sibAccObj = SendinblueAccount::getInstance(); |
| 37 | if($sibAccObj->getSendinblueAccountData()) |
| 38 | { |
| 39 | $this->lastResponseCode = $sibAccObj->getLastResponseCode(); |
| 40 | return $sibAccObj->getSendinblueAccountData(); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | $accData = $this->get('/account'); |
| 45 | if ($this->getLastResponseCode() === self::RESPONSE_CODE_OK) |
| 46 | { |
| 47 | $sibAccObj->setSendinblueAccountData($accData); |
| 48 | $sibAccObj->setLastResponseCode($this->lastResponseCode); |
| 49 | } |
| 50 | return $accData; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function getAttributes() |
| 58 | { |
| 59 | return $this->get("/contacts/attributes"); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param $type ,$name,$data |
| 64 | * @return mixed |
| 65 | */ |
| 66 | public function createAttribute($type, $name, $data) |
| 67 | { |
| 68 | return $this->post("/contacts/attributes/" . $type . "/" . $name, $data); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param $id |
| 73 | * @return mixed |
| 74 | */ |
| 75 | public function getEmailTemplate($id) |
| 76 | { |
| 77 | return $this->get("/smtp/templates/" . $id); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param string $type |
| 82 | * @param array $data |
| 83 | * @return array |
| 84 | */ |
| 85 | public function getAllCampaignsByType($type = self::CAMPAIGN_TYPE_EMAIL, $data = []) |
| 86 | { |
| 87 | $campaigns = []; |
| 88 | |
| 89 | if (!isset($data['offset'])) { |
| 90 | $data['offset'] = 0; |
| 91 | } |
| 92 | |
| 93 | do { |
| 94 | if ($type === self::CAMPAIGN_TYPE_SMS) { |
| 95 | $response = $this->getSmsCampaigns($data); |
| 96 | } else { |
| 97 | $response = $this->getEmailCampaigns($data); |
| 98 | } |
| 99 | |
| 100 | if (isset($response['campaigns']) && is_array($response['campaigns'])) { |
| 101 | $campaigns = array_merge($campaigns, $response['campaigns']); |
| 102 | $data['offset']++; |
| 103 | } |
| 104 | } while (!empty($response['campaigns'])); |
| 105 | |
| 106 | return $campaigns; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param $data |
| 111 | * @return mixed |
| 112 | */ |
| 113 | public function getEmailCampaigns($data) |
| 114 | { |
| 115 | return $this->get("/emailCampaigns", $data); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param $data |
| 120 | * @return mixed |
| 121 | */ |
| 122 | public function getSmsCampaigns($data) |
| 123 | { |
| 124 | return $this->get("/smsCampaigns", $data); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param $data |
| 129 | * @return mixed |
| 130 | */ |
| 131 | public function getEmailTemplates($data) |
| 132 | { |
| 133 | return $this->get("/smtp/templates", $data); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param $data |
| 138 | * @return mixed |
| 139 | */ |
| 140 | public function sendEmail($data) |
| 141 | { |
| 142 | return $this->post("/smtp/email", $data); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param $email |
| 147 | * @return mixed |
| 148 | */ |
| 149 | public function getUser($email) |
| 150 | { |
| 151 | return $this->get("/contacts/" . urlencode($email)); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param $data |
| 156 | * @return mixed |
| 157 | */ |
| 158 | public function createUser($data) |
| 159 | { |
| 160 | return $this->post("/contacts", $data); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return mixed |
| 165 | */ |
| 166 | public function getSenders() |
| 167 | { |
| 168 | return $this->get("/senders"); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param $email ,$data |
| 173 | * @return mixed |
| 174 | */ |
| 175 | public function updateUser($email, $data) |
| 176 | { |
| 177 | return $this->put("/contacts/" . $email, $data); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @param $data |
| 182 | * @return mixed |
| 183 | */ |
| 184 | public function createInstallationInfo($data) |
| 185 | { |
| 186 | return $this->post("/account/partner/information", $data); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param $installationId ,$data |
| 191 | * @return mixed |
| 192 | */ |
| 193 | public function updateInstallationInfo($installationId, $data) |
| 194 | { |
| 195 | return $this->put("/account/partner/information/" . $installationId, $data); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @param $data |
| 200 | * @return mixed |
| 201 | */ |
| 202 | public function createList($data) |
| 203 | { |
| 204 | return $this->post("/contacts/lists", $data); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @param $data |
| 209 | * @return mixed |
| 210 | */ |
| 211 | public function getLists($data) |
| 212 | { |
| 213 | return $this->get("/contacts/lists", $data); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param $data |
| 218 | * @return mixed |
| 219 | */ |
| 220 | public function getAllLists() |
| 221 | { |
| 222 | $lists = array("lists" => array(), "count" => 0); |
| 223 | $offset = 0; |
| 224 | $limit = 50; |
| 225 | do { |
| 226 | $list_data = $this->getLists(array('limit' => $limit, 'offset' => $offset)); |
| 227 | if (isset($list_data["lists"]) && is_array($list_data["lists"])) { |
| 228 | $lists["lists"] = array_merge($lists["lists"], $list_data["lists"]); |
| 229 | $offset += 50; |
| 230 | $lists["count"] = $list_data["count"]; |
| 231 | } |
| 232 | } while (!empty($lists['lists']) && count($lists["lists"]) < $list_data["count"]); |
| 233 | |
| 234 | return $lists; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param $data |
| 239 | * @return mixed |
| 240 | */ |
| 241 | public function createFolder($data) |
| 242 | { |
| 243 | return $this->post("/contacts/folders", $data); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param $data |
| 248 | * @return mixed |
| 249 | */ |
| 250 | public function getFolders($data) |
| 251 | { |
| 252 | return $this->get("/contacts/folders", $data); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @param $data |
| 257 | * @return mixed |
| 258 | */ |
| 259 | public function getAllFolders() |
| 260 | { |
| 261 | $folders = array("folders" => array(), "count" => 0); |
| 262 | $offset = 0; |
| 263 | $limit = 50; |
| 264 | do { |
| 265 | $folder_data = $this->getFolders(array('limit' => $limit, 'offset' => $offset)); |
| 266 | if (isset($folder_data["folders"]) && is_array($folder_data["folders"])) { |
| 267 | $folders["folders"] = array_merge($folders["folders"], $folder_data["folders"]); |
| 268 | $offset += 50; |
| 269 | $folders["count"] = $folder_data["count"]; |
| 270 | } |
| 271 | } while (!empty($folders['folders']) && count($folders["folders"]) < $folder_data["count"]); |
| 272 | |
| 273 | return $folders; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @param $data |
| 278 | * @return mixed |
| 279 | */ |
| 280 | public function importContacts($data) |
| 281 | { |
| 282 | return $this->post('/contacts/import', $data); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @param $endpoint |
| 287 | * @param array $parameters |
| 288 | * @return mixed |
| 289 | */ |
| 290 | public function get($endpoint, $parameters = []) |
| 291 | { |
| 292 | if ($parameters) { |
| 293 | foreach ($parameters as $key => $parameter) { |
| 294 | if (is_bool($parameter)) { |
| 295 | // http_build_query converts bool to int |
| 296 | $parameters[$key] = $parameter ? 'true' : 'false'; |
| 297 | } |
| 298 | } |
| 299 | $endpoint .= '?' . http_build_query($parameters); |
| 300 | } |
| 301 | return $this->makeHttpRequest(self::HTTP_METHOD_GET, $endpoint); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param $endpoint |
| 306 | * @param array $data |
| 307 | * @return mixed |
| 308 | */ |
| 309 | public function post($endpoint, $data = []) |
| 310 | { |
| 311 | return $this->makeHttpRequest(self::HTTP_METHOD_POST, $endpoint, $data); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * @param $endpoint |
| 316 | * @param array $data |
| 317 | * @return mixed |
| 318 | */ |
| 319 | public function put($endpoint, $data = []) |
| 320 | { |
| 321 | return $this->makeHttpRequest(self::HTTP_METHOD_PUT, $endpoint, $data); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @param $method |
| 326 | * @param $endpoint |
| 327 | * @param array $body |
| 328 | * @return mixed |
| 329 | */ |
| 330 | private function makeHttpRequest($method, $endpoint, $body = []) |
| 331 | { |
| 332 | $url = self::API_BASE_URL . $endpoint; |
| 333 | |
| 334 | $args = [ |
| 335 | 'timeout' => 10000, |
| 336 | 'method' => $method, |
| 337 | 'headers' => [ |
| 338 | 'api-key' => $this->apiKey, |
| 339 | 'sib-plugin' => 'wp-'.self::PLUGIN_VERSION, |
| 340 | 'Content-Type' => 'application/json', |
| 341 | 'User-Agent' => self::USER_AGENT |
| 342 | ], |
| 343 | ]; |
| 344 | |
| 345 | if ($method != self::HTTP_METHOD_GET && $method != self::HTTP_METHOD_DELETE) { |
| 346 | if (isset($body['listIds'])) { |
| 347 | $body['listIds'] = $this->getListsIds($body['listIds']); |
| 348 | } |
| 349 | if (isset($body['unlinkListIds'])) { |
| 350 | $body['unlinkListIds'] = $this->getListsIds($body['unlinkListIds']); |
| 351 | } |
| 352 | if(is_array($body)) { |
| 353 | foreach($body as $key => $val) { |
| 354 | if(empty($val) && $val!==false && $val!==0) { |
| 355 | unset($body[$key]); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | $args['body'] = wp_json_encode($body); |
| 360 | } |
| 361 | |
| 362 | $response = wp_remote_request($url, $args); |
| 363 | $this->lastResponseCode = wp_remote_retrieve_response_code($response); |
| 364 | |
| 365 | if (is_wp_error($response)) { |
| 366 | $data = [ |
| 367 | 'code' => $response->get_error_code(), |
| 368 | 'message' => $response->get_error_message() |
| 369 | ]; |
| 370 | } else { |
| 371 | $data = json_decode(wp_remote_retrieve_body($response), true); |
| 372 | } |
| 373 | |
| 374 | return $data; |
| 375 | } |
| 376 | |
| 377 | private function getListsIds($listIds) |
| 378 | { |
| 379 | return array_unique(array_values(array_map('intval', (array)$listIds))); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * @return int |
| 384 | */ |
| 385 | public function getLastResponseCode() |
| 386 | { |
| 387 | return $this->lastResponseCode; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @param $data |
| 392 | * @return mixed all template list |
| 393 | */ |
| 394 | public function getAllEmailTemplates() { |
| 395 | $templates = array( |
| 396 | 'templates' => array(), |
| 397 | 'count' => 0, |
| 398 | ); |
| 399 | $offset = 0; |
| 400 | $limit = 50; |
| 401 | do { |
| 402 | $template_data = $this->getEmailTemplates( |
| 403 | array( |
| 404 | 'templateStatus' => true, |
| 405 | 'limit' => $limit, |
| 406 | 'offset' => $offset, |
| 407 | ) |
| 408 | ); |
| 409 | if ( empty( $template_data ) ) { |
| 410 | break; |
| 411 | } |
| 412 | $templates['templates'] = array_merge( $templates['templates'], $template_data['templates'] ); |
| 413 | $offset += 50; |
| 414 | } while ( ! empty( $templates['templates'] ) && count( $templates['templates'] ) < $template_data['count'] ); |
| 415 | $templates['count'] = count( $templates['templates'] ); |
| 416 | |
| 417 | return $templates; |
| 418 | } |
| 419 | } |
| 420 |