You are here

function simplenews_subscribe in Simplenews 7.2

Subscribe a user to a newsletter or send a confirmation mail.

The $confirm parameter determines the action: FALSE = The user is subscribed TRUE = User receives an email to verify the address and complete the subscription A new subscription account is created when the user is subscribed to the first newsletter

Parameters

string $mail: The email address to subscribe to the newsletter.

integer $newsletter_id: The newsletter ID.

boolean $confirm: TRUE = send confirmation mail; FALSE = subscribe immediate to the newsletter

string $preferred_language: The language code (i.e. 'en', 'nl') of the user preferred language. Use '' for the site default language. Use NULL for the language of the current page.

string $source: Indication for source of subscription. Simplenews uses these sources: website: via any website form (with or without confirmation email) mass subscribe: mass admin UI mass unsubscribe: mass admin UI action: Drupal actions

Related topics

14 calls to simplenews_subscribe()
SimpleNewsAdministrationTestCase::testSubscriptionManagement in tests/simplenews.test
Test newsletter subscription management.
SimpleNewsI18nTestCase::testNewsletterIssueTranslation in tests/simplenews.test
SimpleNewsUpgradePathTestCase::assertUpgradePath in tests/simplenews.test
simplenews_block_form_submit in includes/simplenews.subscription.inc
simplenews_confirm_add_form_submit in includes/simplenews.subscription.inc

... See full list

File

./simplenews.module, line 1482
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_subscribe($mail, $newsletter_id, $confirm = TRUE, $source = 'unknown', $preferred_language = NULL) {
  global $language;

  // Get current subscriptions if any.
  $subscriber = simplenews_subscriber_load_by_mail($mail);

  // If user is not subscribed to ANY newsletter, create a subscription account
  if (!$subscriber) {

    // To subscribe a user:
    //   - Fetch the users uid.
    //   - Determine the user preferred language.
    //   - Add the user to the database.
    //   - Get the full subscription object based on the mail address.
    // Note that step 3 gets subscription data based on mail address because the uid can be 0 (for anonymous users)
    $account = simplenews_load_user_by_mail($mail);

    // If the site is multilingual:
    //  - Anonymous users are subscribed with their preferred language
    //    equal to the language of the current page.
    //  - Registered users will be subscribed with their default language as
    //    set in their account settings.
    // By default the preferred language is not set.
    if (variable_get('language_count', 1) > 1) {
      if ($account->uid) {
        $preferred_language = $account->language;
      }
      else {
        $preferred_language = isset($preferred_language) ? $preferred_language : $language->language;
      }
    }
    else {
      $preferred_language = '';
    }
    $subscriber = entity_create('simplenews_subscriber', array());
    $subscriber->mail = $mail;
    $subscriber->uid = $account->uid;
    $subscriber->language = $preferred_language;
    $subscriber->activated = 1;
    simplenews_subscriber_save($subscriber);
  }
  if ($confirm) {

    // Create an unconfirmed subscription object if it doesn't exist yet.
    if (!isset($subscriber->newsletter_ids[$newsletter_id])) {
      $subscription = new stdClass();
      $subscription->snid = $subscriber->snid;
      $subscription->newsletter_id = $newsletter_id;
      $subscription->status = SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED;
      $subscription->timestamp = REQUEST_TIME;
      $subscription->source = $source;
      simplenews_subscription_save($subscription);
      $subscriber->newsletter_subscription[$newsletter_id] = $subscription;
    }
    simplenews_confirmation_send('subscribe', $subscriber, simplenews_newsletter_load($newsletter_id));
  }
  elseif (!isset($subscriber->newsletter_ids[$newsletter_id])) {

    // Subscribe the user if not already subscribed.
    // @todo rewrite if subscription object is loaded in $subscriber->newsletter_ids[$newsletter_id]
    $subscription = new stdClass();
    $subscription->snid = $subscriber->snid;
    $subscription->newsletter_id = $newsletter_id;
    $subscription->status = SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED;
    $subscription->timestamp = REQUEST_TIME;
    $subscription->source = $source;
    simplenews_subscription_save($subscription);
    $subscriber->newsletter_subscription[$newsletter_id] = $subscription;
    $subscriber->newsletter_ids[$newsletter_id] = $newsletter_id;
    module_invoke_all('simplenews_subscribe', $subscriber, $subscription);
  }
  return TRUE;
}