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,28 +72,39 @@ 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');
}
if($post === null) { function getEditSubscriptionUrl() {
// create page $post = get_post(Setting::getValue('subscription.page'));
return ''; return $this->getSubscriptionUrl($post, 'edit');
} else { }
$url = get_permalink($post);
$params = array( function getUnsubscribeUrl() {
'mailpoet_action=confirm', $post = get_post(Setting::getValue('subscription.page'));
'mailpoet_token='.md5(AUTH_KEY.$this->email), return $this->getSubscriptionUrl($post, 'unsubscribe');
'mailpoet_email='.$this->email }
);
// add parameters
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
$url_params = parse_url($url);
if(empty($url_params['scheme'])) {
$url = get_bloginfo('url').$url;
}
return $url; private function getSubscriptionUrl($post = null, $action = null) {
if($post === null || $action === null) return;
$url = get_permalink($post);
$params = array(
'mailpoet_action='.$action,
'mailpoet_token='.md5(AUTH_KEY.$this->email),
'mailpoet_email='.$this->email
);
// add parameters
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
$url_params = parse_url($url);
if(empty($url_params['scheme'])) {
$url = get_bloginfo('url').$url;
} }
return $url;
} }
function sendConfirmationEmail() { function sendConfirmationEmail() {

View File

@ -52,68 +52,114 @@ class Router {
} }
function getSubscriber() {
$token = (isset($_GET['mailpoet_token']))
? $_GET['mailpoet_token']
: null;
$email = (isset($_GET['mailpoet_email']))
? $_GET['mailpoet_email']
: null;
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) { function setPageTitle($title) {
$action = (isset($_GET['mailpoet_action'])) $action = (isset($_GET['mailpoet_action']))
? $_GET['mailpoet_action'] ? $_GET['mailpoet_action']
: null; : null;
// get subscriber
$subscriber = $this->getSubscriber();
switch($action) { switch($action) {
case 'confirm': case 'confirm':
$token = (isset($_GET['mailpoet_token'])) if($subscriber === 'demo') {
? $_GET['mailpoet_token'] $title = sprintf(__("You've subscribed to: %s"), 'demo');
: null; } else if($subscriber === false) {
$email = (isset($_GET['mailpoet_email'])) $title = __('Your confirmation link expired, please subscribe again.');
? $_GET['mailpoet_email'] } else if($subscriber->id > 0) {
: null; if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
$subscriber->status = Subscriber::STATUS_SUBSCRIBED;
$subscriber->save();
}
$segments = $subscriber->segments()->findMany();
$segment_names = array_map(function($segment) {
return $segment->name;
}, $segments);
if(empty($token) || empty($token)) {
$title = sprintf( $title = sprintf(
__("You've subscribed to: %s"), __("You've subscribed to: %s"),
'demo' join(', ', $segment_names)
); );
} else {
// check token validity
if(md5(AUTH_KEY.$email) !== $token) {
$title = __('Your confirmation link expired, please subscribe again.');
} else {
$subscriber = Subscriber::findOne($email);
if($subscriber !== false) {
if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
$subscriber->status = Subscriber::STATUS_SUBSCRIBED;
$subscriber->save();
}
$segments = $subscriber->segments()->findMany();
$segment_names = array_map(function($segment) {
return $segment->name;
}, $segments);
$title = sprintf(
__("You've subscribed to: %s"),
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();
"Yup, we've added you to our list. ".
"You'll hear from us shortly."
);
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() { function setToken() {