2016-05-11 11:44:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-06-06 17:20:10 +00:00
|
|
|
* Background Updater
|
2016-05-11 11:44:00 +00:00
|
|
|
*
|
2018-03-15 21:14:36 +00:00
|
|
|
* @version 2.6.0
|
|
|
|
* @package WooCommerce/Classes
|
2016-05-11 11:44:00 +00:00
|
|
|
*/
|
2018-02-01 13:42:34 +00:00
|
|
|
|
2018-03-15 21:14:36 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2016-05-11 11:44:00 +00:00
|
|
|
|
2018-02-01 13:42:34 +00:00
|
|
|
if ( ! class_exists( 'WC_Background_Process', false ) ) {
|
|
|
|
include_once dirname( __FILE__ ) . '/abstracts/class-wc-background-process.php';
|
2017-02-16 11:46:01 +00:00
|
|
|
}
|
2016-05-11 11:44:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Background_Updater Class.
|
|
|
|
*/
|
2018-02-01 13:42:34 +00:00
|
|
|
class WC_Background_Updater extends WC_Background_Process {
|
2016-05-11 11:44:00 +00:00
|
|
|
|
|
|
|
/**
|
2017-12-08 16:53:51 +00:00
|
|
|
* Initiate new background process.
|
2016-05-11 11:44:00 +00:00
|
|
|
*/
|
2017-12-08 16:53:51 +00:00
|
|
|
public function __construct() {
|
|
|
|
// Uses unique prefix per blog so each blog has separate queue.
|
|
|
|
$this->prefix = 'wp_' . get_current_blog_id();
|
|
|
|
$this->action = 'wc_updater';
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2016-05-11 11:44:00 +00:00
|
|
|
|
2016-06-02 22:41:19 +00:00
|
|
|
/**
|
|
|
|
* Dispatch updater.
|
|
|
|
*
|
|
|
|
* Updater will still run via cron job if this fails for any reason.
|
2016-05-11 13:22:31 +00:00
|
|
|
*/
|
|
|
|
public function dispatch() {
|
2016-06-02 22:41:19 +00:00
|
|
|
$dispatched = parent::dispatch();
|
2016-08-08 12:59:23 +00:00
|
|
|
$logger = wc_get_logger();
|
2016-06-02 22:41:19 +00:00
|
|
|
|
|
|
|
if ( is_wp_error( $dispatched ) ) {
|
2016-11-22 18:52:16 +00:00
|
|
|
$logger->error(
|
|
|
|
sprintf( 'Unable to dispatch WooCommerce updater: %s', $dispatched->get_error_message() ),
|
2016-12-21 19:15:19 +00:00
|
|
|
array( 'source' => 'wc_db_updates' )
|
2016-11-22 18:52:16 +00:00
|
|
|
);
|
2016-06-02 22:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 10:07:12 +00:00
|
|
|
/**
|
|
|
|
* Handle cron healthcheck
|
|
|
|
*
|
|
|
|
* Restart the background process if not already running
|
|
|
|
* and data exists in the queue.
|
|
|
|
*/
|
|
|
|
public function handle_cron_healthcheck() {
|
|
|
|
if ( $this->is_process_running() ) {
|
|
|
|
// Background process already running.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->is_queue_empty() ) {
|
|
|
|
// No data to process.
|
|
|
|
$this->clear_scheduled_event();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->handle();
|
|
|
|
}
|
|
|
|
|
2016-06-09 10:48:57 +00:00
|
|
|
/**
|
2016-06-15 09:50:43 +00:00
|
|
|
* Schedule fallback event.
|
2016-06-09 10:48:57 +00:00
|
|
|
*/
|
|
|
|
protected function schedule_event() {
|
|
|
|
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) {
|
2016-06-16 14:28:53 +00:00
|
|
|
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
|
2016-06-09 10:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:44:16 +00:00
|
|
|
/**
|
|
|
|
* Is the updater running?
|
2018-02-01 13:42:34 +00:00
|
|
|
*
|
2016-06-13 11:44:16 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_updating() {
|
|
|
|
return false === $this->is_queue_empty();
|
|
|
|
}
|
|
|
|
|
2016-05-11 11:44:00 +00:00
|
|
|
/**
|
|
|
|
* Task
|
|
|
|
*
|
|
|
|
* Override this method to perform any actions required on each
|
|
|
|
* queue item. Return the modified item for further processing
|
|
|
|
* in the next pass through. Or, return false to remove the
|
|
|
|
* item from the queue.
|
|
|
|
*
|
2018-02-01 13:42:34 +00:00
|
|
|
* @param string $callback Update callback function.
|
2018-05-25 15:36:07 +00:00
|
|
|
* @return string|bool
|
2016-05-11 11:44:00 +00:00
|
|
|
*/
|
|
|
|
protected function task( $callback ) {
|
2017-09-05 19:52:39 +00:00
|
|
|
wc_maybe_define_constant( 'WC_UPDATING', true );
|
2016-05-11 11:44:00 +00:00
|
|
|
|
2016-08-08 12:59:23 +00:00
|
|
|
$logger = wc_get_logger();
|
2016-05-11 11:44:00 +00:00
|
|
|
|
2018-02-01 13:42:34 +00:00
|
|
|
include_once dirname( __FILE__ ) . '/wc-update-functions.php';
|
2016-05-11 11:44:00 +00:00
|
|
|
|
2018-05-25 13:18:19 +00:00
|
|
|
$result = false;
|
|
|
|
|
2016-05-11 11:44:00 +00:00
|
|
|
if ( is_callable( $callback ) ) {
|
2016-12-21 19:15:19 +00:00
|
|
|
$logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
|
2018-07-16 17:24:42 +00:00
|
|
|
$result = (bool) call_user_func( $callback, $this );
|
2018-04-16 12:40:38 +00:00
|
|
|
|
2018-05-25 13:18:19 +00:00
|
|
|
if ( $result ) {
|
|
|
|
$logger->info( sprintf( '%s callback needs to run again', $callback ), array( 'source' => 'wc_db_updates' ) );
|
2018-04-16 12:40:38 +00:00
|
|
|
} else {
|
2018-05-25 13:18:19 +00:00
|
|
|
$logger->info( sprintf( 'Finished running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
|
2018-04-16 12:40:38 +00:00
|
|
|
}
|
2018-06-11 13:48:28 +00:00
|
|
|
} else {
|
|
|
|
$logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
|
2016-05-11 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 15:36:07 +00:00
|
|
|
return $result ? $callback : false;
|
2016-05-11 11:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complete
|
|
|
|
*
|
|
|
|
* Override if applicable, but ensure that the below actions are
|
|
|
|
* performed, or, call parent::complete().
|
|
|
|
*/
|
|
|
|
protected function complete() {
|
2016-08-08 12:59:23 +00:00
|
|
|
$logger = wc_get_logger();
|
2016-12-21 19:15:19 +00:00
|
|
|
$logger->info( 'Data update complete', array( 'source' => 'wc_db_updates' ) );
|
2016-05-11 11:44:00 +00:00
|
|
|
WC_Install::update_db_version();
|
|
|
|
parent::complete();
|
|
|
|
}
|
2018-04-16 12:40:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See if the batch limit has been exceeded.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-17 18:16:05 +00:00
|
|
|
public function is_memory_exceeded() {
|
|
|
|
return $this->memory_exceeded();
|
2018-04-16 12:40:38 +00:00
|
|
|
}
|
2016-05-11 11:44:00 +00:00
|
|
|
}
|