PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.5.0
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.5.0
1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Jobs / AbstractBatchedJob.php
hostinger-reach / src / Jobs Last commit date
AbandonedCartsJob.php 6 months ago AbstractBatchedJob.php 6 months ago AbstractJob.php 3 months ago ActionScheduler.php 5 months ago CleanupCartsJob.php 6 months ago ImportJob.php 5 months ago JobInterface.php 7 months ago RecurringJobInterface.php 7 months ago
AbstractBatchedJob.php
78 lines
1 <?php
2
3 namespace Hostinger\Reach\Jobs;
4
5
6 defined( 'ABSPATH' ) || exit;
7
8 abstract class AbstractBatchedJob extends AbstractJob {
9
10 public const BATCH_LIMIT_PER_JOB = 15;
11
12 public function init(): void {
13 add_action( $this->get_create_batch_hook(), array( $this, 'handle_create_batch_action' ), 10, 2 );
14 parent::init();
15 }
16
17 protected function get_create_batch_hook(): string {
18 return "{$this->get_hook_base_name()}/create_batch";
19 }
20
21 public function schedule( array $args = array() ): void {
22 $this->schedule_create_batch_action( 1, $args );
23 }
24
25 public function handle_create_batch_action( int $batch_number, array $args ): void {
26 $items = $this->get_batch( $batch_number, $args );
27
28 if ( empty( $items ) || $batch_number > self::BATCH_LIMIT_PER_JOB ) {
29 $this->handle_complete( $batch_number, $args );
30 } else {
31 $this->set_items_as_processing( $items );
32 $this->schedule_process_action( $items, $args );
33 $this->schedule_create_batch_action( $batch_number + 1, $args );
34 }
35 }
36
37 protected function get_batch_size(): int {
38 return apply_filters( 'hostinger_reach_batch_item_limit', 1 );
39 }
40
41
42 protected function schedule_create_batch_action( int $batch_number, array $args ): void {
43 if ( $this->can_schedule( array( $batch_number ) ) ) {
44 $this->action_scheduler->schedule_immediate(
45 $this->get_create_batch_hook(),
46 array(
47 $batch_number,
48 $args,
49 )
50 );
51 }
52 }
53
54 protected function schedule_process_action( array $items = array(), array $args = array() ): void {
55 $job_data = array(
56 'items' => $items,
57 'args' => $args,
58 );
59 if ( ! $this->is_processing( $job_data ) ) {
60 $this->action_scheduler->schedule_immediate( $this->get_process_item_hook(), array( $job_data ) );
61 }
62 }
63
64 protected function is_processing( array $args = array() ): bool {
65 return $this->action_scheduler->has_scheduled_action( $this->get_process_item_hook(), array( $args ) );
66 }
67
68 protected function handle_complete( int $final_batch_number, array $args ): void {
69 return;
70 }
71
72 protected function set_items_as_processing( array $items ): void {
73 return;
74 }
75
76 abstract protected function get_batch( int $batch_number, array $args ): array;
77 }
78