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