function simplenews_subscribe_user in Simplenews 7
Same name and namespace in other branches
- 5 simplenews.module \simplenews_subscribe_user()
- 6.2 simplenews.module \simplenews_subscribe_user()
- 6 simplenews.module \simplenews_subscribe_user()
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 $tid: The term ID of the newsletter.
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
13 calls to simplenews_subscribe_user()
- SimpleNewsAdministrationTestCase::testSubscriptionManagement in tests/
simplenews.test - Test newsletter subscription management.
- SimpleNewsI18nTestCase::testContentTranslation 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
File
- ./
simplenews.module, line 1281 - Simplenews node handling, sent email, newsletter block and general hooks
Code
function simplenews_subscribe_user($mail, $tid, $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 = new stdClass();
$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->tids[$tid])) {
$subscription = new stdClass();
$subscription->snid = $subscriber->snid;
$subscription->tid = $tid;
$subscription->status = SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED;
$subscription->timestamp = REQUEST_TIME;
$subscription->source = $source;
simplenews_subscription_save($subscription);
$subscriber->newsletter_subscription[$tid] = $subscription;
}
simplenews_confirmation_send('subscribe', $subscriber, simplenews_category_load($tid));
}
elseif (!isset($subscriber->tids[$tid])) {
// Subscribe the user if not already subscribed.
// @todo rewrite if subscription object is loaded in $subscriber->tids[$tid]
$subscription = new stdClass();
$subscription->snid = $subscriber->snid;
$subscription->tid = $tid;
$subscription->status = SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED;
$subscription->timestamp = REQUEST_TIME;
$subscription->source = $source;
simplenews_subscription_save($subscription);
$subscriber->newsletter_subscription[$tid] = $subscription;
$subscriber->tids[$tid] = $tid;
module_invoke_all('simplenews_subscribe_user', $subscriber, $subscription);
}
return TRUE;
}