forked from wp-plugins/wp-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
185 lines (142 loc) · 6.79 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
// Setup an AJAX action to close the donation nag banner on the overview page.
function wp_statistics_close_donation_nag_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$WP_Statistics->update_option( 'disable_donation_nag', true );
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_close_donation_nag', 'wp_statistics_close_donation_nag_action_callback' );
// Setup an AJAX action to delete an agent in the optimization page.
function wp_statistics_delete_agents_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$agent = $_POST['agent-name'];
if( $agent ) {
$result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}statistics_visitor WHERE `agent` = %s", $agent ));
if($result) {
echo sprintf(__('%s agent data deleted successfully.', 'wp_statistics'), '<code>' . $agent . '</code>');
}
else {
_e('No agent data found to remove!', 'wp_statistics');
}
} else {
_e('Please select the desired items.', 'wp_statistics');
}
} else {
_e('Access denied!', 'wp_statistics');
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_delete_agents', 'wp_statistics_delete_agents_action_callback' );
// Setup an AJAX action to delete a platform in the optimization page.
function wp_statistics_delete_platforms_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$platform = $_POST['platform-name'];
if( $platform ) {
$result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}statistics_visitor WHERE `platform` = %s", $platform));
if($result) {
echo sprintf(__('%s platform data deleted successfully.', 'wp_statistics'), '<code>' . $platform . '</code>');
}
else {
_e('No platform data found to remove!', 'wp_statistics');
}
} else {
_e('Please select the desired items.', 'wp_statistics');
}
} else {
_e('Access denied!', 'wp_statistics');
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_delete_platforms', 'wp_statistics_delete_platforms_action_callback' );
// Setup an AJAX action to empty a table in the optimization page.
function wp_statistics_empty_table_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$table_name = $_POST['table-name'];
if($table_name) {
switch( $table_name ) {
case 'useronline':
echo wp_statitiscs_empty_table($wpdb->prefix . 'statistics_useronline');
break;
case 'visit':
echo wp_statitiscs_empty_table($wpdb->prefix . 'statistics_visit');
break;
case 'visitors':
echo wp_statitiscs_empty_table($wpdb->prefix . 'statistics_visitor');
break;
case 'exclusions':
echo wp_statitiscs_empty_table($wpdb->prefix . 'statistics_exclusions');
break;
case 'pages':
echo wp_statitiscs_empty_table($wpdb->prefix . 'statistics_pages');
break;
case 'all':
$result_string = wp_statitiscs_empty_table($wpdb->prefix . 'statistics_useronline');
$result_string .= '<br>' . wp_statitiscs_empty_table($wpdb->prefix . 'statistics_visit');
$result_string .= '<br>' . wp_statitiscs_empty_table($wpdb->prefix . 'statistics_visitor');
$result_string .= '<br>' . wp_statitiscs_empty_table($wpdb->prefix . 'statistics_exclusions');
$result_string .= '<br>' . wp_statitiscs_empty_table($wpdb->prefix . 'statistics_pages');
echo $result_string;
break;
default:
_e('Please select the desired items.', 'wp_statistics');
}
$WP_Statistics->Primary_Values();
} else {
_e('Please select the desired items.', 'wp_statistics');
}
} else {
_e('Access denied!', 'wp_statistics');
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_empty_table', 'wp_statistics_empty_table_action_callback' );
// Setup an AJAX action to purge old data in the optimization page.
function wp_statistics_purge_data_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
require($WP_Statistics->plugin_dir . '/includes/functions/purge.php');
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$purge_days = 0;
if( array_key_exists( 'purge-days', $_POST ) ) {
// Get the number of days to purge data before.
$purge_days = intval($_POST['purge-days']);
}
echo wp_statistics_purge_data( $purge_days );
} else {
_e('Access denied!', 'wp_statistics');
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_purge_data', 'wp_statistics_purge_data_action_callback' );
// Setup an AJAX action to purge visitors with more than a defined number of hits.
function wp_statistics_purge_visitor_hits_action_callback() {
GLOBAL $WP_Statistics, $wpdb; // this is how you get access to the database
require($WP_Statistics->plugin_dir . '/includes/functions/purge-hits.php');
$manage_cap = wp_statistics_validate_capability( $WP_Statistics->get_option('manage_capability', 'manage_options') );
if( current_user_can( $manage_cap ) ) {
$purge_hits = 10;
if( array_key_exists( 'purge-hits', $_POST ) ) {
// Get the number of days to purge data before.
$purge_hits = intval($_POST['purge-hits']);
}
if( $purge_hits < 10 ) {
_e('Number of hits must be greater than or equal to 10!', 'wp_statistics');
}
else {
echo wp_statistics_purge_visitor_hits( $purge_hits );
}
} else {
_e('Access denied!', 'wp_statistics');
}
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_wp_statistics_purge_visitor_hits', 'wp_statistics_purge_visitor_hits_action_callback' );