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
StatisticsExport.php
95 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\Newsletter\NewslettersRepository; |
| 10 | use MailPoet\Newsletter\Statistics\Export\StatisticsExporter; |
| 11 | |
| 12 | /** |
| 13 | * Async statistics export worker. Tasks are scheduled on demand from the |
| 14 | * StatisticsExport API endpoint and store the export parameters in the |
| 15 | * task's meta JSON column. After processing, the file URL is written back |
| 16 | * to meta so the UI can poll for completion and offer the download. |
| 17 | * |
| 18 | * Job meta shape: |
| 19 | * - job_type: 'recipients' | 'bulk' |
| 20 | * - newsletter_id: int (recipients job only) |
| 21 | * - newsletter_ids: int[] (bulk job only) |
| 22 | * - format: StatisticsExporter::FORMAT_CSV | StatisticsExporter::FORMAT_XLSX |
| 23 | * - requested_by: int (WP user id) |
| 24 | * - export_file_url: string (set after processing) |
| 25 | * - total_exported: int (set after processing) |
| 26 | * - error: string (set on failure) |
| 27 | */ |
| 28 | class StatisticsExport extends SimpleWorker { |
| 29 | const TASK_TYPE = 'statistics_export'; |
| 30 | const AUTOMATIC_SCHEDULING = false; |
| 31 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 32 | |
| 33 | const JOB_TYPE_RECIPIENTS = 'recipients'; |
| 34 | const JOB_TYPE_BULK = 'bulk'; |
| 35 | |
| 36 | /** @var StatisticsExporter */ |
| 37 | private $exporter; |
| 38 | |
| 39 | /** @var NewslettersRepository */ |
| 40 | private $newslettersRepository; |
| 41 | |
| 42 | public function __construct( |
| 43 | StatisticsExporter $exporter, |
| 44 | NewslettersRepository $newslettersRepository |
| 45 | ) { |
| 46 | $this->exporter = $exporter; |
| 47 | $this->newslettersRepository = $newslettersRepository; |
| 48 | parent::__construct(); |
| 49 | } |
| 50 | |
| 51 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 52 | $meta = $task->getMeta() ?? []; |
| 53 | unset($meta['export_file_url'], $meta['total_exported'], $meta['error']); |
| 54 | $jobType = isset($meta['job_type']) ? (string)$meta['job_type'] : ''; |
| 55 | $format = isset($meta['format']) ? (string)$meta['format'] : StatisticsExporter::FORMAT_CSV; |
| 56 | |
| 57 | try { |
| 58 | if ($jobType === self::JOB_TYPE_RECIPIENTS) { |
| 59 | $newsletterId = isset($meta['newsletter_id']) ? (int)$meta['newsletter_id'] : 0; |
| 60 | $newsletter = $this->newslettersRepository->findOneById($newsletterId); |
| 61 | if (!$newsletter) { |
| 62 | throw new \RuntimeException(sprintf('Newsletter %d not found.', $newsletterId)); |
| 63 | } |
| 64 | $result = $this->exporter->exportRecipients($newsletter, $format); |
| 65 | } elseif ($jobType === self::JOB_TYPE_BULK) { |
| 66 | $newsletterIds = isset($meta['newsletter_ids']) && is_array($meta['newsletter_ids']) |
| 67 | ? array_values(array_unique(array_filter( |
| 68 | array_map(static fn($id): int => is_scalar($id) ? (int)$id : 0, $meta['newsletter_ids']), |
| 69 | static fn(int $id): bool => $id > 0 |
| 70 | ))) |
| 71 | : []; |
| 72 | $newsletters = []; |
| 73 | foreach ($newsletterIds as $id) { |
| 74 | $newsletter = $this->newslettersRepository->findOneById($id); |
| 75 | if ($newsletter) { |
| 76 | $newsletters[] = $newsletter; |
| 77 | } |
| 78 | } |
| 79 | $result = $this->exporter->exportBulkAggregate($newsletters, $format); |
| 80 | } else { |
| 81 | throw new \RuntimeException(sprintf('Unsupported export job type "%s".', $jobType)); |
| 82 | } |
| 83 | $meta['export_file_url'] = $result['exportFileURL']; |
| 84 | $meta['total_exported'] = $result['totalExported']; |
| 85 | } catch (\Throwable $e) { |
| 86 | $meta['error'] = $e->getMessage(); |
| 87 | } |
| 88 | |
| 89 | $task->setMeta($meta); |
| 90 | $this->scheduledTasksRepository->persist($task); |
| 91 | $this->scheduledTasksRepository->flush(); |
| 92 | return true; |
| 93 | } |
| 94 | } |
| 95 |