Subscription pages
This commit is contained in:
committed by
Vlad
parent
378f6d803a
commit
2cbd2d54f3
@ -278,33 +278,18 @@ const SubscriberList = React.createClass({
|
|||||||
|
|
||||||
if (subscriber.subscriptions.length > 0) {
|
if (subscriber.subscriptions.length > 0) {
|
||||||
let subscribed_segments = [];
|
let subscribed_segments = [];
|
||||||
let unsubscribed_segments = [];
|
|
||||||
|
|
||||||
subscriber.subscriptions.map((subscription) => {
|
subscriber.subscriptions.map((subscription) => {
|
||||||
const segment = this.getSegmentFromId(subscription.segment_id);
|
const segment = this.getSegmentFromId(subscription.segment_id);
|
||||||
if(segment === false) return;
|
if(segment === false) return;
|
||||||
if (subscription.status === 'subscribed') {
|
if (subscription.status === 'subscribed') {
|
||||||
subscribed_segments.push(segment.name);
|
subscribed_segments.push(segment.name);
|
||||||
} else if (subscription.status === 'unsubscribed') {
|
|
||||||
unsubscribed_segments.push(segment.name);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
segments = (
|
segments = (
|
||||||
<span>
|
<span>
|
||||||
<span className="mailpoet_segments_subscribed">
|
<span className="mailpoet_segments_subscribed">
|
||||||
{ subscribed_segments.join(', ') }
|
{ subscribed_segments.join(', ') }
|
||||||
{
|
|
||||||
(
|
|
||||||
subscribed_segments.length > 0
|
|
||||||
&& unsubscribed_segments.length > 0
|
|
||||||
) ? ' / ' : ''
|
|
||||||
}
|
|
||||||
</span>
|
|
||||||
<span
|
|
||||||
className="mailpoet_segments_unsubscribed"
|
|
||||||
title={MailPoet.I18n.t('listsToWhichSubscriberWasSubscribed')}
|
|
||||||
>
|
|
||||||
{ unsubscribed_segments.join(', ') }
|
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
|
@ -13,6 +13,7 @@ class Hooks {
|
|||||||
$this->setupWPUsers();
|
$this->setupWPUsers();
|
||||||
$this->setupImageSize();
|
$this->setupImageSize();
|
||||||
$this->setupListing();
|
$this->setupListing();
|
||||||
|
$this->setupManageSubscription();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupSubscribe() {
|
function setupSubscribe() {
|
||||||
@ -139,6 +140,18 @@ class Hooks {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupManageSubscription() {
|
||||||
|
// handle subscription form submission
|
||||||
|
add_action(
|
||||||
|
'admin_post_mailpoet_subscription_update',
|
||||||
|
'\MailPoet\Subscription\Manage::onSave'
|
||||||
|
);
|
||||||
|
add_action(
|
||||||
|
'admin_post_nopriv_mailpoet_subscription_update',
|
||||||
|
'\MailPoet\Subscription\Manage::onSave'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function setScreenOption($status, $option, $value) {
|
function setScreenOption($status, $option, $value) {
|
||||||
if(preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
|
if(preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
|
||||||
return $value;
|
return $value;
|
||||||
|
@ -156,9 +156,6 @@ class Initializer {
|
|||||||
function setupPages() {
|
function setupPages() {
|
||||||
$pages = new \MailPoet\Settings\Pages();
|
$pages = new \MailPoet\Settings\Pages();
|
||||||
$pages->init();
|
$pages->init();
|
||||||
|
|
||||||
$subscription_pages = new \MailPoet\Subscription\Pages();
|
|
||||||
$subscription_pages->init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupShortcodes() {
|
function setupShortcodes() {
|
||||||
|
@ -74,9 +74,14 @@ class Populator {
|
|||||||
$mailpoet_page_id = (int)$page->ID;
|
$mailpoet_page_id = (int)$page->ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
Setting::setValue('subscription.unsubscribe_page', $mailpoet_page_id);
|
$subscription = Setting::getValue('subscription.pages', array());
|
||||||
Setting::setValue('subscription.manage_page', $mailpoet_page_id);
|
if(empty($subscription)) {
|
||||||
Setting::setValue('subscription.confirmation_page', $mailpoet_page_id);
|
Setting::setValue('subscription.pages', array(
|
||||||
|
'unsubscribe' => $mailpoet_page_id,
|
||||||
|
'manage' => $mailpoet_page_id,
|
||||||
|
'confirmation' => $mailpoet_page_id
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createDefaultSettings() {
|
private function createDefaultSettings() {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace MailPoet\Config;
|
namespace MailPoet\Config;
|
||||||
|
|
||||||
use MailPoet\Cron\Daemon;
|
use MailPoet\Cron\Daemon;
|
||||||
|
use MailPoet\Subscription;
|
||||||
use MailPoet\Statistics\Track\Clicks;
|
use MailPoet\Statistics\Track\Clicks;
|
||||||
use MailPoet\Util\Helpers;
|
use MailPoet\Util\Helpers;
|
||||||
|
|
||||||
@ -22,7 +23,11 @@ class PublicAPI {
|
|||||||
$this->action = isset($_GET['action']) ?
|
$this->action = isset($_GET['action']) ?
|
||||||
Helpers::underscoreToCamelCase($_GET['action']) :
|
Helpers::underscoreToCamelCase($_GET['action']) :
|
||||||
false;
|
false;
|
||||||
$this->data = isset($_GET['data']) ? $_GET['data'] : false;
|
$this->data = (
|
||||||
|
(isset($_GET['data']))
|
||||||
|
? unserialize(base64_decode($_GET['data']))
|
||||||
|
: array()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
@ -38,6 +43,14 @@ class PublicAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function subscription() {
|
||||||
|
try {
|
||||||
|
$subscription = new Subscription\Pages($this->action, $this->data);
|
||||||
|
$this->_checkAndCallMethod($subscription, $this->action);
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function track() {
|
function track() {
|
||||||
try {
|
try {
|
||||||
if ($this->action === 'click') {
|
if ($this->action === 'click') {
|
||||||
|
@ -3,6 +3,7 @@ namespace MailPoet\Config;
|
|||||||
use \MailPoet\Models\Newsletter;
|
use \MailPoet\Models\Newsletter;
|
||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
use \MailPoet\Models\SubscriberSegment;
|
use \MailPoet\Models\SubscriberSegment;
|
||||||
|
use \MailPoet\Subscription;
|
||||||
|
|
||||||
class Shortcodes {
|
class Shortcodes {
|
||||||
function __construct() {
|
function __construct() {
|
||||||
|
@ -16,12 +16,12 @@ class Daemon {
|
|||||||
private $timer;
|
private $timer;
|
||||||
|
|
||||||
function __construct($data) {
|
function __construct($data) {
|
||||||
if (!$data) $this->abortWithError(__('Invalid or missing cron data.'));
|
if(empty($data)) $this->abortWithError(__('Invalid or missing cron data.'));
|
||||||
set_time_limit(0);
|
set_time_limit(0);
|
||||||
ignore_user_abort();
|
ignore_user_abort();
|
||||||
$this->daemon = CronHelper::getDaemon();
|
$this->daemon = CronHelper::getDaemon();
|
||||||
$this->token = CronHelper::createToken();
|
$this->token = CronHelper::createToken();
|
||||||
$this->data = unserialize(base64_decode($data));
|
$this->data = $data;
|
||||||
$this->timer = microtime(true);
|
$this->timer = microtime(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,30 +48,6 @@ class Subscriber extends Model {
|
|||||||
return parent::delete();
|
return parent::delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToSegments(array $segment_ids = array()) {
|
|
||||||
$wp_users_segment = Segment::getWPUsers();
|
|
||||||
|
|
||||||
if($wp_users_segment !== false) {
|
|
||||||
// delete all relations to segments except WP users
|
|
||||||
SubscriberSegment::where('subscriber_id', $this->id)
|
|
||||||
->whereNotEqual('segment_id', $wp_users_segment->id)
|
|
||||||
->deleteMany();
|
|
||||||
} else {
|
|
||||||
// delete all relations to segments
|
|
||||||
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!empty($segment_ids)) {
|
|
||||||
$segments = Segment::whereIn('id', $segment_ids)->findMany();
|
|
||||||
foreach($segments as $segment) {
|
|
||||||
$association = SubscriberSegment::create();
|
|
||||||
$association->subscriber_id = $this->id;
|
|
||||||
$association->segment_id = $segment->id;
|
|
||||||
$association->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendConfirmationEmail() {
|
function sendConfirmationEmail() {
|
||||||
if($this->status === self::STATUS_UNCONFIRMED) {
|
if($this->status === self::STATUS_UNCONFIRMED) {
|
||||||
$signup_confirmation = Setting::getValue('signup_confirmation');
|
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||||
@ -168,14 +144,18 @@ class Subscriber extends Model {
|
|||||||
$subscriber->setExpr('deleted_at', 'NULL');
|
$subscriber->setExpr('deleted_at', 'NULL');
|
||||||
}
|
}
|
||||||
|
|
||||||
// auto subscribe when signup confirmation is disabled
|
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
||||||
if($signup_confirmation_enabled === false) {
|
// auto subscribe when signup confirmation is disabled
|
||||||
$subscriber->set('status', self::STATUS_SUBSCRIBED);
|
if($signup_confirmation_enabled === true) {
|
||||||
|
$subscriber->set('status', self::STATUS_UNCONFIRMED);
|
||||||
|
} else {
|
||||||
|
$subscriber->set('status', self::STATUS_SUBSCRIBED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($subscriber->save()) {
|
if($subscriber->save()) {
|
||||||
// link subscriber to segments
|
// link subscriber to segments
|
||||||
$subscriber->addToSegments($segment_ids);
|
SubscriberSegment::addSubscriptions($subscriber, $segment_ids);
|
||||||
|
|
||||||
// signup confirmation
|
// signup confirmation
|
||||||
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
||||||
@ -409,8 +389,8 @@ class Subscriber extends Model {
|
|||||||
&&
|
&&
|
||||||
($new_status === self::STATUS_UNSUBSCRIBED)
|
($new_status === self::STATUS_UNSUBSCRIBED)
|
||||||
) {
|
) {
|
||||||
// make sure we unsubscribe the user from all lists
|
// make sure we unsubscribe the user from all segments
|
||||||
SubscriberSegment::setSubscriptions($subscriber, array());
|
SubscriberSegment::removeSubscriptions($subscriber);
|
||||||
} else {
|
} else {
|
||||||
if($segment_ids !== false) {
|
if($segment_ids !== false) {
|
||||||
SubscriberSegment::setSubscriptions($subscriber, $segment_ids);
|
SubscriberSegment::setSubscriptions($subscriber, $segment_ids);
|
||||||
|
@ -16,27 +16,55 @@ class SubscriberSegment extends Model {
|
|||||||
return $this->has_one(__NAMESPACE__.'\Subscriber', 'id', 'subscriber_id');
|
return $this->has_one(__NAMESPACE__.'\Subscriber', 'id', 'subscriber_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function setSubscriptions($subscriber, $segment_ids = array()) {
|
static function removeSubscriptions($subscriber, $segment_ids = array()) {
|
||||||
if($subscriber->id > 0) {
|
if($subscriber->id > 0) {
|
||||||
// unsubscribe from current subscriptions
|
if(!empty($segment_ids)) {
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
// subscribe to segments
|
||||||
->findResultSet()
|
foreach($segment_ids as $segment_id) {
|
||||||
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
if((int)$segment_id > 0) {
|
||||||
->save();
|
self::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $subscriber->id,
|
||||||
// subscribe to segments
|
'segment_id' => $segment_id,
|
||||||
foreach($segment_ids as $segment_id) {
|
'status' => Subscriber::STATUS_UNSUBSCRIBED
|
||||||
if((int)$segment_id > 0) {
|
));
|
||||||
self::createOrUpdate(array(
|
}
|
||||||
'subscriber_id' => $subscriber->id,
|
|
||||||
'segment_id' => $segment_id,
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// unsubscribe from all segments
|
||||||
|
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
|
->findResultSet()
|
||||||
|
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
||||||
|
->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $subscriber;
|
static function addSubscriptions($subscriber, $segment_ids = array()) {
|
||||||
|
if($subscriber->id > 0) {
|
||||||
|
if(!empty($segment_ids)) {
|
||||||
|
// subscribe to segments
|
||||||
|
foreach($segment_ids as $segment_id) {
|
||||||
|
if((int)$segment_id > 0) {
|
||||||
|
self::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $subscriber->id,
|
||||||
|
'segment_id' => $segment_id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// subscribe to all segments
|
||||||
|
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
|
->findResultSet()
|
||||||
|
->set('status', Subscriber::STATUS_SUBSCRIBED)
|
||||||
|
->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static function setSubscriptions($subscriber, $segment_ids = array()) {
|
||||||
|
self::removeSubscriptions($subscriber);
|
||||||
|
self::addSubscriptions($subscriber, $segment_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function subscribed($orm) {
|
static function subscribed($orm) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Settings;
|
namespace MailPoet\Settings;
|
||||||
|
use \MailPoet\Subscription;
|
||||||
|
|
||||||
class Pages {
|
class Pages {
|
||||||
function __construct() {
|
function __construct() {
|
||||||
@ -65,9 +66,11 @@ class Pages {
|
|||||||
return array(
|
return array(
|
||||||
'id' => $page->ID,
|
'id' => $page->ID,
|
||||||
'title' => $page->post_title,
|
'title' => $page->post_title,
|
||||||
'preview_url' => add_query_arg(array(
|
'url' => array(
|
||||||
'mailpoet_preview' => 1
|
'unsubscribe' => Subscription\Url::getSubscriptionUrl($page, 'unsubscribe'),
|
||||||
), get_permalink($page->ID))
|
'manage' => Subscription\Url::getSubscriptionUrl($page, 'manage'),
|
||||||
|
'confirm' => Subscription\Url::getSubscriptionUrl($page, 'confirm')
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
32
lib/Subscription/Manage.php
Normal file
32
lib/Subscription/Manage.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Subscription;
|
||||||
|
use \MailPoet\Models\Setting;
|
||||||
|
use \MailPoet\Models\Subscriber;
|
||||||
|
use \MailPoet\Models\SubscriberSegment;
|
||||||
|
use \MailPoet\Util\Url;
|
||||||
|
|
||||||
|
class Manage {
|
||||||
|
|
||||||
|
static function onSave() {
|
||||||
|
$action = (isset($_POST['action']) ? $_POST['action'] : null);
|
||||||
|
if($action !== 'mailpoet_subscription_update') {
|
||||||
|
Url::redirectBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
$reserved_keywords = array('action', 'mailpoet_redirect');
|
||||||
|
$subscriber_data = array_diff_key(
|
||||||
|
$_POST,
|
||||||
|
array_flip($reserved_keywords)
|
||||||
|
);
|
||||||
|
|
||||||
|
if(isset($subscriber_data['email'])) {
|
||||||
|
if($subscriber_data['email'] !== Pages::DEMO_EMAIL) {
|
||||||
|
$subscriber = Subscriber::createOrUpdate($subscriber_data);
|
||||||
|
$errors = $subscriber->getErrors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TBD: success/error messages (not present in MP2)
|
||||||
|
Url::redirectBack();
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ namespace MailPoet\Subscription;
|
|||||||
|
|
||||||
use \MailPoet\Router\Subscribers;
|
use \MailPoet\Router\Subscribers;
|
||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
|
use \MailPoet\Models\SubscriberSegment;
|
||||||
use \MailPoet\Models\CustomField;
|
use \MailPoet\Models\CustomField;
|
||||||
use \MailPoet\Models\Setting;
|
use \MailPoet\Models\Setting;
|
||||||
use \MailPoet\Models\Segment;
|
use \MailPoet\Models\Segment;
|
||||||
@ -13,51 +14,123 @@ use \MailPoet\Subscription;
|
|||||||
class Pages {
|
class Pages {
|
||||||
const DEMO_EMAIL = 'demo@mailpoet.com';
|
const DEMO_EMAIL = 'demo@mailpoet.com';
|
||||||
|
|
||||||
function __construct() {
|
private $action;
|
||||||
|
private $data;
|
||||||
|
private $subscriber;
|
||||||
|
|
||||||
|
function __construct($action, $data) {
|
||||||
|
$this->action = $action;
|
||||||
|
$this->data = $data;
|
||||||
|
$this->subscriber = $this->getSubscriber();
|
||||||
|
|
||||||
|
// handle subscription pages title & content
|
||||||
|
add_filter('wp_title', array($this,'setWindowTitle'), 10, 3);
|
||||||
|
add_filter('document_title_parts', array($this,'setWindowTitleParts'), 10, 1);
|
||||||
|
add_filter('the_title', array($this,'setPageTitle'), 10, 1);
|
||||||
|
add_filter('the_content', array($this,'setPageContent'), 10, 1);
|
||||||
|
|
||||||
|
// manage subscription link shortcode
|
||||||
|
// [mailpoet_manage text="Manage your subscription"]
|
||||||
|
add_shortcode('mailpoet_manage', array($this, 'getManageLink'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
private function isPreview() {
|
||||||
$action = $this->getAction();
|
return (
|
||||||
if($action !== null) {
|
array_key_exists('preview', $_GET)
|
||||||
add_filter('wp_title', array($this,'setWindowTitle'), 10, 3);
|
|| array_key_exists('preview', $this->data)
|
||||||
add_filter('document_title_parts', array($this,'setWindowTitleParts'), 10, 1);
|
|
||||||
add_filter('the_title', array($this,'setPageTitle'), 10, 1);
|
|
||||||
add_filter('the_content', array($this,'setPageContent'), 10, 1);
|
|
||||||
}
|
|
||||||
add_action(
|
|
||||||
'admin_post_mailpoet_subscriber_save',
|
|
||||||
array($this, 'subscriberSave')
|
|
||||||
);
|
|
||||||
add_action(
|
|
||||||
'admin_post_nopriv_mailpoet_subscriber_save',
|
|
||||||
array($this, 'subscriberSave')
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscriberSave() {
|
function getSubscriber() {
|
||||||
$action = (isset($_POST['action']) ? $_POST['action'] : null);
|
$token = (isset($this->data['token'])) ? $this->data['token'] : null;
|
||||||
if($action !== 'mailpoet_subscriber_save') {
|
$email = (isset($this->data['email'])) ? $this->data['email'] : null;
|
||||||
Url::redirectBack();
|
|
||||||
}
|
|
||||||
|
|
||||||
$reserved_keywords = array('action', 'mailpoet_redirect');
|
if(Subscriber::generateToken($email) === $token) {
|
||||||
$subscriber_data = array_diff_key(
|
$subscriber = Subscriber::findOne($email);
|
||||||
$_POST,
|
if($subscriber !== false) {
|
||||||
array_flip($reserved_keywords)
|
return $subscriber;
|
||||||
);
|
|
||||||
if(isset($subscriber_data['email'])) {
|
|
||||||
if($subscriber_data['email'] !== self::DEMO_EMAIL) {
|
|
||||||
$subscriber = Subscriber::createOrUpdate($subscriber_data);
|
|
||||||
$errors = $subscriber->getErrors();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TBD: success/error messages (not present in MP2)
|
return false;
|
||||||
|
|
||||||
Url::redirectBack();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isPreview() {
|
function confirm() {
|
||||||
return (array_key_exists('mailpoet_preview', $_GET));
|
if($this->subscriber !== false) {
|
||||||
|
if($this->subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
|
||||||
|
$this->subscriber->status = Subscriber::STATUS_SUBSCRIBED;
|
||||||
|
$this->subscriber->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsubscribe() {
|
||||||
|
if($this->subscriber !== false) {
|
||||||
|
if($this->subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
|
||||||
|
$this->subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
||||||
|
$this->subscriber->save();
|
||||||
|
SubscriberSegment::setSubscriptions($this->subscriber, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function setPageTitle($page_title = '') {
|
||||||
|
global $post;
|
||||||
|
|
||||||
|
if($post->post_title !== __('MailPoet Page')) return $page_title;
|
||||||
|
if(
|
||||||
|
($this->isMailPoetPage($post->ID) === false)
|
||||||
|
||
|
||||||
|
($page_title !== single_post_title('', false))
|
||||||
|
) {
|
||||||
|
return $page_title;
|
||||||
|
} else {
|
||||||
|
switch($this->action) {
|
||||||
|
case 'confirm':
|
||||||
|
return $this->getConfirmTitle();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'manage':
|
||||||
|
|
||||||
|
return $this->getManageTitle();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'unsubscribe':
|
||||||
|
return $this->getUnsubscribeTitle();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $page_title;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setPageContent($page_content = '[mailpoet_page]') {
|
||||||
|
global $post;
|
||||||
|
|
||||||
|
if(
|
||||||
|
($this->isPreview() === false)
|
||||||
|
&&
|
||||||
|
($this->isMailPoetPage($post->ID) === false)
|
||||||
|
) {
|
||||||
|
return $page_content;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = '';
|
||||||
|
|
||||||
|
switch($this->action) {
|
||||||
|
case 'confirm':
|
||||||
|
$content = $this->getConfirmContent();
|
||||||
|
break;
|
||||||
|
case 'manage':
|
||||||
|
$content = $this->getManageContent();
|
||||||
|
break;
|
||||||
|
case 'unsubscribe':
|
||||||
|
$content = $this->getUnsubscribeContent();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(strpos($page_content, '[mailpoet_page]') !== false) {
|
||||||
|
return str_replace('[mailpoet_page]', $content, $page_content);
|
||||||
|
} else {
|
||||||
|
return $page_content.$content;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setWindowTitle($title, $separator, $separator_location = 'right') {
|
function setWindowTitle($title, $separator, $separator_location = 'right') {
|
||||||
@ -80,93 +153,24 @@ class Pages {
|
|||||||
|
|
||||||
function isMailPoetPage($page_id = null) {
|
function isMailPoetPage($page_id = null) {
|
||||||
$mailpoet_page_ids = array_unique(array_values(
|
$mailpoet_page_ids = array_unique(array_values(
|
||||||
Setting::getValue('subscription', array())
|
Setting::getValue('subscription.pages', array())
|
||||||
));
|
));
|
||||||
|
|
||||||
return (in_array($page_id, $mailpoet_page_ids));
|
return (in_array($page_id, $mailpoet_page_ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPageTitle($page_title = '') {
|
private function getConfirmTitle() {
|
||||||
global $post;
|
|
||||||
|
|
||||||
if($post->post_title !== __('MailPoet Page')) return $page_title;
|
|
||||||
|
|
||||||
if(
|
|
||||||
($this->isMailPoetPage($post->ID) === false)
|
|
||||||
||
|
|
||||||
($page_title !== single_post_title('', false))
|
|
||||||
) {
|
|
||||||
return $page_title;
|
|
||||||
} else {
|
|
||||||
$subscriber = $this->getSubscriber();
|
|
||||||
switch($this->getAction()) {
|
|
||||||
case 'confirm':
|
|
||||||
return $this->getConfirmTitle($subscriber);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'manage':
|
|
||||||
return $this->getManageTitle($subscriber);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'unsubscribe':
|
|
||||||
if($subscriber !== false) {
|
|
||||||
if($subscriber->status !== Subscriber::STATUS_UNSUBSCRIBED) {
|
|
||||||
$subscriber->status = Subscriber::STATUS_UNSUBSCRIBED;
|
|
||||||
$subscriber->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->getUnsubscribeTitle($subscriber);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $page_title;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setPageContent($page_content = '[mailpoet_page]') {
|
|
||||||
global $post;
|
|
||||||
|
|
||||||
if(
|
|
||||||
($this->isPreview() === false)
|
|
||||||
&&
|
|
||||||
($this->isMailPoetPage($post->ID) === false)
|
|
||||||
) {
|
|
||||||
return $page_content;
|
|
||||||
}
|
|
||||||
|
|
||||||
$content = '';
|
|
||||||
$subscriber = $this->getSubscriber();
|
|
||||||
|
|
||||||
switch($this->getAction()) {
|
|
||||||
case 'confirm':
|
|
||||||
$content = $this->getConfirmContent($subscriber);
|
|
||||||
break;
|
|
||||||
case 'manage':
|
|
||||||
$content = $this->getManageContent($subscriber);
|
|
||||||
break;
|
|
||||||
case 'unsubscribe':
|
|
||||||
$content = $this->getUnsubscribeContent($subscriber);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return str_replace('[mailpoet_page]', $content, $page_content);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getConfirmTitle($subscriber) {
|
|
||||||
if($this->isPreview()) {
|
if($this->isPreview()) {
|
||||||
$title = sprintf(
|
$title = sprintf(
|
||||||
__("You've subscribed to: %s"),
|
__("You've subscribed to: %s"),
|
||||||
'demo 1, demo 2'
|
'demo 1, demo 2'
|
||||||
);
|
);
|
||||||
} else if($subscriber === false) {
|
} else if($this->subscriber === false) {
|
||||||
$title = __('Your confirmation link expired, please subscribe again.');
|
$title = __('Your confirmation link expired, please subscribe again.');
|
||||||
} else {
|
} else {
|
||||||
if($subscriber->status !== Subscriber::STATUS_SUBSCRIBED) {
|
|
||||||
$subscriber->status = Subscriber::STATUS_SUBSCRIBED;
|
|
||||||
$subscriber->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
$segment_names = array_map(function($segment) {
|
$segment_names = array_map(function($segment) {
|
||||||
return $segment->name;
|
return $segment->name;
|
||||||
}, $subscriber->segments()->findMany());
|
}, $this->subscriber->segments()->findMany());
|
||||||
|
|
||||||
if(empty($segment_names)) {
|
if(empty($segment_names)) {
|
||||||
$title = __("You've subscribed!");
|
$title = __("You've subscribed!");
|
||||||
@ -180,41 +184,35 @@ class Pages {
|
|||||||
return $title;
|
return $title;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getManageTitle($subscriber) {
|
private function getManageTitle() {
|
||||||
if($this->isPreview()) {
|
if($this->isPreview() || $this->subscriber !== false) {
|
||||||
return sprintf(
|
return __("Manage your subscription");
|
||||||
__('Edit your subscriber profile: %s'),
|
|
||||||
self::DEMO_EMAIL
|
|
||||||
);
|
|
||||||
} else if($subscriber !== false) {
|
|
||||||
return sprintf(
|
|
||||||
__('Edit your subscriber profile: %s'),
|
|
||||||
$subscriber->email
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getUnsubscribeTitle($subscriber) {
|
private function getUnsubscribeTitle() {
|
||||||
if($this->isPreview() || $subscriber !== false) {
|
if($this->isPreview() || $this->subscriber !== false) {
|
||||||
return __("You've unsubscribed!");
|
return __("You've successfully unsubscribed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function getConfirmContent($subscriber) {
|
private function getConfirmContent() {
|
||||||
if($this->isPreview() || $subscriber !== false) {
|
if($this->isPreview() || $this->subscriber !== false) {
|
||||||
return __("Yup, we've added you to our list. You'll hear from us shortly.");
|
return __("Yup, we've added you to our list. You'll hear from us shortly.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getManageContent($subscriber) {
|
private function getManageContent() {
|
||||||
if($this->isPreview()) {
|
if($this->isPreview()) {
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = Subscriber::create();
|
||||||
$subscriber->hydrate(array(
|
$subscriber->hydrate(array(
|
||||||
'email' => self::DEMO_EMAIL
|
'email' => self::DEMO_EMAIL,
|
||||||
|
'first_name' => 'John',
|
||||||
|
'last_name' => 'Doe'
|
||||||
));
|
));
|
||||||
} else if($subscriber !== false) {
|
} else if($this->subscriber !== false) {
|
||||||
$subscriber = $subscriber
|
$subscriber = $this->subscriber
|
||||||
->withCustomFields()
|
->withCustomFields()
|
||||||
->withSubscriptions();
|
->withSubscriptions();
|
||||||
} else {
|
} else {
|
||||||
@ -238,8 +236,8 @@ class Pages {
|
|||||||
->findMany();
|
->findMany();
|
||||||
}
|
}
|
||||||
$subscribed_segment_ids = array();
|
$subscribed_segment_ids = array();
|
||||||
if(!empty($subscriber->subscriptions)) {
|
if(!empty($this->subscriber->subscriptions)) {
|
||||||
foreach ($subscriber->subscriptions as $subscription) {
|
foreach ($this->subscriber->subscriptions as $subscription) {
|
||||||
if($subscription['status'] === Subscriber::STATUS_SUBSCRIBED) {
|
if($subscription['status'] === Subscriber::STATUS_SUBSCRIBED) {
|
||||||
$subscribed_segment_ids[] = $subscription['segment_id'];
|
$subscribed_segment_ids[] = $subscription['segment_id'];
|
||||||
}
|
}
|
||||||
@ -255,16 +253,6 @@ class Pages {
|
|||||||
}, $segments);
|
}, $segments);
|
||||||
|
|
||||||
$fields = array(
|
$fields = array(
|
||||||
array(
|
|
||||||
'id' => 'email',
|
|
||||||
'type' => 'text',
|
|
||||||
'params' => array(
|
|
||||||
'label' => __('Email'),
|
|
||||||
'required' => true,
|
|
||||||
'value' => $subscriber->email,
|
|
||||||
'readonly' => true
|
|
||||||
)
|
|
||||||
),
|
|
||||||
array(
|
array(
|
||||||
'id' => 'first_name',
|
'id' => 'first_name',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
@ -333,53 +321,54 @@ class Pages {
|
|||||||
$form_html = '<form method="POST" '.
|
$form_html = '<form method="POST" '.
|
||||||
'action="'.admin_url('admin-post.php').'" '.
|
'action="'.admin_url('admin-post.php').'" '.
|
||||||
'novalidate>';
|
'novalidate>';
|
||||||
$form_html .= '<input type="hidden" name="action" '.
|
$form_html .= '<input type="hidden" name="action"'.
|
||||||
'value="mailpoet_subscriber_save" />';
|
' value="mailpoet_subscription_update" />';
|
||||||
$form_html .= '<input type="hidden" name="segments" value="" />';
|
$form_html .= '<input type="hidden" name="segments" value="" />';
|
||||||
$form_html .= '<input type="hidden" name="mailpoet_redirect" '.
|
$form_html .= '<input type="hidden" name="mailpoet_redirect" '.
|
||||||
'value="'.Url::getCurrentUrl().'" />';
|
'value="'.Url::getCurrentUrl().'" />';
|
||||||
|
$form_html .= '<input type="hidden" name="email" value="'.$subscriber->email.'" />';
|
||||||
|
|
||||||
|
$form_html .= '<p class="mailpoet_paragraph">';
|
||||||
|
$form_html .= '<label>Email *<br /><strong>'.$subscriber->email.'</strong></label>';
|
||||||
|
$form_html .= '<br /><span style="font-size:85%;">';
|
||||||
|
if($subscriber->wp_user_id !== null) {
|
||||||
|
$form_html .= str_replace(
|
||||||
|
array('[link]', '[/link]'),
|
||||||
|
array('<a href="'.wp_login_url().'" target="_blank">', '</a>'),
|
||||||
|
__('[link]Log in to your account[/link] to update your email.')
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$form_html .= __('Need to change your email address? Unsubscribe here and simply sign up again.');
|
||||||
|
}
|
||||||
|
$form_html .= '</span>';
|
||||||
|
$form_html .= '</p>';
|
||||||
|
|
||||||
|
// subscription form
|
||||||
$form_html .= \MailPoet\Form\Renderer::renderBlocks($form);
|
$form_html .= \MailPoet\Form\Renderer::renderBlocks($form);
|
||||||
$form_html .= '</form>';
|
$form_html .= '</form>';
|
||||||
return $form_html;
|
return $form_html;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getUnsubscribeContent($subscriber) {
|
private function getUnsubscribeContent() {
|
||||||
$content = '';
|
$content = '';
|
||||||
if($this->isPreview() || $subscriber !== false) {
|
if($this->isPreview() || $this->subscriber !== false) {
|
||||||
$content = '<p>'.__("Great, you'll never hear from us again!").'</p>';
|
$content .= '<p>'.__('You made a mistake?').' <strong>';
|
||||||
if($subscriber !== false) {
|
$content .= '[mailpoet_manage]';
|
||||||
$content .= '<p><strong>'.
|
$content .= '</strong></p>';
|
||||||
str_replace(
|
|
||||||
array('[link]', '[/link]'),
|
|
||||||
array('<a href="'.Subscription\Url::getConfirmationUrl($subscriber).'">', '</a>'),
|
|
||||||
__('You made a mistake? [link]Undo unsubscribe.[/link]')
|
|
||||||
).
|
|
||||||
'</strong></p>';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getSubscriber() {
|
function getManageLink($params) {
|
||||||
$token = (isset($_GET['mailpoet_token']))
|
// get label or display default label
|
||||||
? $_GET['mailpoet_token']
|
$text = (
|
||||||
: null;
|
isset($params['text'])
|
||||||
$email = (isset($_GET['mailpoet_email']))
|
? $params['text']
|
||||||
? $_GET['mailpoet_email']
|
: __('Manage your subscription')
|
||||||
: null;
|
);
|
||||||
|
|
||||||
if(Subscriber::generateToken($email) === $token) {
|
return '<a href="'.Subscription\Url::getManageUrl(
|
||||||
$subscriber = Subscriber::findOne($email);
|
$this->subscriber
|
||||||
if($subscriber !== false) {
|
).'">'.$text.'</a>';
|
||||||
return $subscriber;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getAction() {
|
|
||||||
return (isset($_GET['mailpoet_action']))
|
|
||||||
? $_GET['mailpoet_action']
|
|
||||||
: null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,21 +6,21 @@ use \MailPoet\Models\Setting;
|
|||||||
|
|
||||||
class Url {
|
class Url {
|
||||||
static function getConfirmationUrl($subscriber = false) {
|
static function getConfirmationUrl($subscriber = false) {
|
||||||
$post = get_post(Setting::getValue('subscription.confirmation_page'));
|
$post = get_post(Setting::getValue('subscription.pages.confirmation'));
|
||||||
return self::getSubscriptionUrl($post, 'confirm', $subscriber);
|
return self::getSubscriptionUrl($post, 'confirm', $subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getManageUrl($subscriber = false) {
|
static function getManageUrl($subscriber = false) {
|
||||||
$post = get_post(Setting::getValue('subscription.manage_page'));
|
$post = get_post(Setting::getValue('subscription.pages.manage'));
|
||||||
return self::getSubscriptionUrl($post, 'manage', $subscriber);
|
return self::getSubscriptionUrl($post, 'manage', $subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getUnsubscribeUrl($subscriber = false) {
|
static function getUnsubscribeUrl($subscriber = false) {
|
||||||
$post = get_post(Setting::getValue('subscription.unsubscribe_page'));
|
$post = get_post(Setting::getValue('subscription.pages.unsubscribe'));
|
||||||
return self::getSubscriptionUrl($post, 'unsubscribe', $subscriber);
|
return self::getSubscriptionUrl($post, 'unsubscribe', $subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getSubscriptionUrl(
|
static function getSubscriptionUrl(
|
||||||
$post = null, $action = null, $subscriber = false
|
$post = null, $action = null, $subscriber = false
|
||||||
) {
|
) {
|
||||||
if($post === null || $action === null) return;
|
if($post === null || $action === null) return;
|
||||||
@ -28,22 +28,26 @@ class Url {
|
|||||||
$url = get_permalink($post);
|
$url = get_permalink($post);
|
||||||
|
|
||||||
if($subscriber !== false) {
|
if($subscriber !== false) {
|
||||||
|
|
||||||
if(is_object($subscriber)) {
|
if(is_object($subscriber)) {
|
||||||
$subscriber = $subscriber->asArray();
|
$subscriber = $subscriber->asArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = array(
|
$data = array(
|
||||||
'mailpoet_action='.$action,
|
'token' => Subscriber::generateToken($subscriber['email']),
|
||||||
'mailpoet_token='.Subscriber::generateToken($subscriber['email']),
|
'email' => $subscriber['email']
|
||||||
'mailpoet_email='.$subscriber['email']
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$params = array(
|
$data = array(
|
||||||
'mailpoet_action='.$action,
|
'preview' => 1
|
||||||
'mailpoet_preview=1'
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$params = array(
|
||||||
|
'endpoint=subscription',
|
||||||
|
'action='.$action,
|
||||||
|
'data='.base64_encode(serialize($data))
|
||||||
|
);
|
||||||
|
|
||||||
// add parameters
|
// add parameters
|
||||||
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
|
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?').join('&', $params);
|
||||||
|
|
||||||
|
@ -6,13 +6,7 @@ class Url {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function getCurrentUrl() {
|
static function getCurrentUrl() {
|
||||||
global $wp;
|
return home_url(add_query_arg(null, null));
|
||||||
return home_url(
|
|
||||||
add_query_arg(
|
|
||||||
$wp->query_string,
|
|
||||||
$wp->request
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function redirectTo($url = null) {
|
static function redirectTo($url = null) {
|
||||||
|
@ -209,13 +209,13 @@
|
|||||||
<select
|
<select
|
||||||
class="mailpoet_page_selection"
|
class="mailpoet_page_selection"
|
||||||
id="subscription_manage_page"
|
id="subscription_manage_page"
|
||||||
name="subscription[manage_page]"
|
name="subscription[pages][manage]"
|
||||||
>
|
>
|
||||||
<% for page in pages %>
|
<% for page in pages %>
|
||||||
<option
|
<option
|
||||||
value="<%= page.id %>"
|
value="<%= page.id %>"
|
||||||
data-preview-url="<%= page.preview_url|raw %>&mailpoet_action=manage"
|
data-preview-url="<%= page.url.manage|raw %>"
|
||||||
<% if(page.id == settings.subscription.manage_page) %>
|
<% if(page.id == settings.subscription.pages.manage) %>
|
||||||
selected="selected"
|
selected="selected"
|
||||||
<% endif %>
|
<% endif %>
|
||||||
><%= page.title %></option>
|
><%= page.title %></option>
|
||||||
@ -256,6 +256,8 @@
|
|||||||
<%= __('Unsubscribe page') %>
|
<%= __('Unsubscribe page') %>
|
||||||
<p class="description">
|
<p class="description">
|
||||||
<%= __('The page your subscribers see when they click on "Unsubscribe" in your emails.') %>
|
<%= __('The page your subscribers see when they click on "Unsubscribe" in your emails.') %>
|
||||||
|
<br />
|
||||||
|
<%= __('Use this shortcode in your own pages: [mailpoet_manage text="Manage your subscription"].') %>
|
||||||
</p>
|
</p>
|
||||||
</label>
|
</label>
|
||||||
</th>
|
</th>
|
||||||
@ -264,13 +266,13 @@
|
|||||||
<select
|
<select
|
||||||
class="mailpoet_page_selection"
|
class="mailpoet_page_selection"
|
||||||
id="subscription_unsubscribe_page"
|
id="subscription_unsubscribe_page"
|
||||||
name="subscription[unsubscribe_page]"
|
name="subscription[pages][unsubscribe]"
|
||||||
>
|
>
|
||||||
<% for page in pages %>
|
<% for page in pages %>
|
||||||
<option
|
<option
|
||||||
value="<%= page.id %>"
|
value="<%= page.id %>"
|
||||||
data-preview-url="<%= page.preview_url|raw %>&mailpoet_action=unsubscribe"
|
data-preview-url="<%= page.url.unsubscribe|raw %>"
|
||||||
<% if(page.id == settings.subscription.unsubscribe_page) %>
|
<% if(page.id == settings.subscription.pages.unsubscribe) %>
|
||||||
selected="selected"
|
selected="selected"
|
||||||
<% endif %>
|
<% endif %>
|
||||||
><%= page.title %></option>
|
><%= page.title %></option>
|
||||||
|
@ -151,13 +151,13 @@
|
|||||||
<p>
|
<p>
|
||||||
<select
|
<select
|
||||||
class="mailpoet_page_selection"
|
class="mailpoet_page_selection"
|
||||||
name="subscription[confirmation_page]"
|
name="subscription[pages][confirmation]"
|
||||||
>
|
>
|
||||||
<% for page in pages %>
|
<% for page in pages %>
|
||||||
<option
|
<option
|
||||||
value="<%= page.id %>"
|
value="<%= page.id %>"
|
||||||
data-preview-url="<%= page.preview_url|raw %>&mailpoet_action=confirm"
|
data-preview-url="<%= page.url.confirm|raw %>"
|
||||||
<% if(page.id == settings.subscription.confirmation_page) %>
|
<% if(page.id == settings.subscription.pages.confirmation) %>
|
||||||
selected="selected"
|
selected="selected"
|
||||||
<% endif %>
|
<% endif %>
|
||||||
><%= page.title %></option>
|
><%= page.title %></option>
|
||||||
|
Reference in New Issue
Block a user