You are here

function mailchimp_get_memberinfo in Mailchimp 2.x

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_get_memberinfo()
  2. 7.5 mailchimp.module \mailchimp_get_memberinfo()
  3. 7.2 mailchimp.module \mailchimp_get_memberinfo()
  4. 7.3 mailchimp.module \mailchimp_get_memberinfo()
  5. 7.4 mailchimp.module \mailchimp_get_memberinfo()

Get the Mailchimp member info for a given email address and list.

Results are cached in the cache_mailchimp bin which is cleared by the Mailchimp web hooks system when needed.

Parameters

string $list_id: The Mailchimp list ID to get member info for.

string $email: The Mailchimp user email address to load member info for.

bool $reset: Set to TRUE if member info should not be loaded from cache.

Return value

object Member info object, empty if there is no valid info.

11 calls to mailchimp_get_memberinfo()
MailchimpListsSelectWidget::GetMemberStatus in modules/mailchimp_lists/src/Plugin/Field/FieldWidget/MailchimpListsSelectWidget.php
MailchimpListsSelectWidget::memberIsUnsubscribed in modules/mailchimp_lists/src/Plugin/Field/FieldWidget/MailchimpListsSelectWidget.php
MailchimpListsSubscribeDefaultFormatter::viewElements in modules/mailchimp_lists/src/Plugin/Field/FieldFormatter/MailchimpListsSubscribeDefaultFormatter.php
Builds a renderable array for a field value.
MailchimpListsSubscriptionTest::testGetMemberInfo in modules/mailchimp_lists/tests/src/Functional/MailchimpListsSubscriptionTest.php
Tests retrieval of member info for a list and email address.
MailchimpSignupPageForm::submitForm in modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php
Form submission handler.

... See full list

File

./mailchimp.module, line 281
Mailchimp module.

Code

function mailchimp_get_memberinfo($list_id, $email, $reset = FALSE) {
  $cache = \Drupal::cache('mailchimp');
  if (!$reset) {
    $cached_data = $cache
      ->get($list_id . '-' . $email);
    if ($cached_data) {
      return $cached_data->data;
    }
  }

  // Query lists from the MCAPI and store in cache:
  $memberinfo = new stdClass();

  /* @var \Mailchimp\MailchimpLists $mc_lists */
  $mc_lists = mailchimp_get_api_object('MailchimpLists');
  try {
    if (!$mc_lists) {
      throw new Exception('Cannot get member info without Mailchimp API. Check API key has been entered.');
    }
    $result = $mc_lists
      ->getMemberInfo($list_id, $email);
    if (!empty($result->id)) {
      $memberinfo = $result;
      $cache
        ->set($list_id . '-' . $email, $memberinfo);
    }
  } catch (\Exception $e) {

    // A 404 exception code means mailchimp does not have subscription
    // information for given email address. This is not an error and we can
    // cache this information.
    if ($e
      ->getCode() == 404) {
      $cache
        ->set($list_id . '-' . $email, $memberinfo);
    }
    else {
      \Drupal::logger('mailchimp')
        ->error('An error occurred requesting memberinfo for {email} in list {list}. "{message}"', [
        'email' => $email,
        'list' => $list_id,
        'message' => $e
          ->getMessage(),
      ]);
    }
  }
  return $memberinfo;
}