Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
4 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
4 days ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
2 months ago
ExportFilesCleanup.php
2 months ago
InactiveSubscribersMaintenance.php
2 weeks ago
LogCleanup.php
10 months ago
Mixpanel.php
9 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
2 months ago
SendingQueueBodyCleanup.php
2 months ago
SendingTaskSubscribersCleanup.php
2 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
2 months ago
SubscriberLimitNotificationWorker.php
2 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
2 weeks ago
SubscribersEngagementScore.php
3 days ago
SubscribersLastEngagement.php
2 months ago
SubscribersSegmentsCountSync.php
2 weeks ago
SubscribersStatsReport.php
1 year ago
Tracks.php
9 months ago
UnconfirmedSubscribersCleanup.php
2 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
2 weeks ago
index.php
3 years ago
SubscribersLastEngagement.php
68 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Entities\StatisticsClickEntity; |
| 10 | use MailPoet\Entities\StatisticsOpenEntity; |
| 11 | use MailPoet\Entities\SubscriberEntity; |
| 12 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 13 | |
| 14 | class SubscribersLastEngagement extends SimpleWorker { |
| 15 | const AUTOMATIC_SCHEDULING = false; |
| 16 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 17 | const BATCH_SIZE = 2000; |
| 18 | const TASK_TYPE = 'subscribers_last_engagement'; |
| 19 | |
| 20 | /** @var EntityManager */ |
| 21 | private $entityManager; |
| 22 | |
| 23 | public function __construct( |
| 24 | EntityManager $entityManager |
| 25 | ) { |
| 26 | parent::__construct(); |
| 27 | $this->entityManager = $entityManager; |
| 28 | } |
| 29 | |
| 30 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer): bool { |
| 31 | $meta = $task->getMeta(); |
| 32 | $minId = $meta['nextId'] ?? 1; |
| 33 | $highestId = $this->getHighestSubscriberId(); |
| 34 | while ($minId <= $highestId) { |
| 35 | $maxId = $minId + self::BATCH_SIZE; |
| 36 | $this->processBatch($minId, $maxId); |
| 37 | $task->setMeta(['nextId' => $maxId]); |
| 38 | $this->scheduledTasksRepository->persist($task); |
| 39 | $this->scheduledTasksRepository->flush(); |
| 40 | $this->cronHelper->enforceExecutionLimit($timer); // Throws exception and interrupts process if over execution limit |
| 41 | $minId = $maxId; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | private function processBatch(int $minSubscriberId, int $maxSubscriberId): void { |
| 47 | $statisticsClicksTable = $this->entityManager->getClassMetadata(StatisticsClickEntity::class)->getTableName(); |
| 48 | $statisticsOpensTable = $this->entityManager->getClassMetadata(StatisticsOpenEntity::class)->getTableName(); |
| 49 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 50 | |
| 51 | $query = " |
| 52 | UPDATE $subscribersTable as mps |
| 53 | LEFT JOIN (SELECT max(created_at) as created_at, subscriber_id FROM $statisticsOpensTable as mpsoinner GROUP BY mpsoinner.subscriber_id) as mpso ON mpso.subscriber_id = mps.id |
| 54 | LEFT JOIN (SELECT max(created_at) as created_at, subscriber_id FROM $statisticsClicksTable as mpscinner GROUP BY mpscinner.subscriber_id) as mpsc ON mpsc.subscriber_id = mps.id |
| 55 | SET mps.last_engagement_at = NULLIF(GREATEST(COALESCE(mpso.created_at, '0'), COALESCE(mpsc.created_at, '0')), '0') |
| 56 | WHERE mps.last_engagement_at IS NULL AND mps.id >= $minSubscriberId AND mps.id < $maxSubscriberId; |
| 57 | "; |
| 58 | |
| 59 | $this->entityManager->getConnection()->executeStatement($query); |
| 60 | } |
| 61 | |
| 62 | private function getHighestSubscriberId(): int { |
| 63 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 64 | $result = $this->entityManager->getConnection()->executeQuery("SELECT MAX(id) FROM $subscribersTable LIMIT 1;")->fetchNumeric(); |
| 65 | return is_array($result) && isset($result[0]) && is_numeric($result[0]) ? (int)$result[0] : 0; |
| 66 | } |
| 67 | } |
| 68 |