Passed
Push — master ( a722cd...843c92 )
by Chris
03:45
created

monsterinsights_upgrade_license()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 76
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 49
nc 32
nop 0
dl 0
loc 76
rs 8.4905
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
 * Ajax handler for grabbing the upgrade url.
5
 */
6
function monsterinsights_upgrade_license() {
7
8
	check_ajax_referer( 'mi-admin-nonce', 'nonce' );
9
10
	// Check for permissions.
11
	if ( ! current_user_can( 'install_plugins' ) ) {
12
		wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to install plugins.', 'google-analytics-for-wordpress' ) ) );
13
	}
14
15
	// Check license key.
16
	$license = monsterinsights_get_license_key();
17
	if ( empty( $license ) ) {
18
		wp_send_json_error( array( 'message' => esc_html__( 'You are not licensed.', 'google-analytics-for-wordpress' ) ) );
19
	}
20
21
	if ( monsterinsights_is_pro_version() ) {
22
		wp_send_json_error( array( 'message' => esc_html__( 'Only the Lite version can upgrade.', 'google-analytics-for-wordpress' ) ) );
23
	}
24
25
	$url = esc_url_raw(
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
26
		add_query_arg(
27
			array(
28
				'page' => 'monsterinsights_settings',
29
			),
30
			admin_url( 'admin.php' )
31
		)
32
	);
33
34
	// Verify pro version is not installed.
35
	$active = activate_plugin( 'google-analytics-premium/googleanalytics-premium.php', false, false, true );
36
	if ( ! is_wp_error( $active ) ) {
37
		// Deactivate plugin.
38
		deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) );
39
		wp_send_json_error( array(
40
			'message' => esc_html__( 'Pro version is already installed.', 'google-analytics-for-wordpress' ),
41
			'reload'  => true,
42
		) );
43
	}
44
45
	$args = array(
46
		'plugin_name' => 'MonsterInsights Pro',
47
		'plugin_slug' => 'pro',
48
		'plugin_path' => plugin_basename( __FILE__ ),
49
		'plugin_url'  => trailingslashit( WP_PLUGIN_URL ) . 'pro',
50
		'remote_url'  => 'https://monsterinsights.com/',
51
		'version'     => MonsterInsights()->version,
52
		'key'         => $license,
53
	);
54
55
	$updater = new MonsterInsights_Updater( $args );
56
	$addons  = $updater->update_plugins_filter( $updater );
57
	if ( empty( $addons->update->package ) ) {
58
		wp_send_json_error();
59
	}
60
61
	// Redirect.
62
	$oth = hash( 'sha512', wp_rand() );
63
	update_option( 'monsterinsights_one_click_upgrade', $oth );
64
	$version  = MonsterInsights()->version;
65
	$file     = $addons->update->package;
66
	$siteurl  = admin_url();
67
	$endpoint = admin_url( 'admin-ajax.php' );
68
	$redirect = admin_url( 'admin.php?page=monsterinsights_settings' );
69
70
	$url = add_query_arg( array(
71
		'key'      => $license,
72
		'oth'      => $oth,
73
		'endpoint' => $endpoint,
74
		'version'  => $version,
75
		'siteurl'  => $siteurl,
76
		'redirect' => rawurldecode( base64_encode( $redirect ) ),
77
		'file'     => rawurldecode( base64_encode( $file ) ),
78
	), 'https://upgrade.monsterinsights.com' );
79
80
	wp_send_json_success( array(
81
		'url' => $url,
82
	) );
83
84
}
85
86
add_action( 'wp_ajax_monsterinsights_upgrade_license', 'monsterinsights_upgrade_license' );
87
88
/**
89
 * Endpoint for one-click upgrade.
90
 */
91
function monsterinsights_run_one_click_upgrade() {
92
	$error = esc_html__( 'Could not install upgrade. Please download from monsterinsights.com and install manually.', 'google-analytics-for-wordpress' );
93
94
	// verify params present (oth & download link).
95
	$post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( $_REQUEST['oth'] ) : '';
96
	$post_url = ! empty( $_REQUEST['file'] ) ? $_REQUEST['file'] : '';
97
	if ( empty( $post_oth ) || empty( $post_url ) ) {
98
		wp_send_json_error( $error );
99
	}
100
	// Verify oth.
101
	$oth = get_option( 'monsterinsights_one_click_upgrade' );
102
	if ( empty( $oth ) ) {
103
		wp_send_json_error( $error );
104
	}
105
	if ( ! hash_equals( $oth, $post_oth ) ) {
106
		wp_send_json_error( $error );
107
	}
108
	// Delete so cannot replay.
109
	delete_option( 'monsterinsights_one_click_upgrade' );
110
	// Set the current screen to avoid undefined notices.
111
	set_current_screen( 'insights_page_monsterinsights_settings' );
112
	// Prepare variables.
113
	$url = esc_url_raw(
114
		add_query_arg(
115
			array(
116
				'page' => 'monsterinsights-settings',
117
			),
118
			admin_url( 'admin.php' )
119
		)
120
	);
121
	// Verify pro not activated.
122
	if ( monsterinsights_is_pro_version() ) {
123
		wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) );
124
	}
125
	// Verify pro not installed.
126
	$active = activate_plugin( 'google-analytics-premium/googleanalytics-premium.php', $url, false, true );
127
	if ( ! is_wp_error( $active ) ) {
128
		deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) );
129
		wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) );
130
	}
131
	$creds = request_filesystem_credentials( $url, '', false, false, null );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $context of request_filesystem_credentials(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
	$creds = request_filesystem_credentials( $url, '', false, /** @scrutinizer ignore-type */ false, null );
Loading history...
132
	// Check for file system permissions.
133
	if ( false === $creds ) {
134
		wp_send_json_error( $error );
135
	}
136
	if ( ! WP_Filesystem( $creds ) ) {
137
		wp_send_json_error( $error );
138
	}
139
	// We do not need any extra credentials if we have gotten this far, so let's install the plugin.
140
	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
141
	require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/skin.php';
142
	// Do not allow WordPress to search/download translations, as this will break JS output.
143
	remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
144
	// Create the plugin upgrader with our custom skin.
145
	$installer = new Plugin_Upgrader( new MonsterInsights_Skin() );
146
	// Error check.
147
	if ( ! method_exists( $installer, 'install' ) ) {
148
		wp_send_json_error( $error );
149
	}
150
151
	// Check license key.
152
	$license = monsterinsights_get_license_key();
153
	if ( empty( $license ) ) {
154
		wp_send_json_error( new WP_Error( '403', esc_html__( 'You are not licensed.', 'google-analytics-for-wordpress' ) ) );
155
	}
156
	$args = array(
157
		'plugin_name' => 'MonsterInsights Pro',
158
		'plugin_slug' => 'pro',
159
		'plugin_path' => plugin_basename( __FILE__ ),
160
		'plugin_url'  => trailingslashit( WP_PLUGIN_URL ) . 'pro',
161
		'remote_url'  => 'https://monsterinsights.com/',
162
		'version'     => MonsterInsights()->version,
163
		'key'         => $license,
164
	);
165
166
	$updater = new MonsterInsights_Updater( $args );
167
	$addons  = $updater->update_plugins_filter( $updater );
168
	if ( empty( $addons->update->package ) ) {
169
		wp_send_json_error();
170
	}
171
172
	$installer->install( $addons->update->package ); // phpcs:ignore
173
	// Flush the cache and return the newly installed plugin basename.
174
	wp_cache_flush();
175
	if ( $installer->plugin_info() ) {
176
		$plugin_basename = $installer->plugin_info();
177
178
		// Deactivate the lite version first.
179
		deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) );
180
181
		// Activate the plugin silently.
182
		$activated = activate_plugin( $plugin_basename, '', false, true );
183
		if ( ! is_wp_error( $activated ) ) {
184
			wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) );
185
		} else {
186
			// Reactivate the lite plugin if pro activation failed.
187
			activate_plugin( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ), '', false, true );
188
			wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated from the Plugins page inside your WordPress admin.', 'google-analytics-for-wordpress' ) );
189
		}
190
	}
191
	wp_send_json_error( $error );
192
}
193
194
add_action( 'wp_ajax_nopriv_monsterinsights_run_one_click_upgrade', 'monsterinsights_run_one_click_upgrade' );
195