diff --git a/lib/Config/Shortcodes.php b/lib/Config/Shortcodes.php index 319cc1f3a4..237f878d73 100644 --- a/lib/Config/Shortcodes.php +++ b/lib/Config/Shortcodes.php @@ -1,9 +1,11 @@ getManageContent(); } function formWidget($params = array()) { diff --git a/lib/Subscription/Pages.php b/lib/Subscription/Pages.php index 1046283698..9ed6345850 100644 --- a/lib/Subscription/Pages.php +++ b/lib/Subscription/Pages.php @@ -1,4 +1,5 @@ action = $action; $this->data = $data; $this->subscriber = $this->getSubscriber(); @@ -32,8 +36,12 @@ class Pages { // manage subscription link shortcode // [mailpoet_manage text="Manage your subscription"] - add_shortcode('mailpoet_manage', array($this, 'getManageLink')); - add_shortcode('mailpoet_manage_subscription', array($this, 'getManageContent')); + 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() { @@ -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) { @@ -420,4 +429,4 @@ class Pages { $this->subscriber ).'">'.$text.''; } -} +} \ No newline at end of file diff --git a/tests/unit/Config/ShortcodesTest.php b/tests/unit/Config/ShortcodesTest.php index 03218b8422..b2d312c9de 100644 --- a/tests/unit/Config/ShortcodesTest.php +++ b/tests/unit/Config/ShortcodesTest.php @@ -1,10 +1,12 @@ 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); + } }