Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
1 week ago
StatsNotifications
3 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
4 weeks ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
3 months ago
ExportFilesCleanup.php
3 months ago
InactiveSubscribersMaintenance.php
1 month ago
LogCleanup.php
11 months ago
Mixpanel.php
10 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
3 months ago
SendingQueueBodyCleanup.php
3 months ago
SendingTaskSubscribersCleanup.php
3 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberLimitNotificationWorker.php
3 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
1 month ago
SubscribersEngagementScore.php
2 weeks ago
SubscribersLastEngagement.php
3 months ago
SubscribersSegmentsCountSync.php
1 month ago
SubscribersStatsReport.php
1 year ago
Tracks.php
10 months ago
UnconfirmedSubscribersCleanup.php
3 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
1 month ago
index.php
3 years ago
WooCommercePastOrders.php
84 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\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Entities\StatisticsClickEntity; |
| 10 | use MailPoet\Statistics\StatisticsClicksRepository; |
| 11 | use MailPoet\Statistics\Track\WooCommercePurchases; |
| 12 | use MailPoet\WooCommerce\Helper as WCHelper; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | class WooCommercePastOrders extends SimpleWorker { |
| 16 | const TASK_TYPE = 'woocommerce_past_orders'; |
| 17 | const BATCH_SIZE = 20; |
| 18 | |
| 19 | /** @var WCHelper */ |
| 20 | private $woocommerceHelper; |
| 21 | |
| 22 | /** @var WooCommercePurchases */ |
| 23 | private $woocommercePurchases; |
| 24 | |
| 25 | /** @var StatisticsClicksRepository */ |
| 26 | private $statisticsClicksRepository; |
| 27 | |
| 28 | public function __construct( |
| 29 | WCHelper $woocommerceHelper, |
| 30 | StatisticsClicksRepository $statisticsClicksRepository, |
| 31 | WooCommercePurchases $woocommercePurchases |
| 32 | ) { |
| 33 | $this->woocommerceHelper = $woocommerceHelper; |
| 34 | $this->woocommercePurchases = $woocommercePurchases; |
| 35 | $this->statisticsClicksRepository = $statisticsClicksRepository; |
| 36 | parent::__construct(); |
| 37 | } |
| 38 | |
| 39 | public function checkProcessingRequirements() { |
| 40 | return $this->woocommerceHelper->isWooCommerceActive() && empty($this->getCompletedTasks()); // run only once |
| 41 | } |
| 42 | |
| 43 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 44 | $oldestClick = $this->statisticsClicksRepository->findOneBy([], ['createdAt' => 'asc']); |
| 45 | if (!$oldestClick instanceof StatisticsClickEntity) { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | // continue from 'last_processed_id' from previous run |
| 50 | $meta = $task->getMeta(); |
| 51 | $lastId = isset($meta['last_processed_id']) ? $meta['last_processed_id'] : 0; |
| 52 | add_filter('posts_where', function ($where = '') use ($lastId) { |
| 53 | global $wpdb; |
| 54 | return $where . " AND {$wpdb->prefix}posts.ID > " . $lastId; |
| 55 | }, 10, 1); |
| 56 | |
| 57 | $orderIds = $this->woocommerceHelper->wcGetOrders([ |
| 58 | 'date_completed' => '>=' . (($createdAt = $oldestClick->getCreatedAt()) ? $createdAt->format('Y-m-d H:i:s') : null), |
| 59 | 'orderby' => 'ID', |
| 60 | 'order' => 'ASC', |
| 61 | 'limit' => self::BATCH_SIZE, |
| 62 | 'return' => 'ids', |
| 63 | ]); |
| 64 | |
| 65 | if (empty($orderIds)) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | foreach ($orderIds as $orderId) { |
| 70 | $this->woocommercePurchases->trackPurchase($orderId, false); |
| 71 | } |
| 72 | |
| 73 | $task->setMeta(['last_processed_id' => end($orderIds)]); |
| 74 | $this->scheduledTasksRepository->persist($task); |
| 75 | $this->scheduledTasksRepository->flush(); |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | public function getNextRunDate() { |
| 81 | return Carbon::now()->millisecond(0); // schedule immediately |
| 82 | } |
| 83 | } |
| 84 |