You are here

function simplenews_unsubscribe_user in Simplenews 5

Same name and namespace in other branches
  1. 6.2 simplenews.module \simplenews_unsubscribe_user()
  2. 6 simplenews.module \simplenews_unsubscribe_user()
  3. 7 simplenews.module \simplenews_unsubscribe_user()

API function; unsubscribes a user from a newsletter.

Parameters

$mail: The e-mail address to unsubscribe from the newsletter.

$tid: The term ID of the newsletter.

$confirm: Whether we should send a confirmation e-mail and hold off removing this user from the newsletter until he clicks the confirm link in the e-mail.

4 calls to simplenews_unsubscribe_user()
simplenews_block_form_submit in ./simplenews.module
Forms API callback; handles block form (un)subscribe submissions.
simplenews_confirm_removal_form_submit in ./simplenews.module
Forms API callback; handles form submission for a user confirming unsubscribe request.
simplenews_subscription_manager_form_submit in ./simplenews.module
Forms API callback; submit handler for subscription form.
simplenews_user in ./simplenews.module
Implementation of hook_user().

File

./simplenews.module, line 850

Code

function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE) {

  //Prevent mismatches from accidental capitals in mail address
  $mail = strtolower($mail);
  $subscription = simplenews_get_user_subscription($mail);
  if (!($newsletter = taxonomy_get_term($tid))) {
    watchdog('newsletter', t('Could not load newsletter term ID %id', array(
      '%id' => $tid,
    )));
    return FALSE;
  }
  if ($confirm) {

    // Send confirmation e-mail to user to complete unsubscription or to tell
    // them that he or she is not subscribed.
    simplenews_mail_confirm($mail, $newsletter, $subscription ? $subscription->snid : NULL, 'unsubscribe');
  }
  elseif (isset($subscription->tids[$tid])) {

    // If we're not confirming first, just remove the user from the newsletter.
    db_query('DELETE FROM {simplenews_snid_tid} WHERE snid = %d AND tid = %d', $subscription->snid, $tid);

    // Clean up simplenews_subscriptions if no more newsletter subscriptions.
    if (!db_num_rows(db_query("SELECT tid FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
      db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $subscription->snid);
    }
  }
  return TRUE;
}