You are here

function mailchimp_lists_update_7203 in Mailchimp 7.2

Update list webhook endpoint URLs to be more secure.

File

modules/mailchimp_lists/mailchimp_lists.install, line 277
Install, update and uninstall functions for the mailchimp_lists module.

Code

function mailchimp_lists_update_7203() {
  $efq = new EntityFieldQuery();
  $results = $efq
    ->entityCondition('entity_type', 'mailchimp_list')
    ->propertyCondition('list_type', array(
    MAILCHIMP_LISTTYPE_OPTIONAL,
    MAILCHIMP_LISTTYPE_REQUIRED,
  ), 'IN')
    ->execute();
  if (empty($results)) {
    return t('There are no lists to update.');
  }
  $lists = mailchimp_lists_load_multiple(array_keys($results['mailchimp_list']));
  $mcapi = mailchimp_get_api_object();

  // This is the old format of the webhook URL, used below.
  $old_url = $GLOBALS['base_url'] . '/mailchimp/webhook/' . md5($GLOBALS['base_url']);
  foreach ($lists as $list) {
    if (isset($list->settings['webhooks']) && $list->settings['webhooks']) {

      // Delete the old webhook.
      $ret = $mcapi
        ->listWebhookDel($list->mc_list_id, $old_url);
      if (!$ret) {
        return t('Failed to delete old web hook for @list', array(
          '@list' => $list->label,
        ));
      }

      // Now add the new one.
      $ret = $mcapi
        ->listWebhookAdd($list->mc_list_id, mailchimp_webhook_url(), array(
        'unsubscribe',
        'profile',
        'cleaned',
        'upemail',
      ));
      if (!$ret) {
        return t('Failed to create new web hook for @list', array(
          '@list' => $list->label,
        ));
      }
    }
  }
  return t('MailChimp list webhooks have been updated with the new, more secure endpoint URL.');
}