Example #1
0
/**
 * Remove a subscription for an email address to a container
 *
 * @param string     $email  The email address to remove from the subscriptions
 * @param ElggEntity $entity The container entity to unsubscribe from
 *
 * @return boolean true on success else false
 */
function newsletter_unsubscribe_email($email, ElggEntity $entity)
{
    $result = false;
    if (!empty($email) && !empty($entity)) {
        if (newsletter_is_email_address($email) && (elgg_instanceof($entity, "site") || elgg_instanceof($entity, "group"))) {
            // check if not existing user
            $users = get_user_by_email($email);
            if (!empty($users)) {
                // existing user
                $result = newsletter_unsubscribe_user($users[0], $entity);
            } else {
                // email address
                $subscription = newsletter_get_subscription($email);
                if (empty($subscription)) {
                    $subscription = new NewsletterSubscription();
                    $subscription->title = $email;
                    if (!$subscription->save()) {
                        return false;
                    }
                }
                // remove existing subscription (if any)
                $subscription->removeRelationship($entity->getGUID(), NewsletterSubscription::SUBSCRIPTION);
                // add to blocked list
                $result = $subscription->addRelationship($entity->getGUID(), NewsletterSubscription::BLACKLIST);
            }
        }
    }
    return $result;
}