extracted subscription pages code from router

This commit is contained in:
Jonathan Labreuille
2016-02-29 13:33:48 +01:00
parent d2be407ccb
commit c721843c12
4 changed files with 271 additions and 257 deletions

View File

@@ -4,8 +4,6 @@ namespace MailPoet\Config;
use MailPoet\Models; use MailPoet\Models;
use MailPoet\Cron\Supervisor; use MailPoet\Cron\Supervisor;
use MailPoet\Router; use MailPoet\Router;
use MailPoet\Models\Setting;
use MailPoet\Settings\Pages;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@@ -26,6 +24,7 @@ class Initializer {
register_activation_hook(Env::$file, array($this, 'runPopulator')); register_activation_hook(Env::$file, array($this, 'runPopulator'));
add_action('plugins_loaded', array($this, 'setup')); add_action('plugins_loaded', array($this, 'setup'));
add_action('init', array($this, 'onInit'));
add_action('widgets_init', array($this, 'setupWidget')); add_action('widgets_init', array($this, 'setupWidget'));
} }
@@ -39,10 +38,8 @@ class Initializer {
$this->setupPublicAPI(); $this->setupPublicAPI();
$this->setupAnalytics(); $this->setupAnalytics();
$this->setupChangelog(); $this->setupChangelog();
$this->runQueueSupervisor();
$this->setupShortcodes(); $this->setupShortcodes();
$this->setupHooks(); $this->setupHooks();
$this->setupPages();
$this->setupImages(); $this->setupImages();
} catch(\Exception $e) { } catch(\Exception $e) {
// if anything goes wrong during init // if anything goes wrong during init
@@ -51,6 +48,11 @@ class Initializer {
} }
} }
function onInit() {
$this->setupPages();
$this->runQueueSupervisor();
}
function setupDB() { function setupDB() {
\ORM::configure(Env::$db_source_name); \ORM::configure(Env::$db_source_name);
\ORM::configure('username', Env::$db_username); \ORM::configure('username', Env::$db_username);
@@ -147,8 +149,11 @@ class Initializer {
} }
function setupPages() { function setupPages() {
$pages = new Pages(); $pages = new \MailPoet\Settings\Pages();
$pages->init(); $pages->init();
$subscription_pages = new \MailPoet\Subscription\Pages();
$subscription_pages->init();
} }
function setupShortcodes() { function setupShortcodes() {

View File

@@ -2,13 +2,6 @@
namespace MailPoet\Router; namespace MailPoet\Router;
use \MailPoet\Util\Security; use \MailPoet\Util\Security;
use \MailPoet\Models\Subscriber;
use \MailPoet\Models\CustomField;
use \MailPoet\Models\Setting;
use \MailPoet\Models\Segment;
use \MailPoet\Util\Helpers;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class Router { class Router {
@@ -24,7 +17,6 @@ class Router {
'wp_ajax_mailpoet', 'wp_ajax_mailpoet',
array($this, 'setup') array($this, 'setup')
); );
$this->setupPublic();
} }
function setup() { function setup() {
@@ -44,248 +36,6 @@ class Router {
} }
} }
function setupPublic() {
if(isset($_GET['mailpoet_page'])) {
$mailpoet_page = $_GET['mailpoet_page'];
add_filter('wp_title', array($this,'setWindowTitle'));
add_filter('the_title', array($this,'setPageTitle'));
add_filter('the_content', array($this,'setPageContent'));
}
}
function setWindowTitle() {
}
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)) {
$subscriber = Subscriber::create();
$subscriber->email = 'demo@mailpoet.com';
return $subscriber;
}
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 === false) {
$title = __('Your confirmation link expired, please subscribe again.');
} else {
if($subscriber->email === 'demo@mailpoet.com') {
$segment_names = array('demo 1', 'demo 2');
} else {
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;
case 'edit':
if($subscriber !== false) {
$title = sprintf(
__('Edit your subscriber profile: %s'),
$subscriber->email
);
}
break;
case 'unsubscribe':
if($subscriber !== false) {
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;
$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':
if($subscriber !== false) {
$subscriber = $subscriber
->withCustomFields()
->withSubscriptions();
$custom_fields = array_map(function($custom_field) use($subscriber) {
$custom_field->id = 'cf_'.$custom_field->id;
$custom_field = $custom_field->asArray();
$custom_field['params']['value'] = $subscriber->{$custom_field['id']};
return $custom_field;
}, CustomField::findMany());
$segment_ids = Setting::getValue('subscription.segments', array());
if(!empty($segment_ids)) {
$segments = Segment::getPublic()
->whereIn('id', $segment_ids)
->findMany();
} else {
$segments = Segment::getPublic()->findMany();
}
$subscribed_segment_ids = Helpers::arrayColumn(
$subscriber->subscriptions, 'id'
);
$segments = array_map(function($segment) use($subscribed_segment_ids) {
return array(
'id' => $segment->id,
'name' => $segment->name,
'is_checked' => in_array($segment->id, $subscribed_segment_ids)
);
}, $segments);
$fields = array(
array(
'id' => 'email',
'type' => 'text',
'params' => array(
'label' => __('Email'),
'required' => true,
'value' => $subscriber->email
)
),
array(
'id' => 'first_name',
'type' => 'text',
'params' => array(
'label' => __('First name'),
'value' => $subscriber->first_name
)
),
array(
'id' => 'last_name',
'type' => 'text',
'params' => array(
'label' => __('Last name'),
'value' => $subscriber->last_name
)
),
array(
'id' => 'status',
'type' => 'select',
'params' => array(
'label' => __('Status'),
'values' => array(
array(
'value' => array(
Subscriber::STATUS_SUBSCRIBED => __('Subscribed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_SUBSCRIBED
)
),
array(
'value' => array(
Subscriber::STATUS_UNSUBSCRIBED => __('Unsubscribed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_UNSUBSCRIBED
)
),
array(
'value' => array(
Subscriber::STATUS_UNCONFIRMED => __('Unconfirmed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_UNCONFIRMED
)
)
)
)
)
);
$form = array_merge(
$fields,
$custom_fields,
array(
array(
'id' => 'segment',
'type' => 'segment',
'params' => array(
'label' => __('Your lists'),
'values' => $segments
)
),
array(
'id' => 'submit',
'type' => 'submit',
'params' => array(
'label' => __('Subscribe!')
)
)
)
);
$content = \MailPoet\Form\Renderer::renderBlocks($form);
}
break;
case 'unsubscribe':
$content = '<p>'.__("Great, you'll never hear from us again!").'</p>';
if($subscriber !== false) {
$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() {
$global = '<script type="text/javascript">'; $global = '<script type="text/javascript">';
$global .= 'var mailpoet_token = "'.Security::generateToken().'";'; $global .= 'var mailpoet_token = "'.Security::generateToken().'";';

View File

@@ -13,8 +13,8 @@ class Pages {
), ),
'public' => true, 'public' => true,
'has_archive' => false, 'has_archive' => false,
'show_ui' => true, 'show_ui' => WP_DEBUG,
'show_in_menu' => false, 'show_in_menu' => WP_DEBUG,
'rewrite' => false, 'rewrite' => false,
'show_in_nav_menus' => false, 'show_in_nav_menus' => false,
'can_export' => false, 'can_export' => false,

259
lib/Subscription/Pages.php Normal file
View File

@@ -0,0 +1,259 @@
<?php
namespace MailPoet\Subscription;
use \MailPoet\Models\Subscriber;
use \MailPoet\Models\CustomField;
use \MailPoet\Models\Setting;
use \MailPoet\Models\Segment;
use \MailPoet\Util\Helpers;
class Pages {
function __construct() {
}
function init() {
if(isset($_GET['mailpoet_page'])) {
add_filter('wp_title', array($this,'setWindowTitle'));
add_filter('the_title', array($this,'setPageTitle'));
add_filter('the_content', array($this,'setPageContent'));
}
}
function setWindowTitle() {
// TODO
}
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)) {
$subscriber = Subscriber::create();
$subscriber->email = 'demo@mailpoet.com';
return $subscriber;
}
if(md5(AUTH_KEY.$email) === $token) {
$subscriber = Subscriber::findOne($email);
if($subscriber !== false) {
return $subscriber;
}
}
return false;
}
function setPageTitle($title = null) {
$subscriber = $this->getSubscriber();
switch($this->getAction()) {
case 'confirm':
if($subscriber === false) {
$title = __('Your confirmation link expired, please subscribe again.');
} else {
if($subscriber->email === 'demo@mailpoet.com') {
$segment_names = array('demo 1', 'demo 2');
} else {
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($segment_names)) {
$title = __("You've subscribed!");
} else {
$title = sprintf(
__("You've subscribed to: %s"),
join(', ', $segment_names)
);
}
}
break;
case 'edit':
if($subscriber !== false) {
$title = sprintf(
__('Edit your subscriber profile: %s'),
$subscriber->email
);
}
break;
case 'unsubscribe':
if($subscriber !== false) {
if($subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
$subscriber->save();
}
}
$title = __("You've unsubscribed!");
break;
}
return $title;
}
function setPageContent($page_content = '[mailpoet_page]') {
$content = '';
$subscriber = $this->getSubscriber();
switch($this->getAction()) {
case 'confirm':
if($subscriber !== false) {
$content = __(
"Yup, we've added you to our list. ".
"You'll hear from us shortly."
);
}
break;
case 'edit':
if($subscriber !== false) {
$subscriber = $subscriber
->withCustomFields()
->withSubscriptions();
$custom_fields = array_map(function($custom_field) use($subscriber) {
$custom_field->id = 'cf_'.$custom_field->id;
$custom_field = $custom_field->asArray();
$custom_field['params']['value'] = $subscriber->{$custom_field['id']};
return $custom_field;
}, CustomField::findMany());
$segment_ids = Setting::getValue('subscription.segments', array());
if(!empty($segment_ids)) {
$segments = Segment::getPublic()
->whereIn('id', $segment_ids)
->findMany();
} else {
$segments = Segment::getPublic()->findMany();
}
$subscribed_segment_ids = Helpers::arrayColumn(
$subscriber->subscriptions, 'id'
);
$segments = array_map(function($segment) use($subscribed_segment_ids) {
return array(
'id' => $segment->id,
'name' => $segment->name,
'is_checked' => in_array($segment->id, $subscribed_segment_ids)
);
}, $segments);
$fields = array(
array(
'id' => 'email',
'type' => 'text',
'params' => array(
'label' => __('Email'),
'required' => true,
'value' => $subscriber->email
)
),
array(
'id' => 'first_name',
'type' => 'text',
'params' => array(
'label' => __('First name'),
'value' => $subscriber->first_name
)
),
array(
'id' => 'last_name',
'type' => 'text',
'params' => array(
'label' => __('Last name'),
'value' => $subscriber->last_name
)
),
array(
'id' => 'status',
'type' => 'select',
'params' => array(
'label' => __('Status'),
'values' => array(
array(
'value' => array(
Subscriber::STATUS_SUBSCRIBED => __('Subscribed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_SUBSCRIBED
)
),
array(
'value' => array(
Subscriber::STATUS_UNSUBSCRIBED => __('Unsubscribed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_UNSUBSCRIBED
)
),
array(
'value' => array(
Subscriber::STATUS_UNCONFIRMED => __('Unconfirmed')
),
'is_checked' => (
$subscriber->status === Subscriber::STATUS_UNCONFIRMED
)
)
)
)
)
);
$form = array_merge(
$fields,
$custom_fields,
array(
array(
'id' => 'segment',
'type' => 'segment',
'params' => array(
'label' => __('Your lists'),
'values' => $segments
)
),
array(
'id' => 'submit',
'type' => 'submit',
'params' => array(
'label' => __('Subscribe!')
)
)
)
);
$content = \MailPoet\Form\Renderer::renderBlocks($form);
}
break;
case 'unsubscribe':
$content = '<p>'.__("Great, you'll never hear from us again!").'</p>';
if($subscriber !== false) {
$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 str_replace('[mailpoet_page]', $content, $page_content);
}
private function getAction() {
return (isset($_GET['mailpoet_action']))
? $_GET['mailpoet_action']
: null;
}
}