AdminUserSubscription.php
2 months ago
Blacklist.php
1 year ago
Comment.php
2 months ago
Form.php
2 weeks ago
Manage.php
5 days ago
ManageSubscriptionFormRenderer.php
5 days ago
Pages.php
5 days ago
Registration.php
2 months ago
SubscriptionUrlFactory.php
5 days ago
Throttling.php
2 months ago
index.php
3 years ago
SubscriptionUrlFactory.php
145 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscription; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\DI\ContainerWrapper; |
| 9 | use MailPoet\Entities\SubscriberEntity; |
| 10 | use MailPoet\Router\Endpoints\Subscription as SubscriptionEndpoint; |
| 11 | use MailPoet\Router\Router; |
| 12 | use MailPoet\Settings\Pages as SettingsPages; |
| 13 | use MailPoet\Settings\SettingsController; |
| 14 | use MailPoet\Subscribers\LinkTokens; |
| 15 | use MailPoet\WP\Functions as WPFunctions; |
| 16 | |
| 17 | class SubscriptionUrlFactory { |
| 18 | |
| 19 | /** @var SubscriptionUrlFactory */ |
| 20 | private static $instance; |
| 21 | |
| 22 | /** @var WPFunctions */ |
| 23 | private $wp; |
| 24 | |
| 25 | /** @var SettingsController */ |
| 26 | private $settings; |
| 27 | |
| 28 | /** @var LinkTokens */ |
| 29 | private $linkTokens; |
| 30 | |
| 31 | public function __construct( |
| 32 | WPFunctions $wp, |
| 33 | SettingsController $settings, |
| 34 | LinkTokens $linkTokens |
| 35 | ) { |
| 36 | $this->wp = $wp; |
| 37 | $this->settings = $settings; |
| 38 | $this->linkTokens = $linkTokens; |
| 39 | } |
| 40 | |
| 41 | public function getConfirmationUrl(?SubscriberEntity $subscriber = null, ?int $confirmationPageId = null) { |
| 42 | $pageId = $confirmationPageId ?? $this->settings->get('subscription.pages.confirmation'); |
| 43 | $post = $this->getPost($pageId); |
| 44 | return $this->getSubscriptionUrl($post, 'confirm', $subscriber); |
| 45 | } |
| 46 | |
| 47 | public function getConfirmUnsubscribeUrl(?SubscriberEntity $subscriber = null, ?int $queueId = null) { |
| 48 | $post = $this->getPost($this->settings->get('subscription.pages.confirm_unsubscribe')); |
| 49 | $data = $queueId && $subscriber ? ['queueId' => $queueId] : null; |
| 50 | return $this->getSubscriptionUrl($post, 'confirm_unsubscribe', $subscriber, $data); |
| 51 | } |
| 52 | |
| 53 | public function getManageUrl(?SubscriberEntity $subscriber = null) { |
| 54 | $post = $this->getPost($this->settings->get('subscription.pages.manage')); |
| 55 | return $this->getSubscriptionUrl($post, 'manage', $subscriber); |
| 56 | } |
| 57 | |
| 58 | public function getUnsubscribeUrl(?SubscriberEntity $subscriber = null, ?int $queueId = null) { |
| 59 | $post = $this->getPost($this->settings->get('subscription.pages.unsubscribe')); |
| 60 | $data = $queueId && $subscriber ? ['queueId' => $queueId] : null; |
| 61 | return $this->getSubscriptionUrl($post, 'unsubscribe', $subscriber, $data); |
| 62 | } |
| 63 | |
| 64 | public function getUnsubscribeReasonUrl(?SubscriberEntity $subscriber = null, ?int $queueId = null) { |
| 65 | $post = $this->getPost($this->settings->get('subscription.pages.unsubscribe')); |
| 66 | $data = $queueId && $subscriber ? ['queueId' => $queueId] : null; |
| 67 | return $this->getSubscriptionUrl($post, 'unsubscribe_reason', $subscriber, $data); |
| 68 | } |
| 69 | |
| 70 | public function getTrackingOptOutUrl(?SubscriberEntity $subscriber = null) { |
| 71 | $post = $this->getPost($this->settings->get('subscription.pages.manage')); |
| 72 | return $this->getSubscriptionUrl($post, 'tracking_opt_out', $subscriber); |
| 73 | } |
| 74 | |
| 75 | public function getReEngagementUrl(?SubscriberEntity $subscriber = null) { |
| 76 | $reEngagementSetting = $this->settings->get('reEngagement'); |
| 77 | $postId = $reEngagementSetting['page'] ?? null; |
| 78 | |
| 79 | $post = $this->getPost($postId); |
| 80 | return $this->getSubscriptionUrl($post, 're_engagement', $subscriber); |
| 81 | } |
| 82 | |
| 83 | public function getSubscriptionUrl( |
| 84 | $post = null, |
| 85 | $action = null, |
| 86 | ?SubscriberEntity $subscriber = null, |
| 87 | $data = null |
| 88 | ) { |
| 89 | if ($post === null || $action === null) return; |
| 90 | |
| 91 | $url = $this->wp->getPermalink($post); |
| 92 | if ($subscriber !== null) { |
| 93 | $subscriberData = [ |
| 94 | 'token' => $this->linkTokens->getToken($subscriber), |
| 95 | 'email' => $subscriber->getEmail(), |
| 96 | ]; |
| 97 | $data = array_merge($data ?? [], $subscriberData); |
| 98 | } elseif (is_null($data)) { |
| 99 | $data = [ |
| 100 | 'preview' => 1, |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | $params = [ |
| 105 | Router::NAME, |
| 106 | 'endpoint=' . SubscriptionEndpoint::ENDPOINT, |
| 107 | 'action=' . $action, |
| 108 | 'data=' . Router::encodeRequestData($data), |
| 109 | ]; |
| 110 | |
| 111 | // add parameters |
| 112 | $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . join('&', $params); |
| 113 | |
| 114 | $urlParams = parse_url($url); |
| 115 | if (!is_array($urlParams) || empty($urlParams['scheme'])) { |
| 116 | $url = $this->wp->getBloginfo('url') . $url; |
| 117 | } |
| 118 | |
| 119 | return $url; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return SubscriptionUrlFactory |
| 124 | */ |
| 125 | public static function getInstance() { |
| 126 | if (!self::$instance instanceof SubscriptionUrlFactory) { |
| 127 | $linkTokens = ContainerWrapper::getInstance()->get(LinkTokens::class); |
| 128 | self::$instance = new SubscriptionUrlFactory(new WPFunctions, SettingsController::getInstance(), $linkTokens); |
| 129 | } |
| 130 | return self::$instance; |
| 131 | } |
| 132 | |
| 133 | private function getPost($post = null) { |
| 134 | if ($post) { |
| 135 | $postObject = $this->wp->getPost($post); |
| 136 | if ($postObject) { |
| 137 | return $postObject; |
| 138 | } |
| 139 | } |
| 140 | // Resort to a default MailPoet page if no page is selected |
| 141 | $pages = SettingsPages::getMailPoetPages(); |
| 142 | return reset($pages); |
| 143 | } |
| 144 | } |
| 145 |