/
MailjetApi.php
388 lines (346 loc) · 11.4 KB
/
MailjetApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace MailjetPlugin\Includes;
//use Exception;
use Mailjet\Client;
use Mailjet\Resources;
/**
* Define the internationalization functionality.
*
* Loads and defines the internationalization files for this plugin
* so that it is ready for translation.
*
* @since 5.0.0
* @package Mailjet
* @subpackage Mailjet/includes
* @author Your Name <email@example.com>
*/
class MailjetApi
{
private static $mjApiClient = null;
public static function getApiClient()
{
if (self::$mjApiClient instanceof Client) {
return self::$mjApiClient;
}
$mailjetApikey = get_option('mailjet_apikey');
$mailjetApiSecret = get_option('mailjet_apisecret');
if (empty($mailjetApikey) || empty($mailjetApiSecret)) {
throw new \Exception('Missing Mailjet API credentials');
}
$mjClient = new Client($mailjetApikey, $mailjetApiSecret);
$mjClient->addRequestOption(CURLOPT_USERAGENT, 'wordpress-' . MAILJET_VERSION);
$mjClient->addRequestOption('headers', ['User-Agent' => 'wordpress-' . MAILJET_VERSION]);
// Add proxy options for guzzle requests - if the Wordpress site is configured to use Proxy
if (defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT') && defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD')) {
$mjClient->addRequestOption(CURLOPT_HTTPPROXYTUNNEL, 1);
$mjClient->addRequestOption(CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
$mjClient->addRequestOption(CURLOPT_PROXY, WP_PROXY_HOST . ':' . WP_PROXY_PORT);
$mjClient->addRequestOption(CURLOPT_PROXYPORT, WP_PROXY_PORT);
$mjClient->addRequestOption(CURLOPT_PROXYUSERPWD, WP_PROXY_USERNAME . ':' . WP_PROXY_PASSWORD);
}
// We turn of secure protocol for API requests if the wordpress does not support it
if (empty($_SERVER['HTTPS']) || (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'off') || $_SERVER['SERVER_PORT'] != 443) {
$mjClient->setSecureProtocol(false);
}
self::$mjApiClient = $mjClient;
return self::$mjApiClient;
}
public static function getMailjetContactLists()
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = [
'Limit' => '0',
'Sort' => 'Name ASC'
];
try {
$response = $mjApiClient->get(Resources::$Contactslist, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
//return $response->getStatus();
return false;
}
}
public static function createMailjetContactList($listName)
{
if (empty($listName)) {
return false;
}
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$body = [
'Name' => $listName
];
try {
$response = $mjApiClient->post(Resources::$Contactslist, ['body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
return $response;
}
public static function isContactListActive($contactListId)
{
if (!$contactListId) {
return false;
}
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = array(
'ID' => $contactListId
);
try{
$response = $mjApiClient->get(Resources::$Contactslist, array('filters' => $filters));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
$data = $response->getData();
if (isset($data[0]['IsDeleted'])) {
// Return true if the list is not deleted
return !$data[0]['IsDeleted'];
}
}
return false;
}
public static function getContactProperties()
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = array(
'limit' => 0,
'Sort' => 'Name ASC'
);
try {
$response = $mjApiClient->get(Resources::$Contactmetadata, array('filters' => $filters));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
return false;
// return $response->getStatus();
}
}
public static function getPropertyIdByName($name)
{
if (!$name) {
return false;
}
$contactProperties = self::getContactProperties();
if ($contactProperties) {
foreach ($contactProperties as $property) {
if ($property['Name'] === $name) {
return $property['ID'];
}
}
}
return false;
}
public static function createMailjetContactProperty($name, $type = "str")
{
if (empty($name)) {
return false;
}
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
// Name: the name of the custom data field
// DataType: the type of data that is being stored (this can be either a str, int, float or bool)
// NameSpace: this can be either static or historic
$body = [
'Datatype' => $type,
'Name' => $name,
'NameSpace' => "static"
];
try {
$response = $mjApiClient->post(Resources::$Contactmetadata, ['body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
return false;
// return $response->getStatus();
}
}
public static function getMailjetSenders()
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = [
'Limit' => '0',
'Sort' => 'ID DESC'
];
try {
$response = $mjApiClient->get(Resources::$Sender, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
//return $response->getStatus();
return false;
}
}
public static function isValidAPICredentials()
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = [
'Limit' => '1'
];
try {
$response = $mjApiClient->get(Resources::$Contactmetadata, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return true;
// return $response->getData();
} else {
return false;
// return $response->getStatus();
}
}
/**
* Add or Remove a contact to a Mailjet contact list - It can process many or single contact at once
*
* @param $contactListId - int - ID of the contact list to sync contacts
* @param $contacts - array('Email' => ContactEmail, 'Name' => ContactName, 'Properties' => array(propertyName1 => propertyValue1, ...));
* @param string $action - 'addforce', 'adnoforce', 'remove'
* @return array|bool
*/
public static function syncMailjetContacts($contactListId, $contacts, $action = 'addforce')
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$body = [
'Action' => $action,
'Contacts' => $contacts
];
try {
$response = $mjApiClient->post(Resources::$ContactslistManagemanycontacts, ['id' => $contactListId, 'body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
return false;
// return $response->getStatus();
}
}
/**
* Add a contact to a Mailjet contact list
*/
public static function syncMailjetContact($contactListId, $contact, $action = 'addforce')
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$name = isset($contact['Properties']['firstname']) ? $contact['Properties']['firstname'] : '';
$body = [
'Name' => $name,
'Action' => $action,
'Email' => $contact['Email'],
'Properties' => $contact['Properties']
];
try {
$response = $mjApiClient->post(Resources::$ContactslistManagecontact, ['id' => $contactListId, 'body' => $body]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success()) {
return $response->getData();
} else {
return false;
}
}
/**
* Return TRUE if a contact already subscribed to the list and FALSE if it is not, or is added to the list but Unsubscribed
*
* @param $email
* @param $listId
* @return bool
*/
public static function checkContactSubscribedToList($email, $listId)
{
$exists = false;
$existsAndSubscribed = false;
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
$filters = [
'ContactEmail' => $email,
'ContactsList' => $listId,
];
try {
$response = $mjApiClient->get(Resources::$Listrecipient, ['filters' => $filters]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
if ($response->success() && $response->getCount() > 0) {
$data = $response->getData();
$exists = true;
if (isset($data[0]['IsUnsubscribed']) && false == $data[0]['IsUnsubscribed']) {
$existsAndSubscribed = true;
}
}
return $exists && $existsAndSubscribed;
}
public static function getProfileName()
{
try {
$mjApiClient = self::getApiClient();
} catch (\Exception $e) {
return false;
}
try {
$response = $mjApiClient->get(Resources::$Myprofile, []);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
return false;
}
$name = "";
if ($response->success() && $response->getCount() > 0) {
$data = $response->getData();
if (isset($data[0]['Firstname']) && isset($data[0]['Lastname'])) {
$name = $data[0]['Firstname'] . ' ' . $data[0]['Lastname'];
}
}
return $name;
}
}