You are here

function mailchimp_get_segments in Mailchimp 7.3

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_get_segments()
  2. 7.4 mailchimp.module \mailchimp_get_segments()

Wrapper around MCAPI->lists->segments.

Parameters

string $list_id: A MailChimp list id.

bool $reset: Set to TRUE if list segments should not be loaded from cache.

Return value

array Array of segments details.

3 calls to mailchimp_get_segments()
MailchimpListsTestCase::testGetListSegments in modules/mailchimp_lists/tests/mailchimp_lists.test
Tests retrieval of list segments for a list.
mailchimp_lists_add_to_segment_action_form in modules/mailchimp_lists/mailchimp_lists.module
An action configuration form for performing Add To Segment actions.
mailchimp_segment_create in ./mailchimp.module
Wrapper around MCAPI->lists->segmentAdd.

File

./mailchimp.module, line 747
Mailchimp module.

Code

function mailchimp_get_segments($list_id, $reset = NULL) {
  $cache = $reset ? NULL : cache_get($list_id . '-segments', 'cache_mailchimp');

  // Return cached lists:
  if ($cache) {
    return $cache->data;
  }

  // Query segments from the MCAPI and store in cache:
  $segments = array();
  try {
    $mcapi = mailchimp_get_api_object();
    if (!$mcapi) {
      throw new MailchimpException('Cannot get list segments without MailChimp API. Check API key has been entered.');
    }
    $segments = $mcapi->lists
      ->segments($list_id);
    cache_set($list_id . '-segments', $segments, 'cache_mailchimp', CACHE_TEMPORARY);
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred requesting list segment information from MailChimp. "%message"', array(
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
  return $segments;
}