AdminUserSubscription.php
3 months ago
Blacklist.php
1 year ago
Comment.php
1 week ago
Form.php
1 month ago
Manage.php
1 month ago
ManageSubscriptionFormRenderer.php
2 weeks ago
Pages.php
5 days ago
Registration.php
1 week ago
SubscriptionUrlFactory.php
1 month ago
Throttling.php
3 months ago
index.php
3 years ago
Registration.php
176 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\Entities\SubscriberEntity; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoet\Statistics\Track\SubscriberHandler; |
| 11 | use MailPoet\Subscribers\SubscriberActions; |
| 12 | use MailPoet\Subscribers\TrackingConsentCapture; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | class Registration { |
| 16 | |
| 17 | /** @var SettingsController */ |
| 18 | private $settings; |
| 19 | |
| 20 | /** @var SubscriberActions */ |
| 21 | private $subscriberActions; |
| 22 | |
| 23 | /** @var WPFunctions */ |
| 24 | private $wp; |
| 25 | |
| 26 | /** @var SubscriberHandler */ |
| 27 | private $subscriberHandler; |
| 28 | |
| 29 | /** @var TrackingConsentCapture */ |
| 30 | private $trackingConsentCapture; |
| 31 | |
| 32 | public function __construct( |
| 33 | SettingsController $settings, |
| 34 | WPFunctions $wp, |
| 35 | SubscriberActions $subscriberActions, |
| 36 | SubscriberHandler $subscriberHandler, |
| 37 | TrackingConsentCapture $trackingConsentCapture |
| 38 | ) { |
| 39 | $this->settings = $settings; |
| 40 | $this->subscriberActions = $subscriberActions; |
| 41 | $this->wp = $wp; |
| 42 | $this->subscriberHandler = $subscriberHandler; |
| 43 | $this->trackingConsentCapture = $trackingConsentCapture; |
| 44 | } |
| 45 | |
| 46 | public function extendForm() { |
| 47 | $label = $this->settings->get( |
| 48 | 'subscribe.on_register.label', |
| 49 | __('Yes, please add me to your mailing list.', 'mailpoet') |
| 50 | ); |
| 51 | |
| 52 | $form = '<p class="registration-form-mailpoet"> |
| 53 | <label for="mailpoet_subscribe_on_register"> |
| 54 | <input |
| 55 | type="hidden" |
| 56 | id="mailpoet_subscribe_on_register_active" |
| 57 | value="1" |
| 58 | name="mailpoet[subscribe_on_register_active]" |
| 59 | /> |
| 60 | <input |
| 61 | type="checkbox" |
| 62 | id="mailpoet_subscribe_on_register" |
| 63 | value="1" |
| 64 | name="mailpoet[subscribe_on_register]" |
| 65 | /> ' . esc_html($label) . ' |
| 66 | </label> |
| 67 | </p>' . $this->getTrackingConsentField(); |
| 68 | |
| 69 | $filtered = $this->wp->applyFilters('mailpoet_register_form_extend', $form); |
| 70 | $form = is_string($filtered) ? $filtered : $form; |
| 71 | |
| 72 | // We control the template and $form can be considered safe. |
| 73 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 74 | echo $form; |
| 75 | } |
| 76 | |
| 77 | public function onMultiSiteRegister($result) { |
| 78 | if (empty($result['errors']->errors)) { |
| 79 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 80 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 81 | if ( |
| 82 | isset($mailpoetPost['subscribe_on_register']) |
| 83 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 84 | && !empty($result['user_email']) |
| 85 | ) { |
| 86 | $this->subscribeNewUser( |
| 87 | $result['user_name'], |
| 88 | $result['user_email'], |
| 89 | !empty($mailpoetPost['tracking_consent']) |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | public function onRegister( |
| 97 | $errors, |
| 98 | $userLogin, |
| 99 | $userEmail = null |
| 100 | ) { |
| 101 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 102 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 103 | if ( |
| 104 | empty($errors->errors) |
| 105 | && isset($mailpoetPost['subscribe_on_register']) |
| 106 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 107 | && !empty($userEmail) |
| 108 | ) { |
| 109 | $this->subscribeNewUser( |
| 110 | $userLogin, |
| 111 | $userEmail, |
| 112 | !empty($mailpoetPost['tracking_consent']) |
| 113 | ); |
| 114 | } |
| 115 | return $errors; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * A second, independent checkbox. Consent to open and click tracking is never |
| 120 | * inferred from the "add me to your mailing list" box above it, and it is |
| 121 | * only shown on sites that chose to ask. Never pre-ticked: a pre-ticked |
| 122 | * consent box is not valid consent (CJEU Planet49). |
| 123 | */ |
| 124 | private function getTrackingConsentField(): string { |
| 125 | if (!$this->trackingConsentCapture->isCaptureEnabled()) { |
| 126 | return ''; |
| 127 | } |
| 128 | $copy = $this->trackingConsentCapture->getCopy( |
| 129 | SubscriberEntity::TRACKING_CONSENT_METHOD_REGISTRATION |
| 130 | ); |
| 131 | |
| 132 | return '<p class="registration-form-mailpoet-tracking-consent"> |
| 133 | <label for="mailpoet_tracking_consent"> |
| 134 | <input |
| 135 | type="checkbox" |
| 136 | id="mailpoet_tracking_consent" |
| 137 | value="1" |
| 138 | name="mailpoet[tracking_consent]" |
| 139 | /> ' . esc_html($copy) . ' |
| 140 | </label> |
| 141 | </p>'; |
| 142 | } |
| 143 | |
| 144 | private function subscribeNewUser($name, $email, bool $trackingConsent = false) { |
| 145 | $segmentIds = $this->settings->get( |
| 146 | 'subscribe.on_register.segments', |
| 147 | [] |
| 148 | ); |
| 149 | $method = SubscriberEntity::TRACKING_CONSENT_METHOD_REGISTRATION; |
| 150 | $consentData = $this->trackingConsentCapture->getConsentData( |
| 151 | $trackingConsent, |
| 152 | $method, |
| 153 | $this->trackingConsentCapture->getCopy($method), |
| 154 | $this->trackingConsentCapture->isNewSubscriber($email) |
| 155 | ); |
| 156 | |
| 157 | $this->subscriberActions->subscribe( |
| 158 | array_merge([ |
| 159 | 'email' => $email, |
| 160 | 'first_name' => $name, |
| 161 | ], $consentData), |
| 162 | $segmentIds |
| 163 | ); |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * On multisite headers are already sent at this point, tracking will start |
| 168 | * once the user has activated his account at a later stage. |
| 169 | **/ |
| 170 | if (!headers_sent()) { |
| 171 | // start subscriber tracking (by email, we don't have WP user ID yet) |
| 172 | $this->subscriberHandler->identifyByEmail($email); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 |