Test Failed
Push — master ( b3899f...f31e84 )
by Ravinder
05:53
created

Give_Background_Updater   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 216
rs 10
c 0
b 0
f 0
wmc 27
lcom 0
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 4 1
A get_all_batch() 0 3 1
A handle_cron_healthcheck() 0 15 3
A schedule_event() 0 5 2
D task() 0 108 15
A complete() 0 7 1
A get_memory_limit() 0 15 4
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() ) {
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 ) ) {
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
		if ( empty( $update ) ) {
95
			return false;
96
		}
97
98
		/* @var  Give_Updates $give_updates */
99
		$give_updates  = Give_Updates::get_instance();
100
		$resume_update = get_option(
101
			'give_doing_upgrade',
102
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
103
			// Default update.
104
			array(
105
				'update_info' => $update,
106
				'step'        => 1,
107
				'update'      => 1,
108
				'heading'     => sprintf( 'Update %s of {update_count}', 1 ),
109
				'percentage'  => $give_updates->percentage,
110
			)
111
		);
112
113
		// Continuously skip update if previous update does not complete yet.
114
		if (
115
			$resume_update['update_info']['id'] !== $update['id'] &&
116
			! give_has_upgrade_completed( $resume_update['update_info']['id'] )
117
		) {
118
			$batch = Give_Updates::$background_updater->get_all_batch();
119
			$batch_data_count = count( $batch->data );
120
121
			if ( ! empty( $batch ) &&  1 === $batch_data_count ) {
122
				if ( ! empty( $update['depend'] ) ) {
123
124
					$give_updates   = Give_Updates::get_instance();
125
					$all_updates    = $give_updates->get_updates( 'database', 'all' );
126
					$all_update_ids = wp_list_pluck( $all_updates, 'id' );
127
128
					foreach ( $update['depend'] as $depend ) {
129
						if ( give_has_upgrade_completed( $depend ) ) {
130
							continue;
131
						}
132
133
						if ( in_array( $depend, $all_update_ids ) ) {
134
							array_unshift( $batch->data, $all_updates[ array_search( $depend, $all_update_ids ) ] );
135
						}
136
					}
137
138
					if( $batch_data_count !== count( $batch->data ) ) {
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...
139
						update_option( $batch->key, $batch->data );
140
						$this->dispatch();
141
142
						wp_die();
143
					}
144
				}
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
			return $update;
149
		}
150
151
		// Set params.
152
		$resume_update['update_info'] = $update;
153
		$give_updates->step           = absint( $resume_update['step'] );
154
		$give_updates->update         = absint( $resume_update['update'] );
155
		$is_parent_update_completed   = $give_updates->is_parent_updates_completed( $update );
156
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
157
158
		// Skip update if dependency update does not complete yet.
159
		if ( empty( $is_parent_update_completed ) ) {
160
			// @todo: set error when you have only one update with invalid dependency
161
			if ( ! is_null( $is_parent_update_completed ) ) {
162
				return $update;
163
			}
164
165
			return false;
166
		}
167
168
		// Disable cache.
169
		Give_Cache::disable();
170
171
		// Run update.
172
		if ( is_array( $update['callback'] ) ) {
173
			$update['callback'][0]->$update['callback'][1]();
174
		} else {
175
			$update['callback']();
176
		}
177
178
		// Set update info.
179
		$doing_upgrade_args = array(
180
			'update_info'      => $update,
181
			'step'             => ++ $give_updates->step,
182
			'update'           => $give_updates->update,
183
			'heading'          => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ),
184
			'percentage'       => $give_updates->percentage,
185
			'total_percentage' => $give_updates->get_db_update_processing_percentage(),
186
		);
187
188
		// Cache upgrade.
189
		update_option( 'give_doing_upgrade', $doing_upgrade_args );
190
191
		// Enable cache.
192
		Give_Cache::enable();
193
194
		// Check if current update completed or not.
195
		if ( give_has_upgrade_completed( $update['id'] ) ) {
196
			return false;
197
		}
198
199
		return $update;
200
	}
201
202
	/**
203
	 * Complete
204
	 *
205
	 * Override if applicable, but ensure that the below actions are
206
	 * performed, or, call parent::complete().
207
	 */
208
	protected function complete() {
209
		parent::complete();
210
211
		delete_option( 'give_db_update_count' );
212
		delete_option( 'give_doing_upgrade' );
213
		add_option( 'give_show_db_upgrade_complete_notice', 1, '', 'no' );
214
	}
215
216
	/**
217
	 * Get memory limit
218
	 *
219
	 * @return int
220
	 */
221
	protected function get_memory_limit() {
222
		if ( function_exists( 'ini_get' ) ) {
223
			$memory_limit = ini_get( 'memory_limit' );
224
		} else {
225
			// Sensible default.
226
			$memory_limit = '128M';
227
		}
228
229
		if ( ! $memory_limit || '-1' === $memory_limit ) {
230
			// Unlimited, set to 32GB.
231
			$memory_limit = '32000M';
232
		}
233
234
		return intval( $memory_limit ) * 1024 * 1024;
235
	}
236
}
237