Enables [mailpoet_manage] shortcode
Updates code
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<?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;
|
||||||
@@ -9,9 +10,6 @@ use MailPoet\Subscription\Pages;
|
|||||||
use MailPoet\WP\Hooks;
|
use MailPoet\WP\Hooks;
|
||||||
|
|
||||||
class Shortcodes {
|
class Shortcodes {
|
||||||
function __construct() {
|
|
||||||
}
|
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
// form widget shortcode
|
// form widget shortcode
|
||||||
add_shortcode('mailpoet_form', array($this, 'formWidget'));
|
add_shortcode('mailpoet_form', array($this, 'formWidget'));
|
||||||
@@ -36,16 +34,8 @@ class Shortcodes {
|
|||||||
$this, 'renderArchiveSubject'
|
$this, 'renderArchiveSubject'
|
||||||
), 2, 3);
|
), 2, 3);
|
||||||
|
|
||||||
// subscription management
|
// initialize subscription management shortcodes
|
||||||
add_shortcode('mailpoet_manage_subscription', array(
|
|
||||||
$this,
|
|
||||||
'getManageContent'
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getManageContent() {
|
|
||||||
$subscription_page = new Pages();
|
$subscription_page = new Pages();
|
||||||
return $subscription_page->getManageContent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formWidget($params = array()) {
|
function formWidget($params = array()) {
|
||||||
|
@@ -27,16 +27,16 @@ class Subscription {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function confirm() {
|
function confirm() {
|
||||||
$subscription = new UserSubscription\Pages('confirm', $this->data);
|
$subscription = new UserSubscription\Pages('confirm', $this->data, true);
|
||||||
$subscription->confirm();
|
$subscription->confirm();
|
||||||
}
|
}
|
||||||
|
|
||||||
function manage() {
|
function manage() {
|
||||||
$subscription = new UserSubscription\Pages('manage', $this->data);
|
$subscription = new UserSubscription\Pages('manage', $this->data, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function unsubscribe() {
|
function unsubscribe() {
|
||||||
$subscription = new UserSubscription\Pages('unsubscribe', $this->data);
|
$subscription = new UserSubscription\Pages('unsubscribe', $this->data, true);
|
||||||
$subscription->unsubscribe();
|
$subscription->unsubscribe();
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -23,32 +23,26 @@ class Pages {
|
|||||||
private $data;
|
private $data;
|
||||||
private $subscriber;
|
private $subscriber;
|
||||||
|
|
||||||
function __construct($action = false, $data = array()) {
|
function __construct($action = false, $data = array(), $init_page_filters = false) {
|
||||||
$this->action = $action;
|
$this->action = $action;
|
||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
$this->subscriber = $this->getSubscriber();
|
$this->subscriber = $this->getSubscriber();
|
||||||
|
|
||||||
// handle subscription pages title & content
|
// handle subscription pages title & content
|
||||||
|
if($init_page_filters) {
|
||||||
add_filter('wp_title', array($this,'setWindowTitle'), 10, 3);
|
add_filter('wp_title', array($this,'setWindowTitle'), 10, 3);
|
||||||
add_filter('document_title_parts', array($this,'setWindowTitleParts'), 10, 1);
|
add_filter('document_title_parts', array($this,'setWindowTitleParts'), 10, 1);
|
||||||
add_filter('the_title', array($this,'setPageTitle'), 10, 1);
|
add_filter('the_title', array($this,'setPageTitle'), 10, 1);
|
||||||
add_filter('the_content', array($this,'setPageContent'), 10, 1);
|
add_filter('the_content', array($this,'setPageContent'), 10, 1);
|
||||||
|
}
|
||||||
|
|
||||||
// manage subscription link shortcode
|
// manage subscription link shortcodes
|
||||||
// [mailpoet_manage text="Manage your subscription"]
|
|
||||||
if(!shortcode_exists('mailpoet_manage')) {
|
|
||||||
add_shortcode('mailpoet_manage', array($this, 'getManageLink'));
|
add_shortcode('mailpoet_manage', array($this, 'getManageLink'));
|
||||||
}
|
|
||||||
if(!shortcode_exists('mailpoet_manage_subscription')) {
|
|
||||||
add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent'));
|
add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent'));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private function isPreview() {
|
private function isPreview() {
|
||||||
return (
|
return (array_key_exists('preview', $_GET) || array_key_exists('preview', $this->data));
|
||||||
array_key_exists('preview', $_GET)
|
|
||||||
|| array_key_exists('preview', $this->data)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSubscriber() {
|
function getSubscriber() {
|
||||||
@@ -234,7 +228,7 @@ class Pages {
|
|||||||
->withCustomFields()
|
->withCustomFields()
|
||||||
->withSubscriptions();
|
->withSubscriptions();
|
||||||
} else {
|
} else {
|
||||||
return __('You need to be logged in or be a subscriber to our mailing lists to see this page.', 'mailpoet');
|
return __('Subscription management form is only available to mailing lists subscribers.', 'mailpoet');
|
||||||
}
|
}
|
||||||
|
|
||||||
$custom_fields = array_map(function($custom_field) use($subscriber) {
|
$custom_fields = array_map(function($custom_field) use($subscriber) {
|
||||||
@@ -418,6 +412,8 @@ class Pages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getManageLink($params) {
|
function getManageLink($params) {
|
||||||
|
if(!$this->subscriber) return __('Link to subscription management page is only available to mailing lists subscribers.', 'mailpoet');
|
||||||
|
|
||||||
// get label or display default label
|
// get label or display default label
|
||||||
$text = (
|
$text = (
|
||||||
isset($params['text'])
|
isset($params['text'])
|
||||||
|
@@ -52,7 +52,7 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
expect($request_data['newsletter_hash'])->equals($this->newsletter->hash);
|
expect($request_data['newsletter_hash'])->equals($this->newsletter->hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDisplaysManageSubscriptionPageForLoggedinExistingUsers() {
|
function testItDisplaysManageSubscriptionFormForLoggedinExistingUsers() {
|
||||||
$wp_user = wp_set_current_user(1);
|
$wp_user = wp_set_current_user(1);
|
||||||
expect(is_user_logged_in())->true();
|
expect(is_user_logged_in())->true();
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = Subscriber::create();
|
||||||
@@ -60,24 +60,69 @@ class ShortcodesTest extends \MailPoetTest {
|
|||||||
$subscriber->email = $wp_user->data->user_email;
|
$subscriber->email = $wp_user->data->user_email;
|
||||||
$subscriber->wp_user_id = $wp_user->ID;
|
$subscriber->wp_user_id = $wp_user->ID;
|
||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
|
|
||||||
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
$result = do_shortcode('[mailpoet_manage_subscription]');
|
$result = do_shortcode('[mailpoet_manage_subscription]');
|
||||||
expect($result)->contains('form method="POST"');
|
expect($result)->contains('form method="POST"');
|
||||||
expect($result)->contains($subscriber->email);
|
expect($result)->contains($subscriber->email);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotDisplayManageSubscriptionPageForLoggedinNonexistentSubscribers() {
|
function testItDoesNotDisplayManageSubscriptionFormForLoggedinNonexistentSubscribers() {
|
||||||
$wp_user = wp_set_current_user(1);
|
$wp_user = wp_set_current_user(1);
|
||||||
expect(is_user_logged_in())->true();
|
expect(is_user_logged_in())->true();
|
||||||
expect(Subscriber::findOne($wp_user->data->user_email))->false();
|
expect(Subscriber::findOne($wp_user->data->user_email))->false();
|
||||||
|
|
||||||
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
$result = do_shortcode('[mailpoet_manage_subscription]');
|
$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.');
|
expect($result)->contains('Subscription management form is only available to mailing lists subscribers.');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDisplayManageSubscriptionFormForLoggedOutUsers() {
|
||||||
|
wp_set_current_user(0);
|
||||||
|
expect(is_user_logged_in())->false();
|
||||||
|
|
||||||
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
|
$result = do_shortcode('[mailpoet_manage_subscription]');
|
||||||
|
expect($result)->contains('Subscription management form is only available to mailing lists subscribers.');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDisplaysLinkToManageSubscriptionPageForLoggedinExistingUsers() {
|
||||||
|
$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();
|
||||||
|
|
||||||
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
|
$result = do_shortcode('[mailpoet_manage]');
|
||||||
|
expect($result)->contains('Manage your subscription');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotDisplayLinkToManageSubscriptionPageForLoggedinNonexistentSubscribers() {
|
||||||
|
$wp_user = wp_set_current_user(1);
|
||||||
|
expect(is_user_logged_in())->true();
|
||||||
|
expect(Subscriber::findOne($wp_user->data->user_email))->false();
|
||||||
|
|
||||||
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
|
$result = do_shortcode('[mailpoet_manage]');
|
||||||
|
expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotDisplayManageSubscriptionPageForLoggedOutUsers() {
|
function testItDoesNotDisplayManageSubscriptionPageForLoggedOutUsers() {
|
||||||
wp_set_current_user(0);
|
wp_set_current_user(0);
|
||||||
expect(is_user_logged_in())->false();
|
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.');
|
$shortcodes = new Shortcodes();
|
||||||
|
$shortcodes->init();
|
||||||
|
$result = do_shortcode('[mailpoet_manage]');
|
||||||
|
expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.');
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
|
Reference in New Issue
Block a user