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