| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Garbage collector class for supportcandy |
| 8 |
*/ |
| 9 |
if ( ! class_exists( 'WPSC_Task_Scheduler' ) ) : |
| 10 |
|
| 11 |
final class WPSC_Task_Scheduler { |
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize this class |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
|
| 20 |
// schedule cron jobs. |
| 21 |
add_action( 'init', array( __CLASS__, 'schedule' ) ); |
| 22 |
add_action( 'wpsc_execute_scheduled_task', array( __CLASS__, 'execute_scheduled_task' ) ); |
| 23 |
|
| 24 |
// manual runner. |
| 25 |
add_action( 'admin_notices', array( __CLASS__, 'show_manual_notice' ) ); |
| 26 |
add_action( 'wp_ajax_wpsc_initialize_scheduled_task', array( __CLASS__, 'initialize_scheduled_task' ) ); |
| 27 |
add_action( 'wp_ajax_wpsc_run_manual_task', array( __CLASS__, 'run_manual_task' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Schedule tasks. For example, change all ticket records after upgrade, change all customer records, etc. |
| 32 |
* It will shedule tasks by FIFO method only one task at a time. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function schedule() { |
| 37 |
|
| 38 |
$task = self::get_scheduled_task(); |
| 39 |
if ( false === $task ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if ( 'yes' === get_transient( 'wpsc_executing_manual_task' ) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// schedule first task. |
| 48 |
if ( ! wp_next_scheduled( 'wpsc_execute_scheduled_task' ) ) { |
| 49 |
wp_schedule_single_event( time(), 'wpsc_execute_scheduled_task', array( $task ) ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Manual runner admin notice |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function show_manual_notice() { |
| 59 |
|
| 60 |
// return if current user does not have administrator previlages. |
| 61 |
$current_user = WPSC_Current_User::$current_user; |
| 62 |
if ( ! ( ! $current_user->is_guest && $current_user->user->has_cap( 'manage_options' ) ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// return if there is no task scheduled or it is not for manual. |
| 67 |
$task = self::get_scheduled_task(); |
| 68 |
if ( false === $task || 0 == $task->is_manual ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// do not show notice on scheduler page. |
| 73 |
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wpsc-task-manager' ) { // phpcs:ignore |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// show notice. |
| 78 |
$nonce = wp_create_nonce( 'wpsc_manual_task_manager_' . $task->id ); |
| 79 |
?> |
| 80 |
<div class="supportcandy scheduled-task notice notice-warning"> |
| 81 |
<p><?php echo esc_attr( $task->warning_text ); ?></p> |
| 82 |
<p> |
| 83 |
<a href="<?php echo esc_url_raw( admin_url( 'admin.php?page=wpsc-task-manager&task=' . $task->id . '&_wpnonce=' . $nonce ) ); ?>"> |
| 84 |
<?php echo esc_attr( $task->warning_link_text ); ?> |
| 85 |
</a> |
| 86 |
</p> |
| 87 |
</div> |
| 88 |
<?php |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Return scheduled task if any otherwise return false |
| 93 |
* |
| 94 |
* @return mixed |
| 95 |
*/ |
| 96 |
public static function get_scheduled_task() { |
| 97 |
|
| 98 |
$tasks = WPSC_Scheduled_Task::find( |
| 99 |
array( |
| 100 |
'items_per_page' => 1, |
| 101 |
) |
| 102 |
); |
| 103 |
|
| 104 |
return $tasks['total_items'] > 0 ? $tasks['results'][0] : false; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Execute current task |
| 109 |
* |
| 110 |
* @param WPSC_Scheduled_Task $task - task object. |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public static function execute_scheduled_task( $task ) { |
| 114 |
|
| 115 |
if ( ! class_exists( $task->class ) || ! method_exists( $task->class, $task->method ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if ( 'yes' === get_transient( 'wpsc_executing_scheduled_task' ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
set_transient( 'wpsc_executing_scheduled_task', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 124 |
|
| 125 |
call_user_func( array( $task->class, $task->method ), $task ); |
| 126 |
|
| 127 |
delete_transient( 'wpsc_executing_scheduled_task' ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Perform manual scheduled task |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public static function perform_manual_scheduler() { |
| 136 |
|
| 137 |
$task_id = isset( $_REQUEST['task'] ) ? intval( $_REQUEST['task'] ) : 0; |
| 138 |
check_admin_referer( 'wpsc_manual_task_manager_' . $task_id ); |
| 139 |
|
| 140 |
?> |
| 141 |
<div class="wrap"> |
| 142 |
<hr class="wp-header-end"> |
| 143 |
<div id="wpsc-container"> |
| 144 |
<div class="wpsc-settings-page"> |
| 145 |
<div class="wpsc-setting-body"> |
| 146 |
<div class="wpsc-setting-section-body"> |
| 147 |
<div class="wpsc-pg-container"> |
| 148 |
<i class="wpsc-pg-title"><?php esc_attr_e( 'Initializing...', 'supportcandy' ); ?></i> |
| 149 |
<div class="wpsc-pg"> |
| 150 |
<div class="wpsc-pg-label">0%</div> |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
<script> |
| 154 |
var progressbar = jQuery( ".wpsc-pg" ); |
| 155 |
var progressLabel = jQuery( ".wpsc-pg-label" ); |
| 156 |
var progressbarTitle = jQuery( ".wpsc-pg-title" ); |
| 157 |
var settingURL = '<?php echo esc_url_raw( admin_url( 'admin.php?page=wpsc-settings§ion=general-settings&tab=general' ) ); ?>'; |
| 158 |
jQuery(document).ready( async function(){ |
| 159 |
progressbar.progressbar({ value: 0 }); |
| 160 |
supportcandy.temp.wpsc_initialize_scheduled_task = '<?php echo esc_attr( wp_create_nonce( 'wpsc_initialize_scheduled_task' ) ); ?>'; |
| 161 |
wpsc_initialize_task(); |
| 162 |
}); |
| 163 |
function wpsc_initialize_task() { |
| 164 |
var data = { |
| 165 |
action: 'wpsc_initialize_scheduled_task', |
| 166 |
task_id: <?php echo intval( $task_id ); ?>, |
| 167 |
_ajax_nonce: supportcandy.temp.wpsc_initialize_scheduled_task |
| 168 |
}; |
| 169 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 170 |
supportcandy.temp.wpsc_initialize_scheduled_task = response.nonce; |
| 171 |
if ( response.status == 'busy' ) { |
| 172 |
setTimeout(() => { |
| 173 |
wpsc_initialize_task(); |
| 174 |
}, 2000); |
| 175 |
} else if ( response.status == 'unavailable' ) { |
| 176 |
window.location.href = settingURL; |
| 177 |
} else { |
| 178 |
supportcandy.temp.wpsc_run_manual_task = response.nonce; |
| 179 |
progressbarTitle.text( response.pbText ); |
| 180 |
wpsc_runner( response.pages ); |
| 181 |
} |
| 182 |
}); |
| 183 |
} |
| 184 |
async function wpsc_runner( pages ) { |
| 185 |
pages = parseInt( pages ) + 1; |
| 186 |
for ( page=1; page<=pages; page++ ) { |
| 187 |
var response = await wpsc_run_manual_task(); |
| 188 |
if ( response !== false ) { |
| 189 |
supportcandy.temp.wpsc_run_manual_task = response.nonce; |
| 190 |
let percentage = Math.round((page/pages)*100); |
| 191 |
if ( response.status == 'unavailable' ) { |
| 192 |
let percentage = 100; |
| 193 |
progressLabel.text( percentage + '%' ); |
| 194 |
progressbar.progressbar({ value: percentage }); |
| 195 |
break; |
| 196 |
} else { |
| 197 |
progressLabel.text( percentage + '%' ); |
| 198 |
progressbar.progressbar({ value: percentage }); |
| 199 |
} |
| 200 |
} else { |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
setTimeout(() => { |
| 205 |
window.location.href = settingURL; |
| 206 |
}, 1000); |
| 207 |
} |
| 208 |
function wpsc_run_manual_task() { |
| 209 |
var data = { |
| 210 |
action: 'wpsc_run_manual_task', |
| 211 |
task_id: <?php echo intval( $task_id ); ?>, |
| 212 |
_ajax_nonce: supportcandy.temp.wpsc_run_manual_task |
| 213 |
}; |
| 214 |
return new Promise( resolve => { |
| 215 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 216 |
resolve( response ); |
| 217 |
}).fail(function(){ |
| 218 |
resolve(false); |
| 219 |
}); |
| 220 |
}); |
| 221 |
} |
| 222 |
</script> |
| 223 |
</div> |
| 224 |
</div> |
| 225 |
</div> |
| 226 |
</div> |
| 227 |
</div> |
| 228 |
<?php |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Initialize sceduled task callback |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public static function initialize_scheduled_task() { |
| 237 |
|
| 238 |
if ( check_ajax_referer( 'wpsc_initialize_scheduled_task', '_ajax_nonce', false ) != 1 ) { |
| 239 |
wp_send_json_error( 'Unauthorized request!', 400 ); |
| 240 |
} |
| 241 |
|
| 242 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 243 |
wp_send_json_error( 'Unauthorized access!', 401 ); |
| 244 |
} |
| 245 |
|
| 246 |
$id = isset( $_POST['task_id'] ) ? intval( $_POST['task_id'] ) : 0; |
| 247 |
if ( ! $id ) { |
| 248 |
wp_send_json_error( 'Incorrect request!', 400 ); |
| 249 |
} |
| 250 |
|
| 251 |
// return if there is no task scheduled or it is not for manual or task does not match. |
| 252 |
$task = self::get_scheduled_task(); |
| 253 |
if ( false === $task || 0 == $task->is_manual || $id != $task->id ) { |
| 254 |
wp_send_json( |
| 255 |
array( |
| 256 |
'nonce' => '', |
| 257 |
'status' => 'unavailable', |
| 258 |
), |
| 259 |
200 |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
// check whether it is already running by background process. |
| 264 |
if ( 'yes' === get_transient( 'wpsc_executing_scheduled_task' ) ) { |
| 265 |
wp_send_json( |
| 266 |
array( |
| 267 |
'nonce' => wp_create_nonce( 'wpsc_initialize_scheduled_task' ), |
| 268 |
'status' => 'busy', |
| 269 |
), |
| 270 |
200 |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
set_transient( 'wpsc_executing_manual_task', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 275 |
|
| 276 |
// get page count if count is 0. |
| 277 |
if ( $task->pages == 0 ) { |
| 278 |
do_action( 'wpsc_execute_scheduled_task', $task ); |
| 279 |
$task = new WPSC_Scheduled_Task( $id ); |
| 280 |
if ( ! $task->id ) { |
| 281 |
delete_transient( 'wpsc_executing_manual_task' ); |
| 282 |
wp_send_json( |
| 283 |
array( |
| 284 |
'nonce' => '', |
| 285 |
'status' => 'unavailable', |
| 286 |
), |
| 287 |
200 |
| 288 |
); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
// return pages. |
| 293 |
wp_send_json( |
| 294 |
array( |
| 295 |
'nonce' => wp_create_nonce( 'wpsc_run_manual_task' ), |
| 296 |
'status' => 'success', |
| 297 |
'pages' => $task->pages, |
| 298 |
'pbText' => $task->progressbar_text, |
| 299 |
), |
| 300 |
200 |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Run manual task callback |
| 306 |
* |
| 307 |
* @return void |
| 308 |
*/ |
| 309 |
public static function run_manual_task() { |
| 310 |
|
| 311 |
if ( check_ajax_referer( 'wpsc_run_manual_task', '_ajax_nonce', false ) != 1 ) { |
| 312 |
wp_send_json_error( 'Unauthorized request!', 400 ); |
| 313 |
} |
| 314 |
|
| 315 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 316 |
wp_send_json_error( 'Unauthorized access!', 401 ); |
| 317 |
} |
| 318 |
|
| 319 |
$id = isset( $_POST['task_id'] ) ? intval( $_POST['task_id'] ) : 0; |
| 320 |
if ( ! $id ) { |
| 321 |
wp_send_json_error( 'Incorrect request!', 400 ); |
| 322 |
} |
| 323 |
|
| 324 |
// return if there is no task scheduled or it is not for manual or task does not match. |
| 325 |
$task = self::get_scheduled_task(); |
| 326 |
if ( false === $task || 0 == $task->is_manual || $id != $task->id ) { |
| 327 |
delete_transient( 'wpsc_executing_manual_task' ); |
| 328 |
wp_send_json( |
| 329 |
array( |
| 330 |
'nonce' => '', |
| 331 |
'status' => 'unavailable', |
| 332 |
), |
| 333 |
200 |
| 334 |
); |
| 335 |
} |
| 336 |
|
| 337 |
// execute task. |
| 338 |
do_action( 'wpsc_execute_scheduled_task', $task ); |
| 339 |
|
| 340 |
// check whether it is finished. |
| 341 |
$task = new WPSC_Scheduled_Task( $id ); |
| 342 |
if ( ! $task->id ) { |
| 343 |
delete_transient( 'wpsc_executing_manual_task' ); |
| 344 |
wp_send_json( |
| 345 |
array( |
| 346 |
'nonce' => '', |
| 347 |
'status' => 'unavailable', |
| 348 |
), |
| 349 |
200 |
| 350 |
); |
| 351 |
} |
| 352 |
|
| 353 |
// return success. |
| 354 |
wp_send_json( |
| 355 |
array( |
| 356 |
'nonce' => wp_create_nonce( 'wpsc_run_manual_task' ), |
| 357 |
'status' => 'success', |
| 358 |
), |
| 359 |
200 |
| 360 |
); |
| 361 |
} |
| 362 |
} |
| 363 |
endif; |
| 364 |
|
| 365 |
WPSC_Task_Scheduler::init(); |
| 366 |
|