AbandonedCartsJob.php
7 months ago
AbstractBatchedJob.php
7 months ago
AbstractJob.php
5 months ago
ActionScheduler.php
7 months ago
CleanupCartsJob.php
2 weeks ago
ImportJob.php
7 months ago
JobInterface.php
8 months ago
RecurringJobInterface.php
8 months ago
AbstractJob.php
69 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Hostinger\Reach\Jobs; |
| 5 | |
| 6 | use ActionScheduler_QueueRunner; |
| 7 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | abstract class AbstractJob implements JobInterface { |
| 12 | |
| 13 | protected ActionScheduler $action_scheduler; |
| 14 | protected ReachApiHandler $reach_api_handler; |
| 15 | |
| 16 | public function __construct( ActionScheduler $action_scheduler, ReachApiHandler $reach_api_handler ) { |
| 17 | $this->action_scheduler = $action_scheduler; |
| 18 | $this->reach_api_handler = $reach_api_handler; |
| 19 | } |
| 20 | |
| 21 | public function init(): void { |
| 22 | add_action( $this->get_process_item_hook(), array( $this, 'handle_process_items_action' ) ); |
| 23 | add_action( $this->get_start_hook(), array( $this, 'handle_sart_hook_action' ) ); |
| 24 | |
| 25 | if ( $this instanceof RecurringJobInterface && $this->can_schedule_recurrent() ) { |
| 26 | $this->action_scheduler->schedule_recurring( $this->get_interval(), $this->get_start_hook() ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public function can_schedule_recurrent(): bool { |
| 31 | return ! $this->action_scheduler->has_scheduled_action( $this->get_start_hook() ); |
| 32 | } |
| 33 | |
| 34 | public function can_schedule( array $args = array() ): bool { |
| 35 | return $this->reach_api_handler->is_connected() && ! $this->is_running( $args ); |
| 36 | } |
| 37 | |
| 38 | public function handle_sart_hook_action( array $args = array() ): void { |
| 39 | $this->schedule( $args ); |
| 40 | if ( class_exists( 'ActionScheduler_QueueRunner' ) ) { |
| 41 | ActionScheduler_QueueRunner::instance()->run(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public function handle_process_items_action( array $args = array() ): void { |
| 46 | $this->process_items( $args ); |
| 47 | } |
| 48 | |
| 49 | public function get_process_item_hook(): string { |
| 50 | return "{$this->get_hook_base_name()}/process_item"; |
| 51 | } |
| 52 | |
| 53 | public function get_start_hook(): string { |
| 54 | return "{$this->get_hook_base_name()}/start"; |
| 55 | } |
| 56 | |
| 57 | protected function is_running( ?array $args = array() ): bool { |
| 58 | return $this->action_scheduler->has_scheduled_action( $this->get_process_item_hook(), array( $args ) ); |
| 59 | } |
| 60 | |
| 61 | protected function get_hook_base_name(): string { |
| 62 | return "{$this->action_scheduler->get_group()}/jobs/{$this->get_name()}"; |
| 63 | } |
| 64 | |
| 65 | abstract public function get_name(): string; |
| 66 | |
| 67 | abstract protected function process_items( array $args ): void; |
| 68 | } |
| 69 |