Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
5 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
5 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
4 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
BulkConfirmationEmailResend.php
134 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\LogEntity; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Logging\LoggerFactory; |
| 12 | use MailPoet\Logging\LogRepository; |
| 13 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository; |
| 14 | use MailPoet\Subscribers\BulkConfirmationEmailResender; |
| 15 | use MailPoet\Subscribers\ConfirmationEmailMailer; |
| 16 | use MailPoet\Subscribers\SubscribersRepository; |
| 17 | use MailPoet\Tasks\Subscribers\BatchIterator; |
| 18 | use MailPoetVendor\Carbon\Carbon; |
| 19 | |
| 20 | class BulkConfirmationEmailResend extends SimpleWorker { |
| 21 | const TASK_TYPE = 'bulk_confirmation_email_resend'; |
| 22 | const AUTOMATIC_SCHEDULING = false; |
| 23 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 24 | const BATCH_SIZE = 20; |
| 25 | |
| 26 | private const AUDIT_LOG_MESSAGE_COMPLETED = 'Bulk confirmation email resend completed.'; |
| 27 | private const LOG_LEVEL_INFO = 200; |
| 28 | |
| 29 | /** @var ConfirmationEmailMailer */ |
| 30 | private $confirmationEmailMailer; |
| 31 | |
| 32 | /** @var SubscribersRepository */ |
| 33 | private $subscribersRepository; |
| 34 | |
| 35 | /** @var ScheduledTaskSubscribersRepository */ |
| 36 | private $scheduledTaskSubscribersRepository; |
| 37 | |
| 38 | /** @var LogRepository */ |
| 39 | private $logRepository; |
| 40 | |
| 41 | public function __construct( |
| 42 | ConfirmationEmailMailer $confirmationEmailMailer, |
| 43 | SubscribersRepository $subscribersRepository, |
| 44 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository, |
| 45 | LogRepository $logRepository |
| 46 | ) { |
| 47 | $this->confirmationEmailMailer = $confirmationEmailMailer; |
| 48 | $this->subscribersRepository = $subscribersRepository; |
| 49 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 50 | $this->logRepository = $logRepository; |
| 51 | parent::__construct(); |
| 52 | } |
| 53 | |
| 54 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 55 | $subscriberBatches = new BatchIterator($task->getId(), self::BATCH_SIZE); |
| 56 | $sentCount = 0; |
| 57 | $failedCount = 0; |
| 58 | $skippedByReason = []; |
| 59 | $oldestLifecycleDate = Carbon::now()->subDays(BulkConfirmationEmailResender::BULK_CONFIRMATION_MAX_SUBSCRIBER_AGE_DAYS)->millisecond(0); |
| 60 | |
| 61 | foreach ($subscriberBatches as $subscriberIds) { |
| 62 | $this->cronHelper->enforceExecutionLimit($timer); |
| 63 | $sentIds = []; |
| 64 | foreach ($subscriberIds as $subscriberId) { |
| 65 | $subscriber = $this->subscribersRepository->findOneById((int)$subscriberId); |
| 66 | if (!$subscriber instanceof SubscriberEntity) { |
| 67 | $this->saveSkipped($task, (int)$subscriberId, 'not_found', $skippedByReason); |
| 68 | $failedCount++; |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | $result = $this->confirmationEmailMailer->sendAdminConfirmationEmail($subscriber, $oldestLifecycleDate); |
| 74 | } catch (\Throwable $throwable) { |
| 75 | $this->scheduledTaskSubscribersRepository->saveError($task, (int)$subscriberId, 'send_failed:mailer_error'); |
| 76 | $failedCount++; |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if ($result['status'] === 'sent') { |
| 81 | $sentIds[] = (int)$subscriberId; |
| 82 | $sentCount++; |
| 83 | } elseif ($result['status'] === 'skipped') { |
| 84 | $this->saveSkipped($task, (int)$subscriberId, $result['reason'] ?? 'not_found', $skippedByReason); |
| 85 | $failedCount++; |
| 86 | } else { |
| 87 | $this->scheduledTaskSubscribersRepository->saveError($task, (int)$subscriberId, 'send_failed:' . ($result['reason'] ?? 'sending_method')); |
| 88 | $failedCount++; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | $this->scheduledTaskSubscribersRepository->updateProcessedSubscribers($task, $sentIds); |
| 93 | } |
| 94 | |
| 95 | $meta = $task->getMeta() ?? []; |
| 96 | $meta['sent_count'] = $sentCount; |
| 97 | $meta['failed_count'] = $failedCount; |
| 98 | $meta['skipped_by_reason'] = $skippedByReason; |
| 99 | $task->setMeta($meta); |
| 100 | $this->scheduledTasksRepository->persist($task); |
| 101 | $this->scheduledTasksRepository->flush(); |
| 102 | $this->saveCompletionAuditLog($task, $sentCount, $failedCount, $skippedByReason); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param array<string, int> $skippedByReason |
| 108 | */ |
| 109 | private function saveSkipped(ScheduledTaskEntity $task, int $subscriberId, string $reason, array &$skippedByReason): void { |
| 110 | $skippedByReason[$reason] = ($skippedByReason[$reason] ?? 0) + 1; |
| 111 | $this->scheduledTaskSubscribersRepository->saveError($task, $subscriberId, 'skipped:' . $reason); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param array<string, int> $skippedByReason |
| 116 | */ |
| 117 | private function saveCompletionAuditLog(ScheduledTaskEntity $task, int $sentCount, int $failedCount, array $skippedByReason): void { |
| 118 | $log = new LogEntity(); |
| 119 | $log->setName(LoggerFactory::TOPIC_CRON); |
| 120 | $log->setLevel(self::LOG_LEVEL_INFO); |
| 121 | $log->setMessage(self::AUDIT_LOG_MESSAGE_COMPLETED); |
| 122 | $log->setRawMessage(self::AUDIT_LOG_MESSAGE_COMPLETED); |
| 123 | $log->setContext([ |
| 124 | 'action' => 'bulk_confirmation_email_resend_completed', |
| 125 | 'task_id' => $task->getId(), |
| 126 | 'sent_count' => $sentCount, |
| 127 | 'failed_count' => $failedCount, |
| 128 | 'skipped_by_reason' => $skippedByReason, |
| 129 | 'final_status' => ScheduledTaskEntity::STATUS_COMPLETED, |
| 130 | ]); |
| 131 | $this->logRepository->saveLog($log); |
| 132 | } |
| 133 | } |
| 134 |