You are here

function mailchimp_update_member_process in Mailchimp 7.4

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

Wrapper around MailchimpLists::updateMember().

Return value

bool Success or failure.

See also

MailchimpLists::updateMember()

1 call to mailchimp_update_member_process()
mailchimp_update_member in ./mailchimp.module
Update a members list subscription in real time or by adding to the queue.
2 string references to 'mailchimp_update_member_process'
mailchimp_update_local_cache in ./mailchimp.module
Updates the local cache for a user as though a queued request had been processed.
mailchimp_update_member in ./mailchimp.module
Update a members list subscription in real time or by adding to the queue.

File

./mailchimp.module, line 787
Mailchimp module.

Code

function mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, $double_optin = FALSE) {
  $result = FALSE;
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot update member without Mailchimp API. Check API key has been entered.');
    }
    $parameters = array(
      'status' => $double_optin ? \Mailchimp\MailchimpLists::MEMBER_STATUS_PENDING : \Mailchimp\MailchimpLists::MEMBER_STATUS_SUBSCRIBED,
      'email_type' => $format,
    );

    // Set interests.
    if (!empty($interests)) {
      $parameters['interests'] = (object) $interests;
    }

    // Set merge fields.
    if (!empty($merge_vars)) {
      $parameters['merge_fields'] = (object) $merge_vars;
    }

    // Update member.
    $result = $mc_lists
      ->updateMember($list_id, $email, $parameters);
    if (!empty($result) && isset($result->id)) {
      watchdog('mailchimp', '@email was updated in list @list_id.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_NOTICE);

      // Clear user cache:
      mailchimp_cache_clear_member($list_id, $email);
    }
    else {
      watchdog('mailchimp', 'A problem occurred updating @email on list @list.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_WARNING);
    }
  } catch (Exception $e) {
    if ($e
      ->getCode() == '400' && strpos($e
      ->getMessage(), 'Member In Compliance State') !== false && !$double_optin) {
      watchdog('mailchimp', 'Detected "Member In Compliance State" updating @email to list @list.  Trying again using double-opt in.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_INFO);
      return mailchimp_update_member_process($list_id, $email, $merge_vars, $interests, $format, TRUE);
    }
    watchdog('mailchimp', 'An error occurred updating @email on list @list. "%message"', array(
      '@email' => $email,
      '@list' => $list_id,
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
  if ($double_optin) {
    drupal_set_message(t('Please check your email to confirm your subscription'), 'status', FALSE);
  }
  return $result;
}