Captcha.php
4 months ago
CronDaemon.php
3 years ago
ExportDownload.php
2 months ago
FormPreview.php
2 years ago
Subscription.php
5 days ago
TemplateImage.php
2 months ago
Track.php
2 months ago
ViewInBrowser.php
1 month ago
index.php
3 years ago
Subscription.php
177 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Router\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\AccessControl; |
| 9 | use MailPoet\Entities\StatisticsUnsubscribeEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Subscription as UserSubscription; |
| 12 | use MailPoet\Util\Request; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | class Subscription { |
| 16 | const ENDPOINT = 'subscription'; |
| 17 | const ACTION_CONFIRM = 'confirm'; |
| 18 | const ACTION_MANAGE = 'manage'; |
| 19 | const ACTION_UNSUBSCRIBE = 'unsubscribe'; |
| 20 | const ACTION_UNSUBSCRIBE_REASON = 'unsubscribeReason'; |
| 21 | const ACTION_CONFIRM_UNSUBSCRIBE = 'confirmUnsubscribe'; |
| 22 | const ACTION_RE_ENGAGEMENT = 'reEngagement'; |
| 23 | const ACTION_TRACKING_OPT_OUT = 'trackingOptOut'; |
| 24 | |
| 25 | public $allowedActions = [ |
| 26 | self::ACTION_CONFIRM, |
| 27 | self::ACTION_MANAGE, |
| 28 | self::ACTION_UNSUBSCRIBE, |
| 29 | self::ACTION_UNSUBSCRIBE_REASON, |
| 30 | self::ACTION_CONFIRM_UNSUBSCRIBE, |
| 31 | self::ACTION_RE_ENGAGEMENT, |
| 32 | self::ACTION_TRACKING_OPT_OUT, |
| 33 | ]; |
| 34 | |
| 35 | public $permissions = [ |
| 36 | 'global' => AccessControl::NO_ACCESS_RESTRICTION, |
| 37 | ]; |
| 38 | |
| 39 | /** @var UserSubscription\Pages */ |
| 40 | private $subscriptionPages; |
| 41 | |
| 42 | /** @var WPFunctions */ |
| 43 | private $wp; |
| 44 | |
| 45 | /*** @var Request */ |
| 46 | private $request; |
| 47 | |
| 48 | public function __construct( |
| 49 | UserSubscription\Pages $subscriptionPages, |
| 50 | WPFunctions $wp, |
| 51 | Request $request |
| 52 | ) { |
| 53 | $this->subscriptionPages = $subscriptionPages; |
| 54 | $this->wp = $wp; |
| 55 | $this->request = $request; |
| 56 | } |
| 57 | |
| 58 | public function confirm($data) { |
| 59 | $subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_CONFIRM, $data); |
| 60 | $subscription->confirm(); |
| 61 | } |
| 62 | |
| 63 | public function confirmUnsubscribe($data) { |
| 64 | $enableUnsubscribeConfirmation = $this->wp->applyFilters('mailpoet_unsubscribe_confirmation_enabled', true); |
| 65 | if ($this->isPostRequest()) { |
| 66 | $this->performUnsubscribe($data, StatisticsUnsubscribeEntity::METHOD_ONE_CLICK); |
| 67 | exit; |
| 68 | } |
| 69 | |
| 70 | if ($enableUnsubscribeConfirmation) { |
| 71 | $this->initSubscriptionPage(UserSubscription\Pages::ACTION_CONFIRM_UNSUBSCRIBE, $data); |
| 72 | } else { |
| 73 | $this->performUnsubscribe($data, StatisticsUnsubscribeEntity::METHOD_LINK); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function manage($data) { |
| 78 | $this->initSubscriptionPage(UserSubscription\Pages::ACTION_MANAGE, $data); |
| 79 | } |
| 80 | |
| 81 | public function unsubscribe($data) { |
| 82 | if ($this->isPostRequest()) { |
| 83 | if ($this->request->getStringParam('type') === 'confirmation') { |
| 84 | // POST from confirmation page |
| 85 | $this->performUnsubscribe($data, StatisticsUnsubscribeEntity::METHOD_LINK); |
| 86 | } else { |
| 87 | // POST from one click unsubscribe |
| 88 | $this->performUnsubscribe($data, StatisticsUnsubscribeEntity::METHOD_ONE_CLICK); |
| 89 | exit; |
| 90 | } |
| 91 | } else { |
| 92 | // For GET requests, we render the confirmUnsubscribe page, unless it is preview request of successful unsubscribe |
| 93 | // or the subscriber is already unsubscribed. |
| 94 | if (isset($data['preview']) && $data['preview'] && !isset($data['token'])) { |
| 95 | $this->performUnsubscribe($data, StatisticsUnsubscribeEntity::METHOD_LINK); |
| 96 | } elseif ($this->renderUnsubscribePageForAlreadyUnsubscribedSubscriber($data)) { |
| 97 | return; |
| 98 | } else { |
| 99 | $this->confirmUnsubscribe($data); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public function unsubscribeReason($data) { |
| 105 | if (!$this->request->isPost()) { |
| 106 | $this->wp->wpSafeRedirect($this->wp->homeUrl()); |
| 107 | exit; |
| 108 | } |
| 109 | |
| 110 | $nonce = $this->request->getStringParam('_wpnonce'); |
| 111 | if (!$this->wp->wpVerifyNonce($nonce, 'mailpoet_unsubscribe_reason')) { |
| 112 | $this->wp->wpDie(__('Security check failed.', 'mailpoet'), '', ['response' => 403]); |
| 113 | exit; |
| 114 | } |
| 115 | |
| 116 | $subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_UNSUBSCRIBE, $data); |
| 117 | $reason = strtolower($this->wp->sanitizeKey((string)$this->request->getStringParam('reason'))); |
| 118 | $reasonText = $this->request->getTextareaParam('reason_text'); |
| 119 | |
| 120 | $saved = $reason !== '' && $subscription->saveUnsubscribeReason($reason, $reasonText); |
| 121 | $this->wp->wpSafeRedirect($subscription->getUnsubscribeReasonRedirectUrl($saved)); |
| 122 | exit; |
| 123 | } |
| 124 | |
| 125 | public function reEngagement($data) { |
| 126 | $this->initSubscriptionPage(UserSubscription\Pages::ACTION_RE_ENGAGEMENT, $data); |
| 127 | } |
| 128 | |
| 129 | public function trackingOptOut($data) { |
| 130 | $subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_TRACKING_OPT_OUT, $data); |
| 131 | if ($this->isPostRequest()) { |
| 132 | $nonce = $this->request->getStringParam('_wpnonce'); |
| 133 | if (!$this->wp->wpVerifyNonce($nonce, 'mailpoet_tracking_opt_out')) { |
| 134 | $this->wp->wpDie(__('Security check failed.', 'mailpoet'), '', ['response' => 403]); |
| 135 | exit; |
| 136 | } |
| 137 | $subscription->trackingOptOut( |
| 138 | SubscriberEntity::TRACKING_CONSENT_METHOD_FOOTER_LINK, |
| 139 | UserSubscription\Pages::getTrackingOptOutConsentCopy() |
| 140 | ); |
| 141 | } |
| 142 | // GET renders the confirmation page; after POST the same page renders |
| 143 | // its "done" state via getTrackingOptOutContent(). |
| 144 | } |
| 145 | |
| 146 | private function initSubscriptionPage($action, $data) { |
| 147 | return $this->subscriptionPages->init($action, $data, true, true); |
| 148 | } |
| 149 | |
| 150 | private function renderUnsubscribePageForAlreadyUnsubscribedSubscriber($data): bool { |
| 151 | $subscription = $this->subscriptionPages->init(UserSubscription\Pages::ACTION_UNSUBSCRIBE, $data, false, false); |
| 152 | if (!$subscription->isSubscriberUnsubscribed()) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | $this->initSubscriptionPage(UserSubscription\Pages::ACTION_UNSUBSCRIBE, $data); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | private function performUnsubscribe($data, string $method): void { |
| 161 | $subscription = $this->initSubscriptionPage(UserSubscription\Pages::ACTION_UNSUBSCRIBE, $data); |
| 162 | $subscription->unsubscribe($method); |
| 163 | } |
| 164 | |
| 165 | private function isPostRequest(): bool { |
| 166 | if ($this->request->isPost()) { |
| 167 | return true; |
| 168 | } |
| 169 | // For tracking redirects we store original method in the query string |
| 170 | $requestMethod = $this->request->getStringParam('request_method'); |
| 171 | if (!$requestMethod) { |
| 172 | return false; |
| 173 | } |
| 174 | return strtoupper($requestMethod) === 'POST'; |
| 175 | } |
| 176 | } |
| 177 |