You are here

function mailchimp_cron in Mailchimp 5.2

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_cron()
  2. 5 mailchimp.module \mailchimp_cron()
  3. 6.2 mailchimp.module \mailchimp_cron()
  4. 6 mailchimp.module \mailchimp_cron()
  5. 7.5 mailchimp.module \mailchimp_cron()
  6. 7 mailchimp.module \mailchimp_cron()
  7. 7.3 mailchimp.module \mailchimp_cron()
  8. 7.4 mailchimp.module \mailchimp_cron()
  9. 2.x mailchimp.module \mailchimp_cron()

Implementation of hook_cron. Resubscribes all users to the required lists if the user has chosen to do so.

2 string references to 'mailchimp_cron'
mailchimp_admin_settings in ./mailchimp.module
mailchimp_user in ./mailchimp.module

File

./mailchimp.module, line 158

Code

function mailchimp_cron() {
  if (variable_get('mailchimp_cron', FALSE) && ($q = _mailchimp_get_api_object())) {
    $result = db_query('SELECT uid FROM {users} where status = %d', 1);
    if ($result) {
      $lists = _mailchimp_get_required_lists($q
        ->lists());
      foreach ($lists as $key => $list) {
        $lists[$key]['batch'] = array();
        $lists[$key]['mergevars'] = $q
          ->listMergeVars($list['id']);
      }
      while ($row = db_fetch_object($result)) {
        $account = user_load(array(
          'uid' => $row->uid,
        ));
        foreach ((array) $lists as $key => $list) {
          foreach ((array) $account->roles as $rid => $info) {
            if (variable_get('mailchimp_list_' . $list['id'] . '_role_' . $rid, 0)) {
              $lists[$key]['batch'][] = _mailchimp_load_user_list_mergevars($row->uid, $list['id'], $lists[$key]['mergevars']);
              break;
            }
          }
        }
      }
      $count = 0;
      foreach ($lists as $key => $list) {
        if (count($lists[$key]['batch'])) {
          $ret = $q
            ->listBatchSubscribe($list['id'], $lists[$key]['batch'], FALSE, TRUE);
          if ($ret['error_count'] > 0) {
            foreach ((array) $ret['errors'] as $error) {
              watchdog('mailchimp', $error['email_address'] . ': ' . $error['message'], WATCHDOG_ERROR);
            }
          }
        }
        $count += $ret['success_count'];
      }
      watchdog('mailchimp', t('Updated !count records in MailChimp', array(
        '!count' => $count,
      )), WATCHDOG_NOTICE);
    }
  }
}