forked from wp-plugins/wp-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.php
237 lines (163 loc) · 9.35 KB
/
schedule.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
add_filter( 'cron_schedules', 'wp_statistics_addcron' );
function wp_statistics_addcron( $schedules ) {
// Adds once weekly to the existing schedules.
if( !array_key_exists( 'weekly', $schedules ) ) {
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
}
if( !array_key_exists( 'biweekly', $schedules ) ) {
$schedules['biweekly'] = array(
'interval' => 1209600,
'display' => __( 'Once Every 2 Weeks' )
);
}
if( !array_key_exists( '4weeks', $schedules ) ) {
$schedules['4weeks'] = array(
'interval' => 2419200,
'display' => __( 'Once Every 4 Weeks' )
);
}
return $schedules;
}
// Add the report schedule if it doesn't exist and is enabled.
if( !wp_next_scheduled('report_hook') && $WP_Statistics->get_option('stats_report') ) {
wp_schedule_event(time(), $WP_Statistics->get_option('time_report'), 'report_hook');
}
// Remove the report schedule if it does exist and is disabled.
if( wp_next_scheduled('report_hook') && !$WP_Statistics->get_option('stats_report') ) {
wp_unschedule_event(wp_next_scheduled('report_hook'), 'report_hook');
}
// Add the GeoIP update schedule if it doesn't exist and it should be.
if( !wp_next_scheduled('wp_statistics_geoip_hook') && $WP_Statistics->get_option('schedule_geoip') && $WP_Statistics->get_option('geoip') ) {
wp_schedule_event(time(), 'daily', 'wp_statistics_geoip_hook');
}
// Remove the GeoIP update schedule if it does exist and it should shouldn't.
if( wp_next_scheduled('wp_statistics_geoip_hook') && (!$WP_Statistics->get_option('schedule_geoip') || !$WP_Statistics->get_option('geoip') ) ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_geoip_hook'), 'wp_statistics_geoip_hook');
}
// Add the browscap update schedule if it doesn't exist and it should be.
if( !wp_next_scheduled('wp_statistics_browscap_hook') && $WP_Statistics->get_option('schedule_browscap') ) {
wp_schedule_event(time(), 'weekly', 'wp_statistics_browscap_hook');
}
// Remove the browscap update schedule if it does exist and it should shouldn't.
if( wp_next_scheduled('wp_statistics_browscap_hook') && !$WP_Statistics->get_option('schedule_browscap') ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_browscap_hook'), 'wp_statistics_browscap_hook');
}
// Add the referrerspam update schedule if it doesn't exist and it should be.
if( !wp_next_scheduled('wp_statistics_referrerspam_hook') && $WP_Statistics->get_option('schedule_referrerspam') ) {
wp_schedule_event(time(), 'weekly', 'wp_statistics_referrerspam_hook');
}
// Remove the referrerspam update schedule if it does exist and it should shouldn't.
if( wp_next_scheduled('wp_statistics_referrerspam_hook') && !$WP_Statistics->get_option('schedule_referrerspam') ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_referrerspam_hook'), 'wp_statistics_referrerspam_hook');
}
// Add the database maintenance schedule if it doesn't exist and it should be.
if( !wp_next_scheduled('wp_statistics_dbmaint_hook') && $WP_Statistics->get_option('schedule_dbmaint') ) {
wp_schedule_event(time(), 'daily', 'wp_statistics_dbmaint_hook');
}
// Remove the database maintenance schedule if it does exist and it shouldn't.
if( wp_next_scheduled('wp_statistics_dbmaint_hook') && (!$WP_Statistics->get_option('schedule_dbmaint') ) ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_dbmaint_hook'), 'wp_statistics_dbmaint_hook');
}
// Add the visitor database maintenance schedule if it doesn't exist and it should be.
if( !wp_next_scheduled('wp_statistics_dbmaint_visitor_hook') && $WP_Statistics->get_option('schedule_dbmaint_visitor') ) {
wp_schedule_event(time(), 'daily', 'wp_statistics_dbmaint_visitor_hook');
}
// Remove the visitor database maintenance schedule if it does exist and it shouldn't.
if( wp_next_scheduled('wp_statistics_dbmaint_visitor_hook') && (!$WP_Statistics->get_option('schedule_dbmaint_visitor') ) ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_dbmaint_visitor_hook'), 'wp_statistics_dbmaint_visitor_hook');
}
// Remove the add visit row schedule if it does exist and it shouldn't.
if( wp_next_scheduled('wp_statistics_add_visit_hook') && (!$WP_Statistics->get_option('visits') ) ) {
wp_unschedule_event(wp_next_scheduled('wp_statistics_add_visit_hook'), 'wp_statistics_add_visit_hook');
}
// Add the add visit table row schedule if it does exist and it should.
if( !wp_next_scheduled('wp_statistics_add_visit_hook') && $WP_Statistics->get_option('visits')) {
wp_schedule_event(time(), 'daily', 'wp_statistics_add_visit_hook');
}
// This function adds a record for tomorrow to the visit table to avoid a race condition.
function wp_statistics_add_visit_event() {
GLOBAL $wpdb, $WP_Statistics;
$sqlstring = $wpdb->prepare( 'INSERT INTO ' . $wpdb->prefix . 'statistics_visit (last_visit, last_counter, visit) VALUES ( %s, %s, %d)', $WP_Statistics->Current_Date(null, '+1'), $WP_Statistics->Current_date('Y-m-d', '+1'), 0 );
$wpdb->query( $sqlstring );
}
add_action('wp_statistics_add_visit_hook', 'wp_statistics_add_visit_event');
// This function updates the GeoIP database from MaxMind.
function wp_statistics_geoip_event() {
GLOBAL $WP_Statistics;
// Maxmind updates the geoip database on the first Tuesday of the month, to make sure we don't update before they post
// the update, download it two days later.
$thisupdate = strtotime('First Tuesday of this month') + (86400 * 2);
$lastupdate = $WP_Statistics->get_option('last_geoip_dl');
$upload_dir = wp_upload_dir();
// We're also going to look to see if our filesize is to small, this means the plugin stub still exists and should
// be replaced with a proper file.
$dbsize = filesize($upload_dir['basedir'] . '/wp-statistics/GeoLite2-Country.mmdb');
if( $lastupdate < $thisupdate || $dbsize < 1024 ) {
// We can't fire the download function directly here as we rely on some functions that haven't been loaded yet
// in WordPress, so instead just set the flag in the options table and the shutdown hook will take care of the
// actual download at the end of the page.
$WP_Statistics->update_option('update_geoip',TRUE);
}
}
add_action('wp_statistics_geoip_hook', 'wp_statistics_geoip_event');
// This function updates the browscap database.
function wp_statistics_browscap_event() {
GLOBAL $WP_Statistics;
// Check for a new browscap once a week
$WP_Statistics->update_option('update_browscap',TRUE);
}
add_action('wp_statistics_browscap_hook', 'wp_statistics_browscap_event');
// This function updates the browscap database.
function wp_statistics_referrerspam_event() {
GLOBAL $WP_Statistics;
// Check for a new referrerspam once a week
$WP_Statistics->update_option('update_referrerspam',TRUE);
}
add_action('wp_statistics_referrerspam_hook', 'wp_statistics_referrerspam_event');
// This function will purge old records on a schedule based on age.
function wp_statistics_dbmaint_event() {
global $wpdb, $WP_Statistics;
require_once( plugin_dir_path( __FILE__ ) . '/includes/functions/purge.php' );
$purge_days = intval( $WP_Statistics->get_option('schedule_dbmaint_days', FALSE) );
wp_statistics_purge_data( $purge_days );
}
add_action('wp_statistics_dbmaint_hook', 'wp_statistics_dbmaint_event');
// This function will purge visitors with more than a defined number of hits in a day.
function wp_statistics_dbmaint_visitor_event() {
global $wpdb, $WP_Statistics;
require_once( plugin_dir_path( __FILE__ ) . '/includes/functions/purge-hits.php' );
$purge_hits = intval( $WP_Statistics->get_option('schedule_dbmaint_visitor_hits', FALSE) );
wp_statistics_purge_visitor_hits( $purge_hits );
}
add_action('wp_statistics_dbmaint_visitor_hook', 'wp_statistics_dbmaint_visitor_event');
// This function sends the statistics report to the selected users.
function wp_statistics_send_report() {
GLOBAL $WP_Statistics;
// Retrieve the template from the options.
$final_text_report = $WP_Statistics->get_option('content_report');
// Process shortcodes in the template. Note that V8.0 upgrade script replaced the old %option% codes with the appropriate short codes.
$final_text_report = do_shortcode( $final_text_report );
// Send the report through the selected transport agent.
if( $WP_Statistics->get_option('send_report') == 'mail' ) {
$blogname = get_bloginfo('name');
$blogemail = get_bloginfo('admin_email');
$headers[] = "From: $blogname <$blogemail>";
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=utf-8";
if( $WP_Statistics->get_option('email_list') == '' ) { $WP_Statistics->update_option( 'email_list', $blogemail ); }
wp_mail( $WP_Statistics->get_option('email_list'), __('Statistical reporting', 'wp_statistics'), $final_text_report, $headers );
} else if( $WP_Statistics->get_option('send_report') == 'sms' ) {
global $sms;
if( class_exists(get_option('wp_webservice')) ) {
$sms->to = array(get_option('wp_admin_mobile'));
$sms->msg = $final_text_report;
$sms->SendSMS();
}
}
}
add_action('report_hook', 'wp_statistics_send_report');
?>