You are here

function mailchimp_get_interest_categories in Mailchimp 7.5

Gets MailChimp interests categories.

Parameters

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

bool $exclude_hidden: TRUE to exclude interest groups from the list.

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

Return value

array A MailChimp array of Interests Categories.

2 calls to mailchimp_get_interest_categories()
mailchimp_get_list in ./mailchimp.module
Gets a single MailChimp list/audience by ID.
mailchimp_get_lists in ./mailchimp.module
Gets MailChimp lists/audiences. Can be filtered by an array of list IDs.

File

./mailchimp.module, line 377
Mailchimp module.

Code

function mailchimp_get_interest_categories($list_id, $exclude_hidden = TRUE, $reset = FALSE) {
  $cache = $reset ? NULL : cache_get('interests-' . $list_id, 'cache_mailchimp');

  // Return cached interest categories:
  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.');
      }

      // Add interest categories to the list.
      $interests_groups = $mc_lists
        ->getInterestCategories($list_id, array(
        'count' => 500,
      ));
      if ($interests_groups->total_items > 0) {
        foreach ($interests_groups->categories as $interest_category) {
          if (isset($interest_category->type) && $interest_category->type == 'hidden') {
            continue;
          }
          $interest_data = mailchimp_get_interests($list_id, $interest_category->id, $reset);
          if (!empty($interest_data)) {
            $interest_category->interests = $interest_data;
          }
          $interests[] = $interest_category;
        }
      }
      cache_set('interests-' . $list_id, $interests, 'cache_mailchimp', CACHE_PERMANENT);
    } catch (Exception $e) {
      watchdog('mailchimp', 'An error occurred requesting interest information from MailChimp. "%message"', array(
        '%message' => $e
          ->getMessage(),
      ), WATCHDOG_ERROR);
    }
  }

  // Filter hidden interests. We loop here so we pull all interests and save to cache.
  if ($exclude_hidden) {
    $filtered_interests = array();
    foreach ($interests as $interest) {
      if (isset($interest->type) && $interest->type != 'hidden') {
        $filtered_interests[] = $interest;
      }
    }
    return $filtered_interests;
  }
  else {
    return $interests;
  }
}