function mailchimp_lists_cron in Mailchimp 7.2
Implements hook_cron().
Process all records in the mailchimp cron queue.
1 call to mailchimp_lists_cron()
- _mailchimp_cron_batch in includes/
mailchimp.drush.inc
File
- modules/
mailchimp_lists/ mailchimp_lists.module, line 127 - Mailchimp lists module.
Code
function mailchimp_lists_cron() {
$queue = DrupalQueue::get(MAILCHIMP_QUEUE_CRON);
$queue
->createQueue();
$queue_count = $queue
->numberOfItems();
// Claim items off the queue and organize them into batches:
if ($queue_count > 0) {
$lists_by_mc_id = array();
$batches_add = array();
$batches_rem = array();
$count = 0;
$batch_limit = variable_get('mailchimp_batch_limit', 100);
$batch_size = $queue_count < $batch_limit ? $queue_count : $batch_limit;
while ($count < $batch_size) {
if ($item = $queue
->claimItem()) {
$list = mailchimp_lists_load($item->data['list_id']);
$mc_list_id = $list->mc_list_id;
// Store this list object so we can reference it later by its MC ID.
$lists_by_mc_id[$mc_list_id] = $list;
// If all we have is an anonymous email, load it. We check for a value
// first to be backwards-compatible with versions that didn't pass the
// raw email into the queue item.
if (isset($item->data['email'])) {
$email = $item->data['email'];
}
$mergevars = NULL;
// If there is a valid account, load data from it:
if (!empty($item->data['uid'])) {
$account = user_load($item->data['uid']);
$email = $account->mail;
$mergevars = mailchimp_lists_load_user_mergevars($account, $list);
}
switch ($item->data['op']) {
case 'add':
// @todo: test behavior of 'update' operations here
case 'update':
// If $mergevars are empty here, populate with the basic element:
if (empty($mergevars)) {
$mergevars = array(
'EMAIL' => $email,
);
}
if (!empty($item->data['groupings'])) {
$mergevars['GROUPINGS'] = $item->data['groupings'];
}
$batches_add[$mc_list_id][] = $mergevars;
break;
case 'remove':
$batches_rem[$mc_list_id][] = $email;
break;
}
$queue
->deleteItem($item);
}
$count++;
}
// Now we process our batches:
$mcapi = mailchimp_get_api_object();
// Remove if necessary:
$rem_count = 0;
$ret = array();
foreach ($batches_rem as $listid => $batch) {
if (count($batch)) {
// @todo: consider if the delete flag should be true or false
$ret = $mcapi
->listBatchUnsubscribe($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);
}
}
else {
$rem_count++;
}
}
}
watchdog('mailchimp', 'Removed !rem_count records in MailChimp', array(
'!rem_count' => $rem_count,
), WATCHDOG_NOTICE);
// Add if necessary:
$add_count = $update_count = 0;
foreach ($batches_add as $listid => $batch) {
if (count($batch)) {
$list_settings = $lists_by_mc_id[$listid]->settings;
$double_opt_in = $list_settings['doublein'];
$ret = $mcapi
->listBatchSubscribe($listid, $batch, $double_opt_in, TRUE);
if ($ret['error_count'] > 0) {
foreach ((array) $ret['errors'] as $error) {
watchdog('mailchimp', 'MCAPI Error: %errmsg', array(
'%errmsg' => $error['message'],
), WATCHDOG_ERROR);
}
}
}
$add_count += $ret['add_count'];
$update_count += $ret['update_count'];
}
watchdog('mailchimp', 'Added !add_count, updated !update_count records in MailChimp', array(
'!add_count' => $add_count,
'!update_count' => $update_count,
), WATCHDOG_NOTICE);
}
}