PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / batch / class-batch-task.php

class-batch-task.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/batch/class-batch-task.php

158 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Batch Task
4 *
5 * @since 4.0.0
6 */
7
8 namespace wpCloud\StatelessMedia\Batch;
9
10 abstract class BatchTask implements IBatchTask {
11 /**
12 * Current state of the task
13 *
14 * @var array
15 */
16 protected $state = [];
17
18 /**
19 * ID of the task
20 *
21 * @var string
22 */
23 protected $id = 'batch_task';
24
25 /**
26 * Description of the task (for displaying status)
27 *
28 * @var string
29 */
30 protected $description = '';
31
32 /**
33 * Total number of items to process. Default -1 - infinite/unknown
34 *
35 * @var int
36 */
37 protected $total = -1;
38
39 /**
40 * Number of items processed
41 *
42 * @var int
43 */
44 protected $completed = 0;
45
46 /**
47 * Number of items to process in a single batch
48 *
49 * @var int
50 */
51 protected $limit = 20;
52
53 /**
54 * Offset for the next batch (database offset, page token, etc.)
55 *
56 * @var mixed
57 */
58 protected $offset = 0;
59
60 /**
61 * Date and time when processing started
62 *
63 * @var string|null
64 */
65 protected $started = null;
66
67 /**
68 * Initialize the state of the task
69 *
70 * @param mixed $item
71 */
72 public function init_state() {
73 $this->started = time();
74 }
75
76 /**
77 * Get human-friendly description
78 *
79 * @return string
80 */
81 public function get_description() {
82 return $this->description;
83 }
84
85 /**
86 * Get task state
87 *
88 * @return array
89 */
90 public function get_state() {
91 if ( empty($this->started) ) {
92 $this->init_state();
93 }
94
95 // Calling process can add extra data to the state, so we should try to keep it
96 return wp_parse_args([
97 'id' => $this->id,
98 'description' => $this->description,
99 'total' => $this->total,
100 'completed' => $this->completed,
101 'limit' => $this->limit,
102 'offset' => $this->offset,
103 'started' => $this->started,
104 ], $this->state);
105 }
106
107 /**
108 * Restore task state for processing between calls
109 *
110 * @param array $state
111 * @return array
112 */
113 public function set_state($state) {
114 // Calling process can add extra data to the state, so we should try to keep it
115 $this->state = $state;
116
117 // Restore state properties required for processing
118 if ( isset($state['description']) ) {
119 $this->description = $state['description'];
120 }
121
122 if ( isset($state['started']) ) {
123 $this->started = $state['started'];
124 }
125
126 if ( isset($state['total']) ) {
127 $this->total = $state['total'];
128 }
129
130 if ( isset($state['completed']) ) {
131 $this->completed = $state['completed'];
132 }
133
134 if ( isset($state['limit']) ) {
135 $this->limit = $state['limit'];
136 }
137
138 if ( isset($state['offset']) ) {
139 $this->offset = $state['offset'];
140 }
141 }
142
143 /**
144 * Get batch of data to process. False - no more data
145 *
146 * @return array|false
147 */
148 abstract public function get_batch();
149
150 /**
151 * Process single item. If returns false - the item is removed from the queue. Otherwise repeated.
152 *
153 * @param mixed $item
154 * @return mixed|false
155 */
156 abstract public function process_item($item);
157 }
158