basic implementation of confirm/edit/unsubscribe pages

This commit is contained in:
Jonathan Labreuille
2016-02-26 15:25:53 +01:00
parent 3622bc9fcb
commit f1c396f0b0
3 changed files with 117 additions and 57 deletions

View File

@ -73,7 +73,10 @@ class Newsletter extends Model {
}
function withSendingQueue() {
$this->queue = $this->getQueue();
$queue = $this->getQueue();
if($queue !== false) {
$this->queue = $queue->asArray();
}
return $this;
}

View File

@ -72,15 +72,27 @@ class Subscriber extends Model {
function getConfirmationUrl() {
$post = get_post(Setting::getValue('signup_confirmation.page'));
return $this->getSubscriptionUrl($post, 'confirm');
}
function getEditSubscriptionUrl() {
$post = get_post(Setting::getValue('subscription.page'));
return $this->getSubscriptionUrl($post, 'edit');
}
function getUnsubscribeUrl() {
$post = get_post(Setting::getValue('subscription.page'));
return $this->getSubscriptionUrl($post, 'unsubscribe');
}
private function getSubscriptionUrl($post = null, $action = null) {
if($post === null || $action === null) return;
if($post === null) {
// create page
return '';
} else {
$url = get_permalink($post);
$params = array(
'mailpoet_action=confirm',
'mailpoet_action='.$action,
'mailpoet_token='.md5(AUTH_KEY.$this->email),
'mailpoet_email='.$this->email
);
@ -94,7 +106,6 @@ class Subscriber extends Model {
return $url;
}
}
function sendConfirmationEmail() {
if($this->status === self::STATUS_UNCONFIRMED) {

View File

@ -52,13 +52,7 @@ class Router {
}
function setPageTitle($title) {
$action = (isset($_GET['mailpoet_action']))
? $_GET['mailpoet_action']
: null;
switch($action) {
case 'confirm':
function getSubscriber() {
$token = (isset($_GET['mailpoet_token']))
? $_GET['mailpoet_token']
: null;
@ -66,18 +60,34 @@ class Router {
? $_GET['mailpoet_email']
: null;
if(empty($token) || empty($token)) {
$title = sprintf(
__("You've subscribed to: %s"),
'demo'
);
} else {
// check token validity
if(md5(AUTH_KEY.$email) !== $token) {
$title = __('Your confirmation link expired, please subscribe again.');
} else {
if(empty($token) || empty($email)) {
return 'demo';
}
if(md5(AUTH_KEY.$email) === $token) {
$subscriber = Subscriber::findOne($email);
if($subscriber !== false) {
return $subscriber;
}
}
return false;
}
function setPageTitle($title) {
$action = (isset($_GET['mailpoet_action']))
? $_GET['mailpoet_action']
: null;
// get subscriber
$subscriber = $this->getSubscriber();
switch($action) {
case 'confirm':
if($subscriber === 'demo') {
$title = sprintf(__("You've subscribed to: %s"), 'demo');
} else if($subscriber === false) {
$title = __('Your confirmation link expired, please subscribe again.');
} else if($subscriber->id > 0) {
if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
$subscriber->status = Subscriber::STATUS_SUBSCRIBED;
$subscriber->save();
@ -94,26 +104,62 @@ class Router {
join(', ', $segment_names)
);
}
}
}
break;
case 'manage':
// TODO
case 'edit':
if($subscriber->id > 0) {
$title = sprintf(
__('Edit your subscriber profile: %s'),
$subscriber->email
);
}
break;
case 'unsubscribe':
// TODO
if($subscriber->id > 0) {
if($subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
$subscriber->save();
}
}
$title = __("You've unsubscribed!");
break;
}
return $title;
}
function setPageContent($content) {
$action = (isset($_GET['mailpoet_action']))
? $_GET['mailpoet_action']
: null;
return __(
$subscriber = $this->getSubscriber();
switch($action) {
case 'confirm':
$content = __(
"Yup, we've added you to our list. ".
"You'll hear from us shortly."
);
break;
case 'edit':
$subscriber = $subscriber
->withCustomFields()
->withSubscriptions();
$content = 'TODO';
break;
case 'unsubscribe':
$content = '<p>'.__("Great, you'll never hear from us again!").'</p>';
if($subscriber->id > 0) {
$content .= '<p><strong>'.
str_replace(
array('[link]', '[/link]'),
array('<a href="'.$subscriber->getConfirmationUrl().'">', '</a>'),
__('You made a mistake? [link]Undo unsubscribe.[/link]')
).
'</strong></p>';
}
break;
}
return $content;
}
function setToken() {