Allows using manage_subscription shortcode outside of newsletters
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Config;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberSegment;
|
||||
use MailPoet\Newsletter\Url as NewsletterUrl;
|
||||
use MailPoet\Subscription\Pages;
|
||||
use MailPoet\WP\Hooks;
|
||||
|
||||
class Shortcodes {
|
||||
@ -33,6 +35,17 @@ class Shortcodes {
|
||||
Hooks::addFilter('mailpoet_archive_subject', array(
|
||||
$this, 'renderArchiveSubject'
|
||||
), 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()) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Subscription;
|
||||
|
||||
use MailPoet\Models\Subscriber;
|
||||
@ -14,12 +15,15 @@ use MailPoet\Form\Block\Date as FormBlockDate;
|
||||
|
||||
class Pages {
|
||||
const DEMO_EMAIL = 'demo@mailpoet.com';
|
||||
const ACTION_CONFIRM = 'confirm';
|
||||
const ACTION_MANAGE = 'manage';
|
||||
const ACTION_UNSUBSCRIBE = 'unsubscribe';
|
||||
|
||||
private $action;
|
||||
private $data;
|
||||
private $subscriber;
|
||||
|
||||
function __construct($action, $data = array()) {
|
||||
function __construct($action = false, $data = array()) {
|
||||
$this->action = $action;
|
||||
$this->data = $data;
|
||||
$this->subscriber = $this->getSubscriber();
|
||||
@ -32,9 +36,13 @@ class Pages {
|
||||
|
||||
// manage subscription link shortcode
|
||||
// [mailpoet_manage text="Manage your subscription"]
|
||||
if(!shortcode_exists('mailpoet_manage')) {
|
||||
add_shortcode('mailpoet_manage', array($this, 'getManageLink'));
|
||||
}
|
||||
if(!shortcode_exists('mailpoet_manage_subscription')) {
|
||||
add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent'));
|
||||
}
|
||||
}
|
||||
|
||||
private function isPreview() {
|
||||
return (
|
||||
@ -46,14 +54,15 @@ class Pages {
|
||||
function getSubscriber() {
|
||||
$token = (isset($this->data['token'])) ? $this->data['token'] : null;
|
||||
$email = (isset($this->data['email'])) ? $this->data['email'] : null;
|
||||
$wp_user = wp_get_current_user();
|
||||
|
||||
if(Subscriber::generateToken($email) === $token) {
|
||||
$subscriber = Subscriber::findOne($email);
|
||||
if($subscriber !== false) {
|
||||
return $subscriber;
|
||||
if(!$email && $wp_user->exists()) {
|
||||
return Subscriber::where('wp_user_id', $wp_user->ID)->findOne();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
return (Subscriber::generateToken($email) === $token) ?
|
||||
Subscriber::findOne($email) :
|
||||
false;
|
||||
}
|
||||
|
||||
function confirm() {
|
||||
@ -113,13 +122,13 @@ class Pages {
|
||||
} else {
|
||||
// when it's our own page, generate page title based on requested action
|
||||
switch($this->action) {
|
||||
case 'confirm':
|
||||
case self::ACTION_CONFIRM:
|
||||
return $this->getConfirmTitle();
|
||||
|
||||
case 'manage':
|
||||
case self::ACTION_MANAGE:
|
||||
return $this->getManageTitle();
|
||||
|
||||
case 'unsubscribe':
|
||||
case self::ACTION_UNSUBSCRIBE:
|
||||
return $this->getUnsubscribeTitle();
|
||||
}
|
||||
}
|
||||
@ -137,13 +146,13 @@ class Pages {
|
||||
$content = '';
|
||||
|
||||
switch($this->action) {
|
||||
case 'confirm':
|
||||
case self::ACTION_CONFIRM:
|
||||
$content = $this->getConfirmContent();
|
||||
break;
|
||||
case 'manage':
|
||||
case self::ACTION_MANAGE:
|
||||
$content = $this->getManageContent();
|
||||
break;
|
||||
case 'unsubscribe':
|
||||
case self::ACTION_UNSUBSCRIBE:
|
||||
$content = $this->getUnsubscribeContent();
|
||||
break;
|
||||
}
|
||||
@ -225,7 +234,7 @@ class Pages {
|
||||
->withCustomFields()
|
||||
->withSubscriptions();
|
||||
} 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) {
|
||||
|
@ -1,10 +1,12 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Config;
|
||||
|
||||
use Codeception\Util\Fixtures;
|
||||
use Helper\WordPress;
|
||||
use MailPoet\Config\Shortcodes;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Newsletter\Url;
|
||||
use MailPoet\Router\Router;
|
||||
|
||||
@ -49,4 +51,38 @@ class ShortcodesTest extends \MailPoetTest {
|
||||
expect($request_data['newsletter_id'])->isEmpty();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user