| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace Hostinger\Admin\Jobs; |
| 5 |
|
| 6 |
use Exception; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
abstract class AbstractJob implements JobInterface { |
| 11 |
|
| 12 |
protected ActionScheduler $action_scheduler; |
| 13 |
|
| 14 |
public function __construct( ActionScheduler $action_scheduler ) { |
| 15 |
$this->action_scheduler = $action_scheduler; |
| 16 |
} |
| 17 |
|
| 18 |
public function init(): void { |
| 19 |
add_action( $this->get_process_item_hook(), array( $this, 'handle_process_items_action' ) ); |
| 20 |
add_action( |
| 21 |
$this->get_start_hook(), |
| 22 |
function ( $args ) { |
| 23 |
$this->schedule( $args ); |
| 24 |
} |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function can_schedule( $args = array() ): bool { |
| 29 |
return ! $this->is_running( $args ); |
| 30 |
} |
| 31 |
|
| 32 |
public function handle_process_items_action( array $args = array() ): void { |
| 33 |
$this->process_items( $args ); |
| 34 |
} |
| 35 |
|
| 36 |
public function get_process_item_hook(): string { |
| 37 |
return "{$this->get_hook_base_name()}process_item"; |
| 38 |
} |
| 39 |
|
| 40 |
public function get_start_hook(): string { |
| 41 |
return $this->get_name(); |
| 42 |
} |
| 43 |
|
| 44 |
protected function is_running( ?array $args = array() ): bool { |
| 45 |
return $this->action_scheduler->has_scheduled_action( $this->get_process_item_hook(), array( $args ) ); |
| 46 |
} |
| 47 |
|
| 48 |
protected function get_hook_base_name(): string { |
| 49 |
return "{$this->action_scheduler->get_group()}/jobs/{$this->get_name()}/"; |
| 50 |
} |
| 51 |
|
| 52 |
abstract public function get_name(): string; |
| 53 |
abstract protected function process_items( array $args ); |
| 54 |
} |
| 55 |
|