| 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 |
// Load plugin updates. |
| 244 |
$this->__register_plugin_addon_updates(); |
| 245 |
|
| 246 |
// Bailout. |
| 247 |
if ( ! $this->get_total_update_count() ) { |
| 248 |
// Show complete update message if still on update setting page. |
| 249 |
if ( isset( $_GET['page'] ) && 'give-updates' === $_GET['page'] ) { |
| 250 |
// Upgrades |
| 251 |
add_submenu_page( |
| 252 |
'edit.php?post_type=give_forms', |
| 253 |
esc_html__( 'Give Updates Complete', 'give' ), |
| 254 |
__( 'Updates', 'give' ), |
| 255 |
'manage_give_settings', |
| 256 |
'give-updates', |
| 257 |
array( $this, 'render_complete_page' ) |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$is_update = ( $this->is_doing_updates() && ! self::$background_updater->is_paused_process() ); |
| 265 |
|
| 266 |
// Upgrades |
| 267 |
add_submenu_page( |
| 268 |
'edit.php?post_type=give_forms', |
| 269 |
esc_html__( 'Give Updates', 'give' ), |
| 270 |
sprintf( |
| 271 |
'%1$s <span class="update-plugins"%2$s><span class="plugin-count give-update-progress-count">%3$s%4$s</span></span>', |
| 272 |
__( 'Updates', 'give' ), |
| 273 |
isset( $_GET['give-pause-db-upgrades'] ) ? ' style="display:none;"' : '', |
| 274 |
$is_update ? |
| 275 |
$this->get_db_update_processing_percentage() : |
| 276 |
$this->get_total_update_count(), |
| 277 |
$is_update ? '%' : '' |
| 278 |
), |
| 279 |
'manage_give_settings', |
| 280 |
'give-updates', |
| 281 |
array( $this, 'render_page' ) |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Show update related notices |
| 288 |
* |
| 289 |
* @since 2.0 |
| 290 |
* @access public |
| 291 |
*/ |
| 292 |
public function __redirect_admin() { |
| 293 |
// Show db upgrade completed notice. |
| 294 |
if ( |
| 295 |
! wp_doing_ajax() && |
| 296 |
current_user_can( 'manage_give_settings' ) && |
| 297 |
get_option( 'give_show_db_upgrade_complete_notice' ) && |
| 298 |
! isset( $_GET['give-db-update-completed'] ) |
| 299 |
) { |
| 300 |
delete_option( 'give_show_db_upgrade_complete_notice' ); |
| 301 |
|
| 302 |
wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-updates&give-db-update-completed=give_db_upgrade_completed' ) ); |
| 303 |
exit(); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* Pause db upgrade |
| 310 |
* |
| 311 |
* @since 2.0.1 |
| 312 |
* @access public |
| 313 |
* |
| 314 |
* @param bool $force |
| 315 |
* |
| 316 |
* @return bool |
| 317 |
*/ |
| 318 |
public function __pause_db_update( $force = false ) { |
| 319 |
// Bailout. |
| 320 |
if ( |
| 321 |
! $force && |
| 322 |
( |
| 323 |
wp_doing_ajax() || |
| 324 |
! isset( $_GET['page'] ) || |
| 325 |
'give-updates' !== $_GET['page'] || |
| 326 |
! isset( $_GET['give-pause-db-upgrades'] ) || |
| 327 |
self::$background_updater->is_paused_process() |
| 328 |
) |
| 329 |
|
| 330 |
) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
delete_option( 'give_upgrade_error' ); |
| 335 |
|
| 336 |
$this->__health_background_update( $this ); |
| 337 |
$batch = self::$background_updater->get_all_batch(); |
| 338 |
|
| 339 |
// Bailout: if batch is empty |
| 340 |
if ( empty( $batch->data ) ) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
// Remove cache. |
| 345 |
Give_Background_Updater::flush_cache(); |
| 346 |
|
| 347 |
// Do not stop background process immediately if task running. |
| 348 |
// @see Give_Background_Updater::lock_process |
| 349 |
if ( ! $force && self::$background_updater->is_process_running() ) { |
| 350 |
update_option( 'give_pause_upgrade', 1, false ); |
| 351 |
|
| 352 |
return true; |
| 353 |
} |
| 354 |
|
| 355 |
update_option( 'give_paused_batches', $batch, false ); |
| 356 |
delete_option( $batch->key ); |
| 357 |
delete_site_transient( self::$background_updater->get_identifier() . '_process_lock' ); |
| 358 |
wp_clear_scheduled_hook( self::$background_updater->get_cron_identifier() ); |
| 359 |
|
| 360 |
Give()->logs->add( 'Update Pause', print_r( $batch, true ), 0, 'update' ); |
| 361 |
|
| 362 |
/** |
| 363 |
* Fire action when pause db updates |
| 364 |
* |
| 365 |
* @since 2.0.1 |
| 366 |
*/ |
| 367 |
do_action( 'give_pause_db_upgrade', $this ); |
| 368 |
|
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Restart db upgrade |
| 374 |
* |
| 375 |
* @since 2.0.1 |
| 376 |
* @access public |
| 377 |
* |
| 378 |
* @return bool |
| 379 |
*/ |
| 380 |
public function __restart_db_update() { |
| 381 |
// Bailout. |
| 382 |
if ( |
| 383 |
wp_doing_ajax() || |
| 384 |
! isset( $_GET['page'] ) || |
| 385 |
'give-updates' !== $_GET['page'] || |
| 386 |
! isset( $_GET['give-restart-db-upgrades'] ) || |
| 387 |
! self::$background_updater->is_paused_process() |
| 388 |
) { |
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
Give_Background_Updater::flush_cache(); |
| 393 |
$batch = get_option( 'give_paused_batches' ); |
| 394 |
|
| 395 |
if ( ! empty( $batch ) ) { |
| 396 |
wp_cache_delete( $batch->key, 'options' ); |
| 397 |
update_option( $batch->key, $batch->data, false ); |
| 398 |
|
| 399 |
delete_option( 'give_paused_batches' ); |
| 400 |
|
| 401 |
Give()->logs->add( 'Update Restart', print_r( $batch, true ), 0, 'update' ); |
| 402 |
|
| 403 |
|
| 404 |
/** Fire action when restart db updates |
| 405 |
* |
| 406 |
* @since 2.0.1 |
| 407 |
*/ |
| 408 |
do_action( 'give_restart_db_upgrade', $this ); |
| 409 |
|
| 410 |
self::$background_updater->dispatch(); |
| 411 |
} |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Health check for updates. |
| 418 |
* |
| 419 |
* @since 2.0 |
| 420 |
* @access public |
| 421 |
* |
| 422 |
* @param Give_Updates $give_updates |
| 423 |
*/ |
| 424 |
public function __health_background_update( $give_updates ) { |
| 425 |
if ( ! $this->is_doing_updates() ) { |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
Give_Background_Updater::flush_cache(); |
| 430 |
|
| 431 |
/* @var stdClass $batch */ |
| 432 |
$batch = Give_Updates::$background_updater->get_all_batch(); |
| 433 |
$old_batch_update_ids = is_array( $batch->data ) ? wp_list_pluck( $batch->data, 'id' ) : array(); |
| 434 |
$all_updates = $give_updates->get_updates( 'database', 'all' ); |
| 435 |
$all_update_ids = wp_list_pluck( $all_updates, 'id' ); |
| 436 |
$all_batch_update_ids = ! empty( $batch->data ) ? wp_list_pluck( $batch->data, 'id' ) : array(); |
| 437 |
$log_data = ''; |
| 438 |
$doing_upgrade_args = get_option( 'give_doing_upgrade' ); |
| 439 |
|
| 440 |
if ( ! empty( $doing_upgrade_args ) ) { |
| 441 |
$log_data .= 'Doing update:' . "\n"; |
| 442 |
$log_data .= print_r( $doing_upgrade_args, true ) . "\n"; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Add remove upgrade from batch |
| 447 |
*/ |
| 448 |
if ( ! empty( $batch->data ) ) { |
| 449 |
|
| 450 |
foreach ( $batch->data as $index => $update ) { |
| 451 |
$log_data = print_r( $update, true ) . "\n"; |
| 452 |
|
| 453 |
if ( ! is_callable( $update['callback'] ) ) { |
| 454 |
$log_data .= 'Removing missing callback update: ' . "{$update['id']}\n"; |
| 455 |
unset( $batch->data[ $index ] ); |
| 456 |
} elseif ( give_has_upgrade_completed( $update['id'] ) ) { |
| 457 |
$log_data .= 'Removing already completed update: ' . "{$update['id']}\n"; |
| 458 |
unset( $batch->data[ $index ] ); |
| 459 |
} |
| 460 |
|
| 461 |
if ( ! empty( $update['depend'] ) ) { |
| 462 |
|
| 463 |
foreach ( $update['depend'] as $depend ) { |
| 464 |
if ( give_has_upgrade_completed( $depend ) ) { |
| 465 |
$log_data .= 'Completed update: ' . "{$depend}\n"; |
| 466 |
continue; |
| 467 |
} |
| 468 |
|
| 469 |
if ( in_array( $depend, $all_update_ids ) && ! in_array( $depend, $all_batch_update_ids ) ) { |
| 470 |
$log_data .= 'Adding missing update: ' . "{$depend}\n"; |
| 471 |
array_unshift( $batch->data, $all_updates[ array_search( $depend, $all_update_ids ) ] ); |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Add new upgrade to batch |
| 480 |
*/ |
| 481 |
if ( $new_updates = $this->get_updates( 'database', 'new' ) ) { |
| 482 |
$all_batch_update_ids = ! empty( $batch->data ) ? wp_list_pluck( $batch->data, 'id' ) : array(); |
| 483 |
|
| 484 |
foreach ( $new_updates as $index => $new_update ) { |
| 485 |
if ( give_has_upgrade_completed( $new_update['id'] ) || in_array( $new_update['id'], $all_batch_update_ids ) ) { |
| 486 |
unset( $new_updates[ $index ] ); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if ( ! empty( $new_updates ) ) { |
| 491 |
$log_data .= 'Adding new update: ' . "\n"; |
| 492 |
$log_data .= print_r( $new_updates, true ) . "\n"; |
| 493 |
|
| 494 |
$batch->data = array_merge( (array) $batch->data, $new_updates ); |
| 495 |
update_option( 'give_db_update_count', ( absint( get_option( 'give_db_update_count' ) ) + count( $new_updates ) ), false ); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Fix batch |
| 501 |
*/ |
| 502 |
if ( empty( $batch->data ) ) { |
| 503 |
// Complete batch if do not have any data to process. |
| 504 |
self::$background_updater->delete( $batch->key ); |
| 505 |
|
| 506 |
if ( self::$background_updater->has_queue() ) { |
| 507 |
$this->__health_background_update( $this ); |
| 508 |
} else { |
| 509 |
delete_site_transient( self::$background_updater->get_identifier() . '_process_lock' ); |
| 510 |
wp_clear_scheduled_hook( self::$background_updater->get_cron_identifier() ); |
| 511 |
|
| 512 |
self::$background_updater->complete(); |
| 513 |
} |
| 514 |
|
| 515 |
} elseif ( array_diff( wp_list_pluck( $batch->data, 'id' ), $old_batch_update_ids ) ) { |
| 516 |
|
| 517 |
$log_data .= 'Updating batch' . "\n"; |
| 518 |
$log_data .= print_r( $batch, true ); |
| 519 |
|
| 520 |
if ( ! empty( $batch->key ) ) { |
| 521 |
wp_cache_delete( $batch->key, 'options' ); |
| 522 |
update_option( $batch->key, $batch->data, false ); |
| 523 |
} else { |
| 524 |
|
| 525 |
foreach ( $batch->data as $data ) { |
| 526 |
Give_Updates::$background_updater->push_to_queue( $data ); |
| 527 |
} |
| 528 |
|
| 529 |
Give_Updates::$background_updater->save(); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
/** |
| 535 |
* Fix give_doing_upgrade option |
| 536 |
*/ |
| 537 |
if( $fresh_new_db_count = $this->get_total_new_db_update_count( true ) ) { |
| 538 |
update_option( 'give_db_update_count', $fresh_new_db_count, false ); |
| 539 |
} |
| 540 |
|
| 541 |
$doing_upgrade_args['update'] = 1; |
| 542 |
$doing_upgrade_args['heading'] = sprintf( 'Update %s of %s', 1, $fresh_new_db_count ); |
| 543 |
$doing_upgrade_args['total_percentage'] = $this->get_db_update_processing_percentage( true ); |
| 544 |
|
| 545 |
// Remove already completed update from info. |
| 546 |
if ( |
| 547 |
empty( $doing_upgrade_args['update_info'] ) |
| 548 |
|| give_has_upgrade_completed( $doing_upgrade_args['update_info']['id'] ) |
| 549 |
) { |
| 550 |
$doing_upgrade_args['update_info'] = current( array_values( $batch->data ) ); |
| 551 |
$doing_upgrade_args['step'] = 1; |
| 552 |
} |
| 553 |
|
| 554 |
// Check if dependency completed or not. |
| 555 |
if ( isset( $doing_upgrade_args['update_info']['depend'] ) ) { |
| 556 |
foreach ( $doing_upgrade_args['update_info']['depend'] as $depend ) { |
| 557 |
if ( give_has_upgrade_completed( $depend ) ) { |
| 558 |
continue; |
| 559 |
} |
| 560 |
|
| 561 |
$doing_upgrade_args['update_info'] = $all_updates[ array_search( $depend, $all_update_ids ) ]; |
| 562 |
$doing_upgrade_args['step'] = 1; |
| 563 |
$doing_upgrade_args['percentage'] = 0; |
| 564 |
$doing_upgrade_args['total_percentage'] = 0; |
| 565 |
|
| 566 |
break; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if( ! empty( $doing_upgrade_args['update_info'] ) ) { |
| 571 |
update_option( 'give_doing_upgrade', $doing_upgrade_args, false ); |
| 572 |
|
| 573 |
$log_data .= 'Updated doing update:' . "\n"; |
| 574 |
$log_data .= print_r( $doing_upgrade_args, true ) . "\n"; |
| 575 |
} |
| 576 |
|
| 577 |
Give()->logs->add( 'Update Health Check', $log_data, 0, 'update' ); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
/** |
| 582 |
* Show update related notices |
| 583 |
* |
| 584 |
* @since 2.0 |
| 585 |
* @access public |
| 586 |
*/ |
| 587 |
public function __show_notice() { |
| 588 |
$current_screen = get_current_screen(); |
| 589 |
|
| 590 |
// Bailout. |
| 591 |
if ( ! current_user_can( 'manage_give_settings' ) ) { |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
// Run DB updates. |
| 596 |
if ( ! empty( $_GET['give-run-db-update'] ) ) { |
| 597 |
$this->run_db_update(); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
// Bailout. |
| 602 |
if ( in_array( $current_screen->base, array( 'give_forms_page_give-updates', 'update-core' ) ) ) { |
| 603 |
return; |
| 604 |
} |
| 605 |
|
| 606 |
// Show notice if upgrade paused. |
| 607 |
if ( self::$background_updater->is_paused_process() ) { |
| 608 |
ob_start(); |
| 609 |
|
| 610 |
$upgrade_error = get_option( 'give_upgrade_error' ); |
| 611 |
if ( ! $upgrade_error ) : ?> |
| 612 |
<strong><?php _e( 'Database Update', 'give' ); ?></strong> |
| 613 |
– <?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' ); ?> |
| 614 |
<br> |
| 615 |
<br> |
| 616 |
<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"> |
| 617 |
<?php _e( 'Restart the updater', 'give' ); ?> |
| 618 |
</a> |
| 619 |
<?php else: ?> |
| 620 |
<strong><?php _e( 'Database Update', 'give' ); ?></strong> |
| 621 |
– <?php _e( 'An unexpected issue occurred during the database update which caused it to stop automatically. Please contact support for assistance.', 'give' ); ?> |
| 622 |
<a href="<?php echo esc_url('http://docs.givewp.com/troubleshooting-db-updates')?>" target="_blank"><?php _e( 'Read More', 'give' ); ?> »</a> |
| 623 |
<?php |
| 624 |
endif; |
| 625 |
$desc_html = ob_get_clean(); |
| 626 |
|
| 627 |
Give()->notices->register_notice( array( |
| 628 |
'id' => 'give_upgrade_db', |
| 629 |
'type' => 'error', |
| 630 |
'dismissible' => false, |
| 631 |
'description' => $desc_html, |
| 632 |
) ); |
| 633 |
} |
| 634 |
|
| 635 |
// Bailout if doing upgrades. |
| 636 |
if ( $this->is_doing_updates() ) { |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
// Show db upgrade completed notice. |
| 641 |
if ( ! empty( $_GET['give-db-update-completed'] ) ) { |
| 642 |
Give()->notices->register_notice( array( |
| 643 |
'id' => 'give_db_upgrade_completed', |
| 644 |
'type' => 'updated', |
| 645 |
'description' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ), |
| 646 |
'show' => true, |
| 647 |
) ); |
| 648 |
|
| 649 |
// Start update. |
| 650 |
} elseif ( ! empty( $_GET['give-run-db-update'] ) ) { |
| 651 |
$this->run_db_update(); |
| 652 |
|
| 653 |
// Show run the update notice. |
| 654 |
} elseif ( $this->get_total_new_db_update_count() ) { |
| 655 |
ob_start(); |
| 656 |
?> |
| 657 |
<p> |
| 658 |
<strong><?php _e( 'Database Update', 'give' ); ?></strong> |
| 659 |
– <?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' ); ?> |
| 660 |
</p> |
| 661 |
<p class="submit"> |
| 662 |
<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"> |
| 663 |
<?php _e( 'Run the updater', 'give' ); ?> |
| 664 |
</a> |
| 665 |
</p> |
| 666 |
<?php |
| 667 |
$desc_html = ob_get_clean(); |
| 668 |
|
| 669 |
Give()->notices->register_notice( array( |
| 670 |
'id' => 'give_upgrade_db', |
| 671 |
'type' => 'updated', |
| 672 |
'dismissible' => false, |
| 673 |
'description' => $desc_html, |
| 674 |
) ); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Render Give Updates Completed page |
| 680 |
* |
| 681 |
* @since 1.8.12 |
| 682 |
* @access public |
| 683 |
*/ |
| 684 |
public function render_complete_page() { |
| 685 |
include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades-complete.php'; |
| 686 |
} |
| 687 |
|
| 688 |
/** |
| 689 |
* Render Give Updates page |
| 690 |
* |
| 691 |
* @since 1.8.12 |
| 692 |
* @access public |
| 693 |
*/ |
| 694 |
public function render_page() { |
| 695 |
include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades.php'; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Run database upgrades |
| 700 |
* |
| 701 |
* @since 2.0 |
| 702 |
* @access private |
| 703 |
*/ |
| 704 |
private function run_db_update() { |
| 705 |
// Bailout. |
| 706 |
if ( $this->is_doing_updates() || ! $this->get_total_new_db_update_count() ) { |
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
$updates = $this->get_updates( 'database', 'new' ); |
| 711 |
|
| 712 |
foreach ( $updates as $update ) { |
| 713 |
self::$background_updater->push_to_queue( $update ); |
| 714 |
} |
| 715 |
|
| 716 |
add_option( 'give_db_update_count', count( $updates ), '', false ); |
| 717 |
|
| 718 |
add_option( 'give_doing_upgrade', array( |
| 719 |
'update_info' => $updates[0], |
| 720 |
'step' => 1, |
| 721 |
'update' => 1, |
| 722 |
'heading' => sprintf( 'Update %s of %s', 1, count( $updates ) ), |
| 723 |
'percentage' => 0, |
| 724 |
'total_percentage' => 0, |
| 725 |
), '', false ); |
| 726 |
|
| 727 |
self::$background_updater->save()->dispatch(); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
/** |
| 732 |
* Delete resume updates |
| 733 |
* |
| 734 |
* @since 1.8.12 |
| 735 |
* @access public |
| 736 |
*/ |
| 737 |
public function __flush_resume_updates() { |
| 738 |
//delete_option( 'give_doing_upgrade' ); |
| 739 |
update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ), false ); |
| 740 |
|
| 741 |
// Reset counter. |
| 742 |
$this->step = $this->percentage = 0; |
| 743 |
|
| 744 |
$this->update = ( $this->get_total_db_update_count() > $this->update ) ? |
| 745 |
( $this->update + 1 ) : |
| 746 |
$this->update; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
/** |
| 751 |
* Initialize updates |
| 752 |
* |
| 753 |
* @since 2.0 |
| 754 |
* @access public |
| 755 |
* |
| 756 |
* @return void |
| 757 |
*/ |
| 758 |
public function __give_start_updating() { |
| 759 |
// Check permission. |
| 760 |
if ( |
| 761 |
! current_user_can( 'manage_give_settings' ) || |
| 762 |
$this->is_doing_updates() |
| 763 |
) { |
| 764 |
// Run update via ajax |
| 765 |
self::$background_updater->dispatch(); |
| 766 |
|
| 767 |
wp_send_json_error(); |
| 768 |
} |
| 769 |
|
| 770 |
// @todo: validate nonce |
| 771 |
// @todo: set http method to post |
| 772 |
if ( empty( $_POST['run_db_update'] ) ) { |
| 773 |
wp_send_json_error(); |
| 774 |
} |
| 775 |
|
| 776 |
$this->run_db_update(); |
| 777 |
|
| 778 |
wp_send_json_success(); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
/** |
| 783 |
* This function handle ajax query for dn update status. |
| 784 |
* |
| 785 |
* @since 2.0 |
| 786 |
* @access public |
| 787 |
* |
| 788 |
* @return string |
| 789 |
*/ |
| 790 |
public function __give_db_updates_info() { |
| 791 |
$update_info = get_option( 'give_doing_upgrade' ); |
| 792 |
$response_type = ''; |
| 793 |
|
| 794 |
if ( self::$background_updater->is_paused_process() ) { |
| 795 |
$update_info = array( |
| 796 |
'message' => __( 'The updates have been paused.', 'give' ), |
| 797 |
'heading' => '', |
| 798 |
'percentage' => 0, |
| 799 |
); |
| 800 |
|
| 801 |
if ( get_option( 'give_upgrade_error' ) ) { |
| 802 |
$update_info['message'] = __( 'An unexpected issue occurred during the database update which caused it to stop automatically. Please contact support for assistance.', 'give' ); |
| 803 |
} |
| 804 |
|
| 805 |
$response_type = 'error'; |
| 806 |
|
| 807 |
} elseif ( empty( $update_info ) || ! $this->get_total_new_db_update_count( true ) ) { |
| 808 |
$update_info = array( |
| 809 |
'message' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ), |
| 810 |
'heading' => __( 'Updates Completed.', 'give' ), |
| 811 |
'percentage' => 0, |
| 812 |
); |
| 813 |
$response_type = 'success'; |
| 814 |
|
| 815 |
delete_option( 'give_show_db_upgrade_complete_notice' ); |
| 816 |
} |
| 817 |
|
| 818 |
$this->send_ajax_response( $update_info, $response_type ); |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Send ajax response |
| 823 |
* |
| 824 |
* @since 1.8.12 |
| 825 |
* @access public |
| 826 |
* |
| 827 |
* @param $data |
| 828 |
* @param string $type |
| 829 |
*/ |
| 830 |
public function send_ajax_response( $data, $type = '' ) { |
| 831 |
$default = array( |
| 832 |
'message' => '', |
| 833 |
'heading' => '', |
| 834 |
'percentage' => 0, |
| 835 |
'step' => 0, |
| 836 |
'update' => 0, |
| 837 |
); |
| 838 |
|
| 839 |
// Set data. |
| 840 |
$data = wp_parse_args( $data, $default ); |
| 841 |
|
| 842 |
// Enable cache. |
| 843 |
Give_Cache::enable(); |
| 844 |
|
| 845 |
switch ( $type ) { |
| 846 |
case 'success': |
| 847 |
wp_send_json_success( $data ); |
| 848 |
break; |
| 849 |
|
| 850 |
case 'error': |
| 851 |
wp_send_json_error( $data ); |
| 852 |
break; |
| 853 |
|
| 854 |
default: |
| 855 |
wp_send_json( array( |
| 856 |
'data' => $data, |
| 857 |
) ); |
| 858 |
break; |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
/** |
| 863 |
* Set current update percentage. |
| 864 |
* |
| 865 |
* @since 1.8.12 |
| 866 |
* @access public |
| 867 |
* |
| 868 |
* @param $total |
| 869 |
* @param $current_total |
| 870 |
*/ |
| 871 |
public function set_percentage( $total, $current_total ) { |
| 872 |
// Set percentage. |
| 873 |
$this->percentage = $total ? ( ( $current_total ) / $total ) * 100 : 0; |
| 874 |
|
| 875 |
// Verify percentage. |
| 876 |
$this->percentage = ( 100 < $this->percentage ) ? 100 : $this->percentage; |
| 877 |
} |
| 878 |
|
| 879 |
/** |
| 880 |
* Check if parent update completed or not. |
| 881 |
* |
| 882 |
* @since 2.0 |
| 883 |
* @access private |
| 884 |
* |
| 885 |
* @param array $update |
| 886 |
* |
| 887 |
* @return bool|null |
| 888 |
*/ |
| 889 |
public function is_parent_updates_completed( $update ) { |
| 890 |
// Bailout. |
| 891 |
if ( empty( $update['depend'] ) ) { |
| 892 |
return true; |
| 893 |
} |
| 894 |
|
| 895 |
// Check if dependency is valid or not. |
| 896 |
if ( ! $this->has_valid_dependency( $update ) ) { |
| 897 |
return null; |
| 898 |
} |
| 899 |
|
| 900 |
$is_dependency_completed = true; |
| 901 |
|
| 902 |
foreach ( $update['depend'] as $depend ) { |
| 903 |
|
| 904 |
if ( ! give_has_upgrade_completed( $depend ) ) { |
| 905 |
$is_dependency_completed = false; |
| 906 |
break; |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
return $is_dependency_completed; |
| 911 |
} |
| 912 |
|
| 913 |
/** |
| 914 |
* Flag to check if DB updates running or not. |
| 915 |
* |
| 916 |
* @since 2.0 |
| 917 |
* @access public |
| 918 |
* @return bool |
| 919 |
*/ |
| 920 |
public function is_doing_updates() { |
| 921 |
return (bool) get_option( 'give_doing_upgrade' ); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
/** |
| 926 |
* Check if update has valid dependency or not. |
| 927 |
* |
| 928 |
* @since 2.0 |
| 929 |
* @access public |
| 930 |
* |
| 931 |
* @param $update |
| 932 |
* |
| 933 |
* @return bool |
| 934 |
*/ |
| 935 |
public function has_valid_dependency( $update ) { |
| 936 |
$is_valid_dependency = true; |
| 937 |
// $update_ids = wp_list_pluck( $this->get_updates( 'database', 'all' ), 'id' ); |
| 938 |
// |
| 939 |
// foreach ( $update['depend'] as $depend ) { |
| 940 |
// // Check if dependency is valid or not. |
| 941 |
// if ( ! in_array( $depend, $update_ids ) ) { |
| 942 |
// $is_valid_dependency = false; |
| 943 |
// break; |
| 944 |
// } |
| 945 |
// } |
| 946 |
|
| 947 |
return $is_valid_dependency; |
| 948 |
} |
| 949 |
|
| 950 |
/** |
| 951 |
* Get updates. |
| 952 |
* |
| 953 |
* @since 1.8.12 |
| 954 |
* @access public |
| 955 |
* |
| 956 |
* @param string $update_type Tye of update. |
| 957 |
* @param string $status Tye of update. |
| 958 |
* |
| 959 |
* @return array |
| 960 |
*/ |
| 961 |
public function get_updates( $update_type = '', $status = 'all' ) { |
| 962 |
// return all updates. |
| 963 |
if ( empty( $update_type ) ) { |
| 964 |
return $this->updates; |
| 965 |
} |
| 966 |
|
| 967 |
// Get specific update. |
| 968 |
$updates = ! empty( $this->updates[ $update_type ] ) ? $this->updates[ $update_type ] : array(); |
| 969 |
|
| 970 |
// Bailout. |
| 971 |
if ( empty( $updates ) ) { |
| 972 |
return $updates; |
| 973 |
} |
| 974 |
|
| 975 |
switch ( $status ) { |
| 976 |
case 'new': |
| 977 |
// Remove already completed updates. |
| 978 |
wp_cache_delete( 'give_completed_upgrades', 'options' ); |
| 979 |
$completed_updates = give_get_completed_upgrades(); |
| 980 |
|
| 981 |
if ( ! empty( $completed_updates ) ) { |
| 982 |
foreach ( $updates as $index => $update ) { |
| 983 |
if ( in_array( $update['id'], $completed_updates ) ) { |
| 984 |
unset( $updates[ $index ] ); |
| 985 |
} |
| 986 |
} |
| 987 |
$updates = array_values( $updates ); |
| 988 |
} |
| 989 |
|
| 990 |
break; |
| 991 |
} |
| 992 |
|
| 993 |
return $updates; |
| 994 |
} |
| 995 |
|
| 996 |
/** |
| 997 |
* Get addon update count. |
| 998 |
* |
| 999 |
* @since 1.8.12 |
| 1000 |
* @access public |
| 1001 |
* @return int |
| 1002 |
*/ |
| 1003 |
public function get_total_plugin_update_count() { |
| 1004 |
return count( $this->get_updates( 'plugin' ) ); |
| 1005 |
} |
| 1006 |
|
| 1007 |
/** |
| 1008 |
* Get total update count |
| 1009 |
* |
| 1010 |
* @since 1.8.12 |
| 1011 |
* @access public |
| 1012 |
* |
| 1013 |
* @return int |
| 1014 |
*/ |
| 1015 |
public function get_total_update_count() { |
| 1016 |
$db_update_count = $this->get_pending_db_update_count(); |
| 1017 |
$plugin_update_count = $this->get_total_plugin_update_count(); |
| 1018 |
|
| 1019 |
return ( $db_update_count + $plugin_update_count ); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/** |
| 1023 |
* Get total pending updates count |
| 1024 |
* |
| 1025 |
* @since 1.8.12 |
| 1026 |
* @access public |
| 1027 |
* |
| 1028 |
* @return int |
| 1029 |
*/ |
| 1030 |
public function get_pending_db_update_count() { |
| 1031 |
return count( $this->get_updates( 'database', 'new' ) ); |
| 1032 |
} |
| 1033 |
|
| 1034 |
/** |
| 1035 |
* Get total updates count |
| 1036 |
* |
| 1037 |
* @since 1.8.18 |
| 1038 |
* @access public |
| 1039 |
* |
| 1040 |
* @return int |
| 1041 |
*/ |
| 1042 |
public function get_total_db_update_count() { |
| 1043 |
return count( $this->get_updates( 'database', 'all' ) ); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* Get total new updates count |
| 1048 |
* |
| 1049 |
* @since 2.0 |
| 1050 |
* @access public |
| 1051 |
* |
| 1052 |
* @param bool $refresh |
| 1053 |
* |
| 1054 |
* @return int |
| 1055 |
*/ |
| 1056 |
public function get_total_new_db_update_count( $refresh = false ) { |
| 1057 |
$update_count = $this->is_doing_updates() && ! $refresh ? |
| 1058 |
get_option( 'give_db_update_count' ) : |
| 1059 |
$this->get_pending_db_update_count(); |
| 1060 |
|
| 1061 |
return $update_count; |
| 1062 |
} |
| 1063 |
|
| 1064 |
/** |
| 1065 |
* Get total new updates count |
| 1066 |
* |
| 1067 |
* @since 2.0 |
| 1068 |
* @access public |
| 1069 |
* |
| 1070 |
* @param bool $refresh |
| 1071 |
* |
| 1072 |
* @return int |
| 1073 |
*/ |
| 1074 |
public function get_running_db_update( $refresh = false ) { |
| 1075 |
$current_update = 1; |
| 1076 |
|
| 1077 |
if ( $this->is_doing_updates() && ! $refresh ) { |
| 1078 |
$current_update = get_option( 'give_doing_upgrade' ); |
| 1079 |
$current_update = $current_update['update']; |
| 1080 |
} |
| 1081 |
|
| 1082 |
return $current_update; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/** |
| 1086 |
* Get database update processing percentage. |
| 1087 |
* |
| 1088 |
* @since 2.0 |
| 1089 |
* @access public |
| 1090 |
* |
| 1091 |
* @param bool $refresh |
| 1092 |
* |
| 1093 |
* @return float|int |
| 1094 |
*/ |
| 1095 |
public function get_db_update_processing_percentage( $refresh = false ) { |
| 1096 |
// Bailout. |
| 1097 |
if ( ! $this->get_total_new_db_update_count( $refresh ) ) { |
| 1098 |
return 0; |
| 1099 |
} |
| 1100 |
|
| 1101 |
$resume_update = get_option( 'give_doing_upgrade' ); |
| 1102 |
$update_count_percentages = ( ( $this->get_running_db_update( $refresh ) - 1 ) / $this->get_total_new_db_update_count( $refresh ) ) * 100; |
| 1103 |
$update_percentage_share = ( 1 / $this->get_total_new_db_update_count() ) * 100; |
| 1104 |
$upgrade_percentage = ( ( $resume_update['percentage'] * $update_percentage_share ) / 100 ); |
| 1105 |
|
| 1106 |
$final_percentage = $update_count_percentages + $upgrade_percentage; |
| 1107 |
|
| 1108 |
return $this->is_doing_updates() ? |
| 1109 |
( absint( $final_percentage ) ? |
| 1110 |
absint( $final_percentage ) : |
| 1111 |
round( $final_percentage, 2 ) |
| 1112 |
) : |
| 1113 |
0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
/** |
| 1118 |
* Get all update ids. |
| 1119 |
* |
| 1120 |
* @since 2.0.3 |
| 1121 |
* |
| 1122 |
* @return array |
| 1123 |
*/ |
| 1124 |
public function get_update_ids() { |
| 1125 |
$all_updates = $this->get_updates( 'database', 'all' ); |
| 1126 |
$all_update_ids = wp_list_pluck( $all_updates, 'id' ); |
| 1127 |
|
| 1128 |
return $all_update_ids; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/** |
| 1132 |
* Get offset count |
| 1133 |
* |
| 1134 |
* @since 2.0.5 |
| 1135 |
* @access public |
| 1136 |
* |
| 1137 |
* @param int $process_item_count |
| 1138 |
* |
| 1139 |
* @return float|int |
| 1140 |
*/ |
| 1141 |
public function get_offset( $process_item_count ) { |
| 1142 |
return ( 1 === $this->step ) ? |
| 1143 |
0 : |
| 1144 |
( $this->step - 1 ) * $process_item_count; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
Give_Updates::get_instance()->setup(); |
| 1149 |
|