|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MonsterInsights Connect is our service that makes it easy for non-techy users to |
|
4
|
|
|
* upgrade to MonsterInsights Pro without having to manually install the MonsterInsights Pro plugin. |
|
5
|
|
|
* |
|
6
|
|
|
* @package MonsterInsights |
|
7
|
|
|
* @since 7.7.2 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class MonsterInsights_Connect |
|
12
|
|
|
*/ |
|
13
|
|
|
class MonsterInsights_Connect { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* MonsterInsights_Connect constructor. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
|
|
20
|
|
|
$this->hooks(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add hooks for Connect. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function hooks() { |
|
27
|
|
|
|
|
28
|
|
|
add_action( 'wp_ajax_monsterinsights_connect_url', array( $this, 'generate_connect_url' ) ); |
|
29
|
|
|
|
|
30
|
|
|
add_action( 'wp_ajax_nopriv_monsterinsights_connect_process', array( $this, 'process' ) ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Generates and returns MonsterInsights Connect URL. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function generate_connect_url() { |
|
37
|
|
|
|
|
38
|
|
|
check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
|
39
|
|
|
|
|
40
|
|
|
// Check for permissions. |
|
41
|
|
|
if ( ! current_user_can( 'install_plugins' ) ) { |
|
42
|
|
|
wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to install plugins.', 'google-analytics-for-wordpress' ) ) ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ( monsterinsights_is_dev_url( home_url() ) ) { |
|
46
|
|
|
wp_send_json_success( array( |
|
47
|
|
|
'url' => 'https://www.monsterinsights.com/docs/go-lite-pro/#manual-upgrade', |
|
48
|
|
|
) ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
|
52
|
|
|
|
|
53
|
|
|
if ( empty( $key ) ) { |
|
54
|
|
|
wp_send_json_error( |
|
55
|
|
|
array( |
|
56
|
|
|
'message' => esc_html__( 'Please enter your license key to connect.', 'google-analytics-for-wordpress' ), |
|
57
|
|
|
) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Verify pro version is not installed. |
|
62
|
|
|
$active = activate_plugin( 'google-analytics-premium/googleanalytics-premium.php', false, false, true ); |
|
63
|
|
|
if ( ! is_wp_error( $active ) ) { |
|
64
|
|
|
// Deactivate plugin. |
|
65
|
|
|
deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ); |
|
66
|
|
|
wp_send_json_error( array( |
|
67
|
|
|
'message' => esc_html__( 'Pro version is already installed.', 'google-analytics-for-wordpress' ), |
|
68
|
|
|
'reload' => true, |
|
69
|
|
|
) ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Redirect. |
|
73
|
|
|
$oth = hash( 'sha512', wp_rand() ); |
|
74
|
|
|
update_option( 'monsterinsights_connect', array( |
|
75
|
|
|
'key' => $key, |
|
76
|
|
|
'time' => time(), |
|
77
|
|
|
) ); |
|
78
|
|
|
update_option( 'monsterinsights_connect_token', $oth ); |
|
79
|
|
|
$version = MonsterInsights()->version; |
|
80
|
|
|
$siteurl = admin_url(); |
|
81
|
|
|
$endpoint = admin_url( 'admin-ajax.php' ); |
|
82
|
|
|
$redirect = admin_url( 'admin.php?page=monsterinsights_settings' ); |
|
83
|
|
|
|
|
84
|
|
|
$url = add_query_arg( array( |
|
85
|
|
|
'key' => $key, |
|
86
|
|
|
'oth' => $oth, |
|
87
|
|
|
'endpoint' => $endpoint, |
|
88
|
|
|
'version' => $version, |
|
89
|
|
|
'siteurl' => $siteurl, |
|
90
|
|
|
'homeurl' => home_url(), |
|
91
|
|
|
'redirect' => rawurldecode( base64_encode( $redirect ) ), |
|
92
|
|
|
'v' => 2, |
|
93
|
|
|
), 'https://upgrade.monsterinsights.com' ); |
|
94
|
|
|
|
|
95
|
|
|
wp_send_json_success( array( |
|
96
|
|
|
'url' => $url, |
|
97
|
|
|
) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Process MonsterInsights Connect. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function process() { |
|
104
|
|
|
$error = esc_html__( 'Could not install upgrade. Please download from monsterinsights.com and install manually.', 'google-analytics-for-wordpress' ); |
|
105
|
|
|
|
|
106
|
|
|
// verify params present (oth & download link). |
|
107
|
|
|
$post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( $_REQUEST['oth'] ) : ''; |
|
108
|
|
|
$post_url = ! empty( $_REQUEST['file'] ) ? $_REQUEST['file'] : ''; |
|
109
|
|
|
if ( empty( $post_oth ) || empty( $post_url ) ) { |
|
110
|
|
|
wp_send_json_error( $error ); |
|
111
|
|
|
} |
|
112
|
|
|
// Verify oth. |
|
113
|
|
|
$oth = get_option( 'monsterinsights_connect_token' ); |
|
114
|
|
|
if ( empty( $oth ) ) { |
|
115
|
|
|
wp_send_json_error( $error ); |
|
116
|
|
|
} |
|
117
|
|
|
if ( ! hash_equals( $oth, $post_oth ) ) { |
|
118
|
|
|
wp_send_json_error( $error ); |
|
119
|
|
|
} |
|
120
|
|
|
// Delete so cannot replay. |
|
121
|
|
|
delete_option( 'monsterinsights_connect_token' ); |
|
122
|
|
|
// Set the current screen to avoid undefined notices. |
|
123
|
|
|
set_current_screen( 'insights_page_monsterinsights_settings' ); |
|
124
|
|
|
// Prepare variables. |
|
125
|
|
|
$url = esc_url_raw( |
|
126
|
|
|
add_query_arg( |
|
127
|
|
|
array( |
|
128
|
|
|
'page' => 'monsterinsights-settings', |
|
129
|
|
|
), |
|
130
|
|
|
admin_url( 'admin.php' ) |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
// Verify pro not activated. |
|
134
|
|
|
if ( monsterinsights_is_pro_version() ) { |
|
135
|
|
|
wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) ); |
|
136
|
|
|
} |
|
137
|
|
|
// Verify pro not installed. |
|
138
|
|
|
$active = activate_plugin( 'google-analytics-premium/googleanalytics-premium.php', $url, false, true ); |
|
139
|
|
|
if ( ! is_wp_error( $active ) ) { |
|
140
|
|
|
deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ); |
|
141
|
|
|
wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) ); |
|
142
|
|
|
} |
|
143
|
|
|
$creds = request_filesystem_credentials( $url, '', false, false, null ); |
|
|
|
|
|
|
144
|
|
|
// Check for file system permissions. |
|
145
|
|
|
if ( false === $creds ) { |
|
146
|
|
|
wp_send_json_error( $error ); |
|
147
|
|
|
} |
|
148
|
|
|
if ( ! WP_Filesystem( $creds ) ) { |
|
149
|
|
|
wp_send_json_error( $error ); |
|
150
|
|
|
} |
|
151
|
|
|
// We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
|
152
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
153
|
|
|
require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/skin.php'; |
|
154
|
|
|
// Do not allow WordPress to search/download translations, as this will break JS output. |
|
155
|
|
|
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
|
156
|
|
|
// Create the plugin upgrader with our custom skin. |
|
157
|
|
|
$installer = new Plugin_Upgrader( new MonsterInsights_Skin() ); |
|
158
|
|
|
// Error check. |
|
159
|
|
|
if ( ! method_exists( $installer, 'install' ) ) { |
|
160
|
|
|
wp_send_json_error( $error ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Check license key. |
|
164
|
|
|
$license = get_option( 'monsterinsights_connect', false ); |
|
165
|
|
|
if ( empty( $license ) ) { |
|
166
|
|
|
wp_send_json_error( new WP_Error( '403', esc_html__( 'You are not licensed.', 'google-analytics-for-wordpress' ) ) ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$installer->install( $post_url ); // phpcs:ignore |
|
170
|
|
|
// Flush the cache and return the newly installed plugin basename. |
|
171
|
|
|
wp_cache_flush(); |
|
172
|
|
|
|
|
173
|
|
|
if ( $installer->plugin_info() ) { |
|
174
|
|
|
$plugin_basename = $installer->plugin_info(); |
|
175
|
|
|
|
|
176
|
|
|
// Deactivate the lite version first. |
|
177
|
|
|
deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ); |
|
178
|
|
|
|
|
179
|
|
|
// Activate the plugin silently. |
|
180
|
|
|
$activated = activate_plugin( $plugin_basename, '', false, true ); |
|
181
|
|
|
if ( ! is_wp_error( $activated ) ) { |
|
182
|
|
|
wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) ); |
|
183
|
|
|
} else { |
|
184
|
|
|
// Reactivate the lite plugin if pro activation failed. |
|
185
|
|
|
activate_plugin( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ), '', false, true ); |
|
186
|
|
|
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' ) ); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
wp_send_json_error( $error ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
new MonsterInsights_Connect(); |
|
195
|
|
|
|