Skip to content

Commit ca72a19

Browse files
committed
Add support for Mailchimp adding tags
1 parent c60c60a commit ca72a19

File tree

4 files changed

+165
-8
lines changed

4 files changed

+165
-8
lines changed

packages/join-block/src/Services/ActionNetworkService.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public static function signup($data)
2727
return trim($tag);
2828
}, explode(",", $removeTags));
2929

30+
// Allow third-party code to modify tags before they're applied
31+
// Generic filter applies to all services
32+
$addTags = apply_filters('ck_join_flow_add_tags', $addTags, $data, 'action_network');
33+
$removeTags = apply_filters('ck_join_flow_remove_tags', $removeTags, $data, 'action_network');
34+
35+
// Service-specific filter for Action Network-only customization
36+
$addTags = apply_filters('ck_join_flow_action_network_add_tags', $addTags, $data);
37+
$removeTags = apply_filters('ck_join_flow_action_network_remove_tags', $removeTags, $data);
38+
3039
$customFieldValues = [
3140
"How did you hear about us?" => $data['howDidYouHearAboutUs'],
3241
"How did you hear about us? (Details)" => $data['howDidYouHearAboutUsDetails'] ?? "",

packages/join-block/src/Services/JoinService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,21 @@ public static function toggleMemberLapsed($email, $lapsed = true, $paymentDate =
368368
throw $exception;
369369
}
370370
}
371+
372+
if (Settings::get("USE_MAILCHIMP")) {
373+
$joinBlockLog->info("$action member $email as lapsed in Mailchimp");
374+
try {
375+
if ($lapsed) {
376+
MailchimpService::addTag($email, Settings::get("LAPSED_TAG"));
377+
} else {
378+
MailchimpService::removeTag($email, Settings::get("LAPSED_TAG"));
379+
}
380+
$joinBlockLog->info("$done member $email as lapsed in Mailchimp");
381+
} catch (\Exception $exception) {
382+
$joinBlockLog->error("Mailchimp error for email $email: " . $exception->getMessage());
383+
throw $exception;
384+
}
385+
}
371386
}
372387

373388
private static function handleGocardless($data)

packages/join-block/src/Services/MailchimpService.php

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,34 @@ public static function signup($data)
2828
'server' => $server
2929
]);
3030

31+
$addTags = $data["membershipPlan"]["add_tags"] ?? "";
32+
$removeTags = $data["membershipPlan"]["remove_tags"] ?? "";
33+
34+
$addTags = array_map(function ($tag) {
35+
return trim($tag);
36+
}, explode(",", $addTags));
37+
38+
$removeTags = array_map(function ($tag) {
39+
return trim($tag);
40+
}, explode(",", $removeTags));
41+
42+
// Filter out empty tags
43+
$addTags = array_filter($addTags, function ($tag) {
44+
return !empty($tag);
45+
});
46+
$removeTags = array_filter($removeTags, function ($tag) {
47+
return !empty($tag);
48+
});
49+
50+
// Allow third-party code to modify tags before they're applied
51+
// Generic filter applies to all services
52+
$addTags = apply_filters('ck_join_flow_add_tags', $addTags, $data, 'mailchimp');
53+
$removeTags = apply_filters('ck_join_flow_remove_tags', $removeTags, $data, 'mailchimp');
54+
55+
// Service-specific filter for Mailchimp-only customization
56+
$addTags = apply_filters('ck_join_flow_mailchimp_add_tags', $addTags, $data);
57+
$removeTags = apply_filters('ck_join_flow_mailchimp_remove_tags', $removeTags, $data);
58+
3159
if ($data['isUpdateFlow']) {
3260
$mergeFields = [];
3361
} else {
@@ -52,20 +80,125 @@ public static function signup($data)
5280
}
5381
}
5482

83+
$memberData = [
84+
"email_address" => $email,
85+
"status" => "subscribed",
86+
"merge_fields" => $mergeFields
87+
];
88+
89+
// Add tags if present - use simple array of tag names for addListMember
90+
if (!empty($addTags)) {
91+
$memberData["tags"] = array_values($addTags);
92+
}
93+
94+
$memberExists = false;
5595
try {
56-
$mailchimp->lists->addListMember($mailchimp_audience_id, [
57-
"email_address" => $email,
58-
"status" => "subscribed",
59-
"merge_fields" => $mergeFields
60-
]);
96+
$mailchimp->lists->addListMember($mailchimp_audience_id, $memberData);
6197
$joinBlockLog->info("$email added to Mailchimp");
6298
} catch (\GuzzleHttp\Exception\ClientException $e) {
6399
$alreadySignedUp = str_contains($e->getMessage(), "Member Exists");
64100
if ($alreadySignedUp) {
65101
$joinBlockLog->info("$email already in Mailchimp");
102+
$memberExists = true;
66103
} else {
67104
throw $e;
68105
}
69106
}
107+
108+
// Handle tag updates for existing members or if we need to remove tags
109+
// For existing members, we need to add tags via updateListMemberTags
110+
// For new members, we need to remove tags via updateListMemberTags (can't do it in addListMember)
111+
if ($memberExists || !empty($removeTags)) {
112+
try {
113+
$subscriberHash = md5(strtolower($email));
114+
$tagUpdates = [];
115+
116+
// If member exists, add tags that weren't added during creation
117+
if ($memberExists && !empty($addTags)) {
118+
foreach ($addTags as $tag) {
119+
$tagUpdates[] = ["name" => $tag, "status" => "active"];
120+
}
121+
}
122+
123+
// Add tags to remove
124+
if (!empty($removeTags)) {
125+
foreach ($removeTags as $tag) {
126+
$tagUpdates[] = ["name" => $tag, "status" => "inactive"];
127+
}
128+
}
129+
130+
if (!empty($tagUpdates)) {
131+
$mailchimp->lists->updateListMemberTags(
132+
$mailchimp_audience_id,
133+
$subscriberHash,
134+
["tags" => $tagUpdates]
135+
);
136+
$joinBlockLog->info("Updated tags for $email in Mailchimp");
137+
}
138+
} catch (\GuzzleHttp\Exception\ClientException $e) {
139+
$joinBlockLog->error("Failed to update tags for $email in Mailchimp: " . $e->getMessage());
140+
// Don't throw - tag updates are not critical for existing members
141+
}
142+
}
143+
}
144+
145+
public static function addTag($email, $tag)
146+
{
147+
global $joinBlockLog;
148+
149+
$mailchimp_api_key = Settings::get("MAILCHIMP_API_KEY");
150+
$mailchimp_audience_id = Settings::get("MAILCHIMP_AUDIENCE_ID");
151+
# Server name (e.g. us22) is at the end of the API key (e.g. ...-us22)
152+
$array_key_parts = explode("-", $mailchimp_api_key);
153+
$server = array_pop($array_key_parts);
154+
$mailchimp = new ApiClient();
155+
$mailchimp->setConfig([
156+
'apiKey' => $mailchimp_api_key,
157+
'server' => $server
158+
]);
159+
160+
$subscriberHash = md5(strtolower($email));
161+
162+
try {
163+
$mailchimp->lists->updateListMemberTags(
164+
$mailchimp_audience_id,
165+
$subscriberHash,
166+
["tags" => [["name" => $tag, "status" => "active"]]]
167+
);
168+
$joinBlockLog->info("Added tag '$tag' to $email in Mailchimp");
169+
} catch (\GuzzleHttp\Exception\ClientException $e) {
170+
$joinBlockLog->error("Failed to add tag '$tag' to $email in Mailchimp: " . $e->getMessage());
171+
throw $e;
172+
}
173+
}
174+
175+
public static function removeTag($email, $tag)
176+
{
177+
global $joinBlockLog;
178+
179+
$mailchimp_api_key = Settings::get("MAILCHIMP_API_KEY");
180+
$mailchimp_audience_id = Settings::get("MAILCHIMP_AUDIENCE_ID");
181+
# Server name (e.g. us22) is at the end of the API key (e.g. ...-us22)
182+
$array_key_parts = explode("-", $mailchimp_api_key);
183+
$server = array_pop($array_key_parts);
184+
$mailchimp = new ApiClient();
185+
$mailchimp->setConfig([
186+
'apiKey' => $mailchimp_api_key,
187+
'server' => $server
188+
]);
189+
190+
$subscriberHash = md5(strtolower($email));
191+
192+
try {
193+
$mailchimp->lists->updateListMemberTags(
194+
$mailchimp_audience_id,
195+
$subscriberHash,
196+
["tags" => [["name" => $tag, "status" => "inactive"]]]
197+
);
198+
$joinBlockLog->info("Removed tag '$tag' from $email in Mailchimp");
199+
} catch (\GuzzleHttp\Exception\ClientException $e) {
200+
$joinBlockLog->error("Failed to remove tag '$tag' from $email in Mailchimp: " . $e->getMessage());
201+
throw $e;
202+
}
70203
}
71204
}

packages/join-block/src/Settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function init()
7070
$membership_plans_fields = [
7171
Field::make('text', 'lapsed_tag')
7272
->set_default_value("Lapsed - failed payment")
73-
->set_help_text("Will be applied to members in your CMS if they delete or do not pay their subscription"),
73+
->set_help_text("Will be applied to members in Action Network and Mailchimp if they delete or do not pay their subscription"),
7474
Field::make('separator', 'membership_plans_sep', 'Membership Plans'),
7575
$membership_plans,
7676
];
@@ -422,8 +422,8 @@ public static function createMembershipPlansField($name = 'membership_plans')
422422
$payment_frequency_select,
423423
$payment_currency_select,
424424
Field::make('text', 'description'),
425-
Field::make('text', 'add_tags')->set_help_text("Comma-separated tags to add to this member in Action Network."),
426-
Field::make('text', 'remove_tags')->set_help_text("Comma-separated tags to remove from this member in Action Network.")
425+
Field::make('text', 'add_tags')->set_help_text("Comma-separated tags to add to this member in Action Network and Mailchimp."),
426+
Field::make('text', 'remove_tags')->set_help_text("Comma-separated tags to remove from this member in Action Network and Mailchimp.")
427427
])->set_min(1);
428428
return $membership_plans;
429429
}

0 commit comments

Comments
 (0)