Automations
1 year ago
KeyCheck
2 years ago
SendingQueue
1 month ago
StatsNotifications
3 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
2 years ago
Bounce.php
1 month ago
BounceTaskSubscribersCleanup.php
2 months ago
BulkConfirmationEmailResend.php
4 months ago
ExportFilesCleanup.php
3 months ago
InactiveSubscribersMaintenance.php
2 months ago
LogCleanup.php
11 months ago
Mixpanel.php
11 months ago
NewsletterTemplateThumbnails.php
2 years ago
ReEngagementEmailsScheduler.php
2 years ago
Scheduler.php
3 months ago
SendingQueueBodyCleanup.php
4 months ago
SendingTaskSubscribersCleanup.php
4 months ago
SimpleWorker.php
2 years ago
StatisticsExport.php
4 months ago
SubscriberLimitNotificationWorker.php
3 months ago
SubscriberLinkTokens.php
2 years ago
SubscribersCountCacheRecalculation.php
2 months ago
SubscribersEngagementScore.php
2 years ago
SubscribersLastEngagement.php
4 months ago
SubscribersSegmentsCountSync.php
2 months ago
SubscribersStatsReport.php
2 years ago
Tracks.php
11 months ago
UnconfirmedSubscribersCleanup.php
4 months ago
UnsubscribeTokens.php
2 months ago
WooCommercePastOrders.php
2 years ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
2 months ago
index.php
3 years ago
SubscribersEngagementScore.php
80 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\Segments\SegmentsRepository; |
| 10 | use MailPoet\Statistics\StatisticsOpensRepository; |
| 11 | use MailPoet\Subscribers\SubscribersRepository; |
| 12 | use MailPoetVendor\Carbon\Carbon; |
| 13 | |
| 14 | class SubscribersEngagementScore extends SimpleWorker { |
| 15 | const AUTOMATIC_SCHEDULING = true; |
| 16 | const BATCH_SIZE = 60; |
| 17 | const TASK_TYPE = 'subscribers_engagement_score'; |
| 18 | |
| 19 | /** @var SegmentsRepository */ |
| 20 | private $segmentsRepository; |
| 21 | |
| 22 | /** @var StatisticsOpensRepository */ |
| 23 | private $statisticsOpensRepository; |
| 24 | |
| 25 | /** @var SubscribersRepository */ |
| 26 | private $subscribersRepository; |
| 27 | |
| 28 | public function __construct( |
| 29 | SegmentsRepository $segmentsRepository, |
| 30 | StatisticsOpensRepository $statisticsOpensRepository, |
| 31 | SubscribersRepository $subscribersRepository |
| 32 | ) { |
| 33 | parent::__construct(); |
| 34 | $this->segmentsRepository = $segmentsRepository; |
| 35 | $this->statisticsOpensRepository = $statisticsOpensRepository; |
| 36 | $this->subscribersRepository = $subscribersRepository; |
| 37 | } |
| 38 | |
| 39 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 40 | $recalculatedSubscribersCount = $this->recalculateSubscribers(); |
| 41 | if ($recalculatedSubscribersCount > 0) { |
| 42 | $this->scheduleImmediately(); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | $recalculatedSegmentsCount = $this->recalculateSegments(); |
| 47 | if ($recalculatedSegmentsCount > 0) { |
| 48 | $this->scheduleImmediately(); |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | $this->schedule(); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | private function recalculateSubscribers(): int { |
| 57 | $subscribers = $this->subscribersRepository->findByUpdatedScoreNotInLastMonth(self::BATCH_SIZE); |
| 58 | foreach ($subscribers as $subscriber) { |
| 59 | $this->statisticsOpensRepository->recalculateSubscriberScore($subscriber); |
| 60 | } |
| 61 | return count($subscribers); |
| 62 | } |
| 63 | |
| 64 | private function recalculateSegments(): int { |
| 65 | $segments = $this->segmentsRepository->findByUpdatedScoreNotInLastDay(self::BATCH_SIZE); |
| 66 | foreach ($segments as $segment) { |
| 67 | $this->statisticsOpensRepository->recalculateSegmentScore($segment); |
| 68 | } |
| 69 | return count($segments); |
| 70 | } |
| 71 | |
| 72 | public function getNextRunDate() { |
| 73 | // random day of the next week |
| 74 | $date = Carbon::now()->millisecond(0); |
| 75 | $date->addDay(); |
| 76 | $date->setTime(mt_rand(0, 23), mt_rand(0, 59)); |
| 77 | return $date; |
| 78 | } |
| 79 | } |
| 80 |