AdminUserSubscription.php
3 months ago
Blacklist.php
1 year ago
Comment.php
1 day ago
Form.php
1 month ago
Manage.php
1 month ago
ManageSubscriptionFormRenderer.php
3 weeks ago
Pages.php
1 week ago
Registration.php
1 day ago
SubscriptionUrlFactory.php
1 month ago
Throttling.php
3 months ago
index.php
3 years ago
Registration.php
225 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\Segments\WP as WPSegment; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoet\Statistics\Track\SubscriberHandler; |
| 12 | use MailPoet\Subscribers\SubscriberActions; |
| 13 | use MailPoet\Subscribers\SubscribersRepository; |
| 14 | use MailPoet\Subscribers\TrackingConsentCapture; |
| 15 | use MailPoet\WP\Functions as WPFunctions; |
| 16 | |
| 17 | class Registration { |
| 18 | |
| 19 | /** @var SettingsController */ |
| 20 | private $settings; |
| 21 | |
| 22 | /** @var SubscriberActions */ |
| 23 | private $subscriberActions; |
| 24 | |
| 25 | /** @var WPFunctions */ |
| 26 | private $wp; |
| 27 | |
| 28 | /** @var SubscriberHandler */ |
| 29 | private $subscriberHandler; |
| 30 | |
| 31 | /** @var TrackingConsentCapture */ |
| 32 | private $trackingConsentCapture; |
| 33 | |
| 34 | /** @var SubscribersRepository */ |
| 35 | private $subscribersRepository; |
| 36 | |
| 37 | /** @var WPSegment */ |
| 38 | private $wpSegment; |
| 39 | |
| 40 | public function __construct( |
| 41 | SettingsController $settings, |
| 42 | WPFunctions $wp, |
| 43 | SubscriberActions $subscriberActions, |
| 44 | SubscriberHandler $subscriberHandler, |
| 45 | TrackingConsentCapture $trackingConsentCapture, |
| 46 | SubscribersRepository $subscribersRepository, |
| 47 | WPSegment $wpSegment |
| 48 | ) { |
| 49 | $this->settings = $settings; |
| 50 | $this->subscriberActions = $subscriberActions; |
| 51 | $this->wp = $wp; |
| 52 | $this->subscriberHandler = $subscriberHandler; |
| 53 | $this->trackingConsentCapture = $trackingConsentCapture; |
| 54 | $this->subscribersRepository = $subscribersRepository; |
| 55 | $this->wpSegment = $wpSegment; |
| 56 | } |
| 57 | |
| 58 | public function extendForm() { |
| 59 | $label = $this->settings->get( |
| 60 | 'subscribe.on_register.label', |
| 61 | __('Yes, please add me to your mailing list.', 'mailpoet') |
| 62 | ); |
| 63 | |
| 64 | $form = '<p class="registration-form-mailpoet"> |
| 65 | <label for="mailpoet_subscribe_on_register"> |
| 66 | <input |
| 67 | type="hidden" |
| 68 | id="mailpoet_subscribe_on_register_active" |
| 69 | value="1" |
| 70 | name="mailpoet[subscribe_on_register_active]" |
| 71 | /> |
| 72 | <input |
| 73 | type="checkbox" |
| 74 | id="mailpoet_subscribe_on_register" |
| 75 | value="1" |
| 76 | name="mailpoet[subscribe_on_register]" |
| 77 | /> ' . esc_html($label) . ' |
| 78 | </label> |
| 79 | </p>' . $this->getTrackingConsentField(); |
| 80 | |
| 81 | $filtered = $this->wp->applyFilters('mailpoet_register_form_extend', $form); |
| 82 | $form = is_string($filtered) ? $filtered : $form; |
| 83 | |
| 84 | // We control the template and $form can be considered safe. |
| 85 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 86 | echo $form; |
| 87 | } |
| 88 | |
| 89 | public function onMultiSiteRegister($result) { |
| 90 | if (empty($result['errors']->errors)) { |
| 91 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 92 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 93 | if ( |
| 94 | isset($mailpoetPost['subscribe_on_register']) |
| 95 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 96 | && !empty($result['user_email']) |
| 97 | ) { |
| 98 | $this->subscribeNewUser( |
| 99 | $result['user_name'], |
| 100 | $result['user_email'], |
| 101 | !empty($mailpoetPost['tracking_consent']) |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | public function onRegister( |
| 109 | $errors, |
| 110 | $userLogin, |
| 111 | $userEmail = null |
| 112 | ) { |
| 113 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 114 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 115 | if ( |
| 116 | empty($errors->errors) |
| 117 | && isset($mailpoetPost['subscribe_on_register']) |
| 118 | && (bool)$mailpoetPost['subscribe_on_register'] === true |
| 119 | && !empty($userEmail) |
| 120 | ) { |
| 121 | $this->subscribeNewUser( |
| 122 | $userLogin, |
| 123 | $userEmail, |
| 124 | !empty($mailpoetPost['tracking_consent']) |
| 125 | ); |
| 126 | } |
| 127 | return $errors; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * A second, independent checkbox. Consent to open and click tracking is never |
| 132 | * inferred from the "add me to your mailing list" box above it, and it is |
| 133 | * only shown on sites that chose to ask. Never pre-ticked: a pre-ticked |
| 134 | * consent box is not valid consent (CJEU Planet49). |
| 135 | */ |
| 136 | |
| 137 | /** |
| 138 | * Hooked on user_register, after the WP-user sync has created or updated the |
| 139 | * subscriber row for every new WP user, whether or not "add me to your |
| 140 | * mailing list" was ticked. That is the gap this closes: |
| 141 | * onRegister()/onMultiSiteRegister() only read tracking_consent inside the |
| 142 | * subscribe branch, so someone who allowed tracking without subscribing had |
| 143 | * their answer thrown away. Covers single-site, multisite and WooCommerce |
| 144 | * registration alike, since all three create the WP user and fire this hook. |
| 145 | */ |
| 146 | public function applyTrackingConsentOnUserRegister(int $userId): void { |
| 147 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, the value is read as a bool |
| 148 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 149 | if (!isset($mailpoetPost['tracking_consent'])) { |
| 150 | return; |
| 151 | } |
| 152 | $wpUser = $this->wp->getUserdata($userId); |
| 153 | if (!$wpUser || empty($wpUser->user_email)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 154 | return; |
| 155 | } |
| 156 | $subscriber = $this->subscribersRepository->findOneBy( |
| 157 | ['email' => $wpUser->user_email] // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 158 | ); |
| 159 | if (!$subscriber instanceof SubscriberEntity) { |
| 160 | return; |
| 161 | } |
| 162 | $method = SubscriberEntity::TRACKING_CONSENT_METHOD_REGISTRATION; |
| 163 | $this->trackingConsentCapture->applyToSubscriber( |
| 164 | $subscriber, |
| 165 | !empty($mailpoetPost['tracking_consent']), |
| 166 | $method, |
| 167 | $this->trackingConsentCapture->getCopy($method), |
| 168 | $this->wpSegment->wasSubscriberCreatedBySync($userId) |
| 169 | ); |
| 170 | $this->subscribersRepository->flush(); |
| 171 | } |
| 172 | |
| 173 | private function getTrackingConsentField(): string { |
| 174 | if (!$this->trackingConsentCapture->isCaptureEnabled()) { |
| 175 | return ''; |
| 176 | } |
| 177 | $copy = $this->trackingConsentCapture->getCopy( |
| 178 | SubscriberEntity::TRACKING_CONSENT_METHOD_REGISTRATION |
| 179 | ); |
| 180 | |
| 181 | return '<p class="registration-form-mailpoet-tracking-consent"> |
| 182 | <label for="mailpoet_tracking_consent"> |
| 183 | <input |
| 184 | type="checkbox" |
| 185 | id="mailpoet_tracking_consent" |
| 186 | value="1" |
| 187 | name="mailpoet[tracking_consent]" |
| 188 | /> ' . esc_html($copy) . ' |
| 189 | </label> |
| 190 | </p>'; |
| 191 | } |
| 192 | |
| 193 | private function subscribeNewUser($name, $email, bool $trackingConsent = false) { |
| 194 | $segmentIds = $this->settings->get( |
| 195 | 'subscribe.on_register.segments', |
| 196 | [] |
| 197 | ); |
| 198 | $method = SubscriberEntity::TRACKING_CONSENT_METHOD_REGISTRATION; |
| 199 | $consentData = $this->trackingConsentCapture->getConsentData( |
| 200 | $trackingConsent, |
| 201 | $method, |
| 202 | $this->trackingConsentCapture->getCopy($method), |
| 203 | $this->trackingConsentCapture->isNewSubscriber($email) |
| 204 | ); |
| 205 | |
| 206 | $this->subscriberActions->subscribe( |
| 207 | array_merge([ |
| 208 | 'email' => $email, |
| 209 | 'first_name' => $name, |
| 210 | ], $consentData), |
| 211 | $segmentIds |
| 212 | ); |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * On multisite headers are already sent at this point, tracking will start |
| 217 | * once the user has activated his account at a later stage. |
| 218 | **/ |
| 219 | if (!headers_sent()) { |
| 220 | // start subscriber tracking (by email, we don't have WP user ID yet) |
| 221 | $this->subscriberHandler->identifyByEmail($email); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 |