DynamicSegments
1 month ago
RestApi
1 month ago
SegmentDependencyValidator.php
3 years ago
SegmentListingRepository.php
1 month ago
SegmentSaveController.php
2 weeks ago
SegmentSubscribersRepository.php
2 weeks ago
SegmentsFinder.php
3 years ago
SegmentsRepository.php
2 weeks ago
SegmentsSimpleListRepository.php
2 months ago
SubscribersFinder.php
1 month ago
WP.php
2 weeks ago
WooCommerce.php
2 weeks ago
index.php
3 years ago
WP.php
597 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Segments; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\SubscriberChangesNotifier; |
| 9 | use MailPoet\DI\ContainerWrapper; |
| 10 | use MailPoet\Doctrine\WPDB\Connection; |
| 11 | use MailPoet\Entities\SegmentEntity; |
| 12 | use MailPoet\Entities\SubscriberEntity; |
| 13 | use MailPoet\Entities\SubscriberSegmentEntity; |
| 14 | use MailPoet\Logging\LoggerFactory; |
| 15 | use MailPoet\Newsletter\Scheduler\WelcomeScheduler; |
| 16 | use MailPoet\Services\Validator; |
| 17 | use MailPoet\Settings\SettingsController; |
| 18 | use MailPoet\Subscribers\ConfirmationEmailMailer; |
| 19 | use MailPoet\Subscribers\SegmentsCountRecalculator; |
| 20 | use MailPoet\Subscribers\Source; |
| 21 | use MailPoet\Subscribers\SubscriberSegmentRepository; |
| 22 | use MailPoet\Subscribers\SubscribersRepository; |
| 23 | use MailPoet\Util\DBCollationChecker; |
| 24 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 25 | use MailPoet\WP\Functions as WPFunctions; |
| 26 | use MailPoetVendor\Carbon\Carbon; |
| 27 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 28 | |
| 29 | class WP { |
| 30 | |
| 31 | /** @var WPFunctions */ |
| 32 | private $wp; |
| 33 | |
| 34 | /** @var WelcomeScheduler */ |
| 35 | private $welcomeScheduler; |
| 36 | |
| 37 | /** @var WooCommerceHelper */ |
| 38 | private $wooHelper; |
| 39 | |
| 40 | /** @var SubscribersRepository */ |
| 41 | private $subscribersRepository; |
| 42 | |
| 43 | /** @var SubscriberChangesNotifier */ |
| 44 | private $subscriberChangesNotifier; |
| 45 | |
| 46 | /** @var SubscriberSegmentRepository */ |
| 47 | private $subscriberSegmentRepository; |
| 48 | |
| 49 | /** @var Validator */ |
| 50 | private $validator; |
| 51 | |
| 52 | /** @var SegmentsRepository */ |
| 53 | private $segmentsRepository; |
| 54 | |
| 55 | /** @var EntityManager */ |
| 56 | private $entityManager; |
| 57 | |
| 58 | /** @var DBCollationChecker */ |
| 59 | private $collationChecker; |
| 60 | |
| 61 | /** @var string */ |
| 62 | private $subscribersTable; |
| 63 | |
| 64 | /** @var \MailPoetVendor\Doctrine\DBAL\Connection */ |
| 65 | private $databaseConnection; |
| 66 | |
| 67 | /** @var SegmentsCountRecalculator */ |
| 68 | private $segmentsCountRecalculator; |
| 69 | |
| 70 | public function __construct( |
| 71 | WPFunctions $wp, |
| 72 | WelcomeScheduler $welcomeScheduler, |
| 73 | WooCommerceHelper $wooHelper, |
| 74 | SubscribersRepository $subscribersRepository, |
| 75 | SubscriberSegmentRepository $subscriberSegmentRepository, |
| 76 | SubscriberChangesNotifier $subscriberChangesNotifier, |
| 77 | Validator $validator, |
| 78 | SegmentsRepository $segmentsRepository, |
| 79 | EntityManager $entityManager, |
| 80 | DBCollationChecker $collationChecker, |
| 81 | SegmentsCountRecalculator $segmentsCountRecalculator |
| 82 | ) { |
| 83 | $this->wp = $wp; |
| 84 | $this->welcomeScheduler = $welcomeScheduler; |
| 85 | $this->wooHelper = $wooHelper; |
| 86 | $this->subscribersRepository = $subscribersRepository; |
| 87 | $this->subscriberSegmentRepository = $subscriberSegmentRepository; |
| 88 | $this->subscriberChangesNotifier = $subscriberChangesNotifier; |
| 89 | $this->validator = $validator; |
| 90 | $this->segmentsRepository = $segmentsRepository; |
| 91 | $this->entityManager = $entityManager; |
| 92 | $this->collationChecker = $collationChecker; |
| 93 | $this->segmentsCountRecalculator = $segmentsCountRecalculator; |
| 94 | $this->databaseConnection = $this->entityManager->getConnection(); |
| 95 | $this->subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param int $wpUserId |
| 100 | * @param array|false $oldWpUserData |
| 101 | */ |
| 102 | public function synchronizeUser(int $wpUserId, $oldWpUserData = false): void { |
| 103 | $wpUser = \get_userdata($wpUserId); |
| 104 | if ($wpUser === false) return; |
| 105 | |
| 106 | $subscriber = $this->subscribersRepository->findOneBy(['wpUserId' => $wpUserId]); |
| 107 | |
| 108 | $currentFilter = $this->wp->currentFilter(); |
| 109 | // Delete |
| 110 | if (in_array($currentFilter, ['delete_user', 'deleted_user', 'remove_user_from_blog'])) { |
| 111 | if ($subscriber instanceof SubscriberEntity) { |
| 112 | $this->unlinkSubscriberFromWpUser($subscriber, $wpUser); |
| 113 | } |
| 114 | return; |
| 115 | } |
| 116 | $this->handleCreatingOrUpdatingSubscriber($currentFilter, $wpUser, $subscriber, $oldWpUserData); |
| 117 | |
| 118 | // In WP::synchronizeUser, after the subscriber is created |
| 119 | $this->wp->doAction('mailpoet_user_registered', $wpUserId, $subscriber); |
| 120 | } |
| 121 | |
| 122 | private function deleteSubscriber(SubscriberEntity $subscriber): void { |
| 123 | $this->entityManager->wrapInTransaction(function() use ($subscriber): void { |
| 124 | $this->subscriberSegmentRepository->deleteAllBySubscriber($subscriber); |
| 125 | $this->subscribersRepository->remove($subscriber); |
| 126 | $this->subscribersRepository->flush(); |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | private function unlinkSubscriberFromWpUser(SubscriberEntity $subscriber, \WP_User $wpUser): void { |
| 131 | // Backwards-compat escape hatch for sites that need the legacy hard-delete behavior |
| 132 | // (e.g. GDPR-driven account deletion flows). Returning true reproduces pre-STOMAIL-8018 behavior. |
| 133 | $hardDelete = (bool)$this->wp->applyFilters( |
| 134 | 'mailpoet_delete_subscriber_on_wp_user_delete', |
| 135 | false, |
| 136 | $subscriber, |
| 137 | (int)$wpUser->ID // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 138 | ); |
| 139 | if ($hardDelete) { |
| 140 | $this->deleteSubscriber($subscriber); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $this->entityManager->wrapInTransaction(function() use ($subscriber, $wpUser): void { |
| 145 | $wpSegment = $this->segmentsRepository->getWPUsersSegment(); |
| 146 | |
| 147 | // Remove only the WP-Users segment membership; other list subscriptions stay intact. |
| 148 | $this->entityManager->createQueryBuilder() |
| 149 | ->delete(SubscriberSegmentEntity::class, 'ss') |
| 150 | ->where('ss.subscriber = :subscriber AND ss.segment = :segment') |
| 151 | ->setParameter('subscriber', $subscriber) |
| 152 | ->setParameter('segment', $wpSegment) |
| 153 | ->getQuery() |
| 154 | ->execute(); |
| 155 | |
| 156 | $subscriber->setWpUserId(null); |
| 157 | $subscriber->setSource(Source::WORDPRESS_USER_DELETED); |
| 158 | |
| 159 | // If the subscriber was only on the WP-Users list and is not a WC customer, |
| 160 | // they had no list of their own to remain on — trash them instead of leaving a floating row. |
| 161 | // Skip when the subscriber was already trashed (e.g. manually by an admin) so we keep |
| 162 | // the original deleted_at and status as audit information. |
| 163 | $hasOtherActiveSegments = $this->hasOtherActiveSegments($subscriber); |
| 164 | $isWooCustomer = $this->wooHelper->isWooCommerceActive() && in_array('customer', $wpUser->roles, true); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 165 | if (!$hasOtherActiveSegments && !$isWooCustomer && $subscriber->getDeletedAt() === null) { |
| 166 | $subscriber->setStatus(SubscriberEntity::STATUS_UNCONFIRMED); |
| 167 | $subscriber->setDeletedAt(Carbon::now()->millisecond(0)); |
| 168 | } |
| 169 | |
| 170 | $this->subscribersRepository->persist($subscriber); |
| 171 | $this->subscribersRepository->flush(); |
| 172 | }); |
| 173 | $this->segmentsCountRecalculator->recalculateForSubscribers([(int)$subscriber->getId()]); |
| 174 | } |
| 175 | |
| 176 | private function hasOtherActiveSegments(SubscriberEntity $subscriber): bool { |
| 177 | $subscriberId = $subscriber->getId(); |
| 178 | if ($subscriberId === null) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | $count = $this->entityManager->createQueryBuilder() |
| 183 | ->select('COUNT(segment.id)') |
| 184 | ->from(SubscriberSegmentEntity::class, 'subscriberSegment') |
| 185 | ->innerJoin('subscriberSegment.segment', 'segment') |
| 186 | ->where('subscriberSegment.subscriber = :subscriber') |
| 187 | ->andWhere('segment.type != :wpType') |
| 188 | ->andWhere('segment.deletedAt IS NULL') |
| 189 | ->setParameter('subscriber', $subscriber) |
| 190 | ->setParameter('wpType', SegmentEntity::TYPE_WP_USERS) |
| 191 | ->getQuery() |
| 192 | ->getSingleScalarResult(); |
| 193 | |
| 194 | return is_numeric($count) && (int)$count > 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param string $currentFilter |
| 199 | * @param \WP_User $wpUser |
| 200 | * @param ?SubscriberEntity $subscriber |
| 201 | * @param array|false $oldWpUserData |
| 202 | */ |
| 203 | private function handleCreatingOrUpdatingSubscriber(string $currentFilter, \WP_User $wpUser, ?SubscriberEntity $subscriber = null, $oldWpUserData = false): void { |
| 204 | // Add or update |
| 205 | $wpSegment = $this->segmentsRepository->getWPUsersSegment(); |
| 206 | |
| 207 | // find subscriber by email when is null |
| 208 | if (is_null($subscriber)) { |
| 209 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $wpUser->user_email]); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 210 | } |
| 211 | |
| 212 | // get first name & last name |
| 213 | $firstName = html_entity_decode($wpUser->first_name, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 214 | $lastName = html_entity_decode($wpUser->last_name, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 215 | if (empty($wpUser->first_name) && empty($wpUser->last_name)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 216 | $firstName = html_entity_decode($wpUser->display_name, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 217 | } |
| 218 | $signupConfirmationEnabled = SettingsController::getInstance()->get('signup_confirmation.enabled'); |
| 219 | $status = $signupConfirmationEnabled ? SubscriberEntity::STATUS_UNCONFIRMED : SubscriberEntity::STATUS_SUBSCRIBED; |
| 220 | // we want to mark a new subscriber as unsubscribe when the checkbox from registration is unchecked |
| 221 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- type narrowing only, value is read as bool |
| 222 | $mailpoetPost = isset($_POST['mailpoet']) && is_array($_POST['mailpoet']) ? $_POST['mailpoet'] : []; |
| 223 | if (isset($mailpoetPost['subscribe_on_register_active']) && (bool)$mailpoetPost['subscribe_on_register_active'] === true) { |
| 224 | $status = SubscriberEntity::STATUS_UNSUBSCRIBED; |
| 225 | } |
| 226 | |
| 227 | // subscriber data |
| 228 | $wpUserId = (int)$wpUser->ID; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 229 | $data = [ |
| 230 | 'wp_user_id' => $wpUserId, |
| 231 | 'email' => $wpUser->user_email, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 232 | 'first_name' => $firstName, |
| 233 | 'last_name' => $lastName, |
| 234 | 'status' => $status, |
| 235 | 'source' => Source::WORDPRESS_USER, |
| 236 | ]; |
| 237 | |
| 238 | $isRelinkingDeletedWpUser = $subscriber !== null |
| 239 | && $subscriber->getSource() === Source::WORDPRESS_USER_DELETED |
| 240 | && $subscriber->getWpUserId() !== $wpUserId; |
| 241 | if (!is_null($subscriber)) { |
| 242 | $data['id'] = $subscriber->getId(); |
| 243 | unset($data['status']); // don't override status for existing users |
| 244 | unset($data['source']); // don't override source for existing users |
| 245 | if ($isRelinkingDeletedWpUser) { |
| 246 | // Restore the live WP-user source on any re-link, not only the auto-trashed case. |
| 247 | // Only revive deleted_at when the subscriber was actually trashed by the unlink |
| 248 | // (subscribers kept on other lists were never trashed and must keep deleted_at IS NULL). |
| 249 | $data['source'] = Source::WORDPRESS_USER; |
| 250 | if ($subscriber->getDeletedAt() !== null) { |
| 251 | $data['deleted_at'] = null; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | $addingNewUserToDisabledWPSegment = $wpSegment->getDeletedAt() !== null && $currentFilter === 'user_register'; |
| 257 | |
| 258 | $otherActiveSegments = []; |
| 259 | if ($subscriber) { |
| 260 | $otherActiveSegments = array_filter($subscriber->getSegments()->toArray() ?? [], function (SegmentEntity $segment) { |
| 261 | return $segment->getType() !== SegmentEntity::TYPE_WP_USERS && $segment->getDeletedAt() === null; |
| 262 | }); |
| 263 | } |
| 264 | $isWooCustomer = $this->wooHelper->isWooCommerceActive() && in_array('customer', $wpUser->roles, true); |
| 265 | // When WP Segment is disabled force trashed state and unconfirmed status for new WPUsers without active segment |
| 266 | // or who are not WooCommerce customers at the same time since customers are added to the WooCommerce list |
| 267 | if ($addingNewUserToDisabledWPSegment && !$isRelinkingDeletedWpUser && !$otherActiveSegments && !$isWooCustomer) { |
| 268 | $data['deleted_at'] = Carbon::now()->millisecond(0); |
| 269 | $data['status'] = SubscriberEntity::STATUS_UNCONFIRMED; |
| 270 | } |
| 271 | |
| 272 | // Apply filter to allow modifying subscriber data before save |
| 273 | $data = $this->wp->applyFilters('mailpoet_subscriber_data_before_save', $data); |
| 274 | |
| 275 | // Ensure data is an array |
| 276 | if (!is_array($data)) { |
| 277 | // If the filter returned a non-array, log it and use the original data |
| 278 | $logger = LoggerFactory::getInstance()->getLogger(); |
| 279 | $logger->error( |
| 280 | 'Filter mailpoet_subscriber_data_before_save returned non-array data.', |
| 281 | ['data_type' => gettype($data)] |
| 282 | ); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // When updating an existing subscriber's email, remove any other subscriber |
| 287 | // that already holds the new email to avoid a unique constraint violation. |
| 288 | // This can happen when a WP user registers with email A, checks out with |
| 289 | // email B (creating a second subscriber), then changes their account email |
| 290 | // from A to B. |
| 291 | // Uses bulkDelete() to properly clean up related data (segments, tags, |
| 292 | // custom fields) and to restrict deletion to safe duplicates (non-WP, |
| 293 | // non-WooCommerce subscribers). |
| 294 | if ($subscriber !== null && $subscriber->getEmail() !== $data['email']) { |
| 295 | $existingSubscriber = $this->subscribersRepository->findOneBy(['email' => $data['email']]); |
| 296 | if ($existingSubscriber !== null && $existingSubscriber->getId() !== $subscriber->getId()) { |
| 297 | $duplicateId = $existingSubscriber->getId(); |
| 298 | $this->entityManager->detach($existingSubscriber); |
| 299 | $deletedCount = $this->subscribersRepository->bulkDelete([$duplicateId]); |
| 300 | if ($deletedCount === 0) { |
| 301 | // The duplicate is a WP user or WooCommerce customer and cannot be |
| 302 | // safely removed. Skip the email update to avoid a constraint violation. |
| 303 | $logger = LoggerFactory::getInstance()->getLogger(); |
| 304 | $logger->warning( |
| 305 | 'Cannot update subscriber email: duplicate subscriber is a WP user or WooCommerce customer', |
| 306 | ['subscriber_id' => $subscriber->getId(), 'duplicate_id' => $duplicateId, 'email' => $data['email']] |
| 307 | ); |
| 308 | return; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | try { |
| 314 | $subscriber = $this->createOrUpdateSubscriber($data, $subscriber); |
| 315 | } catch (\Exception $e) { |
| 316 | return; // fails silently as this was the behavior before the Doctrine refactor. |
| 317 | } |
| 318 | |
| 319 | // add subscriber to the WP Users segment |
| 320 | $this->subscriberSegmentRepository->subscribeToSegments( |
| 321 | $subscriber, |
| 322 | [$wpSegment] |
| 323 | ); |
| 324 | |
| 325 | if (!$signupConfirmationEnabled && $subscriber->getStatus() === SubscriberEntity::STATUS_SUBSCRIBED && $currentFilter === 'user_register') { |
| 326 | $subscriberSegment = $this->subscriberSegmentRepository->findOneBy([ |
| 327 | 'subscriber' => $subscriber->getId(), |
| 328 | 'segment' => $wpSegment->getId(), |
| 329 | ]); |
| 330 | |
| 331 | if (!is_null($subscriberSegment)) { |
| 332 | $this->wp->doAction('mailpoet_segment_subscribed', $subscriberSegment); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $subscribeOnRegisterEnabled = SettingsController::getInstance()->get('subscribe.on_register.enabled'); |
| 337 | $sendConfirmationEmail = |
| 338 | $signupConfirmationEnabled |
| 339 | && $subscribeOnRegisterEnabled |
| 340 | && $currentFilter !== 'profile_update' |
| 341 | && !$addingNewUserToDisabledWPSegment; |
| 342 | |
| 343 | if ($sendConfirmationEmail && ($subscriber->getStatus() === SubscriberEntity::STATUS_UNCONFIRMED)) { |
| 344 | /** @var ConfirmationEmailMailer $confirmationEmailMailer */ |
| 345 | $confirmationEmailMailer = ContainerWrapper::getInstance()->get(ConfirmationEmailMailer::class); |
| 346 | try { |
| 347 | // Per-list confirmation settings are not resolved here because this path |
| 348 | // subscribes to the WordPress Users segment (TYPE_WP_USERS), |
| 349 | // which does not support custom confirmation overrides. |
| 350 | $confirmationEmailMailer->sendConfirmationEmailOnce($subscriber); |
| 351 | } catch (\Exception $e) { |
| 352 | // ignore errors |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // welcome email |
| 357 | $scheduleWelcomeNewsletter = false; |
| 358 | if (in_array($currentFilter, ['profile_update', 'user_register', 'add_user_role', 'set_user_role'])) { |
| 359 | $scheduleWelcomeNewsletter = true; |
| 360 | } |
| 361 | if ($scheduleWelcomeNewsletter === true) { |
| 362 | $this->welcomeScheduler->scheduleWPUserWelcomeNotification( |
| 363 | $subscriber->getId(), |
| 364 | (array)$wpUser, |
| 365 | (array)$oldWpUserData |
| 366 | ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private function createOrUpdateSubscriber(array $data, ?SubscriberEntity $subscriber = null): SubscriberEntity { |
| 371 | if (is_null($subscriber)) { |
| 372 | $subscriber = new SubscriberEntity(); |
| 373 | } |
| 374 | |
| 375 | $subscriber->setWpUserId($data['wp_user_id']); |
| 376 | $subscriber->setEmail($data['email']); |
| 377 | |
| 378 | // Only set first_name if it's present in the data array |
| 379 | if (isset($data['first_name'])) { |
| 380 | $subscriber->setFirstName($data['first_name']); |
| 381 | } |
| 382 | |
| 383 | // Only set last_name if it's present in the data array |
| 384 | if (isset($data['last_name'])) { |
| 385 | $subscriber->setLastName($data['last_name']); |
| 386 | } |
| 387 | |
| 388 | if (isset($data['status'])) { |
| 389 | $subscriber->setStatus($data['status']); |
| 390 | } |
| 391 | |
| 392 | if (isset($data['source'])) { |
| 393 | $subscriber->setSource($data['source']); |
| 394 | } |
| 395 | |
| 396 | if (array_key_exists('deleted_at', $data)) { |
| 397 | $subscriber->setDeletedAt($data['deleted_at']); |
| 398 | } |
| 399 | |
| 400 | $this->subscribersRepository->persist($subscriber); |
| 401 | $this->subscribersRepository->flush(); |
| 402 | |
| 403 | return $subscriber; |
| 404 | } |
| 405 | |
| 406 | public function synchronizeUsers(): bool { |
| 407 | // Temporarily skip synchronization in WP Playground. |
| 408 | // Some of the queries are not yet supported by the SQLite integration. |
| 409 | if (Connection::isSQLite()) { |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | // Save timestamp about changes and update before insert |
| 414 | $this->subscriberChangesNotifier->subscribersBatchCreate(); |
| 415 | $this->subscriberChangesNotifier->subscribersBatchUpdate(); |
| 416 | |
| 417 | $updatedUsersEmails = $this->updateSubscribersEmails(); |
| 418 | $insertedUsersEmails = $this->insertSubscribers(); |
| 419 | $this->removeUpdatedSubscribersWithInvalidEmail(array_merge($updatedUsersEmails, $insertedUsersEmails)); |
| 420 | // There is high chance that an update will be made |
| 421 | $this->subscriberChangesNotifier->subscribersBatchUpdate(); |
| 422 | unset($updatedUsersEmails); |
| 423 | unset($insertedUsersEmails); |
| 424 | $this->updateFirstNames(); |
| 425 | $this->updateLastNames(); |
| 426 | $this->updateFirstNameIfMissing(); |
| 427 | $this->insertUsersToSegment(); |
| 428 | $this->removeOrphanedSubscribers(); |
| 429 | // insertUsersToSegment adds WP users to the WP-Users segment via raw SQL, |
| 430 | // so refresh segments_count for that segment's members. |
| 431 | // recalculateForSegment() only sees subscribers that still have a membership |
| 432 | // row. Orphans that are hard-deleted by removeOrphanedSubscribers() are fine |
| 433 | // (row gone, count moot). Orphans whose membership is deleted but who survive |
| 434 | // (soft-trashed or still on other lists) are recalculated explicitly inside |
| 435 | // removeOrphanedSubscribersFromWpSegment() before the membership DELETE. |
| 436 | $this->segmentsCountRecalculator->recalculateForSegment((int)$this->segmentsRepository->getWPUsersSegment()->getId()); |
| 437 | $this->subscribersRepository->invalidateTotalSubscribersCache(); |
| 438 | $this->subscribersRepository->refreshAll(); |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | private function removeUpdatedSubscribersWithInvalidEmail(array $updatedEmails): void { |
| 444 | $invalidWpUserIds = array_map(function($item) { |
| 445 | return $item['id']; |
| 446 | }, |
| 447 | array_filter($updatedEmails, function($updatedEmail) { |
| 448 | return !$this->validator->validateEmail($updatedEmail['email']) && $updatedEmail['id'] !== null; |
| 449 | })); |
| 450 | if (!$invalidWpUserIds) { |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | $this->subscribersRepository->removeByWpUserIds($invalidWpUserIds); |
| 455 | } |
| 456 | |
| 457 | private function updateSubscribersEmails(): array { |
| 458 | global $wpdb; |
| 459 | |
| 460 | $stmt = $this->databaseConnection->executeQuery('SELECT NOW();'); |
| 461 | $startTime = $stmt->fetchOne(); |
| 462 | |
| 463 | if (!is_string($startTime)) { |
| 464 | throw new \RuntimeException("Failed to fetch the current time."); |
| 465 | } |
| 466 | |
| 467 | $updateSql = |
| 468 | "UPDATE IGNORE {$this->subscribersTable} s |
| 469 | INNER JOIN {$wpdb->users} as wu ON s.wp_user_id = wu.id |
| 470 | SET s.email = wu.user_email"; |
| 471 | $this->databaseConnection->executeStatement($updateSql); |
| 472 | |
| 473 | $selectSql = |
| 474 | "SELECT wp_user_id as id, email FROM {$this->subscribersTable} |
| 475 | WHERE updated_at >= '{$startTime}'"; |
| 476 | $updatedEmails = $this->databaseConnection->fetchAllAssociative($selectSql); |
| 477 | |
| 478 | return $updatedEmails; |
| 479 | } |
| 480 | |
| 481 | private function insertSubscribers(): array { |
| 482 | global $wpdb; |
| 483 | $wpSegment = $this->segmentsRepository->getWPUsersSegment(); |
| 484 | |
| 485 | if ($wpSegment->getDeletedAt() !== null) { |
| 486 | $subscriberStatus = SubscriberEntity::STATUS_UNCONFIRMED; |
| 487 | $deletedAt = 'CURRENT_TIMESTAMP()'; |
| 488 | } else { |
| 489 | $signupConfirmationEnabled = SettingsController::getInstance()->get('signup_confirmation.enabled'); |
| 490 | $subscriberStatus = $signupConfirmationEnabled ? SubscriberEntity::STATUS_UNCONFIRMED : SubscriberEntity::STATUS_SUBSCRIBED; |
| 491 | $deletedAt = 'null'; |
| 492 | } |
| 493 | |
| 494 | // Fetch users that are not in the subscribers table |
| 495 | $selectSql = |
| 496 | "SELECT u.id, u.user_email as email |
| 497 | FROM {$wpdb->users} u |
| 498 | LEFT JOIN {$this->subscribersTable} AS s ON s.wp_user_id = u.id |
| 499 | WHERE s.wp_user_id IS NULL AND u.user_email != ''"; |
| 500 | $insertedUserIds = $this->databaseConnection->fetchAllAssociative($selectSql); |
| 501 | |
| 502 | // Insert new users into the subscribers table. |
| 503 | // The email-based join can cross a collation boundary when wp_users.user_email and |
| 504 | // mailpoet_subscribers.email use different (but compatible) collations — force a |
| 505 | // matching collation to avoid "Illegal mix of collations" errors. |
| 506 | $emailCollation = $this->collationChecker->getCollateIfNeeded( |
| 507 | $wpdb->users, |
| 508 | 'user_email', |
| 509 | $this->subscribersTable, |
| 510 | 'email' |
| 511 | ); |
| 512 | $insertSql = |
| 513 | "INSERT IGNORE INTO {$this->subscribersTable} (wp_user_id, email, status, created_at, `source`, deleted_at) |
| 514 | SELECT wu.id, wu.user_email, :subscriberStatus, CURRENT_TIMESTAMP(), :source, {$deletedAt} |
| 515 | FROM {$wpdb->users} wu |
| 516 | LEFT JOIN {$this->subscribersTable} s ON wu.id = s.wp_user_id |
| 517 | LEFT JOIN {$this->subscribersTable} existingSubscriber ON wu.user_email = existingSubscriber.email {$emailCollation} |
| 518 | WHERE s.wp_user_id IS NULL AND wu.user_email != '' |
| 519 | ON DUPLICATE KEY UPDATE |
| 520 | wp_user_id = wu.id, |
| 521 | deleted_at = IF( |
| 522 | existingSubscriber.`source` = :deletedSource AND existingSubscriber.deleted_at IS NOT NULL, |
| 523 | NULL, |
| 524 | existingSubscriber.deleted_at |
| 525 | ), |
| 526 | `source` = IF( |
| 527 | existingSubscriber.`source` = :deletedSource, |
| 528 | :source, |
| 529 | existingSubscriber.`source` |
| 530 | )"; |
| 531 | $stmt = $this->databaseConnection->prepare($insertSql); |
| 532 | $stmt->bindValue('subscriberStatus', $subscriberStatus); |
| 533 | $stmt->bindValue('source', Source::WORDPRESS_USER); |
| 534 | $stmt->bindValue('deletedSource', Source::WORDPRESS_USER_DELETED); |
| 535 | $stmt->executeStatement(); |
| 536 | |
| 537 | return $insertedUserIds; |
| 538 | } |
| 539 | |
| 540 | private function updateFirstNames(): void { |
| 541 | global $wpdb; |
| 542 | |
| 543 | $sql = |
| 544 | "UPDATE {$this->subscribersTable} s |
| 545 | JOIN {$wpdb->usermeta} as wpum ON s.wp_user_id = wpum.user_id AND wpum.meta_key = 'first_name' |
| 546 | SET s.first_name = SUBSTRING(wpum.meta_value, 1, 255) |
| 547 | WHERE s.first_name = '' |
| 548 | AND s.wp_user_id IS NOT NULL |
| 549 | AND wpum.meta_value IS NOT NULL"; |
| 550 | |
| 551 | $this->databaseConnection->executeStatement($sql); |
| 552 | } |
| 553 | |
| 554 | private function updateLastNames(): void { |
| 555 | global $wpdb; |
| 556 | |
| 557 | $sql = |
| 558 | "UPDATE {$this->subscribersTable} s |
| 559 | JOIN {$wpdb->usermeta} as wpum ON s.wp_user_id = wpum.user_id AND wpum.meta_key = 'last_name' |
| 560 | SET s.last_name = SUBSTRING(wpum.meta_value, 1, 255) |
| 561 | WHERE s.last_name = '' |
| 562 | AND s.wp_user_id IS NOT NULL |
| 563 | AND wpum.meta_value IS NOT NULL"; |
| 564 | |
| 565 | $this->databaseConnection->executeStatement($sql); |
| 566 | } |
| 567 | |
| 568 | private function updateFirstNameIfMissing(): void { |
| 569 | global $wpdb; |
| 570 | |
| 571 | $sql = |
| 572 | "UPDATE {$this->subscribersTable} s |
| 573 | JOIN {$wpdb->users} wu ON s.wp_user_id = wu.id |
| 574 | SET s.first_name = wu.display_name |
| 575 | WHERE s.first_name = '' |
| 576 | AND s.wp_user_id IS NOT NULL"; |
| 577 | |
| 578 | $this->databaseConnection->executeStatement($sql); |
| 579 | } |
| 580 | |
| 581 | private function insertUsersToSegment(): void { |
| 582 | $wpSegment = $this->segmentsRepository->getWPUsersSegment(); |
| 583 | $subscribersSegmentTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 584 | |
| 585 | $sql = |
| 586 | "INSERT IGNORE INTO {$subscribersSegmentTable} (subscriber_id, segment_id, created_at) |
| 587 | SELECT s.id, '{$wpSegment->getId()}', CURRENT_TIMESTAMP() FROM {$this->subscribersTable} s |
| 588 | WHERE s.wp_user_id > 0"; |
| 589 | |
| 590 | $this->databaseConnection->executeStatement($sql); |
| 591 | } |
| 592 | |
| 593 | private function removeOrphanedSubscribers(): void { |
| 594 | $this->subscribersRepository->removeOrphanedSubscribersFromWpSegment(); |
| 595 | } |
| 596 | } |
| 597 |