upgrades.php ➔ gmb_upgrades_screen()   C
last analyzed

Complexity

Conditions 9
Paths 192

Size

Total Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 192
nop 0
dl 0
loc 84
rs 6.1802
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Upgrade Screen
4
 *
5
 * @subpackage  includes/admin/upgrades
6
 * @copyright   Copyright (c) 2015, WordImpress
7
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
8
 * @since       2.0
9
 */
10
11
// Exit if accessed directly
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
/**
17
 * Render Upgrades Screen
18
 *
19
 * @since 2.0
20
 * @return void
21
 */
22
function gmb_upgrades_screen() {
23
24
	$action = isset( $_GET['gmb-upgrade'] ) ? sanitize_text_field( $_GET['gmb-upgrade'] ) : '';
25
	$step   = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1;
26
	$total  = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false;
27
	$custom = isset( $_GET['custom'] ) ? absint( $_GET['custom'] ) : 0;
28
	$number = isset( $_GET['number'] ) ? absint( $_GET['number'] ) : 100;
29
	$steps  = round( ( $total / $number ), 0 );
30
31
	$doing_upgrade_args = array(
32
		'page'        => 'gmb-upgrades',
33
		'gmb-upgrade' => $action,
34
		'step'        => $step,
35
		'total'       => $total,
36
		'custom'      => $custom,
37
		'steps'       => $steps
38
	);
39
	update_option( 'gmb_doing_upgrade', $doing_upgrade_args );
40
	if ( $step > $steps ) {
41
		// Prevent a weird case where the estimate was off. Usually only a couple.
42
		$steps = $step;
43
	}
44
	?>
45
	<div class="wrap">
46
		<h2><?php _e( 'Maps Builder - Upgrade', 'gmb' ); ?></h2>
47
48
		<?php if ( ! empty( $action ) ) : ?>
49
50
			<div id="gmb-upgrade-status">
51
				<p><?php _e( 'The upgrade process has started, please be patient. This could take several minutes. You will be automatically redirected when the upgrade is finished.', 'gmb' ); ?></p>
52
53
				<?php if ( ! empty( $total ) ) : ?>
54
					<p>
55
						<strong><?php printf( __( 'Step %d of approximately %d running', 'gmb' ), $step, $steps ); ?></strong>
56
					</p>
57
				<?php endif; ?>
58
			</div>
59
			<script type="text/javascript">
60
				setTimeout( function () {
61
					document.location.href = "index.php?gmb_action=<?php echo $action; ?>&step=<?php echo $step; ?>&total=<?php echo $total; ?>&custom=<?php echo $custom; ?>";
62
				}, 250 );
63
			</script>
64
65
		<?php else : ?>
66
67
			<div id="gmb-upgrade-status" class="updated" style="margin-top:15px;">
68
				<p style="margin-bottom:8px;">
69
					<?php _e( 'The upgrade process has started, please do not close your browser or refresh. This could take several minutes. You will be automatically redirected when the upgrade has finished.', 'gmb' ); ?>
70
					<img src="<?php echo GMB_PLUGIN_URL . '/assets/img/loading.gif'; ?>" id="gmb-upgrade-loader" style="position:relative; top:3px;" />
71
				</p>
72
			</div>
73
			<script type="text/javascript">
74
				jQuery( document ).ready( function () {
75
					// Trigger upgrades on page load
76
					var data = {action: 'gmb_trigger_upgrades'};
77
					var el_upgrade_status = jQuery( '#gmb-upgrade-status' );
78
79
					//Trigger via AJAX
80
					jQuery.post( ajaxurl, data, function ( response ) {
81
82
						//Uncomment for debugging
83
//						jQuery( '#gmb-upgrade-status' ).after( response );
84
85
						//Success Message
86
						if ( response == 'complete' ) {
87
88
							el_upgrade_status.hide();
89
							el_upgrade_status.after( '<div class="updated"><p>The upgrade process has completed successfully. Hooray! You will now be redirected back to your previous page.</p></div>' );
90
91
							//Send user back to prev page
92
							setTimeout( function () {
93
								history.back();
94
							}, 4000 );
95
96
						}
97
					} );
98
				} );
99
			</script>
100
101
		<?php endif; ?>
102
103
	</div>
104
	<?php
105
}