You are here

function mailchimp_subscribe_process in Mailchimp 7.4

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

Wrapper around MailchimpLists::addOrUpdateMember().

Return value

object On success a result object will be returned from Mailchimp. On failure an object will be returned with the property success set to FALSE, the response code as a property, and the message as a property. To check for a failure, look for the property 'success' of the object returned to be set to FALSE.

See also

MailchimpLists::addOrUpdateMember()

2 calls to mailchimp_subscribe_process()
mailchimp_automations_trigger_workflow in modules/mailchimp_automations/mailchimp_automations.module
Triggers a workflow automation via the Mailchimp API.
mailchimp_subscribe in ./mailchimp.module
Subscribe a user to a Mailchimp list in real time or by adding to the queue.
2 string references to 'mailchimp_subscribe_process'
mailchimp_subscribe in ./mailchimp.module
Subscribe a user to a Mailchimp list in real time or by adding to the queue.
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 591
Mailchimp module.

Code

function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $format = 'html') {
  $result = FALSE;
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot subscribe to list without Mailchimp API. Check API key has been entered.');
    }
    $parameters = array(
      // If double opt-in is required, set member status to 'pending', but only
      // if the user isn't already subscribed.
      'status' => $double_optin && !mailchimp_is_subscribed($list_id, $email) ? \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;
    }

    // Add member to list.
    $result = $mc_lists
      ->addOrUpdateMember($list_id, $email, $parameters);
    if (isset($result->id)) {
      module_invoke_all('mailchimp_subscribe_user', $list_id, $email, $merge_vars);

      // Clear user cache, just in case there's some cruft leftover:
      mailchimp_cache_clear_member($list_id, $email);
      watchdog('mailchimp', '@email was subscribed to list @list.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_NOTICE);
    }
    else {
      if (!variable_get('mailchimp_test_mode')) {
        watchdog('mailchimp', 'A problem occurred subscribing @email to 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" subscribing @email to list @list.  Trying again using double-opt in.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_INFO);
      return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, TRUE, $format);
    }
    watchdog('mailchimp', 'An error occurred subscribing @email to list @list. Status code @code. "%message"', array(
      '@email' => $email,
      '@list' => $list_id,
      '%message' => $e
        ->getMessage(),
      '@code' => $e
        ->getCode(),
    ), WATCHDOG_ERROR);
    $result = new stdClass();
    $result->success = FALSE;
    $result->status = $e
      ->getCode();
    $result->message = $e
      ->getMessage();
  }
  if ($double_optin) {
    drupal_set_message(t('Please check your email to confirm your subscription'), 'status', FALSE);
  }
  return $result;
}