| 1 |
<?php |
| 2 |
/** |
| 3 |
* Background task |
| 4 |
* |
| 5 |
* @package WPFunnels\Batch |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPFunnels\Batch; |
| 9 |
|
| 10 |
use WP_Background_Process; |
| 11 |
|
| 12 |
/** |
| 13 |
* Background task |
| 14 |
* |
| 15 |
* @abstract |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
abstract class Wpfnl_Background_Task extends WP_Background_Process { |
| 20 |
|
| 21 |
/** |
| 22 |
* Current_item |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $current_item; |
| 27 |
|
| 28 |
/** |
| 29 |
* Provider |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $provider; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* See if the batch limit has been exceeded. |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
protected function batch_limit_exceeded() { |
| 43 |
return $this->time_exceeded() || $this->memory_exceeded(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Handle. |
| 48 |
* |
| 49 |
* Pass each queue item to the task handler, while remaining |
| 50 |
* within server memory and time limit constraints. |
| 51 |
*/ |
| 52 |
protected function handle() { |
| 53 |
$this->lock_process(); |
| 54 |
do { |
| 55 |
$batch = $this->get_batch(); |
| 56 |
|
| 57 |
foreach ( $batch->data as $key => $value ) { |
| 58 |
$task = $this->task( $value ); |
| 59 |
|
| 60 |
if ( false !== $task ) { |
| 61 |
$batch->data[ $key ] = $task; |
| 62 |
} else { |
| 63 |
unset( $batch->data[ $key ] ); |
| 64 |
} |
| 65 |
|
| 66 |
if ( $this->batch_limit_exceeded() ) { |
| 67 |
// Batch limits reached. |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Update or delete current batch. |
| 73 |
if ( ! empty( $batch->data ) ) { |
| 74 |
$this->update( $batch->key, $batch->data ); |
| 75 |
} else { |
| 76 |
$this->delete( $batch->key ); |
| 77 |
} |
| 78 |
} while ( ! $this->batch_limit_exceeded() && ! $this->is_queue_empty() ); |
| 79 |
|
| 80 |
$this->unlock_process(); |
| 81 |
|
| 82 |
// Start next batch or complete process. |
| 83 |
if ( ! $this->is_queue_empty() ) { |
| 84 |
$this->dispatch(); |
| 85 |
} else { |
| 86 |
$this->complete(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Dispatch updater. |
| 93 |
* |
| 94 |
* Updater will still run via cron job if this fails for any reason. |
| 95 |
*/ |
| 96 |
public function dispatch() { |
| 97 |
$dispatched = parent::dispatch(); |
| 98 |
|
| 99 |
if ( is_wp_error( $dispatched ) ) { |
| 100 |
wp_die( esc_attr( $dispatched ) ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Completes the batch operation. |
| 107 |
* |
| 108 |
* This method is called when the batch operation is completed. |
| 109 |
* It triggers the 'on_batch_completed' event and invokes the parent's 'complete()' method. |
| 110 |
* |
| 111 |
* @since 1.0.0 |
| 112 |
*/ |
| 113 |
protected function complete() { |
| 114 |
$this->on_batch_completed(); |
| 115 |
parent::complete(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Handles the 'batch_completed' event. |
| 120 |
* |
| 121 |
* This method is called when the batch operation is completed. |
| 122 |
* You can override this method in child classes to perform additional actions after batch completion. |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
private function on_batch_completed() { |
| 127 |
} |
| 128 |
} |
| 129 |
|