1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles all admin ajax interactions for the MonsterInsights plugin. |
4
|
|
|
* |
5
|
|
|
* @since 6.0.0 |
6
|
|
|
* |
7
|
|
|
* @package MonsterInsights |
8
|
|
|
* @subpackage Ajax |
9
|
|
|
* @author Chris Christoff |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Stores a user setting for the logged in WordPress User |
19
|
|
|
* |
20
|
|
|
* @access public |
21
|
|
|
* @since 6.0.0 |
22
|
|
|
*/ |
23
|
|
|
function monsterinsights_ajax_set_user_setting() { |
24
|
|
|
|
25
|
|
|
// Run a security check first. |
26
|
|
|
check_ajax_referer( 'monsterinsights-set-user-setting', 'nonce' ); |
27
|
|
|
|
28
|
|
|
// Prepare variables. |
29
|
|
|
$name = stripslashes( $_POST['name'] ); |
30
|
|
|
$value = stripslashes( $_POST['value'] ); |
31
|
|
|
|
32
|
|
|
// Set user setting. |
33
|
|
|
set_user_setting( $name, $value ); |
34
|
|
|
|
35
|
|
|
// Send back the response. |
36
|
|
|
wp_send_json_success(); |
37
|
|
|
wp_die(); |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
add_action( 'wp_ajax_monsterinsights_install_addon', 'monsterinsights_ajax_install_addon' ); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Installs a MonsterInsights addon. |
44
|
|
|
* |
45
|
|
|
* @access public |
46
|
|
|
* @since 6.0.0 |
47
|
|
|
*/ |
48
|
|
|
function monsterinsights_ajax_install_addon() { |
49
|
|
|
|
50
|
|
|
// Run a security check first. |
51
|
|
|
check_ajax_referer( 'monsterinsights-install', 'nonce' ); |
52
|
|
|
|
53
|
|
|
if ( ! current_user_can( 'install_plugins' ) ) { |
54
|
|
|
echo json_encode( true ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Install the addon. |
58
|
|
|
if ( isset( $_POST['plugin'] ) ) { |
59
|
|
|
$download_url = $_POST['plugin']; |
60
|
|
|
global $hook_suffix; |
61
|
|
|
|
62
|
|
|
// Set the current screen to avoid undefined notices. |
63
|
|
|
set_current_screen(); |
64
|
|
|
|
65
|
|
|
// Prepare variables. |
66
|
|
|
$method = ''; |
67
|
|
|
$url = add_query_arg( |
68
|
|
|
array( |
69
|
|
|
'page' => 'monsterinsights-settings' |
70
|
|
|
), |
71
|
|
|
admin_url( 'admin.php' ) |
72
|
|
|
); |
73
|
|
|
$url = esc_url( $url ); |
74
|
|
|
|
75
|
|
|
// Start output bufferring to catch the filesystem form if credentials are needed. |
76
|
|
|
ob_start(); |
77
|
|
|
if ( false === ( $creds = request_filesystem_credentials( $url, $method, false, false, null ) ) ) { |
78
|
|
|
$form = ob_get_clean(); |
79
|
|
|
echo json_encode( array( 'form' => $form ) ); |
80
|
|
|
wp_die(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// If we are not authenticated, make it happen now. |
84
|
|
|
if ( ! WP_Filesystem( $creds ) ) { |
85
|
|
|
ob_start(); |
86
|
|
|
request_filesystem_credentials( $url, $method, true, false, null ); |
87
|
|
|
$form = ob_get_clean(); |
88
|
|
|
echo json_encode( array( 'form' => $form ) ); |
89
|
|
|
wp_die(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
93
|
|
|
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
94
|
|
|
$base = MonsterInsights(); |
95
|
|
|
require_once plugin_dir_path( $base->file ) . '/includes/admin/licensing/skin.php'; |
96
|
|
|
|
97
|
|
|
// Create the plugin upgrader with our custom skin. |
98
|
|
|
$installer = new Plugin_Upgrader( $skin = new MonsterInsights_Skin() ); |
99
|
|
|
$installer->install( $download_url ); |
100
|
|
|
|
101
|
|
|
// Flush the cache and return the newly installed plugin basename. |
102
|
|
|
wp_cache_flush(); |
103
|
|
|
if ( $installer->plugin_info() ) { |
104
|
|
|
$plugin_basename = $installer->plugin_info(); |
105
|
|
|
echo json_encode( array( 'plugin' => $plugin_basename ) ); |
106
|
|
|
wp_die(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Send back a response. |
111
|
|
|
echo json_encode( true ); |
112
|
|
|
wp_die(); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
add_action( 'wp_ajax_monsterinsights_activate_addon', 'monsterinsights_ajax_activate_addon' ); |
117
|
|
|
/** |
118
|
|
|
* Activates a MonsterInsights addon. |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* @since 6.0.0 |
122
|
|
|
*/ |
123
|
|
|
function monsterinsights_ajax_activate_addon() { |
124
|
|
|
|
125
|
|
|
// Run a security check first. |
126
|
|
|
check_ajax_referer( 'monsterinsights-activate', 'nonce' ); |
127
|
|
|
|
128
|
|
|
if ( ! current_user_can( 'activate_plugins' ) ) { |
129
|
|
|
echo json_encode( true ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Activate the addon. |
133
|
|
|
if ( isset( $_POST['plugin'] ) ) { |
134
|
|
|
if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) { |
135
|
|
|
$activate = activate_plugin( $_POST['plugin'], NULL, true ); |
136
|
|
|
} else { |
137
|
|
|
$activate = activate_plugin( $_POST['plugin'] ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ( is_wp_error( $activate ) ) { |
141
|
|
|
echo json_encode( array( 'error' => $activate->get_error_message() ) ); |
142
|
|
|
wp_die(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
echo json_encode( true ); |
147
|
|
|
wp_die(); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
add_action( 'wp_ajax_monsterinsights_deactivate_addon', 'monsterinsights_ajax_deactivate_addon' ); |
152
|
|
|
/** |
153
|
|
|
* Deactivates a MonsterInsights addon. |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
* @since 6.0.0 |
157
|
|
|
*/ |
158
|
|
|
function monsterinsights_ajax_deactivate_addon() { |
159
|
|
|
|
160
|
|
|
// Run a security check first. |
161
|
|
|
check_ajax_referer( 'monsterinsights-deactivate', 'nonce' ); |
162
|
|
|
|
163
|
|
|
if ( ! current_user_can( 'activate_plugins' ) ) { |
164
|
|
|
echo json_encode( true ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Deactivate the addon. |
168
|
|
|
if ( isset( $_POST['plugin'] ) ) { |
169
|
|
|
if ( isset( $_POST['isnetwork'] ) && $_POST['isnetwork'] ) { |
170
|
|
|
$deactivate = deactivate_plugins( $_POST['plugin'], false, true ); |
|
|
|
|
171
|
|
|
} else { |
172
|
|
|
$deactivate = deactivate_plugins( $_POST['plugin'] ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
echo json_encode( true ); |
177
|
|
|
wp_die(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Called whenever a notice is dismissed in MonsterInsights or its Addons. |
182
|
|
|
* |
183
|
|
|
* Updates a key's value in the options table to mark the notice as dismissed, |
184
|
|
|
* preventing it from displaying again |
185
|
|
|
* |
186
|
|
|
* @access public |
187
|
|
|
* @since 6.0.0 |
188
|
|
|
*/ |
189
|
|
|
function monsterinsights_ajax_dismiss_notice() { |
190
|
|
|
|
191
|
|
|
// Run a security check first. |
192
|
|
|
check_ajax_referer( 'monsterinsights-dismiss-notice', 'nonce' ); |
193
|
|
|
|
194
|
|
|
// Deactivate the notice |
195
|
|
|
if ( isset( $_POST['notice'] ) ) { |
196
|
|
|
// Init the notice class and mark notice as deactivated |
197
|
|
|
MonsterInsights()->notices->dismiss( $_POST['notice'] ); |
198
|
|
|
|
199
|
|
|
// Return true |
200
|
|
|
echo json_encode( true ); |
201
|
|
|
wp_die(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// If here, an error occurred |
205
|
|
|
echo json_encode( false ); |
206
|
|
|
wp_die(); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
add_action( 'wp_ajax_monsterinsights_ajax_dismiss_notice', 'monsterinsights_ajax_dismiss_notice' ); |