1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Newsletter; |
4
|
|
|
|
5
|
|
|
class ApiDriver implements Newsletter |
6
|
|
|
{ |
7
|
|
|
/** @var \DrewM\MailChimp\MailChimp */ |
8
|
|
|
protected $mailChimp; |
9
|
|
|
|
10
|
|
|
/** @var \Spatie\Newsletter\NewsletterListCollection */ |
11
|
|
|
protected $lists; |
12
|
|
|
|
13
|
|
|
public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists) |
14
|
|
|
{ |
15
|
|
|
$this->mailChimp = $mailChimp; |
|
|
|
|
16
|
|
|
|
17
|
|
|
$this->lists = $lists; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function subscribe(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$list = $this->lists->findByName($listName); |
23
|
|
|
|
24
|
|
|
$options = $this->getSubscriptionOptions($email, $mergeFields, $options); |
25
|
|
|
|
26
|
|
|
$response = $this->mailChimp->post("lists/{$list->getId()}/members", $options); |
27
|
|
|
|
28
|
|
|
if (! $this->lastActionSucceeded()) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $response; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function subscribePending(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
36
|
|
|
{ |
37
|
|
|
$options = array_merge($options, ['status' => 'pending']); |
38
|
|
|
|
39
|
|
|
return $this->subscribe($email, $mergeFields, $listName, $options); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
View Code Duplication |
public function subscribeOrUpdate( |
|
|
|
|
43
|
|
|
string $email, |
44
|
|
|
array $mergeFields = [], |
45
|
|
|
string $listName = '', |
46
|
|
|
array $options = [] |
47
|
|
|
) { |
48
|
|
|
$list = $this->lists->findByName($listName); |
49
|
|
|
|
50
|
|
|
$options = $this->getSubscriptionOptions($email, $mergeFields, $options); |
51
|
|
|
|
52
|
|
|
$response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", |
53
|
|
|
$options); |
54
|
|
|
|
55
|
|
|
if (! $this->lastActionSucceeded()) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getMembers(string $listName = '', array $parameters = []) |
63
|
|
|
{ |
64
|
|
|
$list = $this->lists->findByName($listName); |
65
|
|
|
|
66
|
|
|
return $this->mailChimp->get("lists/{$list->getId()}/members", $parameters); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getMember(string $email, string $listName = '') |
70
|
|
|
{ |
71
|
|
|
$list = $this->lists->findByName($listName); |
72
|
|
|
|
73
|
|
|
return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMemberActivity(string $email, string $listName = '') |
77
|
|
|
{ |
78
|
|
|
$list = $this->lists->findByName($listName); |
79
|
|
|
|
80
|
|
|
return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/activity"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function hasMember(string $email, string $listName = ''): bool |
84
|
|
|
{ |
85
|
|
|
$response = $this->getMember($email, $listName); |
86
|
|
|
|
87
|
|
|
if (! isset($response['email_address'])) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (strtolower($response['email_address']) != strtolower($email)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function isSubscribed(string $email, string $listName = ''): bool |
99
|
|
|
{ |
100
|
|
|
$response = $this->getMember($email, $listName); |
101
|
|
|
|
102
|
|
|
if (! isset($response)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($response['status'] != 'subscribed') { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function unsubscribe(string $email, string $listName = '') |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$list = $this->lists->findByName($listName); |
116
|
|
|
|
117
|
|
|
$response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [ |
118
|
|
|
'status' => 'unsubscribed', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
if (! $this->lastActionSucceeded()) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function updateEmailAddress(string $currentEmailAddress, string $newEmailAddress, string $listName = '') |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$list = $this->lists->findByName($listName); |
131
|
|
|
|
132
|
|
|
$response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", |
133
|
|
|
[ |
134
|
|
|
'email_address' => $newEmailAddress, |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
return $response; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function delete(string $email, string $listName = '') |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$list = $this->lists->findByName($listName); |
143
|
|
|
|
144
|
|
|
$response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}"); |
145
|
|
|
|
146
|
|
|
return $response; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function deletePermanently(string $email, string $listName = '') |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$list = $this->lists->findByName($listName); |
152
|
|
|
|
153
|
|
|
$response = $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/actions/delete-permanent"); |
154
|
|
|
|
155
|
|
|
return $response; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getTags(string $email, string $listName = '') |
159
|
|
|
{ |
160
|
|
|
$list = $this->lists->findByName($listName); |
161
|
|
|
|
162
|
|
|
return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
View Code Duplication |
public function addTags(array $tags, string $email, string $listName = '') |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$list = $this->lists->findByName($listName); |
168
|
|
|
|
169
|
|
|
$payload = collect($tags)->map(function ($tag) { |
170
|
|
|
return ['name' => $tag, 'status' => 'active']; |
171
|
|
|
})->toArray(); |
172
|
|
|
|
173
|
|
|
return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [ |
174
|
|
|
'tags' => $payload, |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public function removeTags(array $tags, string $email, string $listName = '') |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$list = $this->lists->findByName($listName); |
181
|
|
|
|
182
|
|
|
$payload = collect($tags)->map(function ($tag) { |
183
|
|
|
return ['name' => $tag, 'status' => 'inactive']; |
184
|
|
|
})->toArray(); |
185
|
|
|
|
186
|
|
|
return $this->mailChimp->post("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/tags", [ |
187
|
|
|
'tags' => $payload, |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function createCampaign( |
192
|
|
|
string $fromName, |
193
|
|
|
string $replyTo, |
194
|
|
|
string $subject, |
195
|
|
|
string $html = '', |
196
|
|
|
string $listName = '', |
197
|
|
|
array $options = [], |
198
|
|
|
array $contentOptions = [] |
199
|
|
|
) { |
200
|
|
|
$list = $this->lists->findByName($listName); |
201
|
|
|
|
202
|
|
|
$defaultOptions = [ |
203
|
|
|
'type' => 'regular', |
204
|
|
|
'recipients' => [ |
205
|
|
|
'list_id' => $list->getId(), |
206
|
|
|
], |
207
|
|
|
'settings' => [ |
208
|
|
|
'subject_line' => $subject, |
209
|
|
|
'from_name' => $fromName, |
210
|
|
|
'reply_to' => $replyTo, |
211
|
|
|
], |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
$options = array_merge($defaultOptions, $options); |
215
|
|
|
|
216
|
|
|
$response = $this->mailChimp->post('campaigns', $options); |
217
|
|
|
|
218
|
|
|
if (! $this->lastActionSucceeded()) { |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($html === '') { |
223
|
|
|
return $response; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if (! $this->updateContent($response['id'], $html, $contentOptions)) { |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $response; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function updateContent(string $campaignId, string $html, array $options = []) |
234
|
|
|
{ |
235
|
|
|
$defaultOptions = compact('html'); |
236
|
|
|
|
237
|
|
|
$options = array_merge($defaultOptions, $options); |
238
|
|
|
|
239
|
|
|
$response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options); |
240
|
|
|
|
241
|
|
|
if (! $this->lastActionSucceeded()) { |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $response; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getApi(): MailChimp |
249
|
|
|
{ |
250
|
|
|
return $this->mailChimp; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return array|false |
255
|
|
|
*/ |
256
|
|
|
public function getLastError() |
257
|
|
|
{ |
258
|
|
|
return $this->mailChimp->getLastError(); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function lastActionSucceeded(): bool |
262
|
|
|
{ |
263
|
|
|
return $this->mailChimp->success(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected function getSubscriberHash(string $email): string |
267
|
|
|
{ |
268
|
|
|
return $this->mailChimp->subscriberHash($email); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
protected function getSubscriptionOptions(string $email, array $mergeFields, array $options): array |
272
|
|
|
{ |
273
|
|
|
$defaultOptions = [ |
274
|
|
|
'email_address' => $email, |
275
|
|
|
'status' => 'subscribed', |
276
|
|
|
'email_type' => 'html', |
277
|
|
|
]; |
278
|
|
|
|
279
|
|
|
if (count($mergeFields)) { |
280
|
|
|
$defaultOptions['merge_fields'] = $mergeFields; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$options = array_merge($defaultOptions, $options); |
284
|
|
|
|
285
|
|
|
return $options; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..