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() { function withSendingQueue() {
$this->queue = $this->getQueue(); $queue = $this->getQueue();
if($queue !== false) {
$this->queue = $queue->asArray();
}
return $this; return $this;
} }

View File

@ -72,15 +72,27 @@ class Subscriber extends Model {
function getConfirmationUrl() { function getConfirmationUrl() {
$post = get_post(Setting::getValue('signup_confirmation.page')); $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); $url = get_permalink($post);
$params = array( $params = array(
'mailpoet_action=confirm', 'mailpoet_action='.$action,
'mailpoet_token='.md5(AUTH_KEY.$this->email), 'mailpoet_token='.md5(AUTH_KEY.$this->email),
'mailpoet_email='.$this->email 'mailpoet_email='.$this->email
); );
@ -94,7 +106,6 @@ class Subscriber extends Model {
return $url; return $url;
} }
}
function sendConfirmationEmail() { function sendConfirmationEmail() {
if($this->status === self::STATUS_UNCONFIRMED) { if($this->status === self::STATUS_UNCONFIRMED) {

View File

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