class-queue.php
4.03 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Class MC4WP_Queue
*
* @ignore
*/
class MC4WP_Queue
{
/**
* @var MC4WP_Queue_Job[]
*/
protected $jobs;
/**
* @var string
*/
protected $option_name;
/**
* @var bool
*/
protected $dirty = false;
/**
* @var int
*/
const MAX_JOB_COUNT = 1000;
/**
* MC4WP_Ecommerce_Queue constructor.
*
* @param string $option_name
*/
public function __construct($option_name)
{
$this->option_name = $option_name;
register_shutdown_function(array( $this, 'save' ));
}
/**
* Load jobs from option
*/
protected function load()
{
if (! is_null($this->jobs)) {
return;
}
$jobs = get_option($this->option_name, array());
if (! is_array($jobs)) {
$jobs = array();
} else {
$valid_jobs = array();
foreach ($jobs as $i => $obj) {
// filter invalid data from array
if (! is_object($obj) || empty($obj->data)) {
continue;
}
// make sure each job is instance of MC4WP_Queue_Job
if ($obj instanceof MC4WP_Queue_Job) {
$job = $obj;
} else {
$job = new MC4WP_Queue_Job($obj->data);
$job->id = $obj->id;
}
$valid_jobs[] = $job;
}
$jobs = $valid_jobs;
}
$this->jobs = $jobs;
}
/**
* Get all jobs in the queue
*
* @return MC4WP_Queue_Job[] Array of jobs
*/
public function all()
{
$this->load();
return $this->jobs;
}
/**
* Add job to queue
*
* @param mixed $data
* @return boolean
*/
public function put($data)
{
$this->load();
// check if we already have a job with same data
foreach ($this->jobs as $job) {
if ($job->data === $data) {
return false;
}
}
// if we have more than MAX_JOB_COUNT jobs, remove first job item.
// this protects against an ever-growing job list, but also potentially loses jobs if the queue is not processed soon enough.
if (count($this->jobs) > self::MAX_JOB_COUNT) {
array_shift($this->jobs);
}
// add job to end of jobs array
$job = new MC4WP_Queue_Job($data);
$this->jobs[] = $job;
$this->dirty = true;
return true;
}
/**
* Get all jobs in the queue
*
* @return MC4WP_Queue_Job|false
*/
public function get()
{
$this->load();
// do we have jobs?
if (count($this->jobs) === 0) {
return false;
}
// return first element
return reset($this->jobs);
}
/**
* @param MC4WP_Queue_Job $job
*/
public function delete(MC4WP_Queue_Job $job)
{
$this->load();
$index = array_search($job, $this->jobs, true);
// check for "false" here, as 0 is a valid index.
if ($index !== false) {
unset($this->jobs[ $index ]);
$this->jobs = array_values($this->jobs);
$this->dirty = true;
}
}
/**
* @param MC4WP_Queue_Job $job
*/
public function reschedule(MC4WP_Queue_Job $job)
{
$this->load();
// delete job from start of queue
$this->delete($job);
// add job to end of queue
$this->jobs[] = $job;
$this->dirty = true;
}
/**
* Reset queue
*/
public function reset()
{
$this->jobs = array();
$this->dirty = true;
}
/**
* Save the queue
*/
public function save()
{
if (! $this->dirty || is_null($this->jobs)) {
return false;
}
$success = update_option($this->option_name, $this->jobs, false);
if ($success) {
$this->dirty = false;
}
return $success;
}
}