PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / trunk
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell vtrunk
3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 2.8.17 2.8.18 All 259 releases
wpfunnels / admin / import-export / batch-processing / class-wpfnl-background-task.php

class-wpfnl-background-task.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell trunk, at admin/import-export/batch-processing/class-wpfnl-background-task.php

129 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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