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
314 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 | |
| 17 | private $apiKey; |
| 18 | private $lastResponseCode; |
| 19 | |
| 20 | /** |
| 21 | * SendinblueApiClient constructor. |
| 22 | */ |
| 23 | public function __construct() |
| 24 | { |
| 25 | $this->apiKey = get_option(SIB_Manager::API_KEY_V3_OPTION_NAME); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return mixed |
| 30 | */ |
| 31 | public function getAccount() |
| 32 | { |
| 33 | return $this->get('/account'); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return mixed |
| 38 | */ |
| 39 | public function getAttributes() |
| 40 | { |
| 41 | return $this->get("/contacts/attributes"); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param $type ,$name,$data |
| 46 | * @return mixed |
| 47 | */ |
| 48 | public function createAttribute($type, $name, $data) |
| 49 | { |
| 50 | return $this->post("/contacts/attributes/" . $type . "/" . $name, $data); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param $id |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function getEmailTemplate($id) |
| 58 | { |
| 59 | return $this->get("/smtp/templates/" . $id); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param string $type |
| 64 | * @param array $data |
| 65 | * @return array |
| 66 | */ |
| 67 | public function getAllCampaignsByType($type = self::CAMPAIGN_TYPE_EMAIL, $data = []) |
| 68 | { |
| 69 | $campaigns = []; |
| 70 | |
| 71 | if (!isset($data['offset'])) { |
| 72 | $data['offset'] = 0; |
| 73 | } |
| 74 | |
| 75 | do { |
| 76 | if ($type === self::CAMPAIGN_TYPE_SMS) { |
| 77 | $response = $this->getSmsCampaigns($data); |
| 78 | } else { |
| 79 | $response = $this->getEmailCampaigns($data); |
| 80 | } |
| 81 | |
| 82 | if (isset($response['campaigns']) && is_array($response['campaigns'])) { |
| 83 | $campaigns = array_merge($campaigns, $response['campaigns']); |
| 84 | $data['offset']++; |
| 85 | } |
| 86 | } while (!empty($response['campaigns'])); |
| 87 | |
| 88 | return $campaigns; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param $data |
| 93 | * @return mixed |
| 94 | */ |
| 95 | public function getEmailCampaigns($data) |
| 96 | { |
| 97 | return $this->get("/emailCampaigns", $data); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param $data |
| 102 | * @return mixed |
| 103 | */ |
| 104 | public function getSmsCampaigns($data) |
| 105 | { |
| 106 | return $this->get("/smsCampaigns", $data); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param $data |
| 111 | * @return mixed |
| 112 | */ |
| 113 | public function getEmailTemplates($data) |
| 114 | { |
| 115 | return $this->get("/smtp/templates", $data); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param $data |
| 120 | * @return mixed |
| 121 | */ |
| 122 | public function sendEmail($data) |
| 123 | { |
| 124 | return $this->post("/smtp/email", $data); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param $id ,$data |
| 129 | * @return mixed |
| 130 | */ |
| 131 | public function sendTransactionalTemplate($id, $data) |
| 132 | { |
| 133 | return $this->post("/smtp/templates/" . $id . "/send", $data); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param $email |
| 138 | * @return mixed |
| 139 | */ |
| 140 | public function getUser($email) |
| 141 | { |
| 142 | return $this->get("/contacts/" . urlencode($email)); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param $data |
| 147 | * @return mixed |
| 148 | */ |
| 149 | public function createUser($data) |
| 150 | { |
| 151 | return $this->post("/contacts", $data); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return mixed |
| 156 | */ |
| 157 | public function getSenders() |
| 158 | { |
| 159 | return $this->get("/senders"); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param $email ,$data |
| 164 | * @return mixed |
| 165 | */ |
| 166 | public function updateUser($email, $data) |
| 167 | { |
| 168 | return $this->put("/contacts/" . $email, $data); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param $data |
| 173 | * @return mixed |
| 174 | */ |
| 175 | public function createList($data) |
| 176 | { |
| 177 | return $this->post("/contacts/lists", $data); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @param $data |
| 182 | * @return mixed |
| 183 | */ |
| 184 | public function getLists($data) |
| 185 | { |
| 186 | return $this->get("/contacts/lists", $data); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param $data |
| 191 | * @return mixed |
| 192 | */ |
| 193 | public function getAllLists() |
| 194 | { |
| 195 | $lists = array("lists" => array(), "count" => 0); |
| 196 | $offset = 0; |
| 197 | $limit = 50; |
| 198 | do { |
| 199 | $list_data = $this->getLists(array('limit' => $limit, 'offset' => $offset)); |
| 200 | if (isset($list_data["lists"]) && is_array($list_data["lists"])) { |
| 201 | $lists["lists"] = array_merge($lists["lists"], $list_data["lists"]); |
| 202 | $offset += 50; |
| 203 | $lists["count"] = $list_data["count"]; |
| 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 | 'timeout' => 10000, |
| 270 | 'method' => $method, |
| 271 | 'headers' => [ |
| 272 | 'api-key' => $this->apiKey, |
| 273 | 'Content-Type' => 'application/json' |
| 274 | ], |
| 275 | ]; |
| 276 | |
| 277 | if ($method != self::HTTP_METHOD_GET && $method != self::HTTP_METHOD_DELETE) { |
| 278 | if (isset($body['listIds'])) { |
| 279 | $body['listIds'] = $this->getListsIds($body['listIds']); |
| 280 | } |
| 281 | if (isset($body['unlinkListIds'])) { |
| 282 | $body['unlinkListIds'] = $this->getListsIds($body['unlinkListIds']); |
| 283 | } |
| 284 | $args['body'] = wp_json_encode($body); |
| 285 | } |
| 286 | |
| 287 | $response = wp_remote_request($url, $args); |
| 288 | $this->lastResponseCode = wp_remote_retrieve_response_code($response); |
| 289 | |
| 290 | if (is_wp_error($response)) { |
| 291 | $data = [ |
| 292 | 'code' => $response->get_error_code(), |
| 293 | 'message' => $response->get_error_message() |
| 294 | ]; |
| 295 | } else { |
| 296 | $data = json_decode(wp_remote_retrieve_body($response), true); |
| 297 | } |
| 298 | |
| 299 | return $data; |
| 300 | } |
| 301 | |
| 302 | private function getListsIds($listIds) |
| 303 | { |
| 304 | return array_unique(array_values(array_map('intval', (array)$listIds))); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * @return int |
| 309 | */ |
| 310 | public function getLastResponseCode() |
| 311 | { |
| 312 | return $this->lastResponseCode; |
| 313 | } |
| 314 | } |