You are here

function mailchimp_subscribe_process in Mailchimp 7.5

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_subscribe_process()
  2. 7.3 mailchimp.module \mailchimp_subscribe_process()
  3. 7.4 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 724
Mailchimp module.

Code

function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $interests = array(), $double_optin = FALSE, $format = 'html', $gdpr_consent = FALSE) {
  $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;
    }

    // Has GDPR consent been given?
    if ($gdpr_consent) {

      // If the member is already subscribed get the marketing permission id(s)
      // for the list and enable them.
      $was_subscribed = FALSE;
      $marketing_permissions = mailchimp_get_marketing_permissions($list_id, $email);
      if ($marketing_permissions) {
        foreach ($marketing_permissions as $marketing_permission) {
          $parameters['marketing_permissions'][] = [
            'marketing_permission_id' => $marketing_permission->marketing_permission_id,
            'enabled' => TRUE,
          ];
        }
        $was_subscribed = TRUE;
      }
    }
    else {

      // We need to make sure this is set.
      $was_subscribed = FALSE;
    }

    // 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);
      watchdog('mailchimp', '@email was subscribed to list @list.', array(
        '@email' => $email,
        '@list' => $list_id,
      ), WATCHDOG_NOTICE);

      // For newly subscribed members set GDPR consent if it's been given.
      if ($gdpr_consent && !$was_subscribed) {

        // If the member is already subscribed get the marketing permission id(s)
        // for the list and enable them.
        foreach ($result->marketing_permissions as $marketing_permission) {
          $parameters['marketing_permissions'][] = [
            'marketing_permission_id' => $marketing_permission->marketing_permission_id,
            'enabled' => TRUE,
          ];

          // Update the member.
          $result = $mc_lists
            ->addOrUpdateMember($list_id, $email, $parameters);
          if (!isset($result->id)) {
            watchdog('mailchimp', 'A problem occurred setting marketing permissions for @email on list @list.', array(
              '@email' => $email,
              '@list' => $list_id,
            ), WATCHDOG_WARNING);
          }
        }
      }
    }
    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, $gdpr_consent);
    }
    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();
  }

  // Clear user cache, just in case there's some cruft leftover:
  mailchimp_cache_clear_member($list_id, $email);
  if ($double_optin) {
    drupal_set_message(t('Please check your email to confirm your subscription'), 'status', FALSE);
  }
  return $result;
}