1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Give_Updates |
5
|
|
|
* |
6
|
|
|
* @since 1.8.12 |
7
|
|
|
*/ |
8
|
|
|
class Give_Updates { |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Instance. |
12
|
|
|
* |
13
|
|
|
* @since |
14
|
|
|
* @access static |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
static private $instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instance. |
21
|
|
|
* |
22
|
|
|
* @since |
23
|
|
|
* @access public |
24
|
|
|
* @var Give_Background_Updater |
25
|
|
|
*/ |
26
|
|
|
static public $background_updater; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Updates |
30
|
|
|
* |
31
|
|
|
* @since 1.8.12 |
32
|
|
|
* @access private |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $updates = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Current update percentage number |
39
|
|
|
* |
40
|
|
|
* @since 1.8.12 |
41
|
|
|
* @access private |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $percentage = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Current update step number |
48
|
|
|
* |
49
|
|
|
* @since 1.8.12 |
50
|
|
|
* @access private |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public $step = 1; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Current update number |
57
|
|
|
* |
58
|
|
|
* @since 1.8.12 |
59
|
|
|
* @access private |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
public $update = 1; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Singleton pattern. |
66
|
|
|
* |
67
|
|
|
* @since 1.8.12 |
68
|
|
|
* @access private |
69
|
|
|
* |
70
|
|
|
* @param Give_Updates . |
71
|
|
|
*/ |
72
|
|
|
private function __construct() { |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Register updates |
77
|
|
|
* |
78
|
|
|
* @since 1.8.12 |
79
|
|
|
* @access public |
80
|
|
|
* |
81
|
|
|
* @param array $args |
82
|
|
|
*/ |
83
|
|
|
public function register( $args ) { |
84
|
|
|
$args_default = array( |
85
|
|
|
'id' => '', |
86
|
|
|
'version' => '', |
87
|
|
|
'callback' => '', |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$args = wp_parse_args( $args, $args_default ); |
91
|
|
|
|
92
|
|
|
// You can only register database upgrade. |
93
|
|
|
$args['type'] = 'database'; |
94
|
|
|
|
95
|
|
|
// Bailout. |
96
|
|
|
if ( |
97
|
|
|
empty( $args['id'] ) || |
98
|
|
|
empty( $args['version'] ) || |
99
|
|
|
empty( $args['callback'] ) || |
100
|
|
|
! is_callable( $args['callback'] ) |
101
|
|
|
) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Change depend param to array. |
106
|
|
|
if ( isset( $args['depend'] ) && is_string( $args['depend'] ) ) { |
107
|
|
|
$args['depend'] = array( $args['depend'] ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->updates[ $args['type'] ][] = $args; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get instance. |
115
|
|
|
* |
116
|
|
|
* @since |
117
|
|
|
* @access static |
118
|
|
|
* @return static |
119
|
|
|
*/ |
120
|
|
|
static function get_instance() { |
|
|
|
|
121
|
|
|
if ( is_null( self::$instance ) ) { |
122
|
|
|
self::$instance = new self(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return self::$instance; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* |
130
|
|
|
* Setup hook |
131
|
|
|
* |
132
|
|
|
* @since 1.8.12 |
133
|
|
|
* @access public |
134
|
|
|
*/ |
135
|
|
|
public function setup() { |
136
|
|
|
/** |
137
|
|
|
* Load file |
138
|
|
|
*/ |
139
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/class-give-background-updater.php'; |
140
|
|
|
require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php'; |
141
|
|
|
|
142
|
|
|
self::$background_updater = new Give_Background_Updater(); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Setup hooks. |
146
|
|
|
*/ |
147
|
|
|
add_action( 'init', array( $this, '__register_upgrade' ), 9999 ); |
148
|
|
|
add_action( 'give_set_upgrade_completed', array( $this, '__flush_resume_updates' ), 9999 ); |
149
|
|
|
add_action( 'wp_ajax_give_db_updates_info', array( $this, '__give_db_updates_info' ) ); |
150
|
|
|
add_action( 'wp_ajax_give_run_db_updates', array( $this, '__give_start_updating' ) ); |
151
|
|
|
add_action( 'admin_init', array( $this, '__redirect_admin' ) ); |
152
|
|
|
add_action( 'admin_init', array( $this, '__pause_db_update' ), -1 ); |
153
|
|
|
add_action( 'admin_init', array( $this, '__restart_db_update' ), -1 ); |
154
|
|
|
add_action( 'admin_notices', array( $this, '__show_notice' ) ); |
155
|
|
|
add_action( 'give_restart_db_upgrade', array( $this, '__health_background_update' ) ); |
156
|
|
|
|
157
|
|
|
if ( is_admin() ) { |
158
|
|
|
add_action( 'admin_init', array( $this, '__change_donations_label' ), 9999 ); |
159
|
|
|
add_action( 'admin_menu', array( $this, '__register_menu' ), 9999 ); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Register plugin add-on updates. |
165
|
|
|
* |
166
|
|
|
* @since 1.8.12 |
167
|
|
|
* @access public |
168
|
|
|
*/ |
169
|
|
|
public function __register_plugin_addon_updates() { |
|
|
|
|
170
|
|
|
$addons = give_get_plugins(); |
171
|
|
|
$plugin_updates = get_plugin_updates(); |
172
|
|
|
|
173
|
|
|
foreach ( $addons as $key => $info ) { |
174
|
|
|
if ( 'active' != $info['Status'] || 'add-on' != $info['Type'] || empty( $plugin_updates[ $key ] ) ) { |
175
|
|
|
continue; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->updates['plugin'][] = array_merge( $info, (array) $plugin_updates[ $key ] ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Fire custom action hook to register updates |
185
|
|
|
* |
186
|
|
|
* @since 1.8.12 |
187
|
|
|
* @access public |
188
|
|
|
*/ |
189
|
|
|
public function __register_upgrade() { |
|
|
|
|
190
|
|
|
if ( ! is_admin() ) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Fire the hook |
196
|
|
|
* |
197
|
|
|
* @since 1.8.12 |
198
|
|
|
*/ |
199
|
|
|
do_action( 'give_register_updates', $this ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Rename `Donations` menu title if updates exists |
204
|
|
|
* |
205
|
|
|
* @since 1.8.12 |
206
|
|
|
* @access public |
207
|
|
|
*/ |
208
|
|
|
function __change_donations_label() { |
|
|
|
|
209
|
|
|
global $menu; |
210
|
|
|
|
211
|
|
|
// Bailout. |
212
|
|
|
if ( empty( $menu ) || ! $this->get_total_update_count() ) { |
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$is_update = ( $this->is_doing_updates() && ! self::$background_updater->is_paused_process() ); |
217
|
|
|
|
218
|
|
|
foreach ( $menu as $index => $menu_item ) { |
219
|
|
|
if ( 'edit.php?post_type=give_forms' !== $menu_item[2] ) { |
220
|
|
|
continue; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$menu[ $index ][0] = sprintf( |
224
|
|
|
'%1$s <span class="update-plugins"><span class="plugin-count give-update-progress-count">%2$s%3$s</span></span>', |
225
|
|
|
__( 'Donations', 'give' ), |
226
|
|
|
$is_update ? |
227
|
|
|
$this->get_db_update_processing_percentage() : |
228
|
|
|
$this->get_total_update_count(), |
229
|
|
|
$is_update ? '%' : '' |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
break; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Register updates menu |
238
|
|
|
* |
239
|
|
|
* @since 1.8.12 |
240
|
|
|
* @access public |
241
|
|
|
*/ |
242
|
|
|
public function __register_menu() { |
|
|
|
|
243
|
|
|
// Bailout. |
244
|
|
|
if( ! give_test_ajax_works() ) { |
|
|
|
|
245
|
|
|
return; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// Load plugin updates. |
249
|
|
|
$this->__register_plugin_addon_updates(); |
250
|
|
|
|
251
|
|
|
// Bailout. |
252
|
|
|
if ( ! $this->get_total_update_count() ) { |
253
|
|
|
// Show complete update message if still on update setting page. |
254
|
|
|
if ( isset( $_GET['page'] ) && 'give-updates' === $_GET['page'] ) { |
|
|
|
|
255
|
|
|
// Upgrades |
256
|
|
|
add_submenu_page( |
257
|
|
|
'edit.php?post_type=give_forms', |
258
|
|
|
esc_html__( 'Give Updates Complete', 'give' ), |
259
|
|
|
__( 'Updates', 'give' ), |
260
|
|
|
'manage_give_settings', |
261
|
|
|
'give-updates', |
262
|
|
|
array( $this, 'render_complete_page' ) |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$is_update = ( $this->is_doing_updates() && ! self::$background_updater->is_paused_process() ); |
270
|
|
|
|
271
|
|
|
// Upgrades |
272
|
|
|
add_submenu_page( |
273
|
|
|
'edit.php?post_type=give_forms', |
274
|
|
|
esc_html__( 'Give Updates', 'give' ), |
275
|
|
|
sprintf( |
276
|
|
|
'%1$s <span class="update-plugins"%2$s><span class="plugin-count give-update-progress-count">%3$s%4$s</span></span>', |
277
|
|
|
__( 'Updates', 'give' ), |
278
|
|
|
isset( $_GET['give-pause-db-upgrades'] ) ? ' style="display:none;"' : '', |
|
|
|
|
279
|
|
|
$is_update ? |
280
|
|
|
$this->get_db_update_processing_percentage() : |
281
|
|
|
$this->get_total_update_count(), |
282
|
|
|
$is_update ? '%' : '' |
283
|
|
|
), |
284
|
|
|
'manage_give_settings', |
285
|
|
|
'give-updates', |
286
|
|
|
array( $this, 'render_page' ) |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Show update related notices |
293
|
|
|
* |
294
|
|
|
* @since 2.0 |
295
|
|
|
* @access public |
296
|
|
|
*/ |
297
|
|
|
public function __redirect_admin() { |
|
|
|
|
298
|
|
|
// Show db upgrade completed notice. |
299
|
|
|
if ( |
300
|
|
|
! wp_doing_ajax() && |
301
|
|
|
current_user_can( 'manage_give_settings' ) && |
302
|
|
|
get_option( 'give_show_db_upgrade_complete_notice' ) && |
303
|
|
|
! isset( $_GET['give-db-update-completed'] ) |
|
|
|
|
304
|
|
|
) { |
305
|
|
|
delete_option( 'give_show_db_upgrade_complete_notice' ); |
306
|
|
|
|
307
|
|
|
wp_redirect( add_query_arg( array( 'give-db-update-completed' => 'give_db_upgrade_completed' ) ) ); |
308
|
|
|
exit(); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Pause db upgrade |
315
|
|
|
* |
316
|
|
|
* @since 2.0.1 |
317
|
|
|
* @access public |
318
|
|
|
* |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function __pause_db_update() { |
|
|
|
|
322
|
|
|
// Bailout. |
323
|
|
View Code Duplication |
if ( |
|
|
|
|
324
|
|
|
wp_doing_ajax() || |
325
|
|
|
! isset( $_GET['page'] ) || |
|
|
|
|
326
|
|
|
'give-updates' !== $_GET['page'] || |
|
|
|
|
327
|
|
|
! isset( $_GET['give-pause-db-upgrades'] ) || |
|
|
|
|
328
|
|
|
self::$background_updater->is_paused_process() |
329
|
|
|
) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$batch = self::$background_updater->get_all_batch(); |
334
|
|
|
|
335
|
|
|
if ( ! empty( $batch ) ) { |
336
|
|
|
update_option( 'give_paused_batches', $batch, 'no' ); |
337
|
|
|
delete_option( $batch->key ); |
338
|
|
|
delete_site_transient( self::$background_updater->get_identifier() . '_process_lock' ); |
339
|
|
|
wp_clear_scheduled_hook( self::$background_updater->get_cron_identifier() ); |
340
|
|
|
|
341
|
|
|
Give()->logs->add( 'Update Pause', print_r( $batch, true ), 0, 'update' ); |
|
|
|
|
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Fire action when pause db updates |
345
|
|
|
* |
346
|
|
|
* @since 2.0.1 |
347
|
|
|
*/ |
348
|
|
|
do_action( 'give_pause_db_upgrade', $this ); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return true; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Restart db upgrade |
356
|
|
|
* |
357
|
|
|
* @since 2.0.1 |
358
|
|
|
* @access public |
359
|
|
|
* |
360
|
|
|
* @return bool |
361
|
|
|
*/ |
362
|
|
|
public function __restart_db_update() { |
|
|
|
|
363
|
|
|
// Bailout. |
364
|
|
View Code Duplication |
if ( |
|
|
|
|
365
|
|
|
wp_doing_ajax() || |
366
|
|
|
! isset( $_GET['page'] ) || |
|
|
|
|
367
|
|
|
'give-updates' !== $_GET['page'] || |
|
|
|
|
368
|
|
|
! isset( $_GET['give-restart-db-upgrades'] ) || |
|
|
|
|
369
|
|
|
! self::$background_updater->is_paused_process() |
370
|
|
|
) { |
371
|
|
|
return false; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
$batch = get_option( 'give_paused_batches' ); |
375
|
|
|
|
376
|
|
|
if ( ! empty( $batch ) ) { |
377
|
|
|
update_option( $batch->key, $batch->data ); |
378
|
|
|
delete_option( 'give_paused_batches' ); |
379
|
|
|
|
380
|
|
|
Give()->logs->add( 'Update Restart', print_r( $batch, true ), 0, 'update' ); |
|
|
|
|
381
|
|
|
|
|
|
|
|
382
|
|
|
|
383
|
|
|
/** Fire action when restart db updates |
384
|
|
|
* |
385
|
|
|
* @since 2.0.1 |
386
|
|
|
*/ |
387
|
|
|
do_action( 'give_restart_db_upgrade', $this ); |
388
|
|
|
|
389
|
|
|
self::$background_updater->dispatch(); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
return true; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Health check for updates. |
397
|
|
|
* |
398
|
|
|
* @since 2.0 |
399
|
|
|
* @access public |
400
|
|
|
* |
401
|
|
|
* @param Give_Updates $give_updates |
402
|
|
|
*/ |
403
|
|
|
public function __health_background_update( $give_updates ) { |
|
|
|
|
404
|
|
|
if( ! $this->is_doing_updates() ) { |
|
|
|
|
405
|
|
|
return; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$batch = Give_Updates::$background_updater->get_all_batch(); |
409
|
|
|
$batch_data_count = count( $batch->data ); |
410
|
|
|
$all_updates = $give_updates->get_updates( 'database', 'all' ); |
411
|
|
|
$all_update_ids = wp_list_pluck( $all_updates, 'id' ); |
412
|
|
|
$all_batch_update_ids = ! empty( $batch ) ? wp_list_pluck( $batch->data, 'id' ) : array(); |
413
|
|
|
$log_data = ''; |
414
|
|
|
|
415
|
|
|
if ( ! empty( $batch ) ) { |
416
|
|
|
|
417
|
|
|
foreach ( $batch->data as $index => $update ) { |
418
|
|
|
$log_data = print_r( $update, true ) . "\n"; |
|
|
|
|
419
|
|
|
|
420
|
|
|
if ( ! is_callable( $update['callback'] ) ) { |
421
|
|
|
$log_data .= 'Removing missing callback update: ' . "{$update['id']}\n"; |
422
|
|
|
unset( $batch->data[ $index ] ); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if ( ! empty( $update['depend'] ) ) { |
426
|
|
|
|
427
|
|
|
foreach ( $update['depend'] as $depend ) { |
428
|
|
|
if ( give_has_upgrade_completed( $depend ) ) { |
429
|
|
|
$log_data .= 'Completed update: ' . "{$depend}\n"; |
430
|
|
|
continue; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if ( in_array( $depend, $all_update_ids ) && ! in_array( $depend, $all_batch_update_ids ) ) { |
434
|
|
|
$log_data .= 'Adding missing update: ' . "{$depend}\n"; |
435
|
|
|
array_unshift( $batch->data, $all_updates[ array_search( $depend, $all_update_ids ) ] ); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if( $new_updates = $this->get_updates( 'database', 'new' ) ){ |
|
|
|
|
443
|
|
|
$all_batch_update_ids = ! empty( $batch ) ? wp_list_pluck( $batch->data, 'id' ) : array(); |
444
|
|
|
|
445
|
|
|
foreach ( $new_updates as $index => $new_update ) { |
446
|
|
|
if( give_has_upgrade_completed( $new_update['id'] ) || in_array( $new_update['id'], $all_batch_update_ids ) ) { |
|
|
|
|
447
|
|
|
unset( $new_updates[$index] ); |
|
|
|
|
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
if( ! empty( $new_updates ) ) { |
|
|
|
|
452
|
|
|
$log_data .= 'Adding new update: ' . "\n"; |
453
|
|
|
$log_data .= print_r( $new_updates, true ) . "\n"; |
|
|
|
|
454
|
|
|
|
455
|
|
|
$batch->data = array_merge( $batch->data, $new_updates ); |
456
|
|
|
update_option( 'give_db_update_count', ( absint( get_option( 'give_db_update_count' ) ) + count( $new_updates ) ) ); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if ( $batch_data_count !== count( $batch->data ) ) { |
461
|
|
|
$log_data .= 'Updating batch' . "\n"; |
462
|
|
|
$log_data .= print_r( $batch, true ); |
|
|
|
|
463
|
|
|
|
464
|
|
|
update_option( $batch->key, $batch->data ); |
465
|
|
|
|
466
|
|
|
// Reset update info. |
467
|
|
|
$doing_upgrade_args = get_option( 'give_doing_upgrade' ); |
468
|
|
|
// $doing_upgrade_args['update'] = $give_updates->update; |
469
|
|
|
$doing_upgrade_args['heading'] = sprintf( 'Update %s of %s', $doing_upgrade_args['update'], get_option( 'give_db_update_count' ) ); |
470
|
|
|
$doing_upgrade_args['total_percentage'] = $this->get_db_update_processing_percentage(); |
471
|
|
|
update_option( 'give_doing_upgrade', $doing_upgrade_args ); |
472
|
|
|
|
473
|
|
|
Give()->logs->add( 'Update Health Check', $log_data, 0, 'update' ); |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Show update related notices |
480
|
|
|
* |
481
|
|
|
* @since 2.0 |
482
|
|
|
* @access public |
483
|
|
|
*/ |
484
|
|
|
public function __show_notice() { |
|
|
|
|
485
|
|
|
// Bailout. |
486
|
|
|
if ( ! current_user_can( 'manage_give_settings' ) ) { |
487
|
|
|
return; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
// Run DB updates. |
491
|
|
|
if ( ! empty( $_GET['give-run-db-update'] ) ) { |
|
|
|
|
492
|
|
|
$this->run_db_update(); |
493
|
|
|
} |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
496
|
|
|
// Bailout. |
497
|
|
|
if ( isset( $_GET['page'] ) && 'give-updates' === $_GET['page'] ) { |
|
|
|
|
498
|
|
|
return; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
// Show notice if upgrade paused. |
502
|
|
View Code Duplication |
if ( self::$background_updater->is_paused_process() ) { |
|
|
|
|
503
|
|
|
ob_start(); |
504
|
|
|
?> |
505
|
|
|
<p> |
506
|
|
|
<strong><?php _e( 'Database Update', 'give' ); ?></strong> |
507
|
|
|
– <?php _e( 'GiveWP needs to update your database to the latest version. The following process will make updates to your site\'s database. Please create a backup before proceeding.', 'give' ); ?> |
508
|
|
|
</p> |
509
|
|
|
<p class="submit"> |
510
|
|
|
<a href="<?php echo esc_url( add_query_arg( array( 'give-restart-db-upgrades' => 1 ), admin_url( 'edit.php?post_type=give_forms&page=give-updates' ) ) ); ?>" class="button button-primary give-restart-updater-btn"> |
511
|
|
|
<?php _e( 'Restart the updater', 'give' ); ?> |
512
|
|
|
</a> |
513
|
|
|
</p> |
514
|
|
|
<script type="text/javascript"> |
515
|
|
|
jQuery('.give-restart-updater-btn').click('click', function () { |
516
|
|
|
return window.confirm('<?php echo esc_js( __( 'It is recommended that you backup your database before proceeding. Do you want to run the update now?', 'give' ) ); ?>'); // jshint ignore:line |
517
|
|
|
}); |
518
|
|
|
</script> |
519
|
|
|
<?php |
520
|
|
|
$desc_html = ob_get_clean(); |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
523
|
|
|
Give()->notices->register_notice( array( |
524
|
|
|
'id' => 'give_upgrade_db', |
525
|
|
|
'type' => 'error', |
526
|
|
|
'dismissible' => false, |
527
|
|
|
'description' => $desc_html, |
528
|
|
|
) ); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
// Bailout if doing upgrades. |
532
|
|
|
if( $this->is_doing_updates() ) { |
|
|
|
|
533
|
|
|
return; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
// Show notice if ajax is not working. |
537
|
|
|
if ( ! give_test_ajax_works() ) { |
538
|
|
|
Give()->notices->register_notice( |
539
|
|
|
array( |
540
|
|
|
'id' => 'give_db_upgrade_ajax_inaccessible', |
541
|
|
|
'type' => 'error', |
542
|
|
|
'description' => sprintf( '%1$s <a href="%2$s">%3$s</a>', __( 'Give needs to upgrade the database but cannot because AJAX does not appear accessible. This could be because your website is password protected, in maintenance mode, or has a specific hosting configuration or plugin active that is preventing access.', 'give' ), 'http://docs.givewp.com/admin-ajax-error', __( 'Read More', 'give' ) . ' »' ), |
543
|
|
|
'show' => true, |
544
|
|
|
) |
545
|
|
|
); |
546
|
|
|
|
547
|
|
|
return; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
// Show db upgrade completed notice. |
551
|
|
|
if ( ! empty( $_GET['give-db-update-completed'] ) ) { |
|
|
|
|
552
|
|
|
Give()->notices->register_notice( array( |
553
|
|
|
'id' => 'give_db_upgrade_completed', |
554
|
|
|
'type' => 'updated', |
555
|
|
|
'description' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ), |
556
|
|
|
'show' => true, |
557
|
|
|
) ); |
558
|
|
|
|
559
|
|
|
// Start update. |
560
|
|
|
} elseif ( ! empty( $_GET['give-run-db-update'] ) ) { |
|
|
|
|
561
|
|
|
$this->run_db_update(); |
562
|
|
|
|
563
|
|
|
// Show run the update notice. |
564
|
|
View Code Duplication |
} elseif ( $this->get_total_new_db_update_count() ) { |
|
|
|
|
565
|
|
|
ob_start(); |
566
|
|
|
?> |
567
|
|
|
<p> |
568
|
|
|
<strong><?php _e( 'Database Update', 'give' ); ?></strong> |
569
|
|
|
– <?php _e( 'GiveWP needs to update your database to the latest version. The following process will make updates to your site\'s database. Please create a complete backup before proceeding.', 'give' ); ?> |
570
|
|
|
</p> |
571
|
|
|
<p class="submit"> |
572
|
|
|
<a href="<?php echo esc_url( add_query_arg( array( 'give-run-db-update' => 1 ), admin_url( 'edit.php?post_type=give_forms&page=give-updates' ) ) ); ?>" class="button button-primary give-run-update-now"> |
573
|
|
|
<?php _e( 'Run the updater', 'give' ); ?> |
574
|
|
|
</a> |
575
|
|
|
</p> |
576
|
|
|
<script type="text/javascript"> |
577
|
|
|
jQuery('.give-run-update-now').click('click', function () { |
578
|
|
|
return window.confirm('<?php echo esc_js( __( 'It is recommended that you backup your database before proceeding. Do you want to run the update now?', 'give' ) ); ?>'); // jshint ignore:line |
579
|
|
|
}); |
580
|
|
|
</script> |
581
|
|
|
<?php |
582
|
|
|
$desc_html = ob_get_clean(); |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
585
|
|
|
Give()->notices->register_notice( array( |
586
|
|
|
'id' => 'give_upgrade_db', |
587
|
|
|
'type' => 'updated', |
588
|
|
|
'dismissible' => false, |
589
|
|
|
'description' => $desc_html, |
590
|
|
|
) ); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Render Give Updates Completed page |
596
|
|
|
* |
597
|
|
|
* @since 1.8.12 |
598
|
|
|
* @access public |
599
|
|
|
*/ |
600
|
|
|
public function render_complete_page() { |
601
|
|
|
include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades-complete.php'; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Render Give Updates page |
606
|
|
|
* |
607
|
|
|
* @since 1.8.12 |
608
|
|
|
* @access public |
609
|
|
|
*/ |
610
|
|
|
public function render_page() { |
611
|
|
|
include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades.php'; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Run database upgrades |
616
|
|
|
* |
617
|
|
|
* @since 2.0 |
618
|
|
|
* @access private |
619
|
|
|
*/ |
620
|
|
|
private function run_db_update() { |
621
|
|
|
// Bailout. |
622
|
|
|
if ( $this->is_doing_updates() || ! $this->get_total_new_db_update_count() ) { |
623
|
|
|
return; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
$updates = $this->get_updates( 'database', 'new' ); |
627
|
|
|
|
628
|
|
|
foreach ( $updates as $update ) { |
629
|
|
|
self::$background_updater->push_to_queue( $update ); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
add_option( 'give_db_update_count', count( $updates ), '', 'no' ); |
633
|
|
|
|
634
|
|
|
add_option( 'give_doing_upgrade', array( |
635
|
|
|
'update_info' => $updates[0], |
636
|
|
|
'step' => 1, |
637
|
|
|
'update' => 1, |
638
|
|
|
'heading' => sprintf( 'Update %s of %s', 1, count( $updates ) ), |
639
|
|
|
'percentage' => 0, |
640
|
|
|
'total_percentage' => 0, |
641
|
|
|
), '', 'no' ); |
642
|
|
|
|
643
|
|
|
self::$background_updater->save()->dispatch(); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Delete resume updates |
649
|
|
|
* |
650
|
|
|
* @since 1.8.12 |
651
|
|
|
* @access public |
652
|
|
|
*/ |
653
|
|
|
public function __flush_resume_updates() { |
|
|
|
|
654
|
|
|
//delete_option( 'give_doing_upgrade' ); |
655
|
|
|
update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) ); |
656
|
|
|
|
657
|
|
|
// Reset counter. |
658
|
|
|
$this->step = $this->percentage = 0; |
|
|
|
|
659
|
|
|
++ $this->update; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* Initialize updates |
665
|
|
|
* |
666
|
|
|
* @since 2.0 |
667
|
|
|
* @access public |
668
|
|
|
* |
669
|
|
|
* @return void |
670
|
|
|
*/ |
671
|
|
|
public function __give_start_updating() { |
|
|
|
|
672
|
|
|
// Check permission. |
673
|
|
|
if ( |
674
|
|
|
! current_user_can( 'manage_give_settings' ) || |
675
|
|
|
$this->is_doing_updates() |
676
|
|
|
) { |
677
|
|
|
wp_send_json_error(); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
// @todo: validate nonce |
681
|
|
|
// @todo: set http method to post |
682
|
|
|
if ( empty( $_POST['run_db_update'] ) ) { |
|
|
|
|
683
|
|
|
wp_send_json_error(); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
$this->run_db_update(); |
687
|
|
|
|
688
|
|
|
wp_send_json_success(); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* This function handle ajax query for dn update status. |
694
|
|
|
* |
695
|
|
|
* @since 2.0 |
696
|
|
|
* @access public |
697
|
|
|
* |
698
|
|
|
* @return string |
699
|
|
|
*/ |
700
|
|
|
public function __give_db_updates_info() { |
|
|
|
|
701
|
|
|
$update_info = get_option( 'give_doing_upgrade' ); |
702
|
|
|
$response_type = ''; |
703
|
|
|
|
704
|
|
|
if ( empty( $update_info ) ) { |
705
|
|
|
$update_info = array( |
706
|
|
|
'message' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ), |
707
|
|
|
'heading' => __( 'Updates Completed.', 'give' ), |
708
|
|
|
'percentage' => 0, |
709
|
|
|
); |
710
|
|
|
$response_type = 'success'; |
711
|
|
|
|
712
|
|
|
delete_option( 'give_show_db_upgrade_complete_notice' ); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
$this->send_ajax_response( $update_info, $response_type ); |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* Send ajax response |
720
|
|
|
* |
721
|
|
|
* @since 1.8.12 |
722
|
|
|
* @access public |
723
|
|
|
* |
724
|
|
|
* @param $data |
725
|
|
|
* @param string $type |
726
|
|
|
*/ |
727
|
|
|
public function send_ajax_response( $data, $type = '' ) { |
728
|
|
|
$default = array( |
729
|
|
|
'message' => '', |
730
|
|
|
'heading' => '', |
731
|
|
|
'percentage' => 0, |
732
|
|
|
'step' => 0, |
733
|
|
|
'update' => 0, |
734
|
|
|
); |
735
|
|
|
|
736
|
|
|
// Set data. |
737
|
|
|
$data = wp_parse_args( $data, $default ); |
738
|
|
|
|
739
|
|
|
// Enable cache. |
740
|
|
|
Give_Cache::enable(); |
741
|
|
|
|
742
|
|
|
switch ( $type ) { |
743
|
|
|
case 'success': |
744
|
|
|
wp_send_json_success( $data ); |
745
|
|
|
break; |
746
|
|
|
|
747
|
|
|
case 'error': |
748
|
|
|
wp_send_json_error( $data ); |
749
|
|
|
break; |
750
|
|
|
|
751
|
|
|
default: |
752
|
|
|
wp_send_json( array( |
753
|
|
|
'data' => $data, |
754
|
|
|
) ); |
755
|
|
|
break; |
756
|
|
|
} |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* Set current update percentage. |
761
|
|
|
* |
762
|
|
|
* @since 1.8.12 |
763
|
|
|
* @access public |
764
|
|
|
* |
765
|
|
|
* @param $total |
766
|
|
|
* @param $current_total |
767
|
|
|
*/ |
768
|
|
|
public function set_percentage( $total, $current_total ) { |
769
|
|
|
// Set percentage. |
770
|
|
|
$this->percentage = $total ? ( ( $current_total ) / $total ) * 100 : 0; |
|
|
|
|
771
|
|
|
|
772
|
|
|
// Verify percentage. |
773
|
|
|
$this->percentage = ( 100 < $this->percentage ) ? 100 : $this->percentage; |
|
|
|
|
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* Check if parent update completed or not. |
778
|
|
|
* |
779
|
|
|
* @since 2.0 |
780
|
|
|
* @access private |
781
|
|
|
* |
782
|
|
|
* @param array $update |
783
|
|
|
* |
784
|
|
|
* @return bool|null |
785
|
|
|
*/ |
786
|
|
|
public function is_parent_updates_completed( $update ) { |
787
|
|
|
// Bailout. |
788
|
|
|
if ( empty( $update['depend'] ) ) { |
789
|
|
|
return true; |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
// Check if dependency is valid or not. |
793
|
|
|
if ( ! $this->has_valid_dependency( $update ) ) { |
794
|
|
|
return null; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$is_dependency_completed = true; |
798
|
|
|
|
799
|
|
|
foreach ( $update['depend'] as $depend ) { |
800
|
|
|
|
801
|
|
|
if ( ! give_has_upgrade_completed( $depend ) ) { |
802
|
|
|
$is_dependency_completed = false; |
803
|
|
|
break; |
804
|
|
|
} |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
return $is_dependency_completed; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Flag to check if DB updates running or not. |
812
|
|
|
* |
813
|
|
|
* @since 2.0 |
814
|
|
|
* @access public |
815
|
|
|
* @return bool |
816
|
|
|
*/ |
817
|
|
|
public function is_doing_updates() { |
818
|
|
|
return (bool) get_option( 'give_doing_upgrade' ); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* Check if update has valid dependency or not. |
824
|
|
|
* |
825
|
|
|
* @since 2.0 |
826
|
|
|
* @access public |
827
|
|
|
* |
828
|
|
|
* @param $update |
829
|
|
|
* |
830
|
|
|
* @return bool |
831
|
|
|
*/ |
832
|
|
|
public function has_valid_dependency( $update ) { |
|
|
|
|
833
|
|
|
$is_valid_dependency = true; |
834
|
|
|
// $update_ids = wp_list_pluck( $this->get_updates( 'database', 'all' ), 'id' ); |
835
|
|
|
// |
836
|
|
|
// foreach ( $update['depend'] as $depend ) { |
837
|
|
|
// // Check if dependency is valid or not. |
838
|
|
|
// if ( ! in_array( $depend, $update_ids ) ) { |
839
|
|
|
// $is_valid_dependency = false; |
840
|
|
|
// break; |
841
|
|
|
// } |
842
|
|
|
// } |
843
|
|
|
|
844
|
|
|
return $is_valid_dependency; |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
/** |
848
|
|
|
* Get updates. |
849
|
|
|
* |
850
|
|
|
* @since 1.8.12 |
851
|
|
|
* @access public |
852
|
|
|
* |
853
|
|
|
* @param string $update_type Tye of update. |
854
|
|
|
* @param string $status Tye of update. |
855
|
|
|
* |
856
|
|
|
* @return array |
857
|
|
|
*/ |
858
|
|
|
public function get_updates( $update_type = '', $status = 'all' ) { |
859
|
|
|
// return all updates. |
860
|
|
|
if ( empty( $update_type ) ) { |
861
|
|
|
return $this->updates; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
// Get specific update. |
865
|
|
|
$updates = ! empty( $this->updates[ $update_type ] ) ? $this->updates[ $update_type ] : array(); |
866
|
|
|
|
867
|
|
|
// Bailout. |
868
|
|
|
if ( empty( $updates ) ) { |
869
|
|
|
return $updates; |
870
|
|
|
} |
871
|
|
|
|
872
|
|
|
switch ( $status ) { |
873
|
|
|
case 'new': |
874
|
|
|
// Remove already completed updates. |
875
|
|
|
$completed_updates = give_get_completed_upgrades(); |
876
|
|
|
|
877
|
|
|
if ( ! empty( $completed_updates ) ) { |
878
|
|
|
foreach ( $updates as $index => $update ) { |
879
|
|
|
if ( in_array( $update['id'], $completed_updates ) ) { |
880
|
|
|
unset( $updates[ $index ] ); |
881
|
|
|
} |
882
|
|
|
} |
883
|
|
|
$updates = array_values( $updates ); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
break; |
887
|
|
|
} |
888
|
|
|
|
889
|
|
|
return $updates; |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* Get addon update count. |
894
|
|
|
* |
895
|
|
|
* @since 1.8.12 |
896
|
|
|
* @access public |
897
|
|
|
* @return int |
898
|
|
|
*/ |
899
|
|
|
public function get_total_plugin_update_count() { |
900
|
|
|
return count( $this->get_updates( 'plugin' ) ); |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
/** |
904
|
|
|
* Get total update count |
905
|
|
|
* |
906
|
|
|
* @since 1.8.12 |
907
|
|
|
* @access public |
908
|
|
|
* |
909
|
|
|
* @return int |
910
|
|
|
*/ |
911
|
|
|
public function get_total_update_count() { |
912
|
|
|
$db_update_count = $this->get_pending_db_update_count(); |
913
|
|
|
$plugin_update_count = $this->get_total_plugin_update_count(); |
914
|
|
|
|
915
|
|
|
return ( $db_update_count + $plugin_update_count ); |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
/** |
919
|
|
|
* Get total pending updates count |
920
|
|
|
* |
921
|
|
|
* @since 1.8.12 |
922
|
|
|
* @access public |
923
|
|
|
* |
924
|
|
|
* @return int |
925
|
|
|
*/ |
926
|
|
|
public function get_pending_db_update_count() { |
927
|
|
|
return count( $this->get_updates( 'database', 'new' ) ); |
928
|
|
|
} |
929
|
|
|
|
930
|
|
|
/** |
931
|
|
|
* Get total updates count |
932
|
|
|
* |
933
|
|
|
* @since 1.8.18 |
934
|
|
|
* @access public |
935
|
|
|
* |
936
|
|
|
* @return int |
937
|
|
|
*/ |
938
|
|
|
public function get_total_db_update_count() { |
939
|
|
|
return count( $this->get_updates( 'database', 'all' ) ); |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
/** |
943
|
|
|
* Get total new updates count |
944
|
|
|
* |
945
|
|
|
* @since 2.0 |
946
|
|
|
* @access public |
947
|
|
|
* |
948
|
|
|
* @return int |
949
|
|
|
*/ |
950
|
|
|
public function get_total_new_db_update_count() { |
951
|
|
|
return $this->is_doing_updates() ? |
952
|
|
|
get_option( 'give_db_update_count' ) : |
953
|
|
|
$this->get_pending_db_update_count(); |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
/** |
957
|
|
|
* Get total new updates count |
958
|
|
|
* |
959
|
|
|
* @since 2.0 |
960
|
|
|
* @access public |
961
|
|
|
* |
962
|
|
|
* @return int |
963
|
|
|
*/ |
964
|
|
|
public function get_running_db_update() { |
965
|
|
|
$current_update = get_option( 'give_doing_upgrade' ); |
966
|
|
|
|
967
|
|
|
return $this->is_doing_updates() ? |
968
|
|
|
$current_update['update'] : |
969
|
|
|
1; |
970
|
|
|
} |
971
|
|
|
|
972
|
|
|
/** |
973
|
|
|
* Get database update processing percentage. |
974
|
|
|
* |
975
|
|
|
* @since 2.0 |
976
|
|
|
* @access public |
977
|
|
|
* @return float|int |
978
|
|
|
*/ |
979
|
|
|
public function get_db_update_processing_percentage() { |
980
|
|
|
// Bailout. |
981
|
|
|
if ( ! $this->get_total_new_db_update_count() ) { |
982
|
|
|
return 0; |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
$resume_update = get_option( 'give_doing_upgrade' ); |
986
|
|
|
$update_count_percentages = ( ( $this->get_running_db_update() - 1 ) / $this->get_total_new_db_update_count() ) * 100; |
987
|
|
|
$update_percentage_share = ( 1 / $this->get_total_new_db_update_count() ) * 100; |
988
|
|
|
$upgrade_percentage = ( ( $resume_update['percentage'] * $update_percentage_share ) / 100 ); |
989
|
|
|
|
990
|
|
|
$final_percentage = $update_count_percentages + $upgrade_percentage; |
991
|
|
|
|
992
|
|
|
return $this->is_doing_updates() ? |
993
|
|
|
( absint( $final_percentage ) ? |
994
|
|
|
absint( $final_percentage ) : |
995
|
|
|
round( $final_percentage, 2 ) |
996
|
|
|
) : |
997
|
|
|
0; |
998
|
|
|
} |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
Give_Updates::get_instance()->setup(); |
1002
|
|
|
|