You are here

function mailchimp_get_api_object in Mailchimp 7.4

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

Get an instance of the Mailchimp library.

Parameters

string $classname: The Mailchimp library class to instantiate.

string $api_key: The Mailchimp api key to use if not the default.

Return value

Mailchimp Instance of the Mailchimp library class. Can be overridden by $classname.

38 calls to mailchimp_get_api_object()
mailchimp_activity_get_member_activity in modules/mailchimp_activity/mailchimp_activity.module
Get the Mailchimp member activity for a given email address and list.
mailchimp_activity_page in modules/mailchimp_activity/mailchimp_activity.module
Content for the mailchimp log tab on entity instance pages.
mailchimp_admin_settings in includes/mailchimp.admin.inc
Return the Mailchimp global settings form.
mailchimp_apikey_ready_access in ./mailchimp.module
Access callback for mailchimp submodule menu items.
mailchimp_automations_get_automation in modules/mailchimp_automations/mailchimp_automations.module
Wrapper for MailchimpAutomations->getWorkflow().

... See full list

File

./mailchimp.module, line 121
Mailchimp module.

Code

function mailchimp_get_api_object($classname = 'Mailchimp', $api_key = NULL) {

  // Set correct library class namespace depending on test mode.
  if (variable_get('mailchimp_test_mode', FALSE)) {
    $classname = '\\Mailchimp\\Tests\\' . $classname;
  }
  else {
    $classname = '\\Mailchimp\\' . $classname;
  }
  $mailchimp =& drupal_static(__FUNCTION__);
  if (!$api_key && isset($mailchimp) && $mailchimp instanceof $classname) {
    return $mailchimp;
  }
  if (module_exists('libraries')) {
    $library = libraries_load('mailchimp');
  }
  else {
    $library = FALSE;
  }
  if (!$library['installed'] && !class_exists('Mailchimp\\Mailchimp')) {
    $msg = t('Failed to load the Mailchimp PHP library. Please refer to the installation requirements.');
    watchdog('mailchimp', $msg, array(), WATCHDOG_ERROR);
    drupal_set_message($msg, 'error', FALSE);
    return NULL;
  }
  if (!class_exists('\\GuzzleHttp\\Client')) {
    $msg = t('The Mailchimp PHP library is missing the required GuzzleHttp library. Please check the installation notes in README.txt.');
    watchdog('mailchimp', $msg, array(), WATCHDOG_ERROR);
    drupal_set_message($msg, 'error', FALSE);
    return NULL;
  }
  $context = array(
    'api_class' => $classname,
  );
  if (!$api_key) {
    $api_key = $default_api_key = variable_get('mailchimp_api_key', '');

    // Allow modules to alter the default.
    drupal_alter('mailchimp_api_key', $api_key, $context);

    // Check to see if the key was altered.
    if ($api_key !== $default_api_key) {

      // Invalidate all caches because the key was altered.
      mailchimp_cache_clear_all();
    }
  }
  if (!strlen($api_key)) {
    watchdog('mailchimp', 'Mailchimp Error: API Key cannot be blank.', array(), WATCHDOG_ERROR);
    return NULL;
  }

  // Set the timeout to something that won't take down the Drupal site:
  $http_options = [
    'timeout' => 60,
  ];
  $proxy_server = variable_get('proxy_server', '');
  if ($proxy_server) {
    $proxy_url = sprintf('tcp://%s:%d', $proxy_server, variable_get('proxy_port', 8080));
    $http_options['proxy'] = [
      'http' => $proxy_url,
      'https' => $proxy_url,
    ];
    if ($proxy_username = variable_get('proxy_username', '')) {
      $proxy_password = variable_get('proxy_password', '');
      $http_options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
    }
  }
  $http_options['headers']['User-Agent'] = _mailchimp_get_user_agent();
  $mailchimp = new $classname($api_key, 'apikey', $http_options);
  return $mailchimp;
}