You are here

function mailchimp_update_local_cache in Mailchimp 7.3

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_update_local_cache()
  2. 7.4 mailchimp.module \mailchimp_update_local_cache()

Updates the local cache for a user as though a queued request had been processed.

If we don't do this, then users can make changes, but not have them shown on the site until cron runs, which is intensely confusing. See https://www.drupal.org/node/2503597

@string $function The name of the function that the queue runner will call when the update is processed. @array $args The list of args that will be passed to the queue runner.

1 call to mailchimp_update_local_cache()
mailchimp_addto_queue in ./mailchimp.module
Add a MailChimp subscription task to the queue.

File

./mailchimp.module, line 463
Mailchimp module.

Code

function mailchimp_update_local_cache($function, $args) {
  $list_id = isset($args['list_id']) ? $args['list_id'] : NULL;
  $email = isset($args['email']) ? $args['email'] : NULL;
  if (empty($list_id) || empty($email)) {
    return FALSE;
  }
  $cache = mailchimp_get_memberinfo($list_id, $email);
  if (empty($cache)) {

    // Create a new entry.
    cache_set($list_id . '-' . $email, array(
      'merges' => array(),
    ), 'cache_mailchimp', CACHE_TEMPORARY);
    $cache = cache_get($list_id . '-' . $email, 'cache_mailchimp');
    $cache = $cache->data;
  }

  // Handle unsubscribes.
  if ($function == 'mailchimp_unsubscribe_process') {
    $cache['status'] = 'unsubscribed';
    $cache['merges']['GROUPINGS'] = array();
  }

  // Handle subscribes.
  if ($function == 'mailchimp_subscribe_process') {
    $cache['status'] = 'subscribed';
  }

  // Handle member updates.
  if ($function == 'mailchimp_update_member_process' || $function == 'mailchimp_subscribe_process') {

    // Update cached merge vars.
    foreach ($args['merge_vars'] as $key => $value) {

      // Groups are handled separately since the format needs re-written.
      if ($key == 'GROUPINGS') {
        continue;
      }
      $cache['merges'][$key] = $value;
    }

    // Update cached groupings.
    mailchimp_update_local_cached_groupings($args, $cache);
  }

  // Store the data back in the local cache.
  cache_set($list_id . '-' . $email, $cache, 'cache_mailchimp', CACHE_TEMPORARY);
}