Test Failed
Push — release/2.0 ( f7bd0b...627b34 )
by Ravinder
04:42
created

Give_Updates::__show_notice()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 11
nop 0
dl 0
loc 60
rs 6.8358
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Class Give_Updates
5
 *
6
 * @since 1.8.12
7
 */
8
class Give_Updates {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
9
10
	/**
11
	 * Instance.
12
	 *
13
	 * @since
14
	 * @access static
15
	 * @var
16
	 */
17
	static private $instance;
18
19
	/**
20
	 * Instance.
21
	 *
22
	 * @since
23
	 * @access static
24
	 * @var Give_Background_Updater
25
	 */
26
	static private $background_updater;
27
28
	/**
29
	 * Updates
30
	 *
31
	 * @since  1.8.12
32
	 * @access private
33
	 * @var array
34
	 */
35
	private $updates = array();
36
37
	/**
38
	 * Current update percentage number
39
	 *
40
	 * @since  1.8.12
41
	 * @access private
42
	 * @var array
43
	 */
44
	public $percentage = 0;
45
46
	/**
47
	 * Current update step number
48
	 *
49
	 * @since  1.8.12
50
	 * @access private
51
	 * @var array
52
	 */
53
	public $step = 1;
54
55
	/**
56
	 * Current update number
57
	 *
58
	 * @since  1.8.12
59
	 * @access private
60
	 * @var array
61
	 */
62
	public $update = 1;
63
64
	/**
65
	 * Singleton pattern.
66
	 *
67
	 * @since  1.8.12
68
	 * @access private
69
	 *
70
	 * @param Give_Updates .
71
	 */
72
	private function __construct() {
73
	}
74
75
	/**
76
	 * Register updates
77
	 *
78
	 * @since  1.8.12
79
	 * @access public
80
	 *
81
	 * @param array $args
82
	 */
83
	public function register( $args ) {
84
		$args_default = array(
85
			'id'       => '',
86
			'version'  => '',
87
			'callback' => '',
88
		);
89
90
		$args = wp_parse_args( $args, $args_default );
91
92
		// You can only register database upgrade.
93
		$args['type'] = 'database';
94
95
		// Bailout.
96
		if (
97
			empty( $args['id'] ) ||
98
			empty( $args['version'] ) ||
99
			empty( $args['callback'] ) ||
100
			! is_callable( $args['callback'] )
101
		) {
102
			return;
103
		}
104
105
		// Change depend param to array.
106
		if ( isset( $args['depend'] ) && is_string( $args['depend'] ) ) {
107
			$args['depend'] = array( $args['depend'] );
108
		}
109
110
		$this->updates[ $args['type'] ][] = $args;
111
	}
112
113
	/**
114
	 * Get instance.
115
	 *
116
	 * @since
117
	 * @access static
118
	 * @return static
119
	 */
120
	static function get_instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
		if ( is_null( self::$instance ) ) {
122
			self::$instance = new self();
123
		}
124
125
		return self::$instance;
126
	}
127
128
	/**
129
	 *
130
	 * Setup hook
131
	 *
132
	 * @since  1.8.12
133
	 * @access public
134
	 */
135
	public function setup() {
136
		/**
137
		 * Load file
138
		 */
139
		require_once GIVE_PLUGIN_DIR . 'includes/class-give-background-updater.php';
140
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
141
142
		self::$background_updater = new Give_Background_Updater();
143
144
		/**
145
		 * Setup hooks.
146
		 */
147
		add_action( 'init', array( $this, '__register_upgrade' ), 9999 );
148
		add_action( 'give_set_upgrade_completed', array( $this, '__flush_resume_updates' ), 9999 );
149
		add_action( 'wp_ajax_give_db_updates_info', array( $this, '__give_db_updates_info' ) );
150
		add_action( 'wp_ajax_give_run_db_updates', array( $this, '__give_start_updating' ) );
151
		add_action( 'admin_init', array( $this, '__redirect_admin' ) );
152
		add_action( 'admin_notices', array( $this, '__show_notice' ) );
153
154
		if ( is_admin() ) {
155
			add_action( 'admin_init', array( $this, '__change_donations_label' ), 9999 );
156
			add_action( 'admin_menu', array( $this, '__register_menu' ), 9999 );
157
		}
158
	}
159
160
	/**
161
	 * Register plugin add-on updates.
162
	 *
163
	 * @since  1.8.12
164
	 * @access public
165
	 */
166
	public function __register_plugin_addon_updates() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__register_plugin_addon_updates" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
167
		$addons         = give_get_plugins();
168
		$plugin_updates = get_plugin_updates();
169
170
		foreach ( $addons as $key => $info ) {
171
			if ( 'active' != $info['Status'] || 'add-on' != $info['Type'] || empty( $plugin_updates[ $key ] ) ) {
172
				continue;
173
			}
174
175
			$this->updates['plugin'][] = array_merge( $info, (array) $plugin_updates[ $key ] );
176
		}
177
	}
178
179
180
	/**
181
	 * Fire custom action hook to register updates
182
	 *
183
	 * @since  1.8.12
184
	 * @access public
185
	 */
186
	public function __register_upgrade() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__register_upgrade" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
187
		if ( ! is_admin() ) {
188
			return;
189
		}
190
191
		/**
192
		 * Fire the hook
193
		 *
194
		 * @since 1.8.12
195
		 */
196
		do_action( 'give_register_updates', $this );
197
	}
198
199
	/**
200
	 * Rename `Donations` menu title if updates exists
201
	 *
202
	 * @since  1.8.12
203
	 * @access public
204
	 */
205
	function __change_donations_label() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
Method name "Give_Updates::__change_donations_label" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
206
		global $menu;
207
208
		// Bailout.
209
		if ( empty( $menu ) || ! $this->get_total_update_count() ) {
210
			return;
211
		}
212
213
		foreach ( $menu as $index => $menu_item ) {
214
			if ( 'edit.php?post_type=give_forms' !== $menu_item[2] ) {
215
				continue;
216
			}
217
218
			$menu[ $index ][0] = sprintf(
219
				'%1$s <span class="update-plugins"><span class="plugin-count give-update-progress-count">%2$s%3$s</span></span>',
220
				__( 'Donations', 'give' ),
221
				$this->is_doing_updates() ?
222
					$this->get_db_update_processing_percentage() :
223
					$this->get_total_update_count(),
224
				$this->is_doing_updates() ? '%' : ''
225
			);
226
227
			break;
228
		}
229
	}
230
231
	/**
232
	 * Register updates menu
233
	 *
234
	 * @since  1.8.12
235
	 * @access public
236
	 */
237
	public function __register_menu() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__register_menu" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
238
239
		// Load plugin updates.
240
		$this->__register_plugin_addon_updates();
241
242
		// Bailout.
243
		if ( ! $this->get_total_update_count() ) {
244
			// Show complete update message if still on update setting page.
245
			if ( isset( $_GET['page'] ) && 'give-updates' === $_GET['page'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
246
				// Upgrades
247
				add_submenu_page(
248
					'edit.php?post_type=give_forms',
249
					esc_html__( 'Give Updates Complete', 'give' ),
250
					__( 'Updates', 'give' ),
251
					'manage_give_settings',
252
					'give-updates',
253
					array( $this, 'render_complete_page' )
254
				);
255
			}
256
257
			return;
258
		}
259
260
		// Upgrades
261
		add_submenu_page(
262
			'edit.php?post_type=give_forms',
263
			esc_html__( 'Give Updates', 'give' ),
264
			sprintf(
265
				'%1$s <span class="update-plugins"><span class="plugin-count give-update-progress-count">%2$s%3$s</span></span>',
266
				__( 'Updates', 'give' ),
267
				$this->is_doing_updates() ?
268
					$this->get_db_update_processing_percentage() :
269
					$this->get_total_update_count(),
270
				$this->is_doing_updates() ? '%' : ''
271
			),
272
			'manage_give_settings',
273
			'give-updates',
274
			array( $this, 'render_page' )
275
		);
276
	}
277
278
279
	/**
280
	 * Show update related notices
281
	 *
282
	 * @since  2.0
283
	 * @access public
284
	 */
285
	public function __redirect_admin() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__redirect_admin" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
286
		// Show db upgrade completed notice.
287
		if (
288
			! wp_doing_ajax() &&
289
			current_user_can( 'manage_give_settings' ) &&
290
			get_option( 'give_show_db_upgrade_complete_notice' ) &&
291
			! isset( $_GET['give-db-update-completed'] )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
292
		) {
293
			delete_option( 'give_show_db_upgrade_complete_notice' );
294
295
			wp_redirect( add_query_arg( array( 'give-db-update-completed' => 'give_db_upgrade_completed' ) ) );
296
			exit();
297
		}
298
	}
299
300
301
	/**
302
	 * Show update related notices
303
	 *
304
	 * @since  2.0
305
	 * @access public
306
	 */
307
	public function __show_notice() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__show_notice" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
308
		// Bailout.
309
		if ( ! current_user_can( 'manage_give_settings' ) || $this->is_doing_updates() ) {
310
			return;
311
		}
312
313
		// Run DB updates.
314
		if ( ! empty( $_GET['give-run-db-update'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
315
			$this->run_db_update();
316
		}
317
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
318
319
		// Bailout.
320
		if ( isset( $_GET['page'] ) && 'give-updates' === $_GET['page'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
321
			return;
322
		}
323
324
		// Show db upgrade completed notice.
325
		if ( ! empty( $_GET['give-db-update-completed'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
326
			Give()->notices->register_notice( array(
327
				'id'          => 'give_db_upgrade_completed',
328
				'type'        => 'updated',
329
				'description' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ),
330
				'show'        => true,
331
			) );
332
333
			// Start update.
334
		} elseif ( ! empty( $_GET['give-run-db-update'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
335
			$this->run_db_update();
336
337
			// Show run the update notice.
338
		} elseif ( $this->get_total_new_db_update_count() ) {
339
			ob_start();
340
			?>
341
			<p>
342
				<strong><?php _e( 'Database Update', 'give' ); ?></strong>
343
				&nbsp;&#8211;&nbsp;<?php _e( 'GiveWP needs to update your database to the latest version. The following process will make updates to your site\'s database. Please create a complete backup before proceeding.', 'give' ); ?>
344
			</p>
345
			<p class="submit">
346
				<a href="<?php echo esc_url( add_query_arg( array( 'give-run-db-update' => 1 ), admin_url( 'edit.php?post_type=give_forms&page=give-updates' ) ) ); ?>" class="button button-primary give-run-update-now">
347
					<?php _e( 'Run the updater', 'woocommerce' ); ?>
348
				</a>
349
			</p>
350
			<script type="text/javascript">
351
				jQuery('.give-run-update-now').click('click', function () {
352
					return window.confirm('<?php echo esc_js( __( 'It is strongly recommended that you backup your database before proceeding. Do you want to run the update now?', 'give' ) ); ?>'); // jshint ignore:line
353
				});
354
			</script>
355
			<?php
356
			$desc_html = ob_get_clean();
357
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
358
359
			Give()->notices->register_notice( array(
360
				'id'          => 'give_upgrade_db',
361
				'type'        => 'updated',
362
				'dismissible' => false,
363
				'description' => $desc_html,
364
			) );
365
		}
366
	}
367
368
	/**
369
	 * Render Give Updates Completed page
370
	 *
371
	 * @since  1.8.12
372
	 * @access public
373
	 */
374
	public function render_complete_page() {
375
		include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades-complete.php';
376
	}
377
378
	/**
379
	 * Render Give Updates page
380
	 *
381
	 * @since  1.8.12
382
	 * @access public
383
	 */
384
	public function render_page() {
385
		include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades.php';
386
	}
387
388
	/**
389
	 * Run database upgrades
390
	 *
391
	 * @since  2.0
392
	 * @access private
393
	 */
394
	private function run_db_update() {
395
		// Bailout.
396
		if ( $this->is_doing_updates() || ! $this->get_total_new_db_update_count() ) {
397
			return;
398
		}
399
400
		$updates = $this->get_updates( 'database', 'new' );
401
402
		foreach ( $updates as $update ) {
403
			self::$background_updater->push_to_queue( $update );
404
		}
405
406
		add_option( 'give_db_update_count', count( $updates ), '', 'no' );
407
408
		add_option( 'give_doing_upgrade', array(
409
			'update_info' => $updates[0],
410
			'step'        => 1,
411
			'update'      => 1,
412
			'heading'     => sprintf( 'Update %s of %s', 1, count( $updates ) ),
413
			'percentage'  => 0,
414
			'total_percentage'  => 0,
415
		), '', 'no' );
416
417
		self::$background_updater->save()->dispatch();
418
	}
419
420
421
	/**
422
	 * Delete resume updates
423
	 *
424
	 * @since  1.8.12
425
	 * @access public
426
	 */
427
	public function __flush_resume_updates() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__flush_resume_updates" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
428
		//delete_option( 'give_doing_upgrade' );
429
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
430
431
		// Reset counter.
432
		$this->step = $this->percentage = 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 of type integer is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Documentation Bug introduced by
It seems like $this->percentage = 0 of type integer is incompatible with the declared type array of property $step.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
433
		++ $this->update;
434
	}
435
436
437
	/**
438
	 * Initialize updates
439
	 *
440
	 * @since  2.0
441
	 * @access public
442
	 *
443
	 * @return void
444
	 */
445
	public function __give_start_updating() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__give_start_updating" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
446
		// Check permission.
447
		if (
448
			! current_user_can( 'manage_give_settings' ) ||
449
			$this->is_doing_updates()
450
		) {
451
			wp_send_json_error();
452
		}
453
454
		// @todo: validate nonce
455
		// @todo: set http method to post
456
		if ( empty( $_POST['run_db_update'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
457
			wp_send_json_error();
458
		}
459
460
		$this->run_db_update();
461
462
		wp_send_json_success();
463
	}
464
465
466
	/**
467
	 * This function handle ajax query for dn update status.
468
	 *
469
	 * @since  2.0
470
	 * @access public
471
	 *
472
	 * @return string
473
	 */
474
	public function __give_db_updates_info() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Updates::__give_db_updates_info" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
475
		$update_info   = get_option( 'give_doing_upgrade' );
476
		$response_type = '';
477
478
		if ( empty( $update_info ) && ! $this->get_pending_db_update_count() ) {
479
			$update_info   = array(
480
				'message'    => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ),
481
				'heading'    => __( 'Updates Completed.', 'give' ),
482
				'percentage' => 0,
483
			);
484
			$response_type = 'success';
485
486
			delete_option( 'give_show_db_upgrade_complete_notice' );
487
		}
488
489
		$this->send_ajax_response( $update_info, $response_type );
490
	}
491
492
	/**
493
	 * Send ajax response
494
	 *
495
	 * @since  1.8.12
496
	 * @access public
497
	 *
498
	 * @param        $data
499
	 * @param string $type
500
	 */
501
	public function send_ajax_response( $data, $type = '' ) {
502
		$default = array(
503
			'message'    => '',
504
			'heading'    => '',
505
			'percentage' => 0,
506
			'step'       => 0,
507
			'update'     => 0,
508
		);
509
510
		// Set data.
511
		$data = wp_parse_args( $data, $default );
512
513
		// Enable cache.
514
		Give_Cache::enable();
515
516
		switch ( $type ) {
517
			case 'success':
518
				wp_send_json_success( $data );
519
				break;
520
521
			case 'error':
522
				wp_send_json_error( $data );
523
				break;
524
525
			default:
526
				wp_send_json( array(
527
					'data' => $data,
528
				) );
529
				break;
530
		}
531
	}
532
533
	/**
534
	 * Set current update percentage.
535
	 *
536
	 * @since  1.8.12
537
	 * @access public
538
	 *
539
	 * @param $total
540
	 * @param $current_total
541
	 */
542
	public function set_percentage( $total, $current_total ) {
543
		// Set percentage.
544
		$this->percentage = $total ? ( ( $current_total ) / $total ) * 100 : 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $total ? $current_total / $total * 100 : 0 of type integer or double is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
545
546
		// Verify percentage.
547
		$this->percentage = ( 100 < $this->percentage ) ? 100 : $this->percentage;
0 ignored issues
show
Documentation Bug introduced by
It seems like 100 < $this->percentage ? 100 : $this->percentage of type integer or double is incompatible with the declared type array of property $percentage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
548
	}
549
550
	/**
551
	 * Check if parent update completed or not.
552
	 *
553
	 * @since  2.0
554
	 * @access private
555
	 *
556
	 * @param array $update
557
	 *
558
	 * @return bool|null
559
	 */
560
	public function is_parent_updates_completed( $update ) {
561
		// Bailout.
562
		if ( empty( $update['depend'] ) ) {
563
			return true;
564
		}
565
566
		$is_dependency_completed = true;
567
568
		foreach ( $update['depend'] as $depend ) {
569
			// Check if dependency is valid or not.
570
			if ( ! $this->has_valid_dependency( $update ) ) {
571
				$is_dependency_completed = null;
572
				break;
573
			}
574
575
			if ( ! give_has_upgrade_completed( $depend ) ) {
576
				$is_dependency_completed = false;
577
				break;
578
			}
579
		}
580
581
		return $is_dependency_completed;
582
	}
583
584
	/**
585
	 * Flag to check if DB updates running or not.
586
	 *
587
	 * @since  2.0
588
	 * @access public
589
	 * @return bool
590
	 */
591
	public function is_doing_updates() {
592
		return (bool) get_option( 'give_doing_upgrade' );
593
	}
594
595
596
	/**
597
	 * Check if update has valid dependency or not.
598
	 *
599
	 * @since  2.0
600
	 * @access public
601
	 *
602
	 * @param $update
603
	 *
604
	 * @return bool
605
	 */
606
	public function has_valid_dependency( $update ) {
607
		$is_valid_dependency = true;
608
		$update_ids          = wp_list_pluck( $this->get_updates( 'database' ), 'id' );
609
610
		foreach ( $update['depend'] as $depend ) {
611
			// Check if dependency is valid or not.
612
			if ( ! in_array( $depend, $update_ids ) ) {
613
				$is_valid_dependency = false;
614
				break;
615
			}
616
		}
617
618
		return $is_valid_dependency;
619
	}
620
621
	/**
622
	 * Get updates.
623
	 *
624
	 * @since  1.8.12
625
	 * @access public
626
	 *
627
	 * @param string $update_type Tye of update.
628
	 * @param string $status      Tye of update.
629
	 *
630
	 * @return array
631
	 */
632
	public function get_updates( $update_type = '', $status = 'all' ) {
633
		// return all updates.
634
		if ( empty( $update_type ) ) {
635
			return $this->updates;
636
		}
637
638
		// Get specific update.
639
		$updates = ! empty( $this->updates[ $update_type ] ) ? $this->updates[ $update_type ] : array();
640
641
		// Bailout.
642
		if ( empty( $updates ) ) {
643
			return $updates;
644
		}
645
646
		switch ( $status ) {
647
			case 'new':
648
				// Remove already completed updates.
649
				$completed_updates = give_get_completed_upgrades();
650
651
				if ( ! empty( $completed_updates ) ) {
652
					foreach ( $updates as $index => $update ) {
653
						if ( in_array( $update['id'], $completed_updates ) ) {
654
							unset( $updates[ $index ] );
655
						}
656
					}
657
					$updates = array_values( $updates );
658
				}
659
660
				break;
661
		}
662
663
		return $updates;
664
	}
665
666
	/**
667
	 * Get addon update count.
668
	 *
669
	 * @since  1.8.12
670
	 * @access public
671
	 * @return int
672
	 */
673
	public function get_total_plugin_update_count() {
674
		return count( $this->get_updates( 'plugin' ) );
675
	}
676
677
	/**
678
	 * Get total update count
679
	 *
680
	 * @since  1.8.12
681
	 * @access public
682
	 *
683
	 * @return int
684
	 */
685
	public function get_total_update_count() {
686
		$db_update_count     = $this->get_pending_db_update_count();
687
		$plugin_update_count = $this->get_total_plugin_update_count();
688
689
		return ( $db_update_count + $plugin_update_count );
690
	}
691
692
	/**
693
	 * Get total pending updates count
694
	 *
695
	 * @since  1.8.12
696
	 * @access public
697
	 *
698
	 * @return int
699
	 */
700
	public function get_pending_db_update_count() {
701
		return count( $this->get_updates( 'database', 'new' ) );
702
	}
703
704
	/**
705
	 * Get total updates count
706
	 *
707
	 * @since  1.8.18
708
	 * @access public
709
	 *
710
	 * @return int
711
	 */
712
	public function get_total_db_update_count() {
713
		return count( $this->get_updates( 'database', 'all' ) );
714
	}
715
716
	/**
717
	 * Get total new updates count
718
	 *
719
	 * @since  2.0
720
	 * @access public
721
	 *
722
	 * @return int
723
	 */
724
	public function get_total_new_db_update_count() {
725
		return $this->is_doing_updates() ?
726
			get_option( 'give_db_update_count' ) :
727
			$this->get_pending_db_update_count();
728
	}
729
730
	/**
731
	 * Get total new updates count
732
	 *
733
	 * @since  2.0
734
	 * @access public
735
	 *
736
	 * @return int
737
	 */
738
	public function get_running_db_update() {
739
		$current_update = get_option( 'give_doing_upgrade' );
740
741
		return $this->is_doing_updates() ?
742
			$current_update['update'] :
743
			1;
744
	}
745
746
	/**
747
	 * Get database update processing percentage.
748
	 *
749
	 * @since  2.0
750
	 * @access public
751
	 * @return float|int
752
	 */
753
	public function get_db_update_processing_percentage() {
754
		// Bailout.
755
		if ( ! $this->get_total_new_db_update_count() ) {
756
			return 0;
757
		}
758
759
		$resume_update            = get_option( 'give_doing_upgrade' );
760
		$update_count_percentages = ( ( $this->get_running_db_update() - 1 ) / $this->get_total_new_db_update_count() ) * 100;
761
		$update_percentage_share  = ( 1 / $this->get_total_new_db_update_count() ) * 100;
762
		$upgrade_percentage       = ( ( $resume_update['percentage'] * $update_percentage_share ) / 100 );
763
764
		$final_percentage = $update_count_percentages + $upgrade_percentage;
765
766
		return $this->is_doing_updates() ?
767
			( absint( $final_percentage ) ?
768
				absint( $final_percentage ) :
769
				round( $final_percentage, 2 )
770
			) :
771
			0;
772
	}
773
}
774
775
Give_Updates::get_instance()->setup();
776