You are here

function mailchimp_get_segments in Mailchimp 7.4

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

Wrapper around MailchimpLists->getSegments().

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 MailchimpLists->addSegment().

File

./mailchimp.module, line 1030
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 {

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    if (!$mc_lists) {
      throw new MailchimpException('Cannot get list segments without Mailchimp API. Check API key has been entered.');
    }
    $result = $mc_lists
      ->getSegments($list_id, array(
      'count' => 500,
    ));
    $segments = $result->total_items > 0 ? $result->segments : array();
    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;
}