-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathclass-batch.php
More file actions
215 lines (188 loc) · 5.13 KB
/
class-batch.php
File metadata and controls
215 lines (188 loc) · 5.13 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/********************************************************************
* Copyright (C) 2019 Darko Gjorgjijoski (https://darkog.com)
*
* This file is part of WP Batch Processing
*
* WP Batch Processing is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* WP Batch Processing is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WP Batch Processing. If not, see <https://www.gnu.org/licenses/>.
**********************************************************************/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Direct access is not allowed.' );
}
/**
* Class WP_Batch
*
* Extend this class to create your own batch
*
* Note: You must register the instance in the wp_batch_processing_init hook
* in order to show in the admin area.
*
* eg. WP_Batch_Processor::get_instance()->register( $batch );
*/
abstract class WP_Batch {
/**
* Unique identifier of each batch
* @var string
*/
public $id = 'my_first_batch';
/**
* Describe the batch
* @var string
*/
public $title = 'My first batch';
/**
* Data store of batch items
* @var WP_Batch_Item[]
*/
public $items = array();
/**
* Initialize
*/
public function __construct() {
$this->setup();
}
/**
* To setup the batch data use the push() method to add WP_Batch_Item instances to the queue.
*
* Note: If the operation of obtaining data is expensive, cache it to avoid slowdowns.
*
* @return void
*/
abstract public function setup();
/**
* Handles processing of batch item. One at a time.
*
* In order to work it correctly you must return values as follows:
*
* - TRUE - If the item was processed successfully.
* - WP_Error instance - If there was an error. Add message to display it in the admin area.
*
* @param WP_Batch_Item $item
*
* @return bool|\WP_Error
*/
abstract public function process( $item );
/**
* Called when specific process is finished (all items were processed).
* This method can be overriden in the process class.
* @return void
*/
public function finish() {}
/**
* Queues the item for processing.
*
* @param WP_Batch_Item $item
*/
protected function push( $item ) {
if ( ! is_array( $this->items ) ) {
$this->items = array();
}
$this->items[] = $item;
}
/**
* Returns false if no items left for processing or WP_Batch_Item object for processing.
*
* @param WP_Batch_Item $item
*
* @return bool|WP_Batch_Item
*/
public function get_next_item() {
$processed = $this->get_processed_items();
foreach ( $this->items as $item ) {
if ( ! in_array( $item->id, $processed ) ) {
return $item;
}
}
return false;
}
/**
* Check if the batch is finished.
* @return bool
*/
public function is_finished() {
$is_finished = true;
foreach ( $this->items as $item ) {
if ( ! $this->is_processed( $item ) ) {
$is_finished = false;
}
}
return $is_finished;
}
/**
* Check if batch item was processed.
*
* @param WP_Batch_Item $item
*
* @return bool
*/
public function is_processed( $item ) {
return in_array( $item->id, $this->get_processed_items() );
}
/**
* Returns processed batch item ids
* @return array
*/
public function get_processed_items() {
$processed = get_option( $this->get_db_identifier(), array() );
return $processed;
}
/**
* Returns the count of the processed items
* @return int
*/
public function get_processed_count() {
return count( $this->get_processed_items() );
}
/**
* Mark specific id as processed.
*
* @param int $id
*/
public function mark_as_processed( $id ) {
$processed = $this->get_processed_items();
array_push( $processed, $id );
$processed = array_unique( $processed );
update_option( $this->get_db_identifier(), $processed );
}
/**
* Returns the count of the total items
* @return int
*/
public function get_items_count() {
return count( $this->items );
}
/**
* Returns the percentage
* @return float
*/
public function get_percentage() {
$total_items = $this->get_items_count();
$total_processed = $this->get_processed_count();
$percentage = ( ! empty( $total_items ) ) ? 100 - ( ( ( $total_items - $total_processed ) / $total_items ) * 100 ) : 0;
return number_format( (float) $percentage, 2, '.', '' );
}
/**
* Returns the batch wp_options option_name identifier.
* @return string
*/
public function get_db_identifier() {
return 'batch_' . $this->id . '_processed';
}
/**
* Restarts the processed items store
*/
public function restart() {
delete_option( $this->get_db_identifier() );
}
}