Allows using manage_subscription shortcode outside of newsletters

This commit is contained in:
Vlad
2017-11-28 18:31:52 -05:00
parent fbf58f23fc
commit 63ed835d64
3 changed files with 75 additions and 17 deletions

View File

@ -1,9 +1,11 @@
<?php <?php
namespace MailPoet\Config; 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\Newsletter\Url as NewsletterUrl; use MailPoet\Newsletter\Url as NewsletterUrl;
use MailPoet\Subscription\Pages;
use MailPoet\WP\Hooks; use MailPoet\WP\Hooks;
class Shortcodes { class Shortcodes {
@ -33,6 +35,17 @@ class Shortcodes {
Hooks::addFilter('mailpoet_archive_subject', array( Hooks::addFilter('mailpoet_archive_subject', array(
$this, 'renderArchiveSubject' $this, 'renderArchiveSubject'
), 2, 3); ), 2, 3);
// subscription management
add_shortcode('mailpoet_manage_subscription', array(
$this,
'getManageContent'
));
}
function getManageContent() {
$subscription_page = new Pages();
return $subscription_page->getManageContent();
} }
function formWidget($params = array()) { function formWidget($params = array()) {

View File

@ -1,4 +1,5 @@
<?php <?php
namespace MailPoet\Subscription; namespace MailPoet\Subscription;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
@ -14,12 +15,15 @@ use MailPoet\Form\Block\Date as FormBlockDate;
class Pages { class Pages {
const DEMO_EMAIL = 'demo@mailpoet.com'; const DEMO_EMAIL = 'demo@mailpoet.com';
const ACTION_CONFIRM = 'confirm';
const ACTION_MANAGE = 'manage';
const ACTION_UNSUBSCRIBE = 'unsubscribe';
private $action; private $action;
private $data; private $data;
private $subscriber; private $subscriber;
function __construct($action, $data = array()) { function __construct($action = false, $data = array()) {
$this->action = $action; $this->action = $action;
$this->data = $data; $this->data = $data;
$this->subscriber = $this->getSubscriber(); $this->subscriber = $this->getSubscriber();
@ -32,8 +36,12 @@ class Pages {
// manage subscription link shortcode // manage subscription link shortcode
// [mailpoet_manage text="Manage your subscription"] // [mailpoet_manage text="Manage your subscription"]
add_shortcode('mailpoet_manage', array($this, 'getManageLink')); if(!shortcode_exists('mailpoet_manage')) {
add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent')); add_shortcode('mailpoet_manage', array($this, 'getManageLink'));
}
if(!shortcode_exists('mailpoet_manage_subscription')) {
add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent'));
}
} }
private function isPreview() { private function isPreview() {
@ -46,14 +54,15 @@ class Pages {
function getSubscriber() { function getSubscriber() {
$token = (isset($this->data['token'])) ? $this->data['token'] : null; $token = (isset($this->data['token'])) ? $this->data['token'] : null;
$email = (isset($this->data['email'])) ? $this->data['email'] : null; $email = (isset($this->data['email'])) ? $this->data['email'] : null;
$wp_user = wp_get_current_user();
if(Subscriber::generateToken($email) === $token) { if(!$email && $wp_user->exists()) {
$subscriber = Subscriber::findOne($email); return Subscriber::where('wp_user_id', $wp_user->ID)->findOne();
if($subscriber !== false) {
return $subscriber;
}
} }
return false;
return (Subscriber::generateToken($email) === $token) ?
Subscriber::findOne($email) :
false;
} }
function confirm() { function confirm() {
@ -113,13 +122,13 @@ class Pages {
} else { } else {
// when it's our own page, generate page title based on requested action // when it's our own page, generate page title based on requested action
switch($this->action) { switch($this->action) {
case 'confirm': case self::ACTION_CONFIRM:
return $this->getConfirmTitle(); return $this->getConfirmTitle();
case 'manage': case self::ACTION_MANAGE:
return $this->getManageTitle(); return $this->getManageTitle();
case 'unsubscribe': case self::ACTION_UNSUBSCRIBE:
return $this->getUnsubscribeTitle(); return $this->getUnsubscribeTitle();
} }
} }
@ -137,13 +146,13 @@ class Pages {
$content = ''; $content = '';
switch($this->action) { switch($this->action) {
case 'confirm': case self::ACTION_CONFIRM:
$content = $this->getConfirmContent(); $content = $this->getConfirmContent();
break; break;
case 'manage': case self::ACTION_MANAGE:
$content = $this->getManageContent(); $content = $this->getManageContent();
break; break;
case 'unsubscribe': case self::ACTION_UNSUBSCRIBE:
$content = $this->getUnsubscribeContent(); $content = $this->getUnsubscribeContent();
break; break;
} }
@ -225,7 +234,7 @@ class Pages {
->withCustomFields() ->withCustomFields()
->withSubscriptions(); ->withSubscriptions();
} else { } else {
return; return __('You need to be logged in or be a subscriber to our mailing lists to see this page.', 'mailpoet');
} }
$custom_fields = array_map(function($custom_field) use($subscriber) { $custom_fields = array_map(function($custom_field) use($subscriber) {
@ -420,4 +429,4 @@ class Pages {
$this->subscriber $this->subscriber
).'">'.$text.'</a>'; ).'">'.$text.'</a>';
} }
} }

View File

@ -1,10 +1,12 @@
<?php <?php
namespace MailPoet\Test\Config; namespace MailPoet\Test\Config;
use Codeception\Util\Fixtures;
use Helper\WordPress; use Helper\WordPress;
use MailPoet\Config\Shortcodes; use MailPoet\Config\Shortcodes;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
use MailPoet\Models\Subscriber;
use MailPoet\Newsletter\Url; use MailPoet\Newsletter\Url;
use MailPoet\Router\Router; use MailPoet\Router\Router;
@ -49,4 +51,38 @@ class ShortcodesTest extends \MailPoetTest {
expect($request_data['newsletter_id'])->isEmpty(); expect($request_data['newsletter_id'])->isEmpty();
expect($request_data['newsletter_hash'])->equals($this->newsletter->hash); expect($request_data['newsletter_hash'])->equals($this->newsletter->hash);
} }
function testItDisplaysManageSubscriptionPageForLoggedinExistingUsers() {
$wp_user = wp_set_current_user(1);
expect(is_user_logged_in())->true();
$subscriber = Subscriber::create();
$subscriber->hydrate(Fixtures::get('subscriber_template'));
$subscriber->email = $wp_user->data->user_email;
$subscriber->wp_user_id = $wp_user->ID;
$subscriber->save();
$result = do_shortcode('[mailpoet_manage_subscription]');
expect($result)->contains('form method="POST"');
expect($result)->contains($subscriber->email);
}
function testItDoesNotDisplayManageSubscriptionPageForLoggedinNonexistentSubscribers() {
$wp_user = wp_set_current_user(1);
expect(is_user_logged_in())->true();
expect(Subscriber::findOne($wp_user->data->user_email))->false();
$result = do_shortcode('[mailpoet_manage_subscription]');
expect($result)->contains('You need to be logged in or be a subscriber to our mailing lists to see this page.');
}
function testItDoesNotDisplayManageSubscriptionPageForLoggedOutUsers() {
wp_set_current_user(0);
expect(is_user_logged_in())->false();
$result = do_shortcode('[mailpoet_manage_subscription]');
expect($result)->contains('You need to be logged in or be a subscriber to our mailing lists to see this page.');
}
function _after() {
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
\ORM::raw_execute('TRUNCATE ' . Newsletter::$_table);
\ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
}
} }