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
UnsubscribeTokens.php
75 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\NewsletterEntity; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Util\Security; |
| 12 | use MailPoetVendor\Carbon\Carbon; |
| 13 | use MailPoetVendor\Doctrine\DBAL\ParameterType; |
| 14 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 15 | |
| 16 | class UnsubscribeTokens extends SimpleWorker { |
| 17 | const TASK_TYPE = 'unsubscribe_tokens'; |
| 18 | const BATCH_SIZE = 1000; |
| 19 | const AUTOMATIC_SCHEDULING = false; |
| 20 | |
| 21 | /** @var EntityManager */ |
| 22 | private $entityManager; |
| 23 | |
| 24 | public function __construct( |
| 25 | EntityManager $entityManager |
| 26 | ) { |
| 27 | parent::__construct(); |
| 28 | $this->entityManager = $entityManager; |
| 29 | } |
| 30 | |
| 31 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 32 | foreach ([SubscriberEntity::class, NewsletterEntity::class] as $entityClass) { |
| 33 | do { |
| 34 | $this->cronHelper->enforceExecutionLimit($timer); |
| 35 | $updatedCount = $this->addTokens($entityClass); |
| 36 | } while ($updatedCount === self::BATCH_SIZE); |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param class-string $entityClass |
| 43 | */ |
| 44 | private function addTokens(string $entityClass): int { |
| 45 | $tableName = $this->entityManager->getClassMetadata($entityClass)->getTableName(); |
| 46 | $connection = $this->entityManager->getConnection(); |
| 47 | $authKey = defined('AUTH_KEY') ? AUTH_KEY : ''; |
| 48 | |
| 49 | // A direct UPDATE keeps the backfill out of Doctrine's UnitOfWork: changes made to |
| 50 | // PARTIAL-hydrated entities are not registered, so the previous entity-based approach |
| 51 | // computed an empty changeset and silently wrote nothing. The token is derived from |
| 52 | // AUTH_KEY (so it stays unguessable) and salted per entity type (so it avoids |
| 53 | // systematic cross-table collisions; truncated-hash collisions remain extremely unlikely). |
| 54 | return (int)$connection->executeStatement( |
| 55 | "UPDATE {$tableName} SET unsubscribe_token = SUBSTRING(MD5(CONCAT(:authKey, :salt, id)), 1, :tokenLength) WHERE unsubscribe_token IS NULL LIMIT :limit", |
| 56 | [ |
| 57 | 'authKey' => $authKey, |
| 58 | 'salt' => $entityClass, |
| 59 | 'tokenLength' => Security::UNSUBSCRIBE_TOKEN_LENGTH, |
| 60 | 'limit' => self::BATCH_SIZE, |
| 61 | ], |
| 62 | [ |
| 63 | 'authKey' => ParameterType::STRING, |
| 64 | 'salt' => ParameterType::STRING, |
| 65 | 'tokenLength' => ParameterType::INTEGER, |
| 66 | 'limit' => ParameterType::INTEGER, |
| 67 | ] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | public function getNextRunDate() { |
| 72 | return Carbon::now()->millisecond(0); |
| 73 | } |
| 74 | } |
| 75 |