You are here

function simplenews_confirm_subscription in Simplenews 7.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_confirm_subscription()
  2. 6.2 includes/simplenews.subscription.inc \simplenews_confirm_subscription()
  3. 6 simplenews.subscription.inc \simplenews_confirm_subscription()
  4. 7 includes/simplenews.subscription.inc \simplenews_confirm_subscription()

Menu callback: confirm the user's (un)subscription request

This function is called by clicking the confirm link in the confirmation email or the unsubscribe link in the footer of the newsletter. It handles both subscription addition and subscription removal.

Parameters

$action: Either add or remove.

$snid: The subscriber id.

$newsletter_id: The newsletter id.

$timestamp: The timestamp of the request.

$hash: The confirmation hash.

See also

simplenews_confirm_add_form()

simplenews_confirm_removal_form()

1 string reference to 'simplenews_confirm_subscription'
simplenews_menu in ./simplenews.module
Implements hook_menu().

File

includes/simplenews.subscription.inc, line 491
(Un)subscription and (un)subscription confirmation

Code

function simplenews_confirm_subscription($action, $snid, $newsletter_id, $timestamp, $hash) {
  $arguments = array_slice(func_get_args(), 5);

  // Prevent search engines from indexing this page.
  $noindex = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'robots',
      'content' => 'noindex',
    ),
  );
  drupal_add_html_head($noindex, 'simplenews-noindex');
  $subscriber = simplenews_subscriber_load($snid);
  if ($subscriber && $hash == simplenews_generate_hash($subscriber->mail, $action, $timestamp)) {
    $newsletter = simplenews_newsletter_load($newsletter_id);

    // If the hash is valid but timestamp is too old, display form to request
    // a new hash.
    if ($timestamp < REQUEST_TIME - variable_get('simplenews_hash_expiration', 86400)) {
      $context = array(
        'simplenews_subscriber' => $subscriber,
        'newsletter' => $newsletter,
      );
      $token = $action == 'add' ? 'subscribe' : 'unsubscribe';
      return drupal_get_form('simplenews_request_hash', $token, $context);
    }

    // When called with additional arguments the user will be directed to the
    // (un)subscribe confirmation page. The additional arguments will be passed
    // on to the confirmation page.
    if (empty($arguments)) {
      if ($action == 'remove') {
        return drupal_get_form('simplenews_confirm_removal_form', $subscriber->mail, $newsletter);
      }
      elseif ($action == 'add') {
        return drupal_get_form('simplenews_confirm_add_form', $subscriber->mail, $newsletter);
      }
    }
    else {
      if ($action == 'remove') {
        simplenews_unsubscribe($subscriber->mail, $newsletter_id, FALSE, 'website');
        if ($path = variable_get('simplenews_confirm_unsubscribe_page', '')) {
          $path = $path . '/' . implode('/', $arguments);
          drupal_goto($path);
        }
        drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array(
          '%user' => $subscriber->mail,
          '%newsletter' => $newsletter->name,
        )));
        drupal_goto(variable_get('site_frontpage', 'node'));
      }
      elseif ($action == 'add') {
        simplenews_subscribe($subscriber->mail, $newsletter_id, FALSE, 'website');
        if ($path = variable_get('simplenews_confirm_subscribe_page', '')) {
          $path = $path . '/' . implode('/', $arguments);
          drupal_goto($path);
        }
        drupal_set_message(t('%user was added to the %newsletter mailing list.', array(
          '%user' => $subscriber->mail,
          '%newsletter' => $newsletter->name,
        )));
        drupal_goto(variable_get('site_frontpage', 'node'));
      }
    }
  }
  return MENU_NOT_FOUND;
}