Refactor subscription pages and config shortcodes to DI [MAILPOET-2208]

This commit is contained in:
wxa
2019-07-17 20:48:59 +03:00
committed by M. Shull
parent 160d3d0607
commit a2df2cc7bd
8 changed files with 164 additions and 76 deletions

View File

@ -55,6 +55,9 @@ class Initializer {
/** @var PermanentNotices */ /** @var PermanentNotices */
private $permanent_notices; private $permanent_notices;
/** @var Shortcodes */
private $shortcodes;
const INITIALIZED = 'MAILPOET_INITIALIZED'; const INITIALIZED = 'MAILPOET_INITIALIZED';
function __construct( function __construct(
@ -68,7 +71,8 @@ class Initializer {
Changelog $changelog, Changelog $changelog,
Menu $menu, Menu $menu,
CronTrigger $cron_trigger, CronTrigger $cron_trigger,
PermanentNotices $permanent_notices PermanentNotices $permanent_notices,
Shortcodes $shortcodes
) { ) {
$this->renderer_factory = $renderer_factory; $this->renderer_factory = $renderer_factory;
$this->access_control = $access_control; $this->access_control = $access_control;
@ -81,6 +85,7 @@ class Initializer {
$this->menu = $menu; $this->menu = $menu;
$this->cron_trigger = $cron_trigger; $this->cron_trigger = $cron_trigger;
$this->permanent_notices = $permanent_notices; $this->permanent_notices = $permanent_notices;
$this->shortcodes = $shortcodes;
} }
function init() { function init() {
@ -248,8 +253,7 @@ class Initializer {
} }
function setupShortcodes() { function setupShortcodes() {
$shortcodes = new Shortcodes(); $this->shortcodes->init();
$shortcodes->init();
} }
function setupImages() { function setupImages() {

View File

@ -2,6 +2,7 @@
namespace MailPoet\Config; namespace MailPoet\Config;
use MailPoet\Form\Widget;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment; use MailPoet\Models\SubscriberSegment;
@ -10,26 +11,31 @@ use MailPoet\Subscription\Pages;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
class Shortcodes { class Shortcodes {
/** @var Pages */
private $subscription_pages;
/** @var WPFunctions */
private $wp; private $wp;
function __construct() { function __construct(Pages $subscription_pages, WPFunctions $wp) {
$this->wp = new WPFunctions; $this->subscription_pages = $subscription_pages;
$this->wp = $wp;
} }
function init() { function init() {
// form widget shortcode // form widget shortcode
WPFunctions::get()->addShortcode('mailpoet_form', [$this, 'formWidget']); $this->wp->addShortcode('mailpoet_form', [$this, 'formWidget']);
// subscribers count shortcode // subscribers count shortcode
WPFunctions::get()->addShortcode('mailpoet_subscribers_count', [ $this->wp->addShortcode('mailpoet_subscribers_count', [
$this, 'getSubscribersCount', $this, 'getSubscribersCount',
]); ]);
WPFunctions::get()->addShortcode('wysija_subscribers_count', [ $this->wp->addShortcode('wysija_subscribers_count', [
$this, 'getSubscribersCount', $this, 'getSubscribersCount',
]); ]);
// archives page // archives page
WPFunctions::get()->addShortcode('mailpoet_archive', [ $this->wp->addShortcode('mailpoet_archive', [
$this, 'getArchive', $this, 'getArchive',
]); ]);
@ -39,18 +45,18 @@ class Shortcodes {
$this->wp->addFilter('mailpoet_archive_subject', [ $this->wp->addFilter('mailpoet_archive_subject', [
$this, 'renderArchiveSubject', $this, 'renderArchiveSubject',
], 2, 3); ], 2, 3);
// initialize subscription pages data
$this->subscription_pages->init();
// initialize subscription management shortcodes // initialize subscription management shortcodes
$subscription_page = new Pages(); $this->subscription_pages->initShortcodes();
$subscription_page->initShortcodes();
} }
function formWidget($params = []) { function formWidget($params = []) {
// IMPORTANT: fixes conflict with MagicMember // IMPORTANT: fixes conflict with MagicMember
WPFunctions::get()->removeShortcode('user_list'); $this->wp->removeShortcode('user_list');
if (isset($params['id']) && (int)$params['id'] > 0) { if (isset($params['id']) && (int)$params['id'] > 0) {
$form_widget = new \MailPoet\Form\Widget(); $form_widget = new Widget();
return $form_widget->widget([ return $form_widget->widget([
'form' => (int)$params['id'], 'form' => (int)$params['id'],
'form_type' => 'shortcode', 'form_type' => 'shortcode',
@ -66,9 +72,9 @@ class Shortcodes {
} }
if (empty($segment_ids)) { if (empty($segment_ids)) {
return WPFunctions::get()->numberFormatI18n(Subscriber::filter('subscribed')->count()); return $this->wp->numberFormatI18n(Subscriber::filter('subscribed')->count());
} else { } else {
return WPFunctions::get()->numberFormatI18n( return $this->wp->numberFormatI18n(
SubscriberSegment::whereIn('segment_id', $segment_ids) SubscriberSegment::whereIn('segment_id', $segment_ids)
->select('subscriber_id')->distinct() ->select('subscriber_id')->distinct()
->filter('subscribed') ->filter('subscribed')
@ -94,7 +100,7 @@ class Shortcodes {
if (empty($newsletters)) { if (empty($newsletters)) {
return $this->wp->applyFilters( return $this->wp->applyFilters(
'mailpoet_archive_no_newsletters', 'mailpoet_archive_no_newsletters',
WPFunctions::get()->__('Oops! There are no newsletters to display.', 'mailpoet') $this->wp->__('Oops! There are no newsletters to display.', 'mailpoet')
); );
} else { } else {
$title = $this->wp->applyFilters('mailpoet_archive_title', ''); $title = $this->wp->applyFilters('mailpoet_archive_title', '');
@ -119,8 +125,8 @@ class Shortcodes {
} }
function renderArchiveDate($newsletter) { function renderArchiveDate($newsletter) {
return WPFunctions::get()->dateI18n( return $this->wp->dateI18n(
WPFunctions::get()->getOption('date_format'), $this->wp->getOption('date_format'),
strtotime($newsletter->processed_at) strtotime($newsletter->processed_at)
); );
} }

View File

@ -64,6 +64,8 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Config\Menu::class)->setPublic(true); $container->autowire(\MailPoet\Config\Menu::class)->setPublic(true);
$container->autowire(\MailPoet\Config\RendererFactory::class)->setPublic(true); $container->autowire(\MailPoet\Config\RendererFactory::class)->setPublic(true);
$container->autowire(\MailPoet\Config\ServicesChecker::class); $container->autowire(\MailPoet\Config\ServicesChecker::class);
$container->autowire(\MailPoet\Config\Shortcodes::class)
->setShared(false);
$container->register(\MailPoet\Config\Renderer::class) $container->register(\MailPoet\Config\Renderer::class)
->setPublic(true) ->setPublic(true)
->setFactory([new Reference(\MailPoet\Config\RendererFactory::class), 'getRenderer']); ->setFactory([new Reference(\MailPoet\Config\RendererFactory::class), 'getRenderer']);
@ -121,6 +123,8 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Subscription\Comment::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Comment::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\Manage::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Manage::class)->setPublic(true);
$container->autowire(\MailPoet\Subscription\Pages::class)->setPublic(true)
->setShared(false);
$container->autowire(\MailPoet\Subscription\Registration::class)->setPublic(true); $container->autowire(\MailPoet\Subscription\Registration::class)->setPublic(true);
// Newsletter // Newsletter
$container->autowire(\MailPoet\Newsletter\AutomatedLatestContent::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\AutomatedLatestContent::class)->setPublic(true);

View File

@ -21,6 +21,13 @@ class Subscription {
'global' => AccessControl::NO_ACCESS_RESTRICTION, 'global' => AccessControl::NO_ACCESS_RESTRICTION,
]; ];
/** @var UserSubscription\Pages */
private $subscription_pages;
function __construct(UserSubscription\Pages $subscription_pages) {
$this->subscription_pages = $subscription_pages;
}
function confirm($data) { function confirm($data) {
$subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_CONFIRM, $data); $subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_CONFIRM, $data);
$subscription->confirm(); $subscription->confirm();
@ -36,6 +43,6 @@ class Subscription {
} }
private function initSubscriptionPage($action, $data) { private function initSubscriptionPage($action, $data) {
return new UserSubscription\Pages($action, $data, true, true); return $this->subscription_pages->init($action, $data, true, true);
} }
} }

View File

@ -24,27 +24,38 @@ class Pages {
private $action; private $action;
private $data; private $data;
private $subscriber; private $subscriber;
/** @var NewSubscriberNotificationMailer */ /** @var NewSubscriberNotificationMailer */
private $new_subscriber_notification_sender; private $new_subscriber_notification_sender;
/** @var SettingsController */ /** @var SettingsController */
private $settings; private $settings;
/** @var UrlHelper */ /** @var UrlHelper */
private $url_helper; private $url_helper;
function __construct($action = false, $data = [], $init_shortcodes = false, $init_page_filters = false, $new_subscriber_notification_sender = null) { /** @var WPFunctions */
private $wp;
function __construct(
NewSubscriberNotificationMailer $new_subscriber_notification_sender,
WPFunctions $wp,
SettingsController $settings,
UrlHelper $url_helper
) {
$this->wp = $wp;
$this->new_subscriber_notification_sender = $new_subscriber_notification_sender;
$this->settings = $settings;
$this->url_helper = $url_helper;
}
function init($action = false, $data = [], $init_shortcodes = false, $init_page_filters = false) {
$this->action = $action; $this->action = $action;
$this->data = $data; $this->data = $data;
$this->subscriber = $this->getSubscriber(); $this->subscriber = $this->getSubscriber();
if ($init_page_filters) $this->initPageFilters(); if ($init_page_filters) $this->initPageFilters();
if ($init_shortcodes) $this->initShortcodes(); if ($init_shortcodes) $this->initShortcodes();
if ($new_subscriber_notification_sender) { return $this;
$this->new_subscriber_notification_sender = $new_subscriber_notification_sender;
} else {
$this->new_subscriber_notification_sender = new NewSubscriberNotificationMailer();
}
$this->settings = new SettingsController();
$this->url_helper = new UrlHelper(new WPFunctions());
} }
private function isPreview() { private function isPreview() {
@ -52,21 +63,25 @@ class Pages {
} }
function initPageFilters() { function initPageFilters() {
WPFunctions::get()->addFilter('wp_title', [$this,'setWindowTitle'], 10, 3); $this->wp->addFilter('wp_title', [$this,'setWindowTitle'], 10, 3);
WPFunctions::get()->addFilter('document_title_parts', [$this,'setWindowTitleParts'], 10, 1); $this->wp->addFilter('document_title_parts', [$this,'setWindowTitleParts'], 10, 1);
WPFunctions::get()->addFilter('the_title', [$this,'setPageTitle'], 10, 1); $this->wp->addFilter('the_title', [$this,'setPageTitle'], 10, 1);
WPFunctions::get()->addFilter('the_content', [$this,'setPageContent'], 10, 1); $this->wp->addFilter('the_content', [$this,'setPageContent'], 10, 1);
} }
function initShortcodes() { function initShortcodes() {
WPFunctions::get()->addShortcode('mailpoet_manage', [$this, 'getManageLink']); $this->wp->addShortcode('mailpoet_manage', [$this, 'getManageLink']);
WPFunctions::get()->addShortcode('mailpoet_manage_subscription', [$this, 'getManageContent']); $this->wp->addShortcode('mailpoet_manage_subscription', [$this, 'getManageContent']);
} }
function getSubscriber() { private function getSubscriber() {
if (!is_null($this->subscriber)) {
return $this->subscriber;
}
$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 = WPFunctions::get()->wpGetCurrentUser(); $wp_user = $this->wp->wpGetCurrentUser();
if (!$email && $wp_user->exists()) { if (!$email && $wp_user->exists()) {
return Subscriber::where('wp_user_id', $wp_user->ID)->findOne(); return Subscriber::where('wp_user_id', $wp_user->ID)->findOne();
@ -82,6 +97,7 @@ class Pages {
} }
function confirm() { function confirm() {
$this->subscriber = $this->getSubscriber();
if ($this->subscriber === false || $this->subscriber->status === Subscriber::STATUS_SUBSCRIBED) { if ($this->subscriber === false || $this->subscriber->status === Subscriber::STATUS_SUBSCRIBED) {
return false; return false;
} }
@ -130,13 +146,13 @@ class Pages {
global $post; global $post;
if ($this->isPreview() === false && $this->subscriber === false) { if ($this->isPreview() === false && $this->subscriber === false) {
return WPFunctions::get()->__("Hmmm... we don't have a record of you.", 'mailpoet'); return $this->wp->__("Hmmm... we don't have a record of you.", 'mailpoet');
} }
if ( if (
($post->post_title !== WPFunctions::get()->__('MailPoet Page', 'mailpoet')) ($post->post_title !== $this->wp->__('MailPoet Page', 'mailpoet'))
|| ||
($page_title !== WPFunctions::get()->singlePostTitle('', false)) ($page_title !== $this->wp->singlePostTitle('', false))
) { ) {
// when it's a custom page, just return the original page title // when it's a custom page, just return the original page title
return $page_title; return $page_title;
@ -160,7 +176,7 @@ class Pages {
// if we're not in preview mode and the subscriber does not exist // if we're not in preview mode and the subscriber does not exist
if ($this->isPreview() === false && $this->subscriber === false) { if ($this->isPreview() === false && $this->subscriber === false) {
return WPFunctions::get()->__("Your email address doesn't appear in our lists anymore. Sign up again or contact us if this appears to be a mistake.", 'mailpoet'); return $this->wp->__("Your email address doesn't appear in our lists anymore. Sign up again or contact us if this appears to be a mistake.", 'mailpoet');
} }
if (strpos($page_content, '[mailpoet_page]') !== false) { if (strpos($page_content, '[mailpoet_page]') !== false) {
@ -204,7 +220,7 @@ class Pages {
private function getConfirmTitle() { private function getConfirmTitle() {
if ($this->isPreview()) { if ($this->isPreview()) {
$title = sprintf( $title = sprintf(
WPFunctions::get()->__("You have subscribed to: %s", 'mailpoet'), $this->wp->__("You have subscribed to: %s", 'mailpoet'),
'demo 1, demo 2' 'demo 1, demo 2'
); );
} else { } else {
@ -213,10 +229,10 @@ class Pages {
}, $this->subscriber->segments()->findMany()); }, $this->subscriber->segments()->findMany());
if (empty($segment_names)) { if (empty($segment_names)) {
$title = WPFunctions::get()->__("You are now subscribed!", 'mailpoet'); $title = $this->wp->__("You are now subscribed!", 'mailpoet');
} else { } else {
$title = sprintf( $title = sprintf(
WPFunctions::get()->__("You have subscribed to: %s", 'mailpoet'), $this->wp->__("You have subscribed to: %s", 'mailpoet'),
join(', ', $segment_names) join(', ', $segment_names)
); );
} }
@ -226,19 +242,19 @@ class Pages {
private function getManageTitle() { private function getManageTitle() {
if ($this->isPreview() || $this->subscriber !== false) { if ($this->isPreview() || $this->subscriber !== false) {
return WPFunctions::get()->__("Manage your subscription", 'mailpoet'); return $this->wp->__("Manage your subscription", 'mailpoet');
} }
} }
private function getUnsubscribeTitle() { private function getUnsubscribeTitle() {
if ($this->isPreview() || $this->subscriber !== false) { if ($this->isPreview() || $this->subscriber !== false) {
return WPFunctions::get()->__("You are now unsubscribed.", 'mailpoet'); return $this->wp->__("You are now unsubscribed.", 'mailpoet');
} }
} }
private function getConfirmContent() { private function getConfirmContent() {
if ($this->isPreview() || $this->subscriber !== false) { if ($this->isPreview() || $this->subscriber !== false) {
return WPFunctions::get()->__("Yup, we've added you to our email list. You'll hear from us shortly.", 'mailpoet'); return $this->wp->__("Yup, we've added you to our email list. You'll hear from us shortly.", 'mailpoet');
} }
} }
@ -255,7 +271,7 @@ class Pages {
->withCustomFields() ->withCustomFields()
->withSubscriptions(); ->withSubscriptions();
} else { } else {
return WPFunctions::get()->__('Subscription management form is only available to mailing lists subscribers.', 'mailpoet'); return $this->wp->__('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) {
@ -305,7 +321,7 @@ class Pages {
'id' => 'first_name', 'id' => 'first_name',
'type' => 'text', 'type' => 'text',
'params' => [ 'params' => [
'label' => WPFunctions::get()->__('First name', 'mailpoet'), 'label' => $this->wp->__('First name', 'mailpoet'),
'value' => $subscriber->first_name, 'value' => $subscriber->first_name,
'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()), 'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()),
], ],
@ -314,7 +330,7 @@ class Pages {
'id' => 'last_name', 'id' => 'last_name',
'type' => 'text', 'type' => 'text',
'params' => [ 'params' => [
'label' => WPFunctions::get()->__('Last name', 'mailpoet'), 'label' => $this->wp->__('Last name', 'mailpoet'),
'value' => $subscriber->last_name, 'value' => $subscriber->last_name,
'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()), 'disabled' => ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()),
], ],
@ -324,11 +340,11 @@ class Pages {
'type' => 'select', 'type' => 'select',
'params' => [ 'params' => [
'required' => true, 'required' => true,
'label' => WPFunctions::get()->__('Status', 'mailpoet'), 'label' => $this->wp->__('Status', 'mailpoet'),
'values' => [ 'values' => [
[ [
'value' => [ 'value' => [
Subscriber::STATUS_SUBSCRIBED => WPFunctions::get()->__('Subscribed', 'mailpoet'), Subscriber::STATUS_SUBSCRIBED => $this->wp->__('Subscribed', 'mailpoet'),
], ],
'is_checked' => ( 'is_checked' => (
$subscriber->status === Subscriber::STATUS_SUBSCRIBED $subscriber->status === Subscriber::STATUS_SUBSCRIBED
@ -336,7 +352,7 @@ class Pages {
], ],
[ [
'value' => [ 'value' => [
Subscriber::STATUS_UNSUBSCRIBED => WPFunctions::get()->__('Unsubscribed', 'mailpoet'), Subscriber::STATUS_UNSUBSCRIBED => $this->wp->__('Unsubscribed', 'mailpoet'),
], ],
'is_checked' => ( 'is_checked' => (
$subscriber->status === Subscriber::STATUS_UNSUBSCRIBED $subscriber->status === Subscriber::STATUS_UNSUBSCRIBED
@ -344,7 +360,7 @@ class Pages {
], ],
[ [
'value' => [ 'value' => [
Subscriber::STATUS_BOUNCED => WPFunctions::get()->__('Bounced', 'mailpoet'), Subscriber::STATUS_BOUNCED => $this->wp->__('Bounced', 'mailpoet'),
], ],
'is_checked' => ( 'is_checked' => (
$subscriber->status === Subscriber::STATUS_BOUNCED $subscriber->status === Subscriber::STATUS_BOUNCED
@ -356,7 +372,7 @@ class Pages {
], ],
[ [
'value' => [ 'value' => [
Subscriber::STATUS_INACTIVE => WPFunctions::get()->__('Inactive', 'mailpoet'), Subscriber::STATUS_INACTIVE => $this->wp->__('Inactive', 'mailpoet'),
], ],
'is_checked' => ( 'is_checked' => (
$subscriber->status === Subscriber::STATUS_INACTIVE $subscriber->status === Subscriber::STATUS_INACTIVE
@ -378,7 +394,7 @@ class Pages {
'id' => 'segments', 'id' => 'segments',
'type' => 'segment', 'type' => 'segment',
'params' => [ 'params' => [
'label' => WPFunctions::get()->__('Your lists', 'mailpoet'), 'label' => $this->wp->__('Your lists', 'mailpoet'),
'values' => $segments, 'values' => $segments,
], ],
], ],
@ -386,7 +402,7 @@ class Pages {
'id' => 'submit', 'id' => 'submit',
'type' => 'submit', 'type' => 'submit',
'params' => [ 'params' => [
'label' => WPFunctions::get()->__('Save', 'mailpoet'), 'label' => $this->wp->__('Save', 'mailpoet'),
], ],
], ],
] ]
@ -413,22 +429,22 @@ class Pages {
// special case for WP users as they cannot edit their subscriber's email // special case for WP users as they cannot edit their subscriber's email
if ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()) { if ($subscriber->isWPUser() || $subscriber->isWooCommerceUser()) {
// check if subscriber's associated WP user is the currently logged in WP user // check if subscriber's associated WP user is the currently logged in WP user
$wp_current_user = WPFunctions::get()->wpGetCurrentUser(); $wp_current_user = $this->wp->wpGetCurrentUser();
if ($wp_current_user->user_email === $subscriber->email) { if ($wp_current_user->user_email === $subscriber->email) {
$form_html .= Helpers::replaceLinkTags( $form_html .= Helpers::replaceLinkTags(
WPFunctions::get()->__('[link]Edit your profile[/link] to update your email.', 'mailpoet'), $this->wp->__('[link]Edit your profile[/link] to update your email.', 'mailpoet'),
WPFunctions::get()->getEditProfileUrl(), $this->wp->getEditProfileUrl(),
['target' => '_blank'] ['target' => '_blank']
); );
} else { } else {
$form_html .= Helpers::replaceLinkTags( $form_html .= Helpers::replaceLinkTags(
WPFunctions::get()->__('[link]Log in to your account[/link] to update your email.', 'mailpoet'), $this->wp->__('[link]Log in to your account[/link] to update your email.', 'mailpoet'),
WPFunctions::get()->wpLoginUrl(), $this->wp->wpLoginUrl(),
['target' => '_blank'] ['target' => '_blank']
); );
} }
} else { } else {
$form_html .= WPFunctions::get()->__('Need to change your email address? Unsubscribe here, then simply sign up again.', 'mailpoet'); $form_html .= $this->wp->__('Need to change your email address? Unsubscribe here, then simply sign up again.', 'mailpoet');
} }
$form_html .= '</span>'; $form_html .= '</span>';
$form_html .= '</p>'; $form_html .= '</p>';
@ -450,13 +466,13 @@ class Pages {
} }
function getManageLink($params) { function getManageLink($params) {
if (!$this->subscriber) return WPFunctions::get()->__('Link to subscription management page is only available to mailing lists subscribers.', 'mailpoet'); if (!$this->subscriber) return $this->wp->__('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'])
? htmlspecialchars($params['text']) ? htmlspecialchars($params['text'])
: WPFunctions::get()->__('Manage your subscription', 'mailpoet') : $this->wp->__('Manage your subscription', 'mailpoet')
); );
return '<a href="' . Url::getManageUrl( return '<a href="' . Url::getManageUrl(

View File

@ -4,6 +4,7 @@ namespace MailPoet\Test\Config;
use Codeception\Util\Fixtures; use Codeception\Util\Fixtures;
use Helper\WordPress; use Helper\WordPress;
use MailPoet\Config\Shortcodes; use MailPoet\Config\Shortcodes;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Models\SendingQueue; use MailPoet\Models\SendingQueue;
@ -27,7 +28,7 @@ class ShortcodesTest extends \MailPoetTest {
} }
function testItGetsArchives() { function testItGetsArchives() {
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
WordPress::interceptFunction('apply_filters', function() use($shortcodes) { WordPress::interceptFunction('apply_filters', function() use($shortcodes) {
$args = func_get_args(); $args = func_get_args();
$filter_name = array_shift($args); $filter_name = array_shift($args);
@ -65,7 +66,7 @@ class ShortcodesTest extends \MailPoetTest {
$subscriber->wp_user_id = $wp_user->ID; $subscriber->wp_user_id = $wp_user->ID;
$subscriber->save(); $subscriber->save();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $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"');
@ -77,7 +78,7 @@ class ShortcodesTest extends \MailPoetTest {
expect((new WPFunctions)->isUserLoggedIn())->true(); expect((new WPFunctions)->isUserLoggedIn())->true();
expect(Subscriber::findOne($wp_user->data->user_email))->false(); expect(Subscriber::findOne($wp_user->data->user_email))->false();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $shortcodes->init();
$result = do_shortcode('[mailpoet_manage_subscription]'); $result = do_shortcode('[mailpoet_manage_subscription]');
expect($result)->contains('Subscription management form is only available to mailing lists subscribers.'); expect($result)->contains('Subscription management form is only available to mailing lists subscribers.');
@ -87,7 +88,7 @@ class ShortcodesTest extends \MailPoetTest {
wp_set_current_user(0); wp_set_current_user(0);
expect((new WPFunctions)->isUserLoggedIn())->false(); expect((new WPFunctions)->isUserLoggedIn())->false();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $shortcodes->init();
$result = do_shortcode('[mailpoet_manage_subscription]'); $result = do_shortcode('[mailpoet_manage_subscription]');
expect($result)->contains('Subscription management form is only available to mailing lists subscribers.'); expect($result)->contains('Subscription management form is only available to mailing lists subscribers.');
@ -102,7 +103,7 @@ class ShortcodesTest extends \MailPoetTest {
$subscriber->wp_user_id = $wp_user->ID; $subscriber->wp_user_id = $wp_user->ID;
$subscriber->save(); $subscriber->save();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $shortcodes->init();
$result = do_shortcode('[mailpoet_manage]'); $result = do_shortcode('[mailpoet_manage]');
expect($result)->contains('Manage your subscription'); expect($result)->contains('Manage your subscription');
@ -113,7 +114,7 @@ class ShortcodesTest extends \MailPoetTest {
expect((new WPFunctions)->isUserLoggedIn())->true(); expect((new WPFunctions)->isUserLoggedIn())->true();
expect(Subscriber::findOne($wp_user->data->user_email))->false(); expect(Subscriber::findOne($wp_user->data->user_email))->false();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $shortcodes->init();
$result = do_shortcode('[mailpoet_manage]'); $result = do_shortcode('[mailpoet_manage]');
expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.'); expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.');
@ -123,7 +124,7 @@ class ShortcodesTest extends \MailPoetTest {
wp_set_current_user(0); wp_set_current_user(0);
expect((new WPFunctions)->isUserLoggedIn())->false(); expect((new WPFunctions)->isUserLoggedIn())->false();
$shortcodes = new Shortcodes(); $shortcodes = ContainerWrapper::getInstance()->get(Shortcodes::class);
$shortcodes->init(); $shortcodes->init();
$result = do_shortcode('[mailpoet_manage]'); $result = do_shortcode('[mailpoet_manage]');
expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.'); expect($result)->contains('Link to subscription management page is only available to mailing lists subscribers.');

View File

@ -0,0 +1,48 @@
<?php
namespace MailPoet\Test\Router\Endpoints;
use Codeception\Stub;
use Codeception\Stub\Expected;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Router\Endpoints\Subscription;
use MailPoet\Subscription\Pages;
use MailPoet\WP\Functions as WPFunctions;
class SubscriptionTest extends \MailPoetTest {
function _before() {
$this->data = [];
// instantiate class
$this->subscription = ContainerWrapper::getInstance()->get(Subscription::class);
}
function testItDisplaysConfirmPage() {
$pages = Stub::make(Pages::class, [
'wp' => new WPFunctions,
'confirm' => Expected::exactly(1),
], $this);
$subscription = new Subscription($pages);
$subscription->confirm($this->data);
}
function testItDisplaysManagePage() {
$pages = Stub::make(Pages::class, [
'wp' => new WPFunctions,
'getManageLink' => Expected::exactly(1),
'getManageContent' => Expected::exactly(1),
], $this);
$subscription = new Subscription($pages);
$subscription->manage($this->data);
do_shortcode('[mailpoet_manage]');
do_shortcode('[mailpoet_manage_subscription]');
}
function testItDisplaysUnsubscribePage() {
$pages = Stub::make(Pages::class, [
'wp' => new WPFunctions,
'unsubscribe' => Expected::exactly(1),
], $this);
$subscription = new Subscription($pages);
$subscription->unsubscribe($this->data);
}
}

View File

@ -3,6 +3,7 @@ namespace MailPoet\Test\Subscription;
use Codeception\Stub; use Codeception\Stub;
use Codeception\Util\Fixtures; use Codeception\Util\Fixtures;
use MailPoet\DI\ContainerWrapper;
use MailPoet\Models\Newsletter; use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterOption; use MailPoet\Models\NewsletterOption;
use MailPoet\Models\NewsletterOptionField; use MailPoet\Models\NewsletterOptionField;
@ -30,11 +31,12 @@ class PagesTest extends \MailPoetTest {
expect($this->subscriber->getErrors())->false(); expect($this->subscriber->getErrors())->false();
$this->test_data['email'] = $this->subscriber->email; $this->test_data['email'] = $this->subscriber->email;
$this->test_data['token'] = Subscriber::generateToken($this->subscriber->email); $this->test_data['token'] = Subscriber::generateToken($this->subscriber->email);
$this->pages = ContainerWrapper::getInstance()->get(Pages::class);
} }
function testItConfirmsSubscription() { function testItConfirmsSubscription() {
$new_subscriber_notification_sender = Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send' => Stub\Expected::once()]); $new_subscriber_notification_sender = Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send' => Stub\Expected::once()]);
$subscription = new Pages($action = false, $this->test_data, false, false, $new_subscriber_notification_sender); $subscription = $this->pages->init($action = false, $this->test_data, false, false, $new_subscriber_notification_sender);
$subscription->confirm(); $subscription->confirm();
$confirmed_subscriber = Subscriber::findOne($this->subscriber->id); $confirmed_subscriber = Subscriber::findOne($this->subscriber->id);
expect($confirmed_subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED); expect($confirmed_subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
@ -45,13 +47,13 @@ class PagesTest extends \MailPoetTest {
$subscriber = $this->subscriber; $subscriber = $this->subscriber;
$subscriber->status = Subscriber::STATUS_SUBSCRIBED; $subscriber->status = Subscriber::STATUS_SUBSCRIBED;
$subscriber->save(); $subscriber->save();
$subscription = new Pages($action = false, $this->test_data, false, false, $new_subscriber_notification_sender); $subscription = $this->pages->init($action = false, $this->test_data, false, false, $new_subscriber_notification_sender);
expect($subscription->confirm())->false(); expect($subscription->confirm())->false();
} }
function testItSendsWelcomeNotificationUponConfirmingSubscription() { function testItSendsWelcomeNotificationUponConfirmingSubscription() {
$new_subscriber_notification_sender = Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send' => Stub\Expected::once()]); $new_subscriber_notification_sender = Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send' => Stub\Expected::once()]);
$subscription = new Pages($action = false, $this->test_data, false, false, $new_subscriber_notification_sender); $subscription = $this->pages->init($action = false, $this->test_data, false, false, $new_subscriber_notification_sender);
// create segment // create segment
$segment = Segment::create(); $segment = Segment::create();
$segment->hydrate(['name' => 'List #1']); $segment->hydrate(['name' => 'List #1']);
@ -104,7 +106,7 @@ class PagesTest extends \MailPoetTest {
} }
function testItUnsubscribes() { function testItUnsubscribes() {
$pages = new Pages($action = 'unsubscribe', $this->test_data); $pages = $this->pages->init($action = 'unsubscribe', $this->test_data);
$pages->unsubscribe(); $pages->unsubscribe();
$updated_subscriber = Subscriber::findOne($this->subscriber->id); $updated_subscriber = Subscriber::findOne($this->subscriber->id);
expect($updated_subscriber->status)->equals(Subscriber::STATUS_UNSUBSCRIBED); expect($updated_subscriber->status)->equals(Subscriber::STATUS_UNSUBSCRIBED);
@ -112,7 +114,7 @@ class PagesTest extends \MailPoetTest {
function testItDoesntUnsubscribeWhenPreviewing() { function testItDoesntUnsubscribeWhenPreviewing() {
$this->test_data['preview'] = 1; $this->test_data['preview'] = 1;
$pages = new Pages($action = 'unsubscribe', $this->test_data); $pages = $this->pages->init($action = 'unsubscribe', $this->test_data);
$pages->unsubscribe(); $pages->unsubscribe();
$updated_subscriber = Subscriber::findOne($this->subscriber->id); $updated_subscriber = Subscriber::findOne($this->subscriber->id);
expect($updated_subscriber->status)->notEquals(Subscriber::STATUS_UNSUBSCRIBED); expect($updated_subscriber->status)->notEquals(Subscriber::STATUS_UNSUBSCRIBED);