| 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 |
*/ |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WP_Async_Request', false ) ) { |
| 14 |
include_once( dirname( __FILE__ ) . '/libraries/wp-async-request.php' ); |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'WP_Background_Process', false ) ) { |
| 18 |
include_once( dirname( __FILE__ ) . '/libraries/wp-background-process.php' ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* UsersWP_Background_Updater Class. |
| 23 |
*/ |
| 24 |
class UsersWP_Background_Updater extends WP_Background_Process { |
| 25 |
|
| 26 |
/** |
| 27 |
* Initiate new background process. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
// Uses unique prefix per blog so each blog has separate queue. |
| 31 |
$this->prefix = 'wp_' . get_current_blog_id(); |
| 32 |
$this->action = 'uwp_updater'; |
| 33 |
|
| 34 |
parent::__construct(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Dispatch updater. |
| 39 |
* |
| 40 |
* Updater will still run via cron job if this fails for any reason. |
| 41 |
* |
| 42 |
* @since 2.0.0 |
| 43 |
*/ |
| 44 |
public function dispatch() { |
| 45 |
$dispatched = parent::dispatch(); |
| 46 |
|
| 47 |
if ( is_wp_error( $dispatched ) ) { |
| 48 |
uwp_error_log( sprintf( 'Unable to dispatch UsersWP updater: %s', $dispatched->get_error_message() ) ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Handle cron healthcheck |
| 54 |
* |
| 55 |
* Restart the background process if not already running |
| 56 |
* and data exists in the queue. |
| 57 |
* |
| 58 |
* @since 2.0.0 |
| 59 |
*/ |
| 60 |
public function handle_cron_healthcheck() { |
| 61 |
if ( $this->is_process_running() ) { |
| 62 |
// Background process already running. |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if ( $this->is_queue_empty() ) { |
| 67 |
// No data to process. |
| 68 |
$this->clear_scheduled_event(); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$this->handle(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Schedule fallback event. |
| 77 |
* |
| 78 |
* @since 2.0.0 |
| 79 |
*/ |
| 80 |
protected function schedule_event() { |
| 81 |
if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
| 82 |
wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Is the updater running? |
| 88 |
* |
| 89 |
* @since 2.0.0 |
| 90 |
* |
| 91 |
* @return boolean |
| 92 |
*/ |
| 93 |
public function is_updating() { |
| 94 |
return false === $this->is_queue_empty(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Task. |
| 99 |
* |
| 100 |
* Override this method to perform any actions required on each |
| 101 |
* queue item. Return the modified item for further processing |
| 102 |
* in the next pass through. Or, return false to remove the |
| 103 |
* item from the queue. |
| 104 |
* |
| 105 |
* @since 2.0.0 |
| 106 |
* |
| 107 |
* @param string $callback Update callback function. |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
protected function task( $callback ) { |
| 111 |
uwp_maybe_define( 'UWP_UPDATING', true ); |
| 112 |
|
| 113 |
if ( is_callable( $callback ) ) { |
| 114 |
uwp_error_log( sprintf( 'Running %s callback', $callback ) ); |
| 115 |
call_user_func( $callback ); |
| 116 |
uwp_error_log( sprintf( 'Finished %s callback', $callback ) ); |
| 117 |
} else { |
| 118 |
uwp_error_log( sprintf( 'Could not find %s callback', $callback ) ); |
| 119 |
} |
| 120 |
|
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Complete. |
| 126 |
* |
| 127 |
* Override if applicable, but ensure that the below actions are |
| 128 |
* performed, or, call parent::complete(). |
| 129 |
* |
| 130 |
* @since 2.0.0 |
| 131 |
*/ |
| 132 |
protected function complete() { |
| 133 |
uwp_error_log( 'UsersWP data update complete.' ); |
| 134 |
parent::complete(); |
| 135 |
} |
| 136 |
} |
| 137 |
|