This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Lists.php
475 lines (432 loc) · 15.6 KB
/
Lists.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
namespace Klaviyo;
/**
* Class Lists
* @package Klaviyo
*/
class Lists extends KlaviyoAPI
{
/**
* List endpoint constants
*/
const ENDPOINT_ALL = 'all';
const ENDPOINT_EXCLUSIONS = 'exclusions';
const ENDPOINT_GROUP = 'group';
const ENDPOINT_LIST = 'list';
const ENDPOINT_LISTS = 'lists';
const ENDPOINT_MEMBERS = 'members';
const ENDPOINT_SEGMENT = 'segment';
const ENDPOINT_SUBSCRIBE = 'subscribe';
/**
* Lists API arguments
*/
const EMAILS = 'emails';
const PHONE_NUMBERS = 'phone_numbers';
const PUSH_TOKENS = 'push_tokens';
const LIST_NAME = 'list_name';
const MARKER = 'marker';
/**
* Create a new list
* @link https://www.klaviyo.com/docs/api/v2/lists#post-lists
*
* @param string $listName
* Name of list to be created.
*
* @return bool|mixed
*/
public function createList( $listName )
{
$options = $this->createParams(self::LIST_NAME, $listName);
return $this->v2Request( self::ENDPOINT_LISTS, $options, self::HTTP_POST );
}
/**
* Get all lists
* @link https://www.klaviyo.com/docs/api/v2/lists#get-lists
*
* @return bool/mixed
*/
public function getLists() {
return $this->v2Request( self::ENDPOINT_LISTS );
}
/**
* Get information about a list
*
* @deprecated 2.2.6
* @see getListById
*
* @param string $listId
* 6 digit unique identifier of the list
*
* @return bool|mixed
*/
public function getListDetails( $listId )
{
return $this->getListById($listId);
}
/**
* Get information about a list.
* @link https://www.klaviyo.com/docs/api/v2/lists#get-list
*
* @param string $listId
* 6 digit unique identifier of the list
*
* @return bool|mixed
*/
public function getListById($listId)
{
$path = sprintf('%s/%s', self::ENDPOINT_LIST, $listId);
return $this->v2Request($path);
}
/**
* Update a list's properties
*
* @deprecated 2.2.6
* @see updateListNameById
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param $list_name
* String to update list name to
*
* @return bool|mixed
*/
public function updateListDetails( $listId, $list_name )
{
return $this->updateListNameById($listId, $list_name);
}
/**
* Update a list's name.
* @link https://www.klaviyo.com/docs/api/v2/lists#put-list
*
* @param string $listId
* 6 digit unique identifier of the list
*
* @param string $listName
* String to update list name to
*
* @return bool|mixed
*/
public function updateListNameById($listId, $listName)
{
$params = $this->createRequestBody(
array(self::LIST_NAME => $listName)
);
$path = sprintf('%s/%s', self::ENDPOINT_LIST, $listId);
return $this->v2Request($path, $params, self::HTTP_PUT);
}
/**
* Delete a list from an account. This is a destructive operation and cannot be undone. It will also remove flow triggers associated with the list.
* @link https://www.klaviyo.com/docs/api/v2/lists#delete-list
*
* @param $listId
* 6 digit unique identifier of the list
*
* @return bool|mixed
*/
public function deleteList( $listId )
{
$path = sprintf( '%s/%s', self::ENDPOINT_LIST, $listId );
return $this->v2Request( $path, [], self::HTTP_DELETE );
}
/**
* Subscribe or re-subscribe profiles to a list. Profiles will be single or double opted into the specified list in accordance with that list’s settings.
*
* @deprecated 2.2.6
* @see addSubscribersToList
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param array $profiles
* The profiles that you would like to subscribe. Each object in the list must have either an email or phone number key.
* You can also provide additional properties as key-value pairs. If you are a GDPR compliant business, you will need to include $consent in your API call.
* $consent is a Klaviyo special property and only accepts the following values: "email", "web", "sms", "directmail", "mobile".
* If you are updating consent for a phone number or would like to send an opt-in SMS to the profile (for double opt-in lists), include an sms_consent key in the profile with a value of true or false.
*
* @return bool|mixed
* @throws Exception\KlaviyoException
*/
public function subscribeMembersToList( $listId, $profiles )
{
return $this->addSubscribersToList($listId, $profiles);
}
/**
* Subscribe or re-subscribe profiles to a list. Profiles will be single or double opted into the specified list in accordance with that list’s settings.
* @link https://www.klaviyo.com/docs/api/v2/lists#post-subscribe
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param array $profiles
* The profiles that you would like to subscribe. Each object in the list must have either an email or phone number key.
* You can also provide additional properties as key-value pairs. If you are a GDPR compliant business, you will need to include $consent in your API call.
* $consent is a Klaviyo special property and only accepts the following values: "email", "web", "sms", "directmail", "mobile".
* If you are updating consent for a phone number or would like to send an opt-in SMS to the profile (for double opt-in lists), include an sms_consent key in the profile with a value of true or false.
*
* @return bool|mixed
* @throws Exception\KlaviyoException
*/
public function addSubscribersToList($listId, $profiles)
{
$this->checkProfile($profiles);
$profiles = array_map(
function($profile) {
return $profile->toArray();
},
$profiles
);
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE);
$params = $this->createParams(self::PROFILES, $profiles);
return $this->v2Request($path, $params, self::HTTP_POST);
}
/**
* Check if profiles are on a list and not suppressed.
* @link https://www.klaviyo.com/docs/api/v2/lists#get-subscribe
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param array|null $emails
* The emails corresponding to the profiles that you would like to check.
*
* @param array|null $phoneNumbers
* The phone numbers corresponding to the profiles that you would like to check.
* Phone numbers must be in E.164 format.
*
* @param array|null $pushTokens
* The push tokens corresponding to the profiles that you would like to check.
*
* @return bool|mixed
*/
public function checkListSubscriptions ($listId, $emails = null, $phoneNumbers = null, $pushTokens = null )
{
$params = $this->createRequestJson(
$this->filterParams(
array(
self::EMAILS => $emails,
self::PHONE_NUMBERS => $phoneNumbers,
self::PUSH_TOKENS => $pushTokens
)
)
);
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE );
return $this->v2Request( $path, $params, self::HTTP_GET );
}
/**
* Unsubscribe and remove profiles from a list.
*
* @deprecated 2.2.6
* @see deleteSubscribersFromList
*
* @param string $listId
* 6 digit unique identifier of the list
*
* @param array $emails
* The emails corresponding to the profiles that you would like to check.
*
* @return bool|mixed
*/
public function unsubscribeMembersFromList( $listId, $emails )
{
return $this->deleteSubscribersFromList($listId, $emails);
}
/**
* Unsubscribe and remove profiles from a list.
* @link https://www.klaviyo.com/docs/api/v2/lists#delete-subscribe
*
* @param string $listId
* 6 digit unique identifier of the list
*
* @param array $emails
* The emails corresponding to the profiles that you would like to check.
*
* @return bool|mixed
*/
public function deleteSubscribersFromList($listId, $emails)
{
$params = $this->createRequestJson(
$this->filterParams(
array(
self::EMAILS => $emails
)
)
);
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE);
return $this->v2Request($path, $params, self::HTTP_DELETE);
}
/**
* Use this endpoint to add profiles to and remove profiles from Klaviyo lists without changing their subscription or suppression status.
* @link https://www.klaviyo.com/docs/api/v2/lists#post-members
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param array $profiles
* The profiles that you would like to subscribe. Each object in the list must have either an email or phone number key.
* You can also provide additional properties as key-value pairs. If you are a GDPR compliant business, you will need to include $consent in your API call.
* $consent is a Klaviyo special property and only accepts the following values: "email", "web", "sms", "directmail", "mobile".
* If you are updating consent for a phone number or would like to send an opt-in SMS to the profile (for double opt-in lists), include an sms_consent key in the profile with a value of true or false.
*
* @return bool|mixed
*
* @throws Exception\KlaviyoException
*/
public function addMembersToList( $listId, $profiles )
{
$this->checkProfile( $profiles );
$profiles = array_map(
function( $profile ) {
return $profile->toArray();
}, $profiles
);
$path = sprintf( '%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );
$options = $this->createParams( self::PROFILES, $profiles );
return $this->v2Request( $path, $options, self::HTTP_POST );
}
/**
* Check if profiles are on a list.
* @link https://www.klaviyo.com/docs/api/v2/lists#get-members
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param array|null $emails
* The emails corresponding to the profiles that you would like to check.
*
* @param array|null $phoneNumbers
* The phone numbers corresponding to the profiles that you would like to check.
* Phone numbers must be in E.164 format.
*
* @param array|null $pushTokens
*The push tokens corresponding to the profiles that you would like to check.
*
* @return bool|mixed
*/
public function checkListMembership( $listId, $emails = null, $phoneNumbers = null, $pushTokens = null )
{
$params = $this->createRequestJson(
$this->filterParams(
array(
self::EMAILS => $emails,
self::PHONE_NUMBERS => $phoneNumbers,
self::PUSH_TOKENS => $pushTokens
)
)
);
$path = sprintf( '%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );
return $this->v2Request( $path, $params, self::HTTP_GET );
}
/**
* Remove profiles from a list.
* @link https://www.klaviyo.com/docs/api/v2/lists#delete-members
*
* @param string $listId
* 6 digit unique identifier of the list.
*
* @param array $emails
* The emails corresponding to the profiles that you would like to check.
*
* @return bool|mixed
*/
public function removeMembersFromList( $listId, $emails )
{
$params = $this->createRequestJson(
$this->filterParams(
array(
self::EMAILS => $emails
)
)
);
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );
return $this->v2Request( $path, $params, self::HTTP_DELETE );
}
/**
* Get all of the emails and phone numbers that have been excluded from a list along with the exclusion reasons and exclusion time.
* This endpoint uses batching to return the records, so for a large list multiple calls will need to be made to get all of the records.
*
* @deprecated 2.2.6
* @see getListExclusions
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param int $marker
* A marker value returned by a previous GET call. Use this to grab the next batch of records.
*
* @return bool|mixed
*/
public function getAllExclusionsOnList( $listId, $marker = null )
{
return $this->getListExclusions($listId, $marker);
}
/**
* Get all of the emails and phone numbers that have been excluded from a list along with the exclusion reasons and exclusion time.
* This endpoint uses batching to return the records, so for a large list multiple calls will need to be made to get all of the records.
* @link https://www.klaviyo.com/docs/api/v2/lists#get-exclusions-all
*
* @param $listId
* 6 digit unique identifier of the list
*
* @param int $marker
* A marker value returned by a previous GET call. Use this to grab the next batch of records.
*
* @return bool|mixed
*/
public function getListExclusions($listId, $marker = null)
{
$params = $this->createRequestBody(
$this->filterParams(
array(
self::MARKER => $marker
)
)
);
$path = sprintf('%s/%s/%s/%s',self::ENDPOINT_LIST, $listId, self::ENDPOINT_EXCLUSIONS, self::ENDPOINT_ALL);
return $this->v2Request($path, $params);
}
/**
* Get all of the emails, phone numbers, and push tokens for profiles in a given list or segment.
* This endpoint uses batching to return the records, so for a large list or segment multiple calls will need to be made to get all of the records.
*
* @deprecated 2.2.6
* @see getAllMembers
*
* @param $groupId
* 6 digit unique identifier of List/Segment to get member information about
*
* @param int $marker
* A marker value returned by a previous GET call. Use this to grab the next batch of records.
*
* @return bool|mixed
*/
public function getGroupMemberIdentifiers( $groupId, $marker = null )
{
return $this->getAllMembers($groupId, $marker);
}
/**
* Get all of the emails, phone numbers, and push tokens for profiles in a given list or segment.
* This endpoint uses batching to return the records, so for a large list or segment multiple calls will need to be made to get all of the records.
* @link https://www.klaviyo.com/docs/api/v2/lists#get-members-all
*
* @param $groupId
* 6 digit unique identifier of List/Segment to get member information about
*
* @param int $marker
* A marker value returned by a previous GET call. Use this to grab the next batch of records.
*
* @return bool|mixed
*/
public function getAllMembers($groupId, $marker = null)
{
$params = $this->createRequestBody(
$this->filterParams(
array(
self::MARKER => $marker
)
)
);
$path = sprintf('%s/%s/%s/%s',self::ENDPOINT_GROUP, $groupId, self::ENDPOINT_MEMBERS, self::ENDPOINT_ALL);
return $this->v2Request($path, $params);
}
}