You are here

function mailchimp_update_local_cache in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_update_local_cache()
  2. 7.3 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

Parameters

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.

Return value

bool

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 701
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, (object) array(
      'merge_fields' => new stdClass(),
    ), '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';

    // Reset interests.
    $cache->interests = new stdClass();
  }

  // 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.
    if (!isset($cache->merge_fields)) {
      $cache->merge_fields = new stdClass();
    }
    foreach ($args['merge_vars'] as $key => $value) {
      $cache->merge_fields->{$key} = $value;
    }

    // Update cached interests.
    $cache->interests = new stdClass();
    foreach ($args['interests'] as $interest_group => $interests) {
      if (is_array($interests)) {
        foreach ($interests as $interest_id => $value) {
          if ($value !== 0) {
            $cache->interests->{$interest_id} = TRUE;
          }
        }
      }
      elseif ($interests) {
        $cache->interests->{$interest_group} = TRUE;
      }
    }
  }

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