1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<?php
namespace AutomateWoo\Jobs;
use AutomateWoo\ActionScheduler\ActionSchedulerInterface; use AutomateWoo\Exceptions\InvalidArgument; use AutomateWoo\Exceptions\InvalidClass; use AutomateWoo\OptionsStore; use AutomateWoo\Tools\ToolsService; use AutomateWoo\Traits\ArrayValidator; use AutomateWoo\Workflows\Factory as WorkflowsFactory;
defined( 'ABSPATH' ) || exit;
/** * JobRegistry class. * * @since 5.1.0 */ class JobRegistry implements JobRegistryInterface {
use ArrayValidator;
/** * Array of job objects with their names as keys. * * @var JobInterface[] */ protected $jobs;
/** * @var ActionSchedulerInterface */ protected $action_scheduler;
/** * @var OptionsStore */ protected $options_store;
/** * @var ToolsService */ protected $tools_service;
/** * BatchedJobInitializer constructor. * * @param ActionSchedulerInterface $action_scheduler * @param OptionsStore $options_store * @param ToolsService $tools_service */ public function __construct( ActionSchedulerInterface $action_scheduler, OptionsStore $options_store, ToolsService $tools_service ) { $this->action_scheduler = $action_scheduler; $this->options_store = $options_store; $this->tools_service = $tools_service; }
/** * Get a single registered job. * * @param string $name * * @return JobInterface * * @throws InvalidClass|InvalidArgument When there is an error loading jobs. * @throws JobException If the job is not found. */ public function get( string $name ): JobInterface { $this->load_jobs();
if ( ! isset( $this->jobs[ $name ] ) ) { throw JobException::job_does_not_exist( esc_html( $name ) ); }
return $this->jobs[ $name ]; }
/** * Get an array of all registered jobs. * * @return JobInterface[] * * @throws InvalidClass|InvalidArgument When there is an error loading jobs. */ public function list(): array { $this->load_jobs();
return $this->jobs; }
/** * Load jobs. * * Only loads jobs the first time it's called. * * @throws InvalidArgument|InvalidClass When there is an error loading jobs. */ protected function load_jobs() { if ( isset( $this->jobs ) ) { return; } $this->jobs = []; $batched_job_monitor = new ActionSchedulerJobMonitor( $this->action_scheduler );
$midnight_job = new Midnight( $this->action_scheduler, $batched_job_monitor );
$jobs = [ new DeleteFailedQueuedWorkflows( $this->action_scheduler, $batched_job_monitor ), new RunQueuedWorkflows( $this->action_scheduler, $batched_job_monitor ), new SetupRegisteredCustomers( $this->action_scheduler, $batched_job_monitor ), new SetupGuestCustomers( $this->action_scheduler, $batched_job_monitor ), new BatchedWorkflows( $this->action_scheduler, $batched_job_monitor, function ( $id ) { return WorkflowsFactory::get( $id ); } ), new DeleteExpiredCoupons( $this->action_scheduler, $batched_job_monitor, $this->options_store ), new AbandonedCarts( $this->action_scheduler, $batched_job_monitor, $this->options_store ), new WishlistItemOnSale( $this->action_scheduler, $batched_job_monitor ), new ToolTaskRunner( $this->action_scheduler, $batched_job_monitor, $this->tools_service ), $midnight_job, new CheckMidnightJob( $this->action_scheduler, $batched_job_monitor, $midnight_job ), new CheckGmtOffsetChange( $this->action_scheduler, $batched_job_monitor ), new ProductGoesOnSale( $this->action_scheduler, $batched_job_monitor ), new CleanInactiveCarts( $this->action_scheduler, $batched_job_monitor ), ];
/** * Apply filter to registered job objects. * * @param JobInterface[] $jobs * * @since 5.1.0 */ $jobs = apply_filters( 'automatewoo/jobs', $jobs );
// Ensure $jobs is still an array $this->validate_is_array( $jobs );
foreach ( $jobs as $job ) { if ( ! $job instanceof JobInterface ) { throw InvalidClass::does_not_implement_interface( esc_html( get_class( $job ) ), JobInterface::class ); }
$this->jobs[ $job->get_name() ] = $job; } } }
|