AbandonedCartWorker.php
72 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers\Automations; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 10 | use MailPoet\Cron\Workers\SimpleWorker; |
| 11 | use MailPoet\Entities\ScheduledTaskEntity; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class AbandonedCartWorker extends SimpleWorker { |
| 15 | const TASK_TYPE = 'automation_abandoned_cart'; |
| 16 | |
| 17 | const ACTION = 'abandoned_cart'; |
| 18 | |
| 19 | const AUTOMATIC_SCHEDULING = false; |
| 20 | const BATCH_SIZE = 1000; |
| 21 | |
| 22 | private AutomationStorage $automationStorage; |
| 23 | private WPFunctions $wp; |
| 24 | |
| 25 | public function __construct( |
| 26 | AutomationStorage $automationStorage, |
| 27 | WPFunctions $wp |
| 28 | ) { |
| 29 | parent::__construct(); |
| 30 | $this->automationStorage = $automationStorage; |
| 31 | $this->wp = $wp; |
| 32 | } |
| 33 | |
| 34 | public function checkProcessingRequirements() { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 39 | $productIds = $task->getMeta()['product_ids'] ?? []; |
| 40 | $automationId = $task->getMeta()['automation_id'] ?? 0; |
| 41 | $automationVersion = $task->getMeta()['automation_version'] ?? 0; |
| 42 | |
| 43 | if (!$productIds || !$automationId || !$automationVersion) { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | $lastActivityAt = $task->getCreatedAt(); |
| 48 | |
| 49 | $subscribers = $task->getSubscribers(); |
| 50 | if ($subscribers->count() !== 1) { |
| 51 | return true; |
| 52 | } |
| 53 | $subscriber = isset($subscribers[0]) ? $subscribers[0]->getSubscriber() : null; |
| 54 | if (!$subscriber) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | $automation = $this->automationStorage->getAutomation((int)$automationId, (int)$automationVersion); |
| 59 | if (!$automation || $automation->getStatus() !== Automation::STATUS_ACTIVE) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | $this->wp->doAction( |
| 64 | self::ACTION, |
| 65 | $subscriber, |
| 66 | $productIds, |
| 67 | $lastActivityAt |
| 68 | ); |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 |