|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Background Updater |
|
4
|
|
|
* |
|
5
|
|
|
* Uses https://github.com/A5hleyRich/wp-background-processing to handle DB |
|
6
|
|
|
* updates in the background. |
|
7
|
|
|
* |
|
8
|
|
|
* @class Give_Background_Updater |
|
9
|
|
|
* @version 2.0.0 |
|
10
|
|
|
* @package Give/Classes |
|
11
|
|
|
* @category Class |
|
12
|
|
|
* @author WordImpress |
|
13
|
|
|
*/ |
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Give_Background_Updater Class. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Give_Background_Updater extends WP_Background_Process { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $action = 'give_db_updater'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Dispatch updater. |
|
30
|
|
|
* |
|
31
|
|
|
* Updater will still run via cron job if this fails for any reason. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function dispatch() { |
|
34
|
|
|
/* @var WP_Background_Process $dispatched */ |
|
35
|
|
|
parent::dispatch(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Handle cron healthcheck |
|
40
|
|
|
* |
|
41
|
|
|
* Restart the background process if not already running |
|
42
|
|
|
* and data exists in the queue. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function handle_cron_healthcheck() { |
|
45
|
|
|
if ( $this->is_process_running() ) { |
|
46
|
|
|
// Background process already running. |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ( $this->is_queue_empty() ) { |
|
51
|
|
|
// No data to process. |
|
52
|
|
|
$this->clear_scheduled_event(); |
|
53
|
|
|
|
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->handle(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Schedule fallback event. |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function schedule_event() { |
|
64
|
|
|
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
65
|
|
|
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Task |
|
71
|
|
|
* |
|
72
|
|
|
* Override this method to perform any actions required on each |
|
73
|
|
|
* queue item. Return the modified item for further processing |
|
74
|
|
|
* in the next pass through. Or, return false to remove the |
|
75
|
|
|
* item from the queue. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $update Update info |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function task( $update ) { |
|
82
|
|
|
if ( empty( $update ) ) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* @var Give_Updates $give_updates */ |
|
87
|
|
|
$give_updates = Give_Updates::get_instance(); |
|
88
|
|
|
$resume_update = get_option( |
|
89
|
|
|
'give_doing_upgrade', |
|
90
|
|
|
|
|
|
|
|
|
|
91
|
|
|
// Default update. |
|
92
|
|
|
array( |
|
93
|
|
|
'update_info' => $update, |
|
94
|
|
|
'step' => 1, |
|
95
|
|
|
'update' => 1, |
|
96
|
|
|
'heading' => sprintf( 'Update %s of {update_count}', 1 ), |
|
97
|
|
|
'percentage' => $give_updates->percentage, |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
// Continuously skip update if previous update does not complete yet. |
|
102
|
|
|
if( |
|
|
|
|
|
|
103
|
|
|
$resume_update['update_info']['id'] !== $update['id'] && |
|
104
|
|
|
! give_has_upgrade_completed( $resume_update['update_info']['id'] ) |
|
105
|
|
|
) { |
|
106
|
|
|
return $update; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Set params. |
|
110
|
|
|
$resume_update['update_info'] = $update; |
|
111
|
|
|
$give_updates->step = absint( $resume_update['step'] ); |
|
112
|
|
|
$give_updates->update = absint( $resume_update['update'] ); |
|
113
|
|
|
$is_parent_update_completed = $give_updates->is_parent_updates_completed( $update ); |
|
114
|
|
|
|
|
115
|
|
|
// Skip update if dependency update does not complete yet. |
|
116
|
|
|
if ( empty( $is_parent_update_completed ) ) { |
|
117
|
|
|
// @todo: set error when you have only one update with invalid dependency |
|
118
|
|
|
if ( ! is_null( $is_parent_update_completed ) ) { |
|
119
|
|
|
return $update; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Disable cache. |
|
126
|
|
|
Give_Cache::disable(); |
|
127
|
|
|
|
|
128
|
|
|
// Run update. |
|
129
|
|
|
if ( is_array( $update['callback'] ) ) { |
|
130
|
|
|
$update['callback'][0]->$update['callback'][1](); |
|
131
|
|
|
} else { |
|
132
|
|
|
$update['callback'](); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Set update info. |
|
136
|
|
|
$doing_upgrade_args = array( |
|
137
|
|
|
'update_info' => $update, |
|
138
|
|
|
'step' => ++ $give_updates->step, |
|
139
|
|
|
'update' => $give_updates->update, |
|
140
|
|
|
'heading' => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ), |
|
141
|
|
|
'percentage' => $give_updates->percentage, |
|
142
|
|
|
'total_percentage' => $give_updates->get_db_update_processing_percentage(), |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
// Cache upgrade. |
|
146
|
|
|
update_option( 'give_doing_upgrade', $doing_upgrade_args ); |
|
147
|
|
|
|
|
148
|
|
|
// Enable cache. |
|
149
|
|
|
Give_Cache::enable(); |
|
150
|
|
|
|
|
151
|
|
|
// Check if current update completed or not. |
|
152
|
|
|
if ( give_has_upgrade_completed( $update['id'] ) ) { |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $update; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Complete |
|
161
|
|
|
* |
|
162
|
|
|
* Override if applicable, but ensure that the below actions are |
|
163
|
|
|
* performed, or, call parent::complete(). |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function complete() { |
|
166
|
|
|
parent::complete(); |
|
167
|
|
|
|
|
168
|
|
|
delete_option( 'give_db_update_count' ); |
|
169
|
|
|
delete_option( 'give_doing_upgrade' ); |
|
170
|
|
|
add_option( 'give_show_db_upgrade_complete_notice', 1, '', 'no' ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get memory limit |
|
175
|
|
|
* |
|
176
|
|
|
* @return int |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function get_memory_limit() { |
|
179
|
|
|
if ( function_exists( 'ini_get' ) ) { |
|
180
|
|
|
$memory_limit = ini_get( 'memory_limit' ); |
|
181
|
|
|
} else { |
|
182
|
|
|
// Sensible default. |
|
183
|
|
|
$memory_limit = '128M'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if ( ! $memory_limit || '-1' === $memory_limit ) { |
|
187
|
|
|
// Unlimited, set to 32GB. |
|
188
|
|
|
$memory_limit = '32000M'; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return intval( $memory_limit ) * 1024 * 1024; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|