ConfirmationEmailTemplate
2 months ago
ImportExport
1 month ago
RestApi
4 weeks ago
Statistics
2 months ago
BulkActionController.php
4 weeks ago
BulkActionException.php
2 months ago
BulkConfirmationEmailResender.php
3 months ago
ConfirmationEmailCustomizer.php
3 months ago
ConfirmationEmailMailer.php
3 months ago
ConfirmationEmailResolver.php
3 months ago
EngagementDataBackfiller.php
3 months ago
InactiveSubscribersController.php
1 month ago
LinkTokens.php
3 months ago
NewSubscriberNotificationMailer.php
3 months ago
RequiredCustomFieldValidator.php
3 months ago
SegmentsCountRecalculator.php
1 month ago
Source.php
3 months ago
SubscriberActions.php
3 months ago
SubscriberCustomFieldRepository.php
3 years ago
SubscriberIPsRepository.php
2 years ago
SubscriberLimitNotificationEvaluator.php
3 months ago
SubscriberLimitNotificationMailer.php
3 months ago
SubscriberLimitNotificationScheduler.php
3 months ago
SubscriberListingRepository.php
1 month ago
SubscriberPersonalDataEraser.php
3 months ago
SubscriberSaveController.php
4 weeks ago
SubscriberSegmentRepository.php
1 month ago
SubscriberSubscribeController.php
3 days ago
SubscriberTagRepository.php
4 years ago
SubscribersCountsController.php
1 month ago
SubscribersEmailCountsController.php
1 month ago
SubscribersRepository.php
4 weeks ago
TrackingConsentCapture.php
3 days ago
TrackingConsentController.php
1 week ago
index.php
3 years ago
TrackingConsentCapture.php
134 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\SubscriberEntity; |
| 9 | use MailPoet\Subscription\ManageSubscriptionFormRenderer; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | |
| 12 | /** |
| 13 | * Captures tracking consent at signup surfaces (CNIL/Garante). |
| 14 | * |
| 15 | * Consent must be its own control everywhere and must never be derived from the |
| 16 | * general subscribe opt-in, so every collection point asks separately and comes |
| 17 | * through here to decide what to store. Keeping the decision in one place is |
| 18 | * what stops the four surfaces drifting apart. |
| 19 | */ |
| 20 | class TrackingConsentCapture { |
| 21 | /** |
| 22 | * The field id the consent checkbox uses on both the manage-subscription page |
| 23 | * and subscription forms, and therefore the key the posted value arrives on. |
| 24 | */ |
| 25 | const FIELD_ID = 'tracking_consent'; |
| 26 | |
| 27 | private TrackingConsentController $trackingConsentController; |
| 28 | |
| 29 | private SubscribersRepository $subscribersRepository; |
| 30 | |
| 31 | private WPFunctions $wp; |
| 32 | |
| 33 | public function __construct( |
| 34 | TrackingConsentController $trackingConsentController, |
| 35 | SubscribersRepository $subscribersRepository, |
| 36 | WPFunctions $wp |
| 37 | ) { |
| 38 | $this->trackingConsentController = $trackingConsentController; |
| 39 | $this->subscribersRepository = $subscribersRepository; |
| 40 | $this->wp = $wp; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Whether a consent control may be shown at a collection point at all. Sites |
| 45 | * that track everyone without asking never show one, so nobody is prompted |
| 46 | * about a choice the site owner does not offer. |
| 47 | */ |
| 48 | public function isCaptureEnabled(): bool { |
| 49 | return $this->trackingConsentController->areSubscriberControlsVisible(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * The wording shown next to the checkbox, and therefore the wording stored as |
| 54 | * proof. Always read it through here so the stored copy is what was rendered, |
| 55 | * including when a site owner has filtered it. |
| 56 | * |
| 57 | * @param string $method One of SubscriberEntity::TRACKING_CONSENT_METHOD_*. |
| 58 | * @param string|null $override Copy configured for this surface, if any. |
| 59 | */ |
| 60 | public function getCopy(string $method, ?string $override = null): string { |
| 61 | $copy = ($override !== null && trim($override) !== '') |
| 62 | ? $override |
| 63 | : ManageSubscriptionFormRenderer::getTrackingConsentCopy(); |
| 64 | |
| 65 | /** |
| 66 | * Filters the tracking-consent wording shown to subscribers at a |
| 67 | * collection point. |
| 68 | * |
| 69 | * Whatever this returns is both displayed and stored as the proof of what |
| 70 | * the subscriber agreed to, so changing it changes the consent record for |
| 71 | * everyone who consents afterwards. Records already stored keep the wording |
| 72 | * they were given. |
| 73 | * |
| 74 | * @param string $copy The wording about to be shown. |
| 75 | * @param string $method The collection point, one of the |
| 76 | * SubscriberEntity::TRACKING_CONSENT_METHOD_* values. |
| 77 | */ |
| 78 | $filtered = $this->wp->applyFilters('mailpoet_tracking_consent_copy', $copy, $method); |
| 79 | return is_string($filtered) && trim($filtered) !== '' ? $filtered : $copy; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Consent fields to merge into subscriber data on a signup path, or an empty |
| 84 | * array when nothing should be written. |
| 85 | * |
| 86 | * Granting is always recorded. Declining is recorded only for a subscriber |
| 87 | * being created now: someone already on the list who submits a form again |
| 88 | * keeps whatever they chose before, because an unticked box on a signup form |
| 89 | * is not a withdrawal of consent given somewhere else. |
| 90 | * |
| 91 | * @return array<string, string> |
| 92 | */ |
| 93 | public function getConsentData(bool $granted, string $method, string $copy, bool $isNewSubscriber): array { |
| 94 | if (!$this->isCaptureEnabled()) { |
| 95 | return []; |
| 96 | } |
| 97 | if (!$granted && !$isNewSubscriber) { |
| 98 | return []; |
| 99 | } |
| 100 | return [ |
| 101 | self::FIELD_ID => $granted |
| 102 | ? SubscriberEntity::TRACKING_CONSENT_GRANTED |
| 103 | : SubscriberEntity::TRACKING_CONSENT_DENIED, |
| 104 | 'tracking_consent_method' => $method, |
| 105 | 'tracking_consent_copy' => $copy, |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Applies consent straight to an entity, for paths that persist the |
| 111 | * subscriber themselves instead of going through SubscriberSaveController |
| 112 | * (WooCommerce checkout). |
| 113 | */ |
| 114 | public function applyToSubscriber(SubscriberEntity $subscriber, bool $granted, string $method, string $copy, bool $isNewSubscriber): void { |
| 115 | $data = $this->getConsentData($granted, $method, $copy, $isNewSubscriber); |
| 116 | if (!$data) { |
| 117 | return; |
| 118 | } |
| 119 | $subscriber->setTrackingConsent($data[self::FIELD_ID], $method, $copy); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Whether this email would be a newly created subscriber. Signup paths look |
| 124 | * the subscriber up again inside SubscriberActions::subscribe(), so the |
| 125 | * answer has to be taken before that call. |
| 126 | */ |
| 127 | public function isNewSubscriber(?string $email): bool { |
| 128 | if ($email === null || $email === '') { |
| 129 | return true; |
| 130 | } |
| 131 | return $this->subscribersRepository->findOneBy(['email' => $email]) === null; |
| 132 | } |
| 133 | } |
| 134 |