You are here

function mailchimp_update_member_process in Mailchimp 8

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

Wrapper around Mailchimp_Lists::updateMember().

See also

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

File

./mailchimp.module, line 645
Mailchimp module.

Code

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

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mcapi = mailchimp_get_api_object('MailchimpLists');
    $parameters = [
      'status' => $double_optin ? MailchimpLists::MEMBER_STATUS_PENDING : MailchimpLists::MEMBER_STATUS_SUBSCRIBED,
      'email_type' => $format,
    ];

    // Set interests.
    if (!empty($interests)) {
      $selected_interests = [];
      foreach ($interests as $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);
      if ($marketing_permissions) {
        foreach ($marketing_permissions as $marketing_permission) {
          $parameters['marketing_permissions'][] = [
            'marketing_permission_id' => $marketing_permission->marketing_permission_id,
            'enabled' => TRUE,
          ];
        }
      }
    }

    // Update member.
    $result = $mcapi
      ->updateMember($list_id, $email, $parameters);
    if (isset($result->id)) {
      \Drupal::logger('mailchimp')
        ->notice('{email} was updated in list {list_id}.', [
        'email' => $email,
        'list' => $list_id,
      ]);

      // Clear user cache:
      mailchimp_cache_clear_member($list_id, $email);
    }
    else {
      \Drupal::logger('mailchimp')
        ->warning('A problem occurred updating {email} on 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_update_member_process($list_id, $email, $merge_vars, $interests, $format, TRUE, $gdpr_consent);
    }
    \Drupal::logger('mailchimp')
      ->error('An error occurred updating {email} on 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;
}