You are here

function mailchimp_campaign_get_campaigns in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_get_campaigns()
  2. 7.2 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_get_campaigns()
  3. 7.3 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_get_campaigns()
  4. 7.4 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_get_campaigns()
  5. 2.x modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_get_campaigns()

Get Mailchimp campaigns.

Parameters

array $mc_campaign_ids: Array of Mailchimp campaign IDs.

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

Return value

array Associative array of Mailchimp campaigns indexed by campaign ID.

2 calls to mailchimp_campaign_get_campaigns()
mailchimp_campaign_entity_storage_load in modules/mailchimp_campaign/mailchimp_campaign.module
Implements hook_entity_storage_load().
mailchimp_campaign_save_campaign in modules/mailchimp_campaign/mailchimp_campaign.module
Save a campaign in Mailchimp and as a Drupal entity.

File

modules/mailchimp_campaign/mailchimp_campaign.module, line 378
Module file for mailchimp_campaign.

Code

function mailchimp_campaign_get_campaigns(array $mc_campaign_ids, $reset = FALSE) {
  $cache = \Drupal::cache('mailchimp');
  $cached_campaigns = $cache
    ->get('campaigns');
  $campaigns = [];
  foreach ($mc_campaign_ids as $id) {
    if (!isset($cached_campaigns->data[$id]) || $cached_campaigns->data[$id]->status == MAILCHIMP_STATUS_SENDING || $reset) {
      $to_refresh[] = $id;
    }
    else {
      $campaigns[$id] = $cached_campaigns->data[$id];
    }
  }
  if (!empty($to_refresh)) {

    /* @var \Mailchimp\MailchimpCampaigns $mcapi */
    $mcapi = mailchimp_get_api_object('MailchimpCampaigns');
    try {
      if (!$mcapi) {
        throw new Exception('Cannot get list without Mailchimp API. Check API key has been entered.');
      }
    } catch (\Exception $e) {
      \Drupal::messenger()
        ->addError($e
        ->getMessage());
      \Drupal::logger('mailchimp_campaign')
        ->error('An error occurred while getting campaigns: {message}', [
        'message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    foreach ($to_refresh as $campaign_id) {
      try {
        $response = $mcapi
          ->getCampaign($campaign_id);
        if (!empty($response->id)) {
          $campaigns[$response->id] = $response;
        }
      } catch (Exception $e) {
        \Drupal::messenger()
          ->addError(t('%message (Campaign %campaign_id removed from Mailchimp?)', [
          '%message' => $e
            ->getMessage(),
          '%campaign_id' => $campaign_id,
        ]));
        \Drupal::logger('mailchimp_campaign')
          ->error('An error occurred while getting campaigns: {message}', [
          'message' => $e
            ->getMessage(),
        ]);
      }
    }
    $cache
      ->set('campaigns', $campaigns);
  }
  return $campaigns;
}