Test Failed
Push — feature/upgrdae-auto-pause ( 76a297...90fb5e )
by Ravinder
04:37
created

Give_Background_Updater::flush_task_cache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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
 * @class    Give_Background_Updater
9
 * @version  2.0.0
10
 * @package  Give/Classes
11
 * @category Class
12
 * @author   WordImpress
13
 */
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Give_Background_Updater Class.
20
 */
21
class Give_Background_Updater extends WP_Background_Process {
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $action = 'give_db_updater';
27
28
	/**
29
	 * Dispatch updater.
30
	 *
31
	 * Updater will still run via cron job if this fails for any reason.
32
	 */
33
	public function dispatch() {
34
		/* @var WP_Background_Process $dispatched */
35
		parent::dispatch();
36
	}
37
38
39
	/**
40
	 * Get all batches.
41
	 *
42
	 * @since  2.0
43
	 * @access public
44
	 * @return stdClass
45
	 */
46
	public function get_all_batch() {
47
		return parent::get_batch();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get_batch() instead of get_all_batch()). Are you sure this is correct? If so, you might want to change this to $this->get_batch().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48
	}
49
50
	/**
51
	 * Handle cron healthcheck
52
	 *
53
	 * Restart the background process if not already running
54
	 * and data exists in the queue.
55
	 */
56
	public function handle_cron_healthcheck() {
57
		if ( $this->is_process_running() || $this->is_paused_process()  ) {
58
			// Background process already running.
59
			return;
60
		}
61
62
		if ( $this->is_queue_empty() ) {
63
			// No data to process.
64
			$this->clear_scheduled_event();
65
66
			return;
67
		}
68
69
		$this->handle();
70
	}
71
72
	/**
73
	 * Schedule fallback event.
74
	 */
75
	protected function schedule_event() {
76
		if ( ! wp_next_scheduled( $this->cron_hook_identifier ) && ! $this->is_paused_process() ) {
77
			wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
78
		}
79
	}
80
81
	/**
82
	 * Task
83
	 *
84
	 * Override this method to perform any actions required on each
85
	 * queue item. Return the modified item for further processing
86
	 * in the next pass through. Or, return false to remove the
87
	 * item from the queue.
88
	 *
89
	 * @param array $update Update info
90
	 *
91
	 * @return mixed
92
	 */
93
	protected function task( $update ) {
94
		// Pause upgrade immediately if admin pausing upgrades.
95
		if( $this->is_paused_process() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
96
			wp_die();
97
		}
98
99
		if ( empty( $update ) ) {
100
			return false;
101
		}
102
103
		// Delete cache.
104
		$this->flush_task_cache();
105
106
		/* @var  Give_Updates $give_updates */
107
		$give_updates  = Give_Updates::get_instance();
108
		$resume_update = get_option(
109
			'give_doing_upgrade',
110
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
111
			// Default update.
112
			array(
113
				'update_info'      => $update,
114
				'step'             => 1,
115
				'update'           => 1,
116
				'heading'          => sprintf( 'Update %s of {update_count}', 1 ),
117
				'percentage'       => $give_updates->percentage,
118
				'total_percentage' => 0,
119
			)
120
		);
121
122
		// Continuously skip update if previous update does not complete yet.
123
		if (
124
			$resume_update['update_info']['id'] !== $update['id'] &&
125
			! give_has_upgrade_completed( $resume_update['update_info']['id'] )
126
		) {
127
			return $update;
128
		}
129
130
		// Set params.
131
		$resume_update['update_info'] = $update;
132
		$give_updates->step           = absint( $resume_update['step'] );
133
		$give_updates->update         = absint( $resume_update['update'] );
134
		$is_parent_update_completed   = $give_updates->is_parent_updates_completed( $update );
135
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
136
137
		// Skip update if dependency update does not complete yet.
138
		if ( empty( $is_parent_update_completed ) ) {
139
			// @todo: set error when you have only one update with invalid dependency
140
			if ( ! is_null( $is_parent_update_completed ) ) {
141
				return $update;
142
			}
143
144
			return false;
145
		}
146
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
147
148
		// Pause upgrade immediately if found following:
149
		// 1. Running update number greater then total update count
150
		// 2. Processing percentage greater then 100%
151
		if( (
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
152
			101 < $resume_update['total_percentage'] ) ||
153
		    ( $give_updates->get_total_db_update_count() < $resume_update['update'] ) ||
154
		    ! in_array( $resume_update['update_info']['id'], $give_updates->get_update_ids() )
155
		) {
156
			if( ! $this->is_paused_process() ){
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
157
				$give_updates->__pause_db_update(true);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
158
			}
159
160
			update_option( 'give_upgrade_error', 1 );
161
162
			wp_die();
163
		}
164
165
		// Disable cache.
166
		Give_Cache::disable();
167
168
		// Run update.
169
		if ( is_array( $update['callback'] ) ) {
170
			$update['callback'][0]->$update['callback'][1]();
171
		} else {
172
			$update['callback']();
173
		}
174
175
		// Set update info.
176
		$doing_upgrade_args = array(
177
			'update_info'      => $update,
178
			'step'             => ++ $give_updates->step,
179
			'update'           => $give_updates->update,
180
			'heading'          => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ),
181
			'percentage'       => $give_updates->percentage,
182
			'total_percentage' => $give_updates->get_db_update_processing_percentage(),
183
		);
184
185
		// Cache upgrade.
186
		update_option( 'give_doing_upgrade', $doing_upgrade_args );
187
188
		// Enable cache.
189
		Give_Cache::enable();
190
191
		// Check if current update completed or not.
192
		if ( give_has_upgrade_completed( $update['id'] ) ) {
193
			return false;
194
		}
195
196
		return $update;
197
	}
198
199
	/**
200
	 * Complete
201
	 *
202
	 * Override if applicable, but ensure that the below actions are
203
	 * performed, or, call parent::complete().
204
	 */
205
	protected function complete() {
206
		if ( $this->is_paused_process() ) {
207
			return false;
208
		}
209
210
		parent::complete();
211
212
		delete_option( 'give_upgrade_error' );
213
		delete_option( 'give_db_update_count' );
214
		delete_option( 'give_doing_upgrade' );
215
		add_option( 'give_show_db_upgrade_complete_notice', 1, '', 'no' );
216
217
		// Flush cache.
218
		Give_Cache::get_instance()->flush_cache();
219
220
		if ( $cache_keys = Give_Cache::get_options_like( '' ) ) {
221
			Give_Cache::delete( $cache_keys );
222
		}
223
	}
224
225
	/**
226
	 * Get memory limit
227
	 *
228
	 * @return int
229
	 */
230
	protected function get_memory_limit() {
231
		if ( function_exists( 'ini_get' ) ) {
232
			$memory_limit = ini_get( 'memory_limit' );
233
		} else {
234
			// Sensible default.
235
			$memory_limit = '128M';
236
		}
237
238
		if ( ! $memory_limit || '-1' === $memory_limit ) {
239
			// Unlimited, set to 32GB.
240
			$memory_limit = '32000M';
241
		}
242
243
		return intval( $memory_limit ) * 1024 * 1024;
244
	}
245
246
	/**
247
	 * Maybe process queue
248
	 *
249
	 * Checks whether data exists within the queue and that
250
	 * the process is not already running.
251
	 */
252
	public function maybe_handle() {
253
		// Don't lock up other requests while processing
254
		session_write_close();
0 ignored issues
show
introduced by
The use of PHP session function session_write_close() is prohibited.
Loading history...
255
256
		if ( $this->is_process_running() || $this->is_paused_process() ) {
257
			// Background process already running.
258
			wp_die();
259
		}
260
261
		if ( $this->is_queue_empty() ) {
262
			// No data to process.
263
			wp_die();
264
		}
265
266
		check_ajax_referer( $this->identifier, 'nonce' );
267
268
		$this->handle();
269
270
		wp_die();
271
	}
272
273
274
	/**
275
	 * Check if backgound upgrade paused or not.
276
	 *
277
	 * @since 2.0
278
	 * @access public
279
	 * @return bool
280
	 */
281
	public function is_paused_process(){
282
		// Delete cache.
283
		wp_cache_delete( 'give_paused_batches', 'options' );
284
285
		return ! empty( get_option('give_paused_batches') );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
286
	}
287
288
289
	/**
290
	 * Get identifier
291
	 *
292
	 * @since  2.0
293
	 * @access public
294
	 * @return mixed|string
295
	 */
296
	public function get_identifier() {
297
		return $this->identifier;
298
	}
299
300
	/**
301
	 * Get cron identifier
302
	 *
303
	 * @since  2.0
304
	 * @access public
305
	 * @return mixed|string
306
	 */
307
	public function get_cron_identifier() {
308
		return $this->cron_hook_identifier;
309
	}
310
311
312
	/**
313
	 * Flush background update related cache to prevent task to go to stalled state.
314
	 *
315
	 * @since 2.0.3
316
	 */
317
	private function flush_task_cache() {
318
319
		$options = array(
320
			'give_doing_upgrade',
321
			'give_paused_batches',
322
			'give_upgrade_error',
323
			'give_db_update_count',
324
			'give_doing_upgrade',
325
			'give_show_db_upgrade_complete_notice',
326
		);
327
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
328
329
		foreach ( $options as $option ) {
330
			wp_cache_delete( $option, 'options' );
331
		}
332
	}
333
}
334