DynamicSegments
1 day ago
RestApi
2 months ago
SegmentDependencyValidator.php
3 years ago
SegmentListingRepository.php
2 months ago
SegmentSaveController.php
1 month ago
SegmentSubscribersRepository.php
1 month ago
SegmentsFinder.php
3 years ago
SegmentsRepository.php
1 month ago
SegmentsSimpleListRepository.php
3 months ago
SubscribersFinder.php
2 months ago
WP.php
1 day ago
WPUserDeleteNotice.php
4 weeks ago
WooCommerce.php
1 day ago
index.php
3 years ago
WooCommerce.php
696 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Segments; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Config\SubscriberChangesNotifier; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Entities\SubscriberSegmentEntity; |
| 12 | use MailPoet\Services\Validator; |
| 13 | use MailPoet\Settings\SettingsController; |
| 14 | use MailPoet\Subscribers\SegmentsCountRecalculator; |
| 15 | use MailPoet\Subscribers\Source; |
| 16 | use MailPoet\Subscribers\SubscriberSaveController; |
| 17 | use MailPoet\Subscribers\SubscriberSegmentRepository; |
| 18 | use MailPoet\Subscribers\SubscribersRepository; |
| 19 | use MailPoet\WooCommerce\Helper as WCHelper; |
| 20 | use MailPoet\WooCommerce\Subscription; |
| 21 | use MailPoet\WP\Functions as WPFunctions; |
| 22 | use MailPoetVendor\Carbon\Carbon; |
| 23 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 24 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 25 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 26 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 27 | |
| 28 | class WooCommerce { |
| 29 | /** |
| 30 | * Per-email record of whether synchronizeGuestCustomer() inserted a brand new |
| 31 | * subscriber row this request, or found one that already existed. |
| 32 | * insertSubscribers() uses INSERT IGNORE, so a caller looking at the row |
| 33 | * afterwards cannot tell "just created" from "already there" — that is |
| 34 | * captured at insert time instead. Read by WooCommerce\Subscription, which |
| 35 | * runs on the same hook at a later priority and has no other way to know. |
| 36 | * |
| 37 | * @var array<string, bool> |
| 38 | */ |
| 39 | private $guestSyncCreatedSubscriber = []; |
| 40 | |
| 41 | /** @var SettingsController */ |
| 42 | private $settings; |
| 43 | |
| 44 | /** @var WPFunctions */ |
| 45 | private $wp; |
| 46 | |
| 47 | /** @var WP */ |
| 48 | private $wpSegment; |
| 49 | |
| 50 | /** @var string|null */ |
| 51 | private $mailpoetEmailCollation; |
| 52 | |
| 53 | /** @var string|null */ |
| 54 | private $wpPostmetaValueCollation; |
| 55 | |
| 56 | /** @var SubscribersRepository */ |
| 57 | private $subscribersRepository; |
| 58 | |
| 59 | /** @var SegmentsRepository */ |
| 60 | private $segmentsRepository; |
| 61 | |
| 62 | /** @var SubscriberSegmentRepository */ |
| 63 | private $subscriberSegmentRepository; |
| 64 | |
| 65 | /** @var SubscriberSaveController */ |
| 66 | private $subscriberSaveController; |
| 67 | |
| 68 | /** @var WCHelper */ |
| 69 | private $woocommerceHelper; |
| 70 | |
| 71 | /** @var EntityManager */ |
| 72 | private $entityManager; |
| 73 | |
| 74 | /** @var Connection */ |
| 75 | private $connection; |
| 76 | |
| 77 | /** @var SubscriberChangesNotifier */ |
| 78 | private $subscriberChangesNotifier; |
| 79 | |
| 80 | /** @var Validator */ |
| 81 | private $validator; |
| 82 | |
| 83 | /** @var SegmentsCountRecalculator */ |
| 84 | private $segmentsCountRecalculator; |
| 85 | |
| 86 | public function __construct( |
| 87 | SettingsController $settings, |
| 88 | WPFunctions $wp, |
| 89 | WCHelper $woocommerceHelper, |
| 90 | SubscribersRepository $subscribersRepository, |
| 91 | SegmentsRepository $segmentsRepository, |
| 92 | SubscriberSegmentRepository $subscriberSegmentRepository, |
| 93 | SubscriberSaveController $subscriberSaveController, |
| 94 | WP $wpSegment, |
| 95 | EntityManager $entityManager, |
| 96 | Connection $connection, |
| 97 | SubscriberChangesNotifier $subscriberChangesNotifier, |
| 98 | Validator $validator, |
| 99 | SegmentsCountRecalculator $segmentsCountRecalculator |
| 100 | ) { |
| 101 | $this->settings = $settings; |
| 102 | $this->wp = $wp; |
| 103 | $this->wpSegment = $wpSegment; |
| 104 | $this->subscribersRepository = $subscribersRepository; |
| 105 | $this->segmentsRepository = $segmentsRepository; |
| 106 | $this->subscriberSegmentRepository = $subscriberSegmentRepository; |
| 107 | $this->subscriberSaveController = $subscriberSaveController; |
| 108 | $this->woocommerceHelper = $woocommerceHelper; |
| 109 | $this->entityManager = $entityManager; |
| 110 | $this->connection = $connection; |
| 111 | $this->subscriberChangesNotifier = $subscriberChangesNotifier; |
| 112 | $this->validator = $validator; |
| 113 | $this->segmentsCountRecalculator = $segmentsCountRecalculator; |
| 114 | } |
| 115 | |
| 116 | public function shouldShowWooCommerceSegment(): bool { |
| 117 | return $this->woocommerceHelper->isWooCommerceActive(); |
| 118 | } |
| 119 | |
| 120 | public function synchronizeRegisteredCustomer(int $wpUserId, ?string $currentFilter = null): bool { |
| 121 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 122 | |
| 123 | $currentFilter = $currentFilter ?: $this->wp->currentFilter(); |
| 124 | switch ($currentFilter) { |
| 125 | case 'woocommerce_delete_customer': |
| 126 | // subscriber should be already deleted in WP users sync |
| 127 | // unsubscribeUsersFromSegment() recomputes segments_count for the rows it |
| 128 | // removes, so no whole-segment sweep is needed here. |
| 129 | $this->unsubscribeUsersFromSegment(); // remove leftover association |
| 130 | break; |
| 131 | case 'woocommerce_new_customer': |
| 132 | case 'woocommerce_created_customer': |
| 133 | $newCustomer = true; |
| 134 | case 'woocommerce_update_customer': |
| 135 | default: |
| 136 | $wpUser = $this->wp->getUserdata($wpUserId); |
| 137 | $subscriber = $this->subscribersRepository->findOneBy(['wpUserId' => $wpUserId]); |
| 138 | |
| 139 | if ($wpUser === false || $subscriber === null) { |
| 140 | // registered customers should exist as WP users and WP segment subscribers |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | $data = [ |
| 145 | 'is_woocommerce_user' => 1, |
| 146 | ]; |
| 147 | if (!empty($newCustomer)) { |
| 148 | $data['source'] = Source::WOOCOMMERCE_USER; |
| 149 | } |
| 150 | $data['id'] = $subscriber->getId(); |
| 151 | if ($wpUser->first_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 152 | $data['first_name'] = $wpUser->first_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 153 | } |
| 154 | if ($wpUser->last_name) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 155 | $data['last_name'] = $wpUser->last_name; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 156 | } |
| 157 | $subscriber = $this->subscriberSaveController->createOrUpdate($data, $subscriber); |
| 158 | // add subscriber to the WooCommerce Customers segment when relation doesn't exist |
| 159 | $subscriberSegment = $this->subscriberSegmentRepository->findOneBy(['subscriber' => $subscriber, 'segment' => $wcSegment]); |
| 160 | |
| 161 | if (!$subscriberSegment && $this->shouldSubscribeToWooSegment()) { |
| 162 | $this->subscriberSegmentRepository->subscribeToSegments( |
| 163 | $subscriber, |
| 164 | [$wcSegment] |
| 165 | ); |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Should subscribe to the Woo segment when creating a new woo customer and not on checkout |
| 175 | * or when on checkout and MailPoet subscribe optin is enabled and checked. |
| 176 | */ |
| 177 | protected function shouldSubscribeToWooSegment(): bool { |
| 178 | $checkoutOptinEnabled = (bool)$this->settings->get(Subscription::OPTIN_ENABLED_SETTING_NAME); |
| 179 | $checkoutOptinChecked = !empty($_POST[Subscription::CHECKOUT_OPTIN_INPUT_NAME]); |
| 180 | |
| 181 | return !$this->woocommerceHelper->isCheckoutRequest() || ($checkoutOptinEnabled && $checkoutOptinChecked); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Whether the given email's subscriber row did not exist before the most |
| 186 | * recent synchronizeGuestCustomer() call in this request. Defaults to false, |
| 187 | * meaning "treat as pre-existing", for any email that call never saw: a |
| 188 | * missing signal must never cause an unearned overwrite of an earlier |
| 189 | * consent choice. |
| 190 | */ |
| 191 | public function wasNewlyCreatedByGuestSync(string $email): bool { |
| 192 | return $this->guestSyncCreatedSubscriber[$email] ?? false; |
| 193 | } |
| 194 | |
| 195 | public function synchronizeGuestCustomer(int $orderId): void { |
| 196 | $wcOrder = $this->woocommerceHelper->wcGetOrder($orderId); |
| 197 | |
| 198 | if (!$wcOrder instanceof \WC_Order) return; |
| 199 | $signupConfirmation = $this->settings->get('signup_confirmation'); |
| 200 | $status = SubscriberEntity::STATUS_UNSUBSCRIBED; |
| 201 | if ((bool)$signupConfirmation['enabled'] === false && $this->shouldSubscribeToWooSegment()) { |
| 202 | $status = SubscriberEntity::STATUS_SUBSCRIBED; |
| 203 | } |
| 204 | |
| 205 | $wasNewlyCreated = false; |
| 206 | $email = $this->insertSubscriberFromOrder($wcOrder, $status, $wasNewlyCreated); |
| 207 | |
| 208 | if (empty($email)) { |
| 209 | return; |
| 210 | } |
| 211 | $this->guestSyncCreatedSubscriber[$email] = $wasNewlyCreated; |
| 212 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $email]); |
| 213 | |
| 214 | if ($subscriber) { |
| 215 | $firstName = $wcOrder->get_billing_first_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 216 | $lastName = $wcOrder->get_billing_last_name(); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 217 | if ($firstName) { |
| 218 | $subscriber->setFirstName($firstName); |
| 219 | } |
| 220 | if ($lastName) { |
| 221 | $subscriber->setLastName($lastName); |
| 222 | } |
| 223 | if ($firstName || $lastName) { |
| 224 | $this->subscribersRepository->flush(); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public function synchronizeCustomers(int $lastCheckedOrderId = 0, ?int $highestOrderId = null, int $batchSize = 1000): int { |
| 230 | |
| 231 | $this->wpSegment->synchronizeUsers(); // synchronize registered users |
| 232 | |
| 233 | $this->markRegisteredCustomers(); |
| 234 | |
| 235 | $processedOrders = $this->insertSubscribersFromOrders($lastCheckedOrderId, $batchSize); |
| 236 | $this->updateNames($processedOrders); |
| 237 | |
| 238 | $lastCheckedOrderId = $lastCheckedOrderId + $batchSize; |
| 239 | if (!$highestOrderId || $lastCheckedOrderId >= $highestOrderId) { |
| 240 | $this->insertUsersToSegment(); |
| 241 | $this->unsubscribeUsersFromSegment(); |
| 242 | $this->removeOrphanedSubscribers(); |
| 243 | $this->updateStatus(); |
| 244 | $this->updateGlobalStatus(); |
| 245 | // The bulk operations above add/remove/restatus the WooCommerce segment's |
| 246 | // memberships en masse via raw SQL, so refresh segments_count for all |
| 247 | // members regardless of status — some may have just transitioned away |
| 248 | // from subscribed and must be recomputed too. |
| 249 | $this->segmentsCountRecalculator->recalculateForSegment((int)$this->segmentsRepository->getWooCommerceSegment()->getId(), false); |
| 250 | } |
| 251 | |
| 252 | $this->subscribersRepository->invalidateTotalSubscribersCache(); |
| 253 | return $lastCheckedOrderId; |
| 254 | } |
| 255 | |
| 256 | private function ensureColumnCollation(): void { |
| 257 | if ($this->mailpoetEmailCollation && $this->wpPostmetaValueCollation) { |
| 258 | return; |
| 259 | } |
| 260 | global $wpdb; |
| 261 | |
| 262 | $mailpoetEmailColumn = $wpdb->get_row($wpdb->prepare( |
| 263 | "SHOW FULL COLUMNS FROM %i WHERE Field = 'email'", |
| 264 | $this->subscribersRepository->getTableName() |
| 265 | )); |
| 266 | $this->mailpoetEmailCollation = $mailpoetEmailColumn->Collation; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 267 | $wpPostmetaValueColumn = $wpdb->get_row($wpdb->prepare( |
| 268 | "SHOW FULL COLUMNS FROM %i WHERE Field = 'meta_value'", |
| 269 | $wpdb->postmeta |
| 270 | )); |
| 271 | $this->wpPostmetaValueCollation = $wpPostmetaValueColumn->Collation; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * In MySQL, if you have the same charset and collation in joined tables' columns it's perfect; |
| 276 | * if you have different charsets, utf8 and utf8mb4, it works too; but if you have the same charset |
| 277 | * with different collations, e.g. utf8mb4_unicode_ci and utf8mb4_unicode_520_ci, it will fail |
| 278 | * with an 'Illegal mix of collations' error. That's why we need an optional COLLATE clause to fix this. |
| 279 | */ |
| 280 | private function needsCollationChange(): bool { |
| 281 | $this->ensureColumnCollation(); |
| 282 | $collation1 = (string)$this->mailpoetEmailCollation; |
| 283 | $collation2 = (string)$this->wpPostmetaValueCollation; |
| 284 | |
| 285 | if ($collation1 === $collation2) { |
| 286 | return false; |
| 287 | } |
| 288 | [$charset1] = explode('_', $collation1); |
| 289 | [$charset2] = explode('_', $collation2); |
| 290 | |
| 291 | return $charset1 === $charset2; |
| 292 | } |
| 293 | |
| 294 | private function markRegisteredCustomers(): void { |
| 295 | // Mark WP users having a customer role as WooCommerce subscribers |
| 296 | global $wpdb; |
| 297 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 298 | $this->connection->executeQuery(" |
| 299 | UPDATE LOW_PRIORITY {$subscribersTable} mps |
| 300 | JOIN {$wpdb->users} wu ON mps.wp_user_id = wu.id |
| 301 | JOIN {$wpdb->usermeta} wpum ON wu.id = wpum.user_id AND wpum.meta_key = :capabilities |
| 302 | SET is_woocommerce_user = 1, source = :source |
| 303 | WHERE wpum.meta_value LIKE '%\"customer\"%' |
| 304 | ", ['capabilities' => $wpdb->prefix . 'capabilities', 'source' => Source::WOOCOMMERCE_USER]); |
| 305 | } |
| 306 | |
| 307 | private function insertSubscriberFromOrder(\WC_Order $wcOrder, string $status, bool &$wasNewlyCreated = false): ?string { |
| 308 | $email = $wcOrder->get_billing_email(); |
| 309 | |
| 310 | if (!$email || !$this->validator->validateEmail($email)) { |
| 311 | return null; |
| 312 | } |
| 313 | |
| 314 | $wasNewlyCreated = $this->insertSubscribers([$email], $status) > 0; |
| 315 | return $email; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * @return array<string, int> |
| 320 | */ |
| 321 | private function insertSubscribersFromOrders(int $lastProcessedOrderId, int $batchSize): array { |
| 322 | global $wpdb; |
| 323 | |
| 324 | $parameters = [ |
| 325 | 'lowestOrderId' => $lastProcessedOrderId, |
| 326 | 'highestOrderId' => $lastProcessedOrderId + $batchSize, |
| 327 | ]; |
| 328 | $parametersType = [ |
| 329 | 'lowestOrderId' => ParameterType::INTEGER, |
| 330 | 'highestOrderId' => ParameterType::INTEGER, |
| 331 | ]; |
| 332 | |
| 333 | if ($this->woocommerceHelper->isWooCommerceCustomOrdersTableEnabled()) { |
| 334 | $ordersTable = $this->woocommerceHelper->getOrdersTableName(); |
| 335 | $query = "SELECT id AS order_id, billing_email AS email |
| 336 | FROM `{$ordersTable}` |
| 337 | WHERE type = 'shop_order' AND billing_email != '' AND (id > :lowestOrderId AND id <= :highestOrderId) |
| 338 | ORDER BY id"; |
| 339 | } else { |
| 340 | $query = "SELECT wpp.id AS order_id, wppm.meta_value AS email |
| 341 | FROM `{$wpdb->posts}` wpp |
| 342 | JOIN `{$wpdb->postmeta}` wppm ON wpp.ID = wppm.post_id AND wppm.meta_key = '_billing_email' AND wppm.meta_value != '' |
| 343 | WHERE wpp.post_type = 'shop_order' |
| 344 | AND (wpp.ID > :lowestOrderId AND wpp.ID <= :highestOrderId) |
| 345 | ORDER BY wpp.id"; |
| 346 | } |
| 347 | |
| 348 | $result = $this->connection->executeQuery($query, $parameters, $parametersType)->fetchAllAssociative(); |
| 349 | |
| 350 | $processedOrders = []; |
| 351 | foreach ($result as $item) { |
| 352 | if (!is_string($item['email']) || !$this->validator->validateEmail($item['email']) || !is_numeric($item['order_id'])) { |
| 353 | continue; |
| 354 | } |
| 355 | // because data in result are sorted by id, we can replace the previous order id |
| 356 | $processedOrders[$item['email']] = (int)$item['order_id']; |
| 357 | } |
| 358 | |
| 359 | if (count($processedOrders)) { |
| 360 | $this->insertSubscribers(array_keys($processedOrders)); |
| 361 | } |
| 362 | |
| 363 | return $processedOrders; |
| 364 | } |
| 365 | |
| 366 | private function insertSubscribers(array $emails, string $status = SubscriberEntity::STATUS_SUBSCRIBED): int { |
| 367 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 368 | $subscribersValues = []; |
| 369 | $now = Carbon::now()->format('Y-m-d H:i:s'); |
| 370 | $source = Source::WOOCOMMERCE_USER; |
| 371 | foreach ($emails as $email) { |
| 372 | /** @var string $email */ |
| 373 | $email = $this->connection->quote($email); |
| 374 | $email = strval($email); |
| 375 | $subscribersValues[] = "(1, {$email}, '{$status}', '{$now}', '{$now}', '{$source}')"; |
| 376 | } |
| 377 | |
| 378 | // Save timestamp about changes before insert |
| 379 | $this->subscriberChangesNotifier->subscribersBatchUpdate(); |
| 380 | // Update existing subscribers |
| 381 | $this->connection->executeQuery(' |
| 382 | UPDATE ' . $subscribersTable . ' mps |
| 383 | SET mps.is_woocommerce_user = 1 |
| 384 | WHERE mps.email IN (:emails) |
| 385 | ', ['emails' => $emails], ['emails' => ArrayParameterType::STRING]); |
| 386 | |
| 387 | // Save timestamp about new subscribers before insert |
| 388 | $this->subscriberChangesNotifier->subscribersBatchCreate(); |
| 389 | // Insert new subscribers |
| 390 | // executeStatement, not executeQuery: the affected-row count is what tells |
| 391 | // a caller whether INSERT IGNORE actually inserted or silently discarded. |
| 392 | $insertedCount = $this->connection->executeStatement(' |
| 393 | INSERT IGNORE INTO ' . $subscribersTable . ' (`is_woocommerce_user`, `email`, `status`, `created_at`, `last_subscribed_at`, `source`) VALUES |
| 394 | ' . implode(',', $subscribersValues) . ' |
| 395 | '); |
| 396 | |
| 397 | return (int)$insertedCount; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * @param array<string, int> $orders |
| 402 | */ |
| 403 | private function updateNames(array $orders): void { |
| 404 | global $wpdb; |
| 405 | if (!$orders) { |
| 406 | return; |
| 407 | } |
| 408 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 409 | |
| 410 | if ($this->woocommerceHelper->isWooCommerceCustomOrdersTableEnabled()) { |
| 411 | $addressesTableName = $this->woocommerceHelper->getAddressesTableName(); |
| 412 | $metaData = []; |
| 413 | $results = $this->connection->executeQuery( |
| 414 | " |
| 415 | SELECT order_id, first_name, last_name |
| 416 | FROM {$addressesTableName} |
| 417 | WHERE order_id IN (:orderIds) and address_type = 'billing'", |
| 418 | ['orderIds' => array_values($orders)], |
| 419 | ['orderIds' => ArrayParameterType::INTEGER] |
| 420 | )->fetchAllAssociative(); |
| 421 | |
| 422 | // format data in the same format that is used when querying wp_postmeta (see below). |
| 423 | foreach ($results as $result) { |
| 424 | $firstNameData['post_id'] = $result['order_id']; |
| 425 | $firstNameData['meta_key'] = '_billing_first_name'; |
| 426 | $firstNameData['meta_value'] = $result['first_name']; |
| 427 | $metaData[] = $firstNameData; |
| 428 | |
| 429 | $lastNameData['post_id'] = $result['order_id']; |
| 430 | $lastNameData['meta_key'] = '_billing_last_name'; |
| 431 | $lastNameData['meta_value'] = $result['last_name']; |
| 432 | $metaData[] = $lastNameData; |
| 433 | } |
| 434 | } else { |
| 435 | $metaKeys = [ |
| 436 | '_billing_first_name', |
| 437 | '_billing_last_name', |
| 438 | ]; |
| 439 | $metaData = $this->connection->executeQuery( |
| 440 | " |
| 441 | SELECT post_id, meta_key, meta_value |
| 442 | FROM {$wpdb->postmeta} |
| 443 | WHERE meta_key IN ('_billing_first_name', '_billing_last_name') AND post_id IN (:postIds) |
| 444 | ", |
| 445 | ['metaKeys' => $metaKeys, 'postIds' => array_values($orders)], |
| 446 | ['metaKeys' => ArrayParameterType::STRING, 'postIds' => ArrayParameterType::INTEGER] |
| 447 | )->fetchAllAssociative(); |
| 448 | } |
| 449 | |
| 450 | $subscribersData = []; |
| 451 | foreach ($orders as $email => $postId) { |
| 452 | $subscribersData[$postId]['email'] = $email; |
| 453 | } |
| 454 | |
| 455 | foreach ($metaData as $row) { |
| 456 | if (!$row['meta_value']) { |
| 457 | continue; |
| 458 | } |
| 459 | $postId = is_numeric($row['post_id']) ? (int)$row['post_id'] : 0; |
| 460 | $metaKey = is_string($row['meta_key']) ? $row['meta_key'] : ''; |
| 461 | if ($postId === 0 || $metaKey === '') { |
| 462 | continue; |
| 463 | } |
| 464 | $subscribersData[$postId][$metaKey] = $row['meta_value']; |
| 465 | } |
| 466 | |
| 467 | $now = (Carbon::now())->format('Y-m-d H:i:s'); |
| 468 | foreach ($subscribersData as $subscriber) { |
| 469 | $data = []; |
| 470 | $data['woocommerce_synced_at'] = $now; |
| 471 | if (!empty($subscriber['_billing_first_name'])) $data['first_name'] = $subscriber['_billing_first_name']; |
| 472 | if (!empty($subscriber['_billing_last_name'])) $data['last_name'] = $subscriber['_billing_last_name']; |
| 473 | $this->connection->update($subscribersTable, $data, ['email' => $subscriber['email']]); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | private function insertUsersToSegment(): void { |
| 478 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 479 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 480 | $subscriberSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 481 | // Subscribe WC users to segment |
| 482 | $this->connection->executeQuery( |
| 483 | " |
| 484 | INSERT IGNORE INTO {$subscriberSegmentsTable} (subscriber_id, segment_id, created_at) |
| 485 | SELECT id, :segmentId, CURRENT_TIMESTAMP() |
| 486 | FROM {$subscribersTable} |
| 487 | WHERE is_woocommerce_user = 1 |
| 488 | ", |
| 489 | ['segmentId' => $wcSegment->getId()], |
| 490 | ['segmentId' => ParameterType::INTEGER] |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | private function unsubscribeUsersFromSegment(): void { |
| 495 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 496 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 497 | $subscriberSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 498 | |
| 499 | // Capture the affected subscriber ids before the DELETE: once the membership |
| 500 | // rows are gone, recalculateForSegment() can no longer see these subscribers, |
| 501 | // so a surviving subscriber would keep a stale segments_count. Recompute them |
| 502 | // explicitly afterwards (same pattern as SegmentsRepository::bulkDelete()). |
| 503 | $affectedIds = $this->connection->executeQuery( |
| 504 | " |
| 505 | SELECT mpss.subscriber_id FROM {$subscriberSegmentsTable} mpss |
| 506 | LEFT JOIN {$subscribersTable} mps ON mpss.subscriber_id = mps.id |
| 507 | WHERE mpss.segment_id = :segmentId AND mpss.status = :subscribedStatus |
| 508 | AND (mps.is_woocommerce_user = 0 OR mps.email = '' OR mps.email IS NULL) |
| 509 | ", |
| 510 | ['segmentId' => $wcSegment->getId(), 'subscribedStatus' => SubscriberEntity::STATUS_SUBSCRIBED], |
| 511 | ['segmentId' => ParameterType::INTEGER, 'subscribedStatus' => ParameterType::STRING] |
| 512 | )->fetchFirstColumn(); |
| 513 | |
| 514 | // Unsubscribe non-WC or invalid users from segment |
| 515 | $this->connection->executeQuery( |
| 516 | " |
| 517 | DELETE mpss FROM {$subscriberSegmentsTable} mpss |
| 518 | LEFT JOIN {$subscribersTable} mps ON mpss.subscriber_id = mps.id |
| 519 | WHERE mpss.segment_id = :segmentId AND (mps.is_woocommerce_user = 0 OR mps.email = '' OR mps.email IS NULL) |
| 520 | ", |
| 521 | ['segmentId' => $wcSegment->getId()], |
| 522 | ['segmentId' => ParameterType::INTEGER] |
| 523 | ); |
| 524 | |
| 525 | $subscriberIds = array_map(function ($id): int { |
| 526 | return is_numeric($id) ? (int)$id : 0; |
| 527 | }, $affectedIds); |
| 528 | $this->segmentsCountRecalculator->recalculateForSubscribers($subscriberIds); |
| 529 | } |
| 530 | |
| 531 | private function updateGlobalStatus(): void { |
| 532 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 533 | $subscriberSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 534 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 535 | // Set global status unsubscribed to all woocommerce users without any segment |
| 536 | $this->connection->executeQuery( |
| 537 | " |
| 538 | UPDATE {$subscribersTable} mps |
| 539 | LEFT JOIN {$subscriberSegmentsTable} mpss ON mpss.subscriber_id = mps.id |
| 540 | SET mps.status = :statusUnsubscribed |
| 541 | WHERE mpss.id IS NULL |
| 542 | AND mps.is_woocommerce_user = 1 |
| 543 | ", |
| 544 | ['statusUnsubscribed' => SubscriberEntity::STATUS_UNSUBSCRIBED], |
| 545 | ['statusUnsubscribed' => ParameterType::INTEGER] |
| 546 | ); |
| 547 | // SET global status unsubscribed to all woocommerce users who have only 1 segment and it is woocommerce segment and they are not subscribed |
| 548 | // You can't specify target table 'mps' for update in FROM clause |
| 549 | $this->connection->executeQuery( |
| 550 | " |
| 551 | UPDATE {$subscribersTable} mps |
| 552 | JOIN {$subscriberSegmentsTable} mpss ON mps.id = mpss.subscriber_id AND mpss.segment_id = :segmentId AND mpss.status = :statusUnsubscribed |
| 553 | SET mps.status = :statusUnsubscribed |
| 554 | WHERE mps.id IN ( |
| 555 | SELECT s.id -- get all subscribers with exactly 1 segment |
| 556 | FROM (SELECT id FROM {$subscribersTable} WHERE is_woocommerce_user = 1) s |
| 557 | JOIN {$subscriberSegmentsTable} ss on s.id = ss.subscriber_id |
| 558 | GROUP BY s.id |
| 559 | HAVING COUNT(ss.id) = 1 |
| 560 | ) |
| 561 | ", |
| 562 | ['statusUnsubscribed' => SubscriberEntity::STATUS_UNSUBSCRIBED, 'segmentId' => $wcSegment->getId()], |
| 563 | ['statusUnsubscribed' => ParameterType::STRING, 'segmentId' => ParameterType::INTEGER] |
| 564 | ); |
| 565 | } |
| 566 | |
| 567 | private function removeOrphanedSubscribers(): void { |
| 568 | // Remove orphaned WooCommerce segment subscribers (not having a matching WC customer email), |
| 569 | // e.g. if WC orders were deleted directly from the database |
| 570 | // or a customer role was revoked and a user has no orders |
| 571 | global $wpdb; |
| 572 | |
| 573 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 574 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 575 | $subscriberSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 576 | |
| 577 | // Unmark registered customers |
| 578 | |
| 579 | // Insert WC customer IDs to a temporary table for left join to use an index |
| 580 | $tmpTableName = Env::$dbPrefix . 'tmp_wc_ids'; |
| 581 | // Registered users with orders |
| 582 | if ($this->woocommerceHelper->isWooCommerceCustomOrdersTableEnabled()) { |
| 583 | $ordersTable = $this->woocommerceHelper->getOrdersTableName(); |
| 584 | // Exclude guest orders (customer_id = 0) as they are not registered users |
| 585 | $registeredCustomersSubQuery = "SELECT DISTINCT customer_id AS id FROM `{$ordersTable}` WHERE type = 'shop_order' AND customer_id > 0"; |
| 586 | } else { |
| 587 | // Exclude guest orders (meta_value = 0 or empty) as they are not registered users |
| 588 | $registeredCustomersSubQuery = "SELECT DISTINCT wppm.meta_value AS id FROM {$wpdb->postmeta} wppm |
| 589 | JOIN {$wpdb->posts} wpp ON wppm.post_id = wpp.ID |
| 590 | AND wpp.post_type = 'shop_order' |
| 591 | WHERE wppm.meta_key = '_customer_user' AND wppm.meta_value > 0"; |
| 592 | } |
| 593 | |
| 594 | $this->connection->executeQuery(" |
| 595 | CREATE TEMPORARY TABLE {$tmpTableName} |
| 596 | (`id` int(11) unsigned NOT NULL, UNIQUE(`id`), PRIMARY KEY (`id`)) AS |
| 597 | {$registeredCustomersSubQuery} |
| 598 | "); |
| 599 | // Registered users with a customer role |
| 600 | $this->connection->executeQuery(" |
| 601 | INSERT IGNORE INTO {$tmpTableName} |
| 602 | SELECT DISTINCT wpum.user_id AS id FROM {$wpdb->usermeta} wpum |
| 603 | WHERE wpum.meta_key = :capabilities AND wpum.meta_value LIKE '%\"customer\"%' |
| 604 | ", ['capabilities' => $wpdb->prefix . 'capabilities']); |
| 605 | |
| 606 | // Unmark WC list registered users which aren't WC customers anymore |
| 607 | $subQb = $this->connection->createQueryBuilder(); |
| 608 | $subQb->select('mps.id') |
| 609 | ->from($subscribersTable, 'mps') |
| 610 | ->join('mps', $subscriberSegmentsTable, 'mpss', 'mps.id = mpss.subscriber_id AND mpss.segment_id = :segmentId') |
| 611 | ->leftJoin('mps', $tmpTableName, 'wctmp', 'mps.wp_user_id = wctmp.id') |
| 612 | ->where('mps.is_woocommerce_user = 1') |
| 613 | ->andWhere('wctmp.id IS NULL') |
| 614 | ->andWhere('mps.wp_user_id IS NOT NULL'); |
| 615 | $qb = $this->connection->createQueryBuilder(); |
| 616 | $qb->update($subscribersTable) |
| 617 | ->set('is_woocommerce_user', '0') |
| 618 | ->where("id IN (SELECT id FROM ({$subQb->getSQL()}) AS sq) ") |
| 619 | ->setParameter('segmentId', $wcSegment->getId()); |
| 620 | $qb->execute(); |
| 621 | |
| 622 | $this->connection->executeQuery("DROP TABLE {$tmpTableName}"); |
| 623 | |
| 624 | // Remove guest customers |
| 625 | |
| 626 | // Insert WC customer emails to a temporary table and ensure matching collations |
| 627 | // between MailPoet and WooCommerce emails for left join to use an index |
| 628 | $tmpTableName = Env::$dbPrefix . 'tmp_wc_emails'; |
| 629 | if ($this->needsCollationChange()) { |
| 630 | $collation = "COLLATE $this->mailpoetEmailCollation"; |
| 631 | } else { |
| 632 | $collation = "COLLATE $this->wpPostmetaValueCollation"; |
| 633 | } |
| 634 | |
| 635 | if ($this->woocommerceHelper->isWooCommerceCustomOrdersTableEnabled()) { |
| 636 | $ordersTable = $this->woocommerceHelper->getOrdersTableName(); |
| 637 | $guestCustomersSubQuery = "SELECT DISTINCT billing_email AS email FROM `{$ordersTable}` WHERE type = 'shop_order' AND billing_email IS NOT NULL AND billing_email != ''"; |
| 638 | } else { |
| 639 | $guestCustomersSubQuery = "SELECT DISTINCT wppm.meta_value AS email FROM {$wpdb->postmeta} wppm |
| 640 | JOIN {$wpdb->posts} wpp ON wppm.post_id = wpp.ID |
| 641 | AND wpp.post_type = 'shop_order' |
| 642 | WHERE wppm.meta_key = '_billing_email'"; |
| 643 | } |
| 644 | |
| 645 | $this->connection->executeQuery(" |
| 646 | CREATE TEMPORARY TABLE {$tmpTableName} |
| 647 | (`email` varchar(150) NOT NULL, UNIQUE(`email`), PRIMARY KEY (`email`)) {$collation} |
| 648 | {$guestCustomersSubQuery} |
| 649 | "); |
| 650 | |
| 651 | // Remove WC list guest users which aren't WC customers anymore |
| 652 | $subQb = $this->connection->createQueryBuilder(); |
| 653 | $subQb->select('mps.id') |
| 654 | ->from($subscribersTable, 'mps') |
| 655 | ->join('mps', $subscriberSegmentsTable, 'mpss', 'mps.id = mpss.subscriber_id AND mpss.segment_id = :segmentId') |
| 656 | ->leftJoin('mps', $tmpTableName, 'wctmp', 'mps.email = wctmp.email') |
| 657 | ->where('mps.is_woocommerce_user = 1') |
| 658 | ->andWhere('wctmp.email IS NULL') |
| 659 | ->andWhere('mps.wp_user_id IS NULL'); |
| 660 | $qb = $this->connection->createQueryBuilder(); |
| 661 | $qb->delete($subscribersTable) |
| 662 | ->where("id IN (SELECT id FROM ({$subQb->getSQL()}) AS sq) ") |
| 663 | ->setParameter('segmentId', $wcSegment->getId()); |
| 664 | $qb->execute(); |
| 665 | |
| 666 | $this->connection->executeQuery("DROP TABLE {$tmpTableName}"); |
| 667 | } |
| 668 | |
| 669 | private function updateStatus(): void { |
| 670 | $subscribeOldCustomers = $this->settings->get('mailpoet_subscribe_old_woocommerce_customers.enabled', false); |
| 671 | if ($subscribeOldCustomers !== "1") { |
| 672 | $status = SubscriberEntity::STATUS_UNSUBSCRIBED; |
| 673 | } else { |
| 674 | $status = SubscriberEntity::STATUS_SUBSCRIBED; |
| 675 | } |
| 676 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 677 | $subscriberSegmentsTable = $this->entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 678 | $wcSegment = $this->segmentsRepository->getWooCommerceSegment(); |
| 679 | |
| 680 | $this->connection->executeQuery( |
| 681 | " |
| 682 | UPDATE LOW_PRIORITY {$subscriberSegmentsTable} AS mpss |
| 683 | JOIN {$subscribersTable} AS mps ON mpss.subscriber_id = mps.id |
| 684 | SET mpss.status = :status |
| 685 | WHERE |
| 686 | mpss.segment_id = :segmentId |
| 687 | AND mps.confirmed_at IS NULL |
| 688 | AND mps.confirmed_ip IS NULL |
| 689 | AND mps.is_woocommerce_user = 1 |
| 690 | ", |
| 691 | ['status' => $status, 'segmentId' => $wcSegment->getId()], |
| 692 | ['status' => ParameterType::STRING, 'segmentId' => ParameterType::INTEGER] |
| 693 | ); |
| 694 | } |
| 695 | } |
| 696 |