You are here

function mailchimp_unsubscribe_process in Mailchimp 7.4

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_unsubscribe_process()
  2. 7.5 mailchimp.module \mailchimp_unsubscribe_process()
  3. 7.3 mailchimp.module \mailchimp_unsubscribe_process()
  4. 2.x mailchimp.module \mailchimp_unsubscribe_process()

Unsubscribe a member from a list.

Return value

bool Success or failure.

1 call to mailchimp_unsubscribe_process()
mailchimp_unsubscribe_member in ./mailchimp.module
Unsubscribe a member from a list.
2 string references to 'mailchimp_unsubscribe_process'
mailchimp_unsubscribe_member in ./mailchimp.module
Unsubscribe a member from a list.
mailchimp_update_local_cache in ./mailchimp.module
Updates the local cache for a user as though a queued request had been processed.

File

./mailchimp.module, line 973
Mailchimp module.

Code

function mailchimp_unsubscribe_process($list_id, $email, $delete, $goodbye, $notify) {
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot unsubscribe from list without Mailchimp API. Check API key has been entered.');
    }
    if ($delete) {

      // Remove member from list.
      $mc_lists
        ->removeMember($list_id, $email);
      watchdog('mailchimp', '@email was removed from list @list_id.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_INFO);
    }
    else {

      // Unsubscribe member.
      $parameters = array(
        'status' => MailchimpLists::MEMBER_STATUS_UNSUBSCRIBED,
      );
      $mc_lists
        ->updateMember($list_id, $email, $parameters);
      watchdog('mailchimp', '@email was unsubscribed from list @list_id.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_INFO);
    }
    module_invoke_all('mailchimp_unsubscribe_user', $list_id, $email);

    // Clear user cache:
    mailchimp_cache_clear_member($list_id, $email);
    return TRUE;
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred unsubscribing @email from list @list. "%message"', array(
      '@email' => $email,
      '@list' => $list_id,
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
}