|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Common\Background_Process; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Background_Process extends \Wordlift_Plugin_WP_Background_Process { |
|
6
|
|
|
/** |
|
7
|
|
|
* @var \Wordlift_Log_Service |
|
8
|
|
|
*/ |
|
9
|
|
|
private $log; |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Data_Source |
|
12
|
|
|
*/ |
|
13
|
|
|
private $data_source; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Background_Process constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param $data_source Data_Source |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct( $data_source ) { |
|
21
|
|
|
parent::__construct(); |
|
22
|
|
|
$this->data_source = $data_source; |
|
23
|
|
|
$this->log = \Wordlift_Log_Service::get_logger( get_class() ); |
|
24
|
|
|
// Set value for action key. |
|
25
|
|
|
$this->action = $this->get_action_key(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The key which is used to store the Sync_State class for the current process |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected abstract function get_state_storage_key(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The key which is used as prefix to store the options. |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected abstract function get_action_key(); |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check whether the process must cancel or not. |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool Whether to cancel or not the process. |
|
45
|
|
|
*/ |
|
46
|
|
|
private function must_cancel() { |
|
47
|
|
|
|
|
48
|
|
|
return get_transient( "{$this->action}__cancel" ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Cancels the current process. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function cancel() { |
|
55
|
|
|
|
|
56
|
|
|
$action = $this->action; |
|
57
|
|
|
$this->log->debug( "Cancelling ${action}..." ); |
|
58
|
|
|
|
|
59
|
|
|
// Cleanup the process data. |
|
60
|
|
|
$this->cancel_process(); |
|
61
|
|
|
|
|
62
|
|
|
// Set the state to cancelled. |
|
63
|
|
|
$state = $this->get_state(); |
|
64
|
|
|
$state->set_state( 'cancelled' ); |
|
65
|
|
|
update_option( $this->get_state_storage_key(), $state, false ); |
|
66
|
|
|
|
|
67
|
|
|
// Finally delete the transient. |
|
68
|
|
|
delete_transient( "{$this->action}__cancel" ); |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the sync state. |
|
75
|
|
|
* |
|
76
|
|
|
* @return Sync_State The {@link Sync_State}. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function get_state() { |
|
79
|
|
|
|
|
80
|
|
|
try { |
|
81
|
|
|
return get_option( $this->get_state_storage_key(), Sync_State::unknown() ); |
|
82
|
|
|
} catch ( \Exception $e ) { |
|
83
|
|
|
return Sync_State::unknown(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* This function is called: |
|
91
|
|
|
* - To start a new Synchronization, by passing a {@link Sync_Start_Message} instance. |
|
92
|
|
|
* - To process a item, by passing a numeric ID. |
|
93
|
|
|
* |
|
94
|
|
|
* This function returns the parameter for the next call or NULL if there are no more items to process. |
|
95
|
|
|
* |
|
96
|
|
|
* @param int[] $items An array of item IDs. |
|
97
|
|
|
* |
|
98
|
|
|
* @return int[]|false The next IDs or false if there are no more. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function task( $items ) { |
|
101
|
|
|
|
|
102
|
|
|
// Check if we must cancel. |
|
103
|
|
|
if ( $this->must_cancel() ) { |
|
104
|
|
|
$this->cancel(); |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ( $items && is_array( $items ) ) { |
|
|
|
|
|
|
109
|
|
|
$this->log->debug( sprintf( "Synchronizing items %s...", implode( ', ', $items ) ) ); |
|
110
|
|
|
} |
|
111
|
|
|
// Sync the item. |
|
112
|
|
|
if ( $this->process_items( $items ) ) { |
|
113
|
|
|
// Update the process state, set the index in state in order |
|
114
|
|
|
// to reflect the new items. |
|
115
|
|
|
$this->update_batch_index(); |
|
116
|
|
|
// Get the next batch for processing. |
|
117
|
|
|
return $this->get_next_batch(); |
|
118
|
|
|
} |
|
119
|
|
|
else { |
|
120
|
|
|
// Return the failed term ids again. |
|
121
|
|
|
return $items; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Process all the items in the current batch. |
|
127
|
|
|
* @param $items |
|
128
|
|
|
* |
|
129
|
|
|
* @return bool If all items are successfully processed. |
|
130
|
|
|
*/ |
|
131
|
|
|
abstract protected function process_items( $items ); |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Return next batch of items after processing. |
|
135
|
|
|
* @return int[] or false |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function get_next_batch() { |
|
138
|
|
|
return $this->data_source->next(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
private function update_batch_index() { |
|
144
|
|
|
$next = $this->data_source->next(); |
|
145
|
|
|
$next_state = isset( $next ) ? 'started' : 'ended'; |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Update the synchronization meta data, by increasing the current index. |
|
149
|
|
|
* |
|
150
|
|
|
* @var Sync_State $sync The {@link Sync_State}. |
|
151
|
|
|
*/ |
|
152
|
|
|
$state = self::get_state() |
|
153
|
|
|
->increment_index( $this->data_source->get_batch_size() ) |
|
154
|
|
|
->set_state( $next_state ); |
|
155
|
|
|
update_option( $this->get_state_storage_key(), $state, false ); |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.