ConfirmationEmailTemplate
1 month ago
ImportExport
2 weeks ago
RestApi
4 days ago
Statistics
1 month ago
BulkActionController.php
4 days ago
BulkActionException.php
2 months ago
BulkConfirmationEmailResender.php
2 months ago
ConfirmationEmailCustomizer.php
2 months ago
ConfirmationEmailMailer.php
2 months ago
ConfirmationEmailResolver.php
2 months ago
EngagementDataBackfiller.php
2 months ago
InactiveSubscribersController.php
5 days ago
LinkTokens.php
2 months ago
NewSubscriberNotificationMailer.php
2 months ago
RequiredCustomFieldValidator.php
2 months ago
SegmentsCountRecalculator.php
2 weeks ago
Source.php
2 months ago
SubscriberActions.php
2 months ago
SubscriberCustomFieldRepository.php
3 years ago
SubscriberIPsRepository.php
2 years ago
SubscriberLimitNotificationEvaluator.php
2 months ago
SubscriberLimitNotificationMailer.php
2 months ago
SubscriberLimitNotificationScheduler.php
2 months ago
SubscriberListingRepository.php
2 weeks ago
SubscriberPersonalDataEraser.php
2 months ago
SubscriberSaveController.php
4 days ago
SubscriberSegmentRepository.php
2 weeks ago
SubscriberSubscribeController.php
2 weeks ago
SubscriberTagRepository.php
4 years ago
SubscribersCountsController.php
2 weeks ago
SubscribersEmailCountsController.php
2 weeks ago
SubscribersRepository.php
4 days ago
TrackingConsentController.php
5 days ago
index.php
3 years ago
SubscriberSaveController.php
421 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\ConflictException; |
| 9 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 10 | use MailPoet\Doctrine\Validator\ValidationException; |
| 11 | use MailPoet\Entities\StatisticsUnsubscribeEntity; |
| 12 | use MailPoet\Entities\SubscriberEntity; |
| 13 | use MailPoet\Entities\SubscriberSegmentEntity; |
| 14 | use MailPoet\Entities\SubscriberTagEntity; |
| 15 | use MailPoet\Newsletter\Scheduler\WelcomeScheduler; |
| 16 | use MailPoet\Segments\SegmentsRepository; |
| 17 | use MailPoet\Settings\SettingsController; |
| 18 | use MailPoet\Statistics\Track\Unsubscribes; |
| 19 | use MailPoet\Tags\TagRepository; |
| 20 | use MailPoet\Util\Security; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | use MailPoetVendor\Carbon\Carbon; |
| 23 | |
| 24 | class SubscriberSaveController { |
| 25 | /** @var CustomFieldsRepository */ |
| 26 | private $customFieldsRepository; |
| 27 | |
| 28 | /** @var Security */ |
| 29 | private $security; |
| 30 | |
| 31 | /** @var SettingsController */ |
| 32 | private $settings; |
| 33 | |
| 34 | /** @var SegmentsRepository */ |
| 35 | private $segmentsRepository; |
| 36 | |
| 37 | /** @var SubscriberCustomFieldRepository */ |
| 38 | private $subscriberCustomFieldRepository; |
| 39 | |
| 40 | /** @var SubscribersRepository */ |
| 41 | private $subscribersRepository; |
| 42 | |
| 43 | /** @var SubscriberSegmentRepository */ |
| 44 | private $subscriberSegmentRepository; |
| 45 | |
| 46 | /** @var SubscriberTagRepository */ |
| 47 | private $subscriberTagRepository; |
| 48 | |
| 49 | /** @var TagRepository */ |
| 50 | private $tagRepository; |
| 51 | |
| 52 | /** @var Unsubscribes */ |
| 53 | private $unsubscribesTracker; |
| 54 | |
| 55 | /** @var WelcomeScheduler */ |
| 56 | private $welcomeScheduler; |
| 57 | |
| 58 | /** @var WPFunctions */ |
| 59 | private $wp; |
| 60 | |
| 61 | public function __construct( |
| 62 | CustomFieldsRepository $customFieldsRepository, |
| 63 | Security $security, |
| 64 | SettingsController $settings, |
| 65 | SegmentsRepository $segmentsRepository, |
| 66 | SubscriberCustomFieldRepository $subscriberCustomFieldRepository, |
| 67 | SubscribersRepository $subscribersRepository, |
| 68 | SubscriberSegmentRepository $subscriberSegmentRepository, |
| 69 | SubscriberTagRepository $subscriberTagRepository, |
| 70 | TagRepository $tagRepository, |
| 71 | Unsubscribes $unsubscribesTracker, |
| 72 | WelcomeScheduler $welcomeScheduler, |
| 73 | WPFunctions $wp |
| 74 | ) { |
| 75 | $this->customFieldsRepository = $customFieldsRepository; |
| 76 | $this->security = $security; |
| 77 | $this->settings = $settings; |
| 78 | $this->segmentsRepository = $segmentsRepository; |
| 79 | $this->subscriberSegmentRepository = $subscriberSegmentRepository; |
| 80 | $this->subscribersRepository = $subscribersRepository; |
| 81 | $this->subscriberCustomFieldRepository = $subscriberCustomFieldRepository; |
| 82 | $this->tagRepository = $tagRepository; |
| 83 | $this->unsubscribesTracker = $unsubscribesTracker; |
| 84 | $this->welcomeScheduler = $welcomeScheduler; |
| 85 | $this->wp = $wp; |
| 86 | $this->subscriberTagRepository = $subscriberTagRepository; |
| 87 | } |
| 88 | |
| 89 | public function filterOutReservedColumns(array $subscriberData): array { |
| 90 | $reservedColumns = [ |
| 91 | 'id', |
| 92 | 'wp_user_id', |
| 93 | 'is_woocommerce_user', |
| 94 | 'status', |
| 95 | 'subscribed_ip', |
| 96 | 'confirmed_ip', |
| 97 | 'confirmed_at', |
| 98 | 'created_at', |
| 99 | 'updated_at', |
| 100 | 'deleted_at', |
| 101 | 'unconfirmed_data', |
| 102 | ]; |
| 103 | return array_diff_key( |
| 104 | $subscriberData, |
| 105 | array_flip($reservedColumns) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @throws ConflictException |
| 111 | * @throws ValidationException |
| 112 | * @throws \Exception |
| 113 | */ |
| 114 | public function save(array $data): SubscriberEntity { |
| 115 | if (!empty($data)) { |
| 116 | $data = $this->wp->stripslashesDeep($data); |
| 117 | } |
| 118 | |
| 119 | // Proof-of-consent fields are stamped server-side only. The manage page sets |
| 120 | // them by calling createOrUpdate() directly; this client-data path (admin/API) |
| 121 | // may change the consent state but must never forge the record of how or |
| 122 | // against what wording it was given. |
| 123 | unset($data['tracking_consent_method'], $data['tracking_consent_copy']); |
| 124 | |
| 125 | if (empty($data['segments'])) { |
| 126 | $data['segments'] = []; |
| 127 | } |
| 128 | $data['segments'] = array_merge($data['segments'], $this->getNonDefaultSubscribedSegments($data)); |
| 129 | $newSegments = $this->findNewSegments($data); |
| 130 | |
| 131 | if (empty($data['tags'])) { |
| 132 | $data['tags'] = []; |
| 133 | } |
| 134 | |
| 135 | $oldSubscriber = $this->findSubscriber($data); |
| 136 | $oldStatus = $oldSubscriber ? $oldSubscriber->getStatus() : null; |
| 137 | if ( |
| 138 | $oldSubscriber instanceof SubscriberEntity |
| 139 | && isset($data['status']) |
| 140 | && ($data['status'] === SubscriberEntity::STATUS_UNSUBSCRIBED) |
| 141 | && ($oldSubscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) |
| 142 | ) { |
| 143 | $currentUser = $this->wp->wpGetCurrentUser(); |
| 144 | $this->unsubscribesTracker->track( |
| 145 | (int)$oldSubscriber->getId(), |
| 146 | StatisticsUnsubscribeEntity::SOURCE_ADMINISTRATOR, |
| 147 | null, |
| 148 | $currentUser->display_name // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | if (isset($data['email']) && $this->isNewEmail($data['email'], $oldSubscriber)) { |
| 153 | $this->verifyEmailIsUnique($data['email']); |
| 154 | } |
| 155 | |
| 156 | $subscriber = $this->createOrUpdate($data, $oldSubscriber); |
| 157 | |
| 158 | $this->updateCustomFields($data, $subscriber); |
| 159 | $this->updateTags($data['tags'] ?? [], $subscriber); |
| 160 | |
| 161 | $segments = isset($data['segments']) ? $this->findSegments($data['segments']) : null; |
| 162 | // check for status change |
| 163 | if ( |
| 164 | $oldStatus === SubscriberEntity::STATUS_SUBSCRIBED |
| 165 | && $subscriber->getStatus() === SubscriberEntity::STATUS_UNSUBSCRIBED |
| 166 | ) { |
| 167 | // make sure we unsubscribe the user from all segments |
| 168 | $this->subscriberSegmentRepository->unsubscribeFromSegments($subscriber); |
| 169 | } elseif ($segments !== null) { |
| 170 | $this->subscriberSegmentRepository->resetSubscriptions($subscriber, $segments); |
| 171 | } |
| 172 | |
| 173 | if (!empty($newSegments)) { |
| 174 | $this->welcomeScheduler->scheduleSubscriberWelcomeNotification($subscriber->getId(), $newSegments); |
| 175 | } |
| 176 | |
| 177 | // when global status changes to subscribed, fire subscribed hook for all subscribed segments |
| 178 | if ( |
| 179 | $subscriber->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED |
| 180 | && $oldStatus !== null // don't trigger for new subscribers (handled in subscriber segments repository) |
| 181 | && $oldStatus !== SubscriberEntity::STATUS_SUBSCRIBED |
| 182 | ) { |
| 183 | $segments = $subscriber->getSubscriberSegments(); |
| 184 | foreach ($segments as $subscriberSegment) { |
| 185 | if ($subscriberSegment->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED) { |
| 186 | $this->wp->doAction('mailpoet_segment_subscribed', $subscriberSegment); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return $subscriber; |
| 192 | } |
| 193 | |
| 194 | private function getNonDefaultSubscribedSegments(array $data): array { |
| 195 | if (!isset($data['id']) || (int)$data['id'] <= 0) { |
| 196 | return []; |
| 197 | } |
| 198 | |
| 199 | $subscribedSegments = $this->subscriberSegmentRepository->getNonDefaultSubscribedSegments($data['id']); |
| 200 | return array_filter(array_map(function(SubscriberSegmentEntity $subscriberSegment): int { |
| 201 | $segment = $subscriberSegment->getSegment(); |
| 202 | if (!$segment) { |
| 203 | return 0; |
| 204 | } |
| 205 | return (int)$segment->getId(); |
| 206 | }, $subscribedSegments)); |
| 207 | } |
| 208 | |
| 209 | private function findSegments(array $segmentIds): array { |
| 210 | return $this->segmentsRepository->findByIds($segmentIds); |
| 211 | } |
| 212 | |
| 213 | private function findNewSegments(array $data): array { |
| 214 | $oldSegmentIds = []; |
| 215 | if (isset($data['id']) && (int)$data['id'] > 0) { |
| 216 | $subscribersSegments = $this->subscriberSegmentRepository->findBy(['subscriber' => $data['id']]); |
| 217 | foreach ($subscribersSegments as $subscribersSegment) { |
| 218 | $segment = $subscribersSegment->getSegment(); |
| 219 | if (!$segment) { |
| 220 | continue; |
| 221 | } |
| 222 | $oldSegmentIds[] = (int)$segment->getId(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return array_diff($data['segments'], $oldSegmentIds); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @throws ValidationException |
| 231 | */ |
| 232 | public function createOrUpdate(array $data, ?SubscriberEntity $subscriber): SubscriberEntity { |
| 233 | if (!$subscriber) { |
| 234 | $subscriber = $this->createSubscriber(); |
| 235 | if (!isset($data['source'])) $data['source'] = Source::ADMINISTRATOR; |
| 236 | } |
| 237 | |
| 238 | if (isset($data['email'])) $subscriber->setEmail($data['email']); |
| 239 | if (isset($data['first_name'])) $subscriber->setFirstName(sanitize_text_field($data['first_name'])); |
| 240 | if (isset($data['last_name'])) $subscriber->setLastName(sanitize_text_field($data['last_name'])); |
| 241 | if (isset($data['status'])) $subscriber->setStatus($data['status']); |
| 242 | if (isset($data['tracking_consent'])) { |
| 243 | $subscriber->setTrackingConsent( |
| 244 | (string)$data['tracking_consent'], |
| 245 | $data['tracking_consent_method'] ?? SubscriberEntity::TRACKING_CONSENT_METHOD_ADMIN, |
| 246 | $data['tracking_consent_copy'] ?? null |
| 247 | ); |
| 248 | } |
| 249 | if (isset($data['source'])) $subscriber->setSource($data['source']); |
| 250 | if (isset($data['wp_user_id'])) $subscriber->setWpUserId($data['wp_user_id']); |
| 251 | if (isset($data['subscribed_ip'])) $subscriber->setSubscribedIp($data['subscribed_ip']); |
| 252 | if (isset($data['confirmed_ip'])) $subscriber->setConfirmedIp($data['confirmed_ip']); |
| 253 | if (isset($data['is_woocommerce_user'])) $subscriber->setIsWoocommerceUser((bool)$data['is_woocommerce_user']); |
| 254 | if ($this->settings->isSettingEnabled('collect_subscriber_timezones.enabled')) { |
| 255 | $timeZone = SubscriberEntity::sanitizeTimeZone($data[SubscriberEntity::TIME_ZONE_FIELD_NAME] ?? null); |
| 256 | if ($timeZone !== null) { |
| 257 | $subscriber->setTimeZone($timeZone); |
| 258 | $subscriber->setTimeZoneSource(SubscriberEntity::TIME_ZONE_SOURCE_FORM); |
| 259 | $subscriber->setTimeZoneConfidence(SubscriberEntity::TIME_ZONE_CONFIDENCE_BROWSER); |
| 260 | $subscriber->setTimeZoneUpdatedAt(Carbon::now()->millisecond(0)); |
| 261 | } |
| 262 | } |
| 263 | if (array_key_exists('timezone', $data)) { |
| 264 | $this->updateTimeZoneManually($subscriber, $data['timezone']); |
| 265 | } |
| 266 | $createdAt = isset($data['created_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['created_at']) : null; |
| 267 | if ($createdAt) $subscriber->setCreatedAt($createdAt); |
| 268 | $confirmedAt = isset($data['confirmed_at']) ? Carbon::createFromFormat('Y-m-d H:i:s', $data['confirmed_at']) : null; |
| 269 | if ($confirmedAt) $subscriber->setConfirmedAt($confirmedAt); |
| 270 | |
| 271 | // wipe any unconfirmed data at this point |
| 272 | $subscriber->setUnconfirmedData(null); |
| 273 | |
| 274 | // Validate the email (Saving group) + everything else (Default group) |
| 275 | $subscriber->setValidationGroups(['Saving', 'Default']); |
| 276 | |
| 277 | try { |
| 278 | $this->subscribersRepository->persist($subscriber); |
| 279 | $this->subscribersRepository->flush(); |
| 280 | } catch (ValidationException $exception) { |
| 281 | // detach invalid entity because it can block another work with doctrine |
| 282 | $this->subscribersRepository->detach($subscriber); |
| 283 | throw $exception; |
| 284 | } |
| 285 | |
| 286 | return $subscriber; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @param mixed $value |
| 291 | */ |
| 292 | private function updateTimeZoneManually(SubscriberEntity $subscriber, $value): void { |
| 293 | if ($value === '' || $value === null) { |
| 294 | if ($subscriber->getTimeZone() === null) { |
| 295 | return; |
| 296 | } |
| 297 | $subscriber->setTimeZone(null); |
| 298 | $subscriber->setTimeZoneSource(null); |
| 299 | $subscriber->setTimeZoneConfidence(null); |
| 300 | $subscriber->setTimeZoneUpdatedAt(null); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | $timeZone = SubscriberEntity::sanitizeTimeZone($value); |
| 305 | if ($timeZone === null || $timeZone === $subscriber->getTimeZone()) { |
| 306 | return; |
| 307 | } |
| 308 | $subscriber->setTimeZone($timeZone); |
| 309 | $subscriber->setTimeZoneSource(SubscriberEntity::TIME_ZONE_SOURCE_MANUAL); |
| 310 | $subscriber->setTimeZoneConfidence(SubscriberEntity::TIME_ZONE_CONFIDENCE_MANUAL); |
| 311 | $subscriber->setTimeZoneUpdatedAt(Carbon::now()->millisecond(0)); |
| 312 | } |
| 313 | |
| 314 | private function isNewEmail(string $email, ?SubscriberEntity $subscriber): bool { |
| 315 | if ($subscriber && ($subscriber->getEmail() === $email)) return false; |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @throws ConflictException |
| 321 | */ |
| 322 | private function verifyEmailIsUnique(string $email): void { |
| 323 | $existingSubscriber = $this->subscribersRepository->findOneBy(['email' => $email]); |
| 324 | if ($existingSubscriber) { |
| 325 | // translators: %s is email address which already exists. |
| 326 | $exceptionMessage = sprintf(__('A subscriber with E-mail "%s" already exists.', 'mailpoet'), $email); |
| 327 | throw new ConflictException($exceptionMessage); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private function createSubscriber(): SubscriberEntity { |
| 332 | $subscriber = new SubscriberEntity(); |
| 333 | $subscriber->setUnsubscribeToken($this->security->generateUnsubscribeTokenByEntity($subscriber)); |
| 334 | $subscriber->setLinkToken(Security::generateHash(SubscriberEntity::LINK_TOKEN_LENGTH)); |
| 335 | $subscriber->setStatus(!$this->settings->get('signup_confirmation.enabled') ? SubscriberEntity::STATUS_SUBSCRIBED : SubscriberEntity::STATUS_UNCONFIRMED); |
| 336 | |
| 337 | return $subscriber; |
| 338 | } |
| 339 | |
| 340 | private function findSubscriber(array &$data): ?SubscriberEntity { |
| 341 | if (isset($data['id']) && (int)$data['id'] > 0) { |
| 342 | $subscriber = $this->subscribersRepository->findOneById((int)$data['id']); |
| 343 | unset($data['id']); |
| 344 | return $subscriber; |
| 345 | } |
| 346 | |
| 347 | return null; |
| 348 | } |
| 349 | |
| 350 | public function updateCustomFields(array $data, SubscriberEntity $subscriber): void { |
| 351 | $customFieldsMap = []; |
| 352 | foreach ($data as $key => $value) { |
| 353 | if (strpos($key, 'cf_') === 0) { |
| 354 | $customFieldsMap[(int)substr($key, 3)] = $value; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (empty($customFieldsMap)) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | $customFields = $this->customFieldsRepository->findBy(['id' => array_keys($customFieldsMap), 'deletedAt' => null]); |
| 363 | foreach ($customFields as $customField) { |
| 364 | $customFieldId = $customField->getId(); |
| 365 | if ($customFieldId === null || !array_key_exists($customFieldId, $customFieldsMap)) { |
| 366 | continue; |
| 367 | } |
| 368 | $this->subscriberCustomFieldRepository->createOrUpdate($subscriber, $customField, $customFieldsMap[$customFieldId]); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Replaces the subscriber's tags with the given list. |
| 374 | * |
| 375 | * $tags is either an array of arrays containing name, id etc. of the tag or an array of strings - the names |
| 376 | * of the tag. The tag names are used to upsert tags; any existing tag not present in the list is removed. |
| 377 | * |
| 378 | * Fires the `mailpoet_subscriber_tag_added` and `mailpoet_subscriber_tag_removed` WP actions, which is how |
| 379 | * tag automations are triggered. |
| 380 | */ |
| 381 | public function updateTags(array $tags, SubscriberEntity $subscriber): void { |
| 382 | $removedTags = []; |
| 383 | |
| 384 | $tags = array_map( |
| 385 | function($tag): string { |
| 386 | if (is_array($tag)) { |
| 387 | return array_key_exists('name', $tag) ? (string)$tag['name'] : ''; |
| 388 | } |
| 389 | return (string)$tag; |
| 390 | }, |
| 391 | $tags |
| 392 | ); |
| 393 | foreach ($subscriber->getSubscriberTags() as $subscriberTag) { |
| 394 | $tag = $subscriberTag->getTag(); |
| 395 | if (!$tag || !in_array($tag->getName(), $tags, true)) { |
| 396 | $subscriber->getSubscriberTags()->removeElement($subscriberTag); |
| 397 | $removedTags[] = $subscriberTag; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | $newlyAddedTags = []; |
| 402 | foreach ($tags as $tagName) { |
| 403 | $tag = $this->tagRepository->createOrUpdate(['name' => $tagName]); |
| 404 | $subscriberTag = $subscriber->getSubscriberTag($tag); |
| 405 | if (!$subscriberTag) { |
| 406 | $subscriberTag = new SubscriberTagEntity($tag, $subscriber); |
| 407 | $subscriber->getSubscriberTags()->add($subscriberTag); |
| 408 | $this->subscriberTagRepository->persist($subscriberTag); |
| 409 | $newlyAddedTags[] = $subscriberTag; |
| 410 | } |
| 411 | } |
| 412 | $this->subscriberTagRepository->flush(); |
| 413 | foreach ($newlyAddedTags as $subscriberTag) { |
| 414 | $this->wp->doAction('mailpoet_subscriber_tag_added', $subscriberTag); |
| 415 | } |
| 416 | foreach ($removedTags as $subscriberTag) { |
| 417 | $this->wp->doAction('mailpoet_subscriber_tag_removed', $subscriberTag); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 |