PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / abstracts / class-evf-background-process.php
everest-forms / includes / abstracts Last commit date
legacy 6 years ago class-evf-background-process.php 7 years ago class-evf-deprecated-hooks.php 6 years ago class-evf-form-fields.php 2 years ago class-evf-integration.php 5 years ago class-evf-log-handler.php 6 years ago class-evf-session.php 7 years ago class-evf-settings-api.php 4 years ago
class-evf-background-process.php
213 lines
1 <?php
2 /**
3 * Abstract WP_Background_Process class.
4 *
5 * Uses https://github.com/A5hleyRich/wp-background-processing to handle DB
6 * updates in the background.
7 *
8 * @package EverestForms/Classes
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 if ( ! class_exists( 'WP_Async_Request', false ) ) {
14 include_once dirname( EVF_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php';
15 }
16
17 if ( ! class_exists( 'WP_Background_Process', false ) ) {
18 include_once dirname( EVF_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php';
19 }
20
21 /**
22 * EVF_Background_Process class.
23 */
24 abstract class EVF_Background_Process extends WP_Background_Process {
25
26 /**
27 * Is queue empty.
28 *
29 * @return bool
30 */
31 protected function is_queue_empty() {
32 global $wpdb;
33
34 $table = $wpdb->options;
35 $column = 'option_name';
36
37 if ( is_multisite() ) {
38 $table = $wpdb->sitemeta;
39 $column = 'meta_key';
40 }
41
42 $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
43
44 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
45
46 return ! ( $count > 0 );
47 }
48
49 /**
50 * Get batch.
51 *
52 * @return stdClass Return the first batch from the queue.
53 */
54 protected function get_batch() {
55 global $wpdb;
56
57 $table = $wpdb->options;
58 $column = 'option_name';
59 $key_column = 'option_id';
60 $value_column = 'option_value';
61
62 if ( is_multisite() ) {
63 $table = $wpdb->sitemeta;
64 $column = 'meta_key';
65 $key_column = 'meta_id';
66 $value_column = 'meta_value';
67 }
68
69 $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
70
71 $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1", $key ) ); // @codingStandardsIgnoreLine.
72
73 $batch = new stdClass();
74 $batch->key = $query->$column;
75 $batch->data = array_filter( (array) maybe_unserialize( $query->$value_column ) );
76
77 return $batch;
78 }
79
80 /**
81 * See if the batch limit has been exceeded.
82 *
83 * @return bool
84 */
85 protected function batch_limit_exceeded() {
86 return $this->time_exceeded() || $this->memory_exceeded();
87 }
88
89 /**
90 * Handle.
91 *
92 * Pass each queue item to the task handler, while remaining
93 * within server memory and time limit constraints.
94 */
95 protected function handle() {
96 $this->lock_process();
97
98 do {
99 $batch = $this->get_batch();
100
101 foreach ( $batch->data as $key => $value ) {
102 $task = $this->task( $value );
103
104 if ( false !== $task ) {
105 $batch->data[ $key ] = $task;
106 } else {
107 unset( $batch->data[ $key ] );
108 }
109
110 if ( $this->batch_limit_exceeded() ) {
111 // Batch limits reached.
112 break;
113 }
114 }
115
116 // Update or delete current batch.
117 if ( ! empty( $batch->data ) ) {
118 $this->update( $batch->key, $batch->data );
119 } else {
120 $this->delete( $batch->key );
121 }
122 } while ( ! $this->batch_limit_exceeded() && ! $this->is_queue_empty() );
123
124 $this->unlock_process();
125
126 // Start next batch or complete process.
127 if ( ! $this->is_queue_empty() ) {
128 $this->dispatch();
129 } else {
130 $this->complete();
131 }
132 }
133
134 /**
135 * Get memory limit.
136 *
137 * @return int
138 */
139 protected function get_memory_limit() {
140 if ( function_exists( 'ini_get' ) ) {
141 $memory_limit = ini_get( 'memory_limit' );
142 } else {
143 // Sensible default.
144 $memory_limit = '128M';
145 }
146
147 if ( ! $memory_limit || -1 === intval( $memory_limit ) ) {
148 // Unlimited, set to 32GB.
149 $memory_limit = '32G';
150 }
151
152 return wp_convert_hr_to_bytes( $memory_limit );
153 }
154
155 /**
156 * Schedule cron healthcheck.
157 *
158 * @param array $schedules Schedules.
159 * @return array
160 */
161 public function schedule_cron_healthcheck( $schedules ) {
162 $interval = apply_filters( $this->identifier . '_cron_interval', 5 );
163
164 if ( property_exists( $this, 'cron_interval' ) ) {
165 $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval_identifier );
166 }
167
168 // Adds every 5 minutes to the existing schedules.
169 $schedules[ $this->identifier . '_cron_interval' ] = array(
170 'interval' => MINUTE_IN_SECONDS * $interval,
171 /* translators: %d: interval */
172 'display' => sprintf( __( 'Every %d minutes', 'everest-forms' ), $interval ),
173 );
174
175 return $schedules;
176 }
177
178 /**
179 * Delete all batches.
180 *
181 * @return EVF_Background_Process
182 */
183 public function delete_all_batches() {
184 global $wpdb;
185
186 $table = $wpdb->options;
187 $column = 'option_name';
188
189 if ( is_multisite() ) {
190 $table = $wpdb->sitemeta;
191 $column = 'meta_key';
192 }
193
194 $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';
195
196 $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$column} LIKE %s", $key ) ); // @codingStandardsIgnoreLine.
197
198 return $this;
199 }
200
201 /**
202 * Kill process.
203 *
204 * Stop processing queue items, clear cronjob and delete all batches.
205 */
206 public function kill_process() {
207 if ( ! $this->is_queue_empty() ) {
208 $this->delete_all_batches();
209 wp_clear_scheduled_hook( $this->cron_hook_identifier );
210 }
211 }
212 }
213