You are here

function mailchimp_get_interests in Mailchimp 7.5

Gets MailChimp interests.

Parameters

string $list_id: The unique ID of the list provided by MailChimp.

string $interest_category_id: The unique ID of the interest category provided by MailChimp.

bool $reset: TRUE to reset interests cache and load from MailChimp.

Return value

array A MailChimp array of Interests.

2 calls to mailchimp_get_interests()
mailchimp_get_interest_categories in ./mailchimp.module
Gets MailChimp interests categories.
mailchimp_interest_groups_form_elements in ./mailchimp.module
Helper function to generate form elements for a list's interest groups.

File

./mailchimp.module, line 451
Mailchimp module.

Code

function mailchimp_get_interests($list_id, $interest_category_id, $reset = FALSE) {
  $cid = 'interests-' . $list_id . '-' . $interest_category_id;
  $cache = $reset ? NULL : cache_get($cid, 'cache_mailchimp');

  // Return cached interests:
  if ($cache) {
    $interests = $cache->data;
  }
  else {
    $interests = [];
    try {

      /* @var \Mailchimp\MailchimpLists $mc_lists */
      $mc_lists = mailchimp_get_api_object('MailchimpLists');
      if (!$mc_lists) {
        throw new MailchimpException('Cannot get lists without MailChimp API. Check API key has been entered.');
      }
      $interest_data = $mc_lists
        ->getInterests($list_id, $interest_category_id, array(
        'count' => 500,
      ));
      if ($interest_data->total_items > 0) {
        $interests = $interest_data->interests;
      }
      cache_set($cid, $interests, 'cache_mailchimp', CACHE_PERMANENT);
    } catch (Exception $e) {
      watchdog('mailchimp', 'An error occurred requesting interest information from MailChimp. "%message"', [
        '%message' => $e
          ->getMessage(),
      ], WATCHDOG_ERROR);
    }
  }
  return $interests;
}