Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle interest groups in unsubscribe webhook. #355

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions CRM/Mailchimp/Page/WebHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,14 @@ public function subscribe() {
public function unsubscribe() {

try {
$this->contact_id = $this->sync->guessContactIdSingle(
$this->request_data['email'],
$this->request_data['merges']['FNAME'],
$this->request_data['merges']['LNAME'],
$must_be_in_group=TRUE
);
$this->contact_id = !empty($this->request_data['merges']['CONTACT_ID'])
? $this->request_data['merges']['CONTACT_ID']
: $this->sync->guessContactIdSingle(
$this->request_data['email'],
$this->request_data['merges']['FNAME'],
$this->request_data['merges']['LNAME'],
$must_be_in_group=TRUE
);
if (!$this->contact_id) {
// Hmm. We don't think they *are* subscribed.
// Nothing for us to do.
Expand All @@ -188,6 +190,8 @@ public function unsubscribe() {
'group_id' => $this->sync->membership_group_id,
'status' => 'Removed',
]);
// Handle interest groups.
$this->updateInterestsFromMerges(TRUE);
}
/**
* Handle profile update requests.
Expand Down Expand Up @@ -445,11 +449,15 @@ public function findOrCreateContact() {
* Mailchimp still sends interests to webhooks in an old school way.
*
* So it's left to us to identify the interests and groups that they refer to.
*
* @param bool $isUnsubscribe
* Whether we are processing an unsubscribe action, so should remove the contact from groups.
*/
public function updateInterestsFromMerges() {
public function updateInterestsFromMerges($isUnsubscribe = FALSE) {

// Get a list of CiviCRM group Ids that this contact should be in.
$should_be_in = $this->sync->convertMailchimpWebhookGroupsToCiviGroupIds($this->request_data['merges']['GROUPINGS']);
// Empty if we are processing an unsubscribe action.
$should_be_in = $isUnsubscribe ? [] : $this->sync->convertMailchimpWebhookGroupsToCiviGroupIds($this->request_data['merges']['GROUPINGS']);

// Now get a list of all the groups they *are* in.
$result = civicrm_api3('Contact', 'getsingle', ['return' => 'group', 'contact_id' => $this->contact_id]);
Expand Down