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