You are here

function mailchimp_get_campaign_data in Mailchimp 7.5

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

Wrapper around MailchimpCampaigns->getCampaign() to return data for a given campaign.

Data is stored in the MailChimp cache.

Parameters

string $campaign_id: The ID of the campaign to get data for.

bool $reset: Set to TRUE if campaign data should not be loaded from cache.

Return value

mixed Array of campaign data or FALSE if not found.

1 call to mailchimp_get_campaign_data()
MailchimpCampaignsTestCase::testGetCampaign in modules/mailchimp_campaign/tests/mailchimp_campaigns.test
Tests retrieval of a specific campaign.

File

./mailchimp.module, line 1390
Mailchimp module.

Code

function mailchimp_get_campaign_data($campaign_id, $reset = FALSE) {
  $cache = $reset ? NULL : cache_get('campaign_' . $campaign_id, 'cache_mailchimp');
  $campaign_data = FALSE;

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

    /* @var \Mailchimp\MailchimpCampaigns $mc_campaigns */
    $mc_campaigns = mailchimp_get_api_object('MailchimpCampaigns');
    if (!$mc_campaigns) {
      throw new MailchimpException('Cannot get list without MailChimp API. Check API key has been entered.');
    }
    $response = $mc_campaigns
      ->getCampaign($campaign_id);
    if (!empty($response->id)) {
      $campaign_data = $response;
      cache_set('campaign_' . $campaign_id, $response, 'cache_mailchimp', CACHE_TEMPORARY);
    }
    else {
      $campaign_data = FALSE;
    }
  } catch (Exception $e) {
    watchdog('mailchimp', 'An error occurred retrieving campaign data for @campaign. "%message"', array(
      '@campaign' => $campaign_id,
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
  return $campaign_data;
}