Test Failed
Push — issues/2531 ( f57328...8a1be7 )
by Ravinder
04:40
created

Give_Updates::has_valid_dependency()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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 count-%2$s"><span class="plugin-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_new_db_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 count-%2$s"><span class="plugin-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_new_db_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
		if (
309
			! current_user_can( 'manage_give_settings' ) ||
310
			( 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...
311
			$this->is_doing_updates()
312
		) {
313
			return;
314
		}
315
316
		// Show db upgrade completed notice.
317
		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...
318
			Give()->notices->register_notice( array(
319
				'id'          => 'give_db_upgrade_completed',
320
				'type'        => 'updated',
321
				'description' => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ),
322
				'show'        => true,
323
			) );
324
325
			// Show update running notice.
326
		} 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...
327
			$this->run_db_update();
328
329
			ob_start();
330
			?>
331
			<p>
332
				<strong><?php _e( 'Give database update', 'give' ); ?></strong>
333
				&nbsp;&#8211;&nbsp;<?php _e( 'Your database is being updated in the background.', 'give' ); ?>
334
				&nbsp;<a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-updates' ); ?>">
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'admin_url'
Loading history...
335
					<?php _e( 'Click here to check database update progress.', 'give' ); ?>
336
				</a>
337
			</p>
338
			<?php
339
			$desc_html = ob_get_clean();
340
			Give()->notices->register_notice( array(
341
				'id'          => 'give_upgrade_db',
342
				'type'        => 'updated',
343
				'description' => $desc_html,
344
			) );
345
346
			// Show run the update notice.
347
		} elseif ( $this->get_total_new_db_update_count() ) {
348
			ob_start();
349
			?>
350
			<p>
351
				<strong><?php _e( 'Give database update', 'give' ); ?></strong>
352
				&nbsp;&#8211;&nbsp;<?php _e( 'We need to update your site database to the latest version.  The following process will make updates to your site\'s database. Please create a database backup before proceeding with updates.', 'give' ); ?>
353
			</p>
354
			<p class="submit">
355
				<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-settings' ) ) ); ?>" class="button button-primary give-run-update-now">
356
					<?php _e( 'Run the updater', 'woocommerce' ); ?>
357
				</a>
358
			</p>
359
			<script type="text/javascript">
360
				jQuery('.give-run-update-now').click('click', function () {
361
					return window.confirm('<?php echo esc_js( __( 'It is strongly recommended that you backup your database before proceeding. Are you sure you wish to run the updater now?', 'give' ) ); ?>'); // jshint ignore:line
362
				});
363
			</script>
364
			<?php
365
			$desc_html = ob_get_clean();
366
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
367
368
			Give()->notices->register_notice( array(
369
				'id'          => 'give_upgrade_db',
370
				'type'        => 'updated',
371
				'description' => $desc_html,
372
			) );
373
		}
374
	}
375
376
	/**
377
	 * Render Give Updates Completed page
378
	 *
379
	 * @since  1.8.12
380
	 * @access public
381
	 */
382
	public function render_complete_page() {
383
		include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades-complete.php';
384
	}
385
386
	/**
387
	 * Render Give Updates page
388
	 *
389
	 * @since  1.8.12
390
	 * @access public
391
	 */
392
	public function render_page() {
393
		include_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/views/upgrades.php';
394
	}
395
396
	/**
397
	 * Run database upgrades
398
	 *
399
	 * @since  2.0
400
	 * @access private
401
	 */
402
	private function run_db_update() {
403
		// Bailout.
404
		if ( $this->is_doing_updates() || ! $this->get_total_new_db_update_count() ) {
405
			return;
406
		}
407
408
		$updates = $this->get_updates( 'database', 'new' );
409
410
		foreach ( $updates as $update ) {
411
			self::$background_updater->push_to_queue( $update );
412
		}
413
414
		add_option( 'give_db_update_count', count( $updates ), '', 'no' );
415
416
		add_option( 'give_doing_upgrade', array(
417
			'update_info' => $updates[0],
418
			'step'        => 1,
419
			'update'      => 1,
420
			'heading'     => sprintf( 'Update %s of %s', 1, count( $updates ) ),
421
			'percentage'  => 0,
422
		), '', 'no' );
423
424
		self::$background_updater->save()->dispatch();
425
	}
426
427
428
	/**
429
	 * Delete resume updates
430
	 *
431
	 * @since  1.8.12
432
	 * @access public
433
	 */
434
	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...
435
		//delete_option( 'give_doing_upgrade' );
436
		update_option( 'give_version', preg_replace( '/[^0-9.].*/', '', GIVE_VERSION ) );
437
438
		// Reset counter.
439
		$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...
440
		++ $this->update;
441
	}
442
443
444
	/**
445
	 * Initialize updates
446
	 *
447
	 * @since  2.0
448
	 * @access public
449
	 *
450
	 * @return void
451
	 */
452
	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...
453
		// Check permission.
454
		if (
455
			! current_user_can( 'manage_give_settings' ) ||
456
			$this->is_doing_updates()
457
		) {
458
			wp_send_json_error();
459
		}
460
461
		// @todo: validate nonce
462
		// @todo: set http method to post
463
		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...
464
			wp_send_json_error();
465
		}
466
467
		$this->run_db_update();
468
469
		wp_send_json_success();
470
	}
471
472
473
	/**
474
	 * This function handle ajax query for dn update status.
475
	 *
476
	 * @since  2.0
477
	 * @access public
478
	 *
479
	 * @return string
480
	 */
481
	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...
482
		$update_info   = get_option( 'give_doing_upgrade' );
483
		$response_type = '';
484
485
		if ( empty( $update_info ) && ! $this->get_pending_db_update_count() ) {
486
			$update_info   = array(
487
				'message'    => __( 'Give database updates completed successfully. Thank you for updating to the latest version!', 'give' ),
488
				'heading'    => __( 'Updates Completed.', 'give' ),
489
				'percentage' => 0,
490
			);
491
			$response_type = 'success';
492
493
			delete_option( 'give_show_db_upgrade_complete_notice' );
494
		}
495
496
		$this->send_ajax_response( $update_info, $response_type );
497
	}
498
499
	/**
500
	 * Send ajax response
501
	 *
502
	 * @since  1.8.12
503
	 * @access public
504
	 *
505
	 * @param        $data
506
	 * @param string $type
507
	 */
508
	public function send_ajax_response( $data, $type = '' ) {
509
		$default = array(
510
			'message'    => '',
511
			'heading'    => '',
512
			'percentage' => 0,
513
			'step'       => 0,
514
			'update'     => 0,
515
		);
516
517
		// Set data.
518
		$data = wp_parse_args( $data, $default );
519
520
		// Enable cache.
521
		Give_Cache::enable();
522
523
		switch ( $type ) {
524
			case 'success':
525
				wp_send_json_success( $data );
526
				break;
527
528
			case 'error':
529
				wp_send_json_error( $data );
530
				break;
531
532
			default:
533
				wp_send_json( array(
534
					'data' => $data,
535
				) );
536
				break;
537
		}
538
	}
539
540
	/**
541
	 * Set current update percentage.
542
	 *
543
	 * @since  1.8.12
544
	 * @access public
545
	 *
546
	 * @param $total
547
	 * @param $current_total
548
	 */
549
	public function set_percentage( $total, $current_total ) {
550
		// Set percentage.
551
		$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...
552
553
		// Verify percentage.
554
		$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...
555
	}
556
557
	/**
558
	 * Check if parent update completed or not.
559
	 *
560
	 * @since  2.0
561
	 * @access private
562
	 *
563
	 * @param array $update
564
	 *
565
	 * @return bool|null
566
	 */
567
	public function is_parent_updates_completed( $update ) {
568
		// Bailout.
569
		if ( empty( $update['depend'] ) ) {
570
			return true;
571
		}
572
573
		$is_dependency_completed = true;
574
575
		foreach ( $update['depend'] as $depend ) {
576
			// Check if dependency is valid or not.
577
			if ( ! $this->has_valid_dependency( $update ) ) {
578
				$is_dependency_completed = null;
579
				break;
580
			}
581
582
			if ( ! give_has_upgrade_completed( $depend ) ) {
583
				$is_dependency_completed = false;
584
				break;
585
			}
586
		}
587
588
		return $is_dependency_completed;
589
	}
590
591
	/**
592
	 * Flag to check if DB updates running or not.
593
	 *
594
	 * @since  2.0
595
	 * @access public
596
	 * @return bool
597
	 */
598
	public function is_doing_updates() {
599
		return (bool) get_option( 'give_doing_upgrade' );
600
	}
601
602
603
	/**
604
	 * Check if update has valid dependency or not.
605
	 *
606
	 * @since  2.0
607
	 * @access public
608
	 *
609
	 * @param $update
610
	 *
611
	 * @return bool
612
	 */
613
	public function has_valid_dependency( $update ) {
614
		$is_valid_dependency = true;
615
		$update_ids          = wp_list_pluck( $this->get_updates( 'database' ), 'id' );
616
617
		foreach ( $update['depend'] as $depend ) {
618
			// Check if dependency is valid or not.
619
			if ( ! in_array( $depend, $update_ids ) ) {
620
				$is_valid_dependency = false;
621
				break;
622
			}
623
		}
624
625
		return $is_valid_dependency;
626
	}
627
628
	/**
629
	 * Get updates.
630
	 *
631
	 * @since  1.8.12
632
	 * @access public
633
	 *
634
	 * @param string $update_type Tye of update.
635
	 * @param string $status      Tye of update.
636
	 *
637
	 * @return array
638
	 */
639
	public function get_updates( $update_type = '', $status = 'all' ) {
640
		// return all updates.
641
		if ( empty( $update_type ) ) {
642
			return $this->updates;
643
		}
644
645
		// Get specific update.
646
		$updates = ! empty( $this->updates[ $update_type ] ) ? $this->updates[ $update_type ] : array();
647
648
		// Bailout.
649
		if ( empty( $updates ) ) {
650
			return $updates;
651
		}
652
653
		switch ( $status ) {
654
			case 'new':
655
				// Remove already completed updates.
656
				$completed_updates = give_get_completed_upgrades();
657
658
				if ( ! empty( $completed_updates ) ) {
659
					foreach ( $updates as $index => $update ) {
660
						if ( in_array( $update['id'], $completed_updates ) ) {
661
							unset( $updates[ $index ] );
662
						}
663
					}
664
					$updates = array_values( $updates );
665
				}
666
667
				break;
668
		}
669
670
		return $updates;
671
	}
672
673
	/**
674
	 * Get addon update count.
675
	 *
676
	 * @since  1.8.12
677
	 * @access public
678
	 * @return int
679
	 */
680
	public function get_total_plugin_update_count() {
681
		return count( $this->get_updates( 'plugin' ) );
682
	}
683
684
	/**
685
	 * Get total update count
686
	 *
687
	 * @since  1.8.12
688
	 * @access public
689
	 *
690
	 * @return int
691
	 */
692
	public function get_total_update_count() {
693
		$db_update_count     = $this->get_pending_db_update_count();
694
		$plugin_update_count = $this->get_total_plugin_update_count();
695
696
		return ( $db_update_count + $plugin_update_count );
697
	}
698
699
	/**
700
	 * Get total pending updates count
701
	 *
702
	 * @since  1.8.12
703
	 * @access public
704
	 *
705
	 * @return int
706
	 */
707
	public function get_pending_db_update_count() {
708
		return count( $this->get_updates( 'database', 'new' ) );
709
	}
710
711
	/**
712
	 * Get total updates count
713
	 *
714
	 * @since  1.8.18
715
	 * @access public
716
	 *
717
	 * @return int
718
	 */
719
	public function get_total_db_update_count() {
720
		return count( $this->get_updates( 'database', 'all' ) );
721
	}
722
723
	/**
724
	 * Get total new updates count
725
	 *
726
	 * @since  2.0
727
	 * @access public
728
	 *
729
	 * @return int
730
	 */
731
	public function get_total_new_db_update_count() {
732
		return $this->is_doing_updates() ?
733
			get_option( 'give_db_update_count' ) :
734
			$this->get_pending_db_update_count();
735
	}
736
737
	/**
738
	 * Get total new updates count
739
	 *
740
	 * @since  2.0
741
	 * @access public
742
	 *
743
	 * @return int
744
	 */
745
	public function get_running_db_update() {
746
		$current_update = get_option( 'give_doing_upgrade' );
747
748
		return $this->is_doing_updates() ?
749
			$current_update['update'] :
750
			1;
751
	}
752
753
	/**
754
	 * Get database update processing percentage.
755
	 *
756
	 * @since  2.0
757
	 * @access public
758
	 * @return float|int
759
	 */
760
	public function get_db_update_processing_percentage() {
761
		// Bailout.
762
		if ( ! $this->get_total_new_db_update_count() ) {
763
			return 0;
764
		}
765
766
		$resume_update            = get_option( 'give_doing_upgrade' );
767
		$update_count_percentages = ( ( $this->get_running_db_update() - 1 ) / $this->get_total_new_db_update_count() ) * 100;
768
		$update_percentage_share  = ( 1 / $this->get_total_new_db_update_count() ) * 100;
769
		$upgrade_percentage       = ( ( $resume_update['percentage'] * $update_percentage_share ) / 100 );
770
771
		$final_percentage = $update_count_percentages + $upgrade_percentage;
772
773
		return $this->is_doing_updates() ?
774
			( absint( $final_percentage ) ?
775
				absint( $final_percentage ) :
776
				round( $final_percentage, 2 )
777
			) :
778
			0;
779
	}
780
}
781
782
Give_Updates::get_instance()->setup();
783