You are here

function mailchimp_cron in Mailchimp 7

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_cron()
  2. 5.2 mailchimp.module \mailchimp_cron()
  3. 5 mailchimp.module \mailchimp_cron()
  4. 6.2 mailchimp.module \mailchimp_cron()
  5. 6 mailchimp.module \mailchimp_cron()
  6. 7.5 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()

Implements hook_cron().

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

1 call to mailchimp_cron()
_mailchimp_cron_batch in ./mailchimp.drush.inc
4 string references to 'mailchimp_cron'
mailchimp_admin_settings in ./mailchimp.admin.inc
implementation of hook_admin_settings
mailchimp_uninstall in ./mailchimp.install
Implements hook_uninstall().
mailchimp_user_insert in ./mailchimp.module
Implements hook_user_insert().
mailchimp_user_update in ./mailchimp.module
Implements hook_user_update().

File

./mailchimp.module, line 329
Mailchimp module.

Code

function mailchimp_cron() {
  if (variable_get('mailchimp_cron', FALSE) && ($q = _mailchimp_get_api_object())) {
    $sql = "SELECT uid FROM {mailchimp_user} WHERE status = :status";
    $result = db_query_range($sql, 0, variable_get('mailchimp_batch_limit', 100), array(
      ':status' => MAILCHIMP_USERSTATUS_PENDING,
    ));
    if ($result) {
      $lists = _mailchimp_get_required_lists();
      $batches = array();
      foreach ($result as $row) {
        if ($account = user_load($row->uid)) {
          db_query('UPDATE {mailchimp_user} SET status = :status WHERE uid = :uid', array(
            ':status' => MAILCHIMP_USERSTATUS_CURRENT,
            ':uid' => $account->uid,
          ));

          // We don't update people if their status = 0 (but perhaps we could unsubscribe them?)
          if ($account->status) {
            foreach ($lists as $key => $list) {
              foreach ($account->roles as $rid => $info) {
                if (isset($list->roles[$rid])) {
                  $batches[$list->id][] = _mailchimp_load_user_list_mergevars($account, $list->id);
                  break;
                }
              }
            }
          }
        }
        else {

          // user exists in mc_user table even though they don't have an account, remove
          db_query('DELETE FROM {mailchimp_user} WHERE uid = :uid', array(
            ':uid' => $account->uid,
          ));
        }
      }
      $count = 0;
      $ret = array();
      foreach ($batches as $listid => $batch) {
        if (count($batch)) {
          $ret = $q
            ->listBatchSubscribe($listid, $batch, FALSE, TRUE);
          if ($ret['error_count'] > 0) {
            foreach ((array) $ret['errors'] as $error) {
              watchdog('mailchimp', 'MCAPI Error: %errmsg', array(
                '!errmsg' => $error['message'],
              ), WATCHDOG_ERROR);
            }
          }
        }
        $count += $ret['success_count'];
      }
      watchdog('mailchimp', 'Updated !count records in MailChimp', array(
        '!count' => $count,
      ), WATCHDOG_NOTICE);
    }
  }
}