@@ -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}
0 commit comments