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
ExportFilesCleanup.php
55 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Newsletter\Statistics\Export\StatisticsExporter; |
| 11 | use MailPoet\Router\Endpoints\ExportDownload; |
| 12 | use MailPoet\Subscribers\ImportExport\Export\Export; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | class ExportFilesCleanup extends SimpleWorker { |
| 16 | const TASK_TYPE = 'export_files_cleanup'; |
| 17 | const DELETE_FILES_AFTER_X_DAYS = 1; |
| 18 | const DELETE_STATS_FILES_AFTER_X_DAYS = 7; |
| 19 | |
| 20 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 21 | $this->cleanup( |
| 22 | Export::getExportPath() . '/' . Export::getFilePrefix() . '*.*', |
| 23 | self::DELETE_FILES_AFTER_X_DAYS |
| 24 | ); |
| 25 | $this->cleanup( |
| 26 | Env::$tempPath . '/' . Export::getFilePrefix() . '*.*', |
| 27 | self::DELETE_FILES_AFTER_X_DAYS |
| 28 | ); |
| 29 | $this->cleanup( |
| 30 | ExportDownload::getExportDirectory() . '/' . StatisticsExporter::FILE_PREFIX . '*.*', |
| 31 | self::DELETE_STATS_FILES_AFTER_X_DAYS |
| 32 | ); |
| 33 | $this->cleanup( |
| 34 | Env::$tempPath . '/' . StatisticsExporter::FILE_PREFIX . '*.*', |
| 35 | self::DELETE_STATS_FILES_AFTER_X_DAYS |
| 36 | ); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | private function cleanup(string $globPattern, int $deleteAfterDays): void { |
| 41 | $iterator = new \GlobIterator($globPattern); |
| 42 | foreach ($iterator as $file) { |
| 43 | if (is_string($file)) { |
| 44 | continue; |
| 45 | } |
| 46 | $name = $file->getPathname(); |
| 47 | $created = $file->getMTime(); |
| 48 | $now = new Carbon(); |
| 49 | if (Carbon::createFromTimestamp((int)$created)->lessThan($now->subDays($deleteAfterDays))) { |
| 50 | unlink($name); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 |