-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMailchimpAPI.php
269 lines (227 loc) · 6.93 KB
/
MailchimpAPI.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
<?php
class MailchimpAPI {
protected $client;
public $error_message = '';
public $error_code = '';
public function __construct($api_key) {
$this->client = new MailchimpClient($api_key);
}
/**
* Formats email address exactly as mailchimp requires it.
*
* @param $email_address
* @return string
*/
public function get_subscriber_hash($email_address) {
return md5(strtolower(trim($email_address)));
}
/**
* Gets list merge fields (if any)
*
* @param $list_id
* @param array $args
* @return array
*/
public function get_list_merge_fields($list_id, $args = []) {
$data = $this->client->get(sprintf('/lists/%s/merge-fields', $list_id), $args);
if (is_object($data) && isset($data->merge_fields)) {
return $data->merge_fields;
}
return [];
}
/**
* returns details of a specific single list
* and can return/check list against array of arguments
*
* @param $list_id
* @return object[]
*/
public function get_list($list_id) {
return $this->client->get(sprintf('/lists/%s', $list_id));
}
/**
* returns details of a all lists and
* can return/check lists against array of arguments
*
* @param array $args
* @return array
*/
public function get_lists($args = []) {
$data = $this->client->get('/lists', $args);
if (is_object($data) && isset($data->lists)) {
return $data->lists;
}
return [];
}
/**
* Creates a list based on array of arguments
*
* @param array $args
* @return array
*/
public function create_list($args = []) {
return $this->client->post('/lists', $args);
}
/**
* Update/Edit a specific list based on array of arguments
*
* @param $list_id
* @param array $args
* @return array
*/
public function update_list($list_id, $args = []) {
$data = $this->client->patch(sprintf('/lists/%s', $list_id), $args);
if (is_object($data) && isset($data->lists)) {
return $data->lists;
}
return [];
}
/**
* Delete list by id
*
* @param $list_id
* @return array
*/
public function delete_list($list_id) {
$data = $this->client->delete(sprintf('/lists/%s', $list_id));
if (is_object($data) && isset($data->lists)) {
return $data->lists;
}
return [];
}
/**
* Add a subscriber to a list based on
* list id and array of details of subscriber.
* !! If subscriber already exists, this updates them (except email)
*
* @param $list_id
* @param $args
* @return object[]
*/
public function add_list_member($list_id, $args) {
$subscriber_hash = $this->get_subscriber_hash($args['email_address']);
// Mailchimp object requirement check
if (isset($args['merge_fields'])) {
$args['merge_fields'] = (object) $args['merge_fields'];
}
return $this->client->put(sprintf('/lists/%s/members/%s', $list_id, $subscriber_hash), $args);
}
/**
* Gets details of specific list member in specific list
*
* @param $list_id
* @param $email_address
* @param array $args
* @return object[]
*/
public function get_list_member($list_id, $email_address, $args = []) {
$subscriber_hash = $this->get_subscriber_hash($email_address);
return $this->client->get(sprintf('/lists/%s/members/%s', $list_id, $subscriber_hash), $args);
}
/**
* Update subscriber in a specific list with
* the arguments passed in by the array.
*
* @param $list_id
* @param $email_address
* @param $args
* @return object[]
*/
public function update_list_member($list_id, $email_address, $args) {
$subscriber_hash = $this->get_subscriber_hash($email_address);
// Mailchimp object requirement check
if (isset($args['merge_fields'])) {
$args['merge_fields'] = (object) $args['merge_fields'];
}
return $this->client->put(sprintf('/lists/%s/members/%s', $list_id, $subscriber_hash), $args);
}
/**
* Delete subscriber from the list
* @param $list_id
* @param $email_address
*
* @return bool
*/
public function delete_list_member($list_id, $email_address) {
$subscriber_hash = $this->get_subscriber_hash($email_address);
$data = $this->client->delete(sprintf('/lists/%s/members/%s', $list_id, $subscriber_hash));
// http://stackoverflow.com/a/2127324
return !!$data;
}
/**
* Checks if the user with email address
* is also a subscriber of the provided list.
* @param $list_id
* @param $email_address
*
* @return bool
*/
public function is_member($list_id, $email_address) {
try {
$data = $this->get_list_member($list_id, $email_address);
return !empty($data->id);
} catch (MailchimpException $error) {
$this->error_code = $error->getCode();
$this->error_message = $error;
return false;
}
}
public function is_subscriber($list_id, $email_address) {
if ($this->is_member($list_id, $email_address)) {
$member = $this->get_list_member($list_id, $email_address);
return $member->status === 'subscribed';
} else {
return false;
}
}
public function subscribe($list_id, array $args = []) {
$is_subscriber = $this->is_subscriber($list_id, $args['email_address']);
$is_member =$this->is_member($list_id, $args['email_address']);
if (!$is_subscriber && $is_member) {
return $this->update_list_member($list_id, $args['email_address'], ['status' => 'subscribed']);
} else if (!$is_subscriber && !$is_member) {
return $this->add_list_member($list_id, $args);
}
return [];
}
public function unsubscribe($list_id, $email_address) {
$is_subscriber = $this->is_subscriber($list_id, $email_address);
if ($is_subscriber) {
return $this->update_list_member($list_id, $email_address, ['status' => 'unsubscribed']);
}
return [];
}
/**
* Debugging: Returns the details of last response body.
* @return string
*/
public function get_last_response_body() {
return $this->client->get_last_response_body();
}
/**
* Debugging: Returns array details of last header response.
* @return array
*/
public function get_last_response_headers() {
return $this->client->get_last_response_headers();
}
/**
* Resets error properties.
*/
public function reset_error() {
$this->error_message = '';
$this->error_code = '';
}
/**
* @return string
*/
public function get_error_message() {
return $this->error_message;
}
/**
* @return string
*/
public function get_error_code() {
return $this->error_code;
}
}