You are here

function mailchimp_subscribe_process in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 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 Mailchimp_Lists::subscribe().

See also

Mailchimp_Lists::subscribe()

1 call 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.
1 string reference 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.

File

./mailchimp.module, line 454
Mailchimp module.

Code

function mailchimp_subscribe_process($list_id, $email, $merge_vars = NULL, $interests = [], $double_optin = FALSE, $format = 'html', $language = NULL, $gdpr_consent = FALSE) {
  $config = \Drupal::config('mailchimp.settings');
  $result = FALSE;
  try {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new Exception('Cannot subscribe to list without Mailchimp API. Check API key has been entered.');
    }
    $parameters = [
      // If double opt-in is required, set member status to 'pending'.
      'status' => $double_optin ? MailchimpLists::MEMBER_STATUS_PENDING : MailchimpLists::MEMBER_STATUS_SUBSCRIBED,
      'email_type' => $format,
    ];
    if (!empty($language)) {
      $parameters['language'] = $language;
    }

    // Set interests.
    if (!empty($interests)) {
      $selected_interests = [];
      foreach ($interests as $interest_group) {

        // This could happen in case the selected interest group
        // is set to display radio inputs. So we either do an
        // explicit check here, or simply transform the single string
        // value to an array in order to pass the condition check below.
        if (!is_array($interest_group)) {
          $interest_group = [
            $interest_group => $interest_group,
          ];
        }
        foreach ($interest_group as $interest_id => $interest_status) {
          $selected_interests[$interest_id] = $interest_status !== 0;
        }
      }
      if (!empty($selected_interests)) {
        $parameters['interests'] = (object) $selected_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.
      $marketing_permissions = mailchimp_get_marketing_permissions($list_id, $email);
      $was_subscribed = FALSE;
      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)) {
      \Drupal::moduleHandler()
        ->invokeAll('mailchimp_subscribe_success', [
        $list_id,
        $email,
        $merge_vars,
      ]);

      // Clear user cache, just in case there's some cruft leftover:
      mailchimp_cache_clear_member($list_id, $email);
      \Drupal::logger('mailchimp')
        ->notice('{email} was subscribed to list {list}.', [
        'email' => $email,
        'list' => $list_id,
      ]);

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

        // 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)) {
          \Drupal::logger('mailchimp')
            ->warning('A problem occurred setting marketing permissions for {email} on list {list}.', [
            'email' => $email,
            'list' => $list_id,
          ]);
        }
      }
    }
    else {
      if (!$config
        ->get('test_mode')) {
        \Drupal::logger('mailchimp')
          ->warning('A problem occurred subscribing {email} to list {list}.', [
          'email' => $email,
          'list' => $list_id,
        ]);
      }
    }
  } catch (\Exception $e) {
    if ($e
      ->getCode() == '400' && strpos($e
      ->getMessage(), 'Member In Compliance State') !== FALSE && !$double_optin) {
      \Drupal::logger('mailchimp')
        ->error('Detected "Member In Compliance State" subscribing {email} to list {list}. Trying again using double-opt in.', [
        'email' => $email,
        'list' => $list_id,
      ]);
      return mailchimp_subscribe_process($list_id, $email, $merge_vars, $interests, TRUE, $format, $language, $gdpr_consent);
    }
    \Drupal::logger('mailchimp')
      ->error('An error occurred subscribing {email} to list {list}. "{message}"', [
      'email' => $email,
      'list' => $list_id,
      'message' => $e
        ->getMessage(),
    ]);
  }
  if ($double_optin) {
    \Drupal::messenger()
      ->addStatus(t('Please check your email to confirm your subscription.'), FALSE);
  }
  return $result;
}