PluginProbe
Simple Statistics for Feeds / trunk
Simple Statistics for Feeds vtrunk
20260809 trunk 20170325 20170326 20170801 20171024 20171103 20180508 20180820 20181116 20190220 20190310 20190501 20190902 20191106 20200317 20200808 20201114 20210210 20210717 20220117 20220517 20221001 20230303 20230311 All 35 releases
simple-feed-stats / simple-feed-stats.php

simple-feed-stats.php in Simple Statistics for Feeds trunk, at simple-feed-stats.php

993 lines 25.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Simple Statistics for Feeds
4 Plugin URI: https://perishablepress.com/simple-feed-stats/
5 Description: Tracks your feeds, adds custom content, and displays your feed statistics on your site.
6 Tags: rss, feed, stats, statistics, subscribers
7 Author: Jeff Starr
8 Author URI: https://plugin-planet.com/
9 Donate link: https://monzillamedia.com/donate.html
10 Contributors: specialk
11 Requires at least: 4.7
12 Tested up to: 7.1
13 Stable tag: 20260809
14 Version: 20260809
15 Requires PHP: 5.6.20
16 Text Domain: simple-feed-stats
17 Domain Path: /languages
18 License: GPL v2 or later
19
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License
22 as published by the Free Software Foundation; either version
23 2 of the License, or (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 with this program. If not, visit: https://www.gnu.org/licenses/
32
33 Copyright 2012-2026 Monzilla Media. All rights reserved.
34 */
35
36 if (!defined('ABSPATH')) die();
37
38 require_once('sfs-admin.php');
39 require_once('sfs-shortcodes.php');
40
41 $sfs_wp_vers = '4.7';
42 $sfs_version = '20260809';
43 $sfs_options = get_option('sfs_options', sfs_default_options());
44
45 define('SFS_PLUGIN_FILE', 'simple-feed-stats/simple-feed-stats.php');
46 define('SFS_PLUGIN_PATH', plugin_dir_path(__FILE__));
47
48 register_activation_hook(__FILE__, 'sfs_on_activate');
49 register_deactivation_hook(__FILE__, 'sfs_delete_options_on_deactivation');
50 register_deactivation_hook(__FILE__, 'sfs_delete_table_on_deactivation');
51 add_filter('plugin_action_links', 'sfs_plugin_action_links', 10, 2);
52 add_filter('plugin_row_meta', 'add_sfs_links', 10, 2);
53 add_action('admin_init', 'sfs_require_wp_version');
54
55
56
57 // enable shortcodes in widgets & post content
58 if (isset($sfs_options['sfs_enable_shortcodes']) && $sfs_options['sfs_enable_shortcodes']) {
59
60 add_filter('the_content', 'do_shortcode', 10);
61 add_filter('widget_text', 'do_shortcode', 10);
62
63 }
64
65
66
67 // random string
68 function sfs_randomizer() {
69
70 $sfs_randomizer = rand(1000000, 9999999);
71
72 return $sfs_randomizer;
73
74 }
75
76
77
78 // string cleaner
79 function sfs_clean($string) {
80
81 $string = trim($string);
82 $string = strip_tags($string);
83 $string = htmlspecialchars($string, ENT_QUOTES, get_option('blog_charset', 'UTF-8'));
84 $string = str_replace("\n", "", $string);
85 $string = trim($string);
86
87 return $string;
88
89 }
90
91
92
93 // boolval fallback
94 if (!function_exists('boolval')) {
95
96 function boolval($val) {
97
98 return (bool) $val;
99
100 }
101
102 }
103
104
105
106 // inline css
107 function sfs_include_badge_styles() {
108
109 global $sfs_options;
110
111 if (isset($sfs_options['sfs_custom_styles']) && !empty($sfs_options['sfs_custom_styles'])) {
112
113 $sfs_badge_styles = wp_strip_all_tags($sfs_options['sfs_custom_styles']);
114
115 echo '<style type="text/css">'. "\n";
116 echo $sfs_badge_styles . "\n";
117 echo '</style>'. "\n";
118
119 }
120
121 }
122 add_action('wp_head', 'sfs_include_badge_styles');
123
124
125
126 // custom footer content
127 function sfs_feed_content($content) {
128
129 global $sfs_options;
130
131 if (
132 (isset($sfs_options['sfs_feed_content_before']) && !empty($sfs_options['sfs_feed_content_before'])) ||
133 (isset($sfs_options['sfs_feed_content_after']) && !empty($sfs_options['sfs_feed_content_after']))
134 ) {
135
136 if (is_feed()) {
137
138 return $sfs_options['sfs_feed_content_before'] . $content . $sfs_options['sfs_feed_content_after'];
139
140 }
141
142 }
143
144 return $content;
145
146 }
147 add_filter('the_content', 'sfs_feed_content');
148 add_filter('the_excerpt', 'sfs_feed_content');
149
150
151
152 // ip address
153 function sfs_get_ip_address($override = false) {
154
155 global $sfs_options;
156
157 $disable = isset($sfs_options['sfs_disable_ip']) ? $sfs_options['sfs_disable_ip'] : 0;
158
159 if (isset($_SERVER)) {
160
161 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
162 $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
163
164 } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
165 $ip_address = $_SERVER['HTTP_CLIENT_IP'];
166
167 } else {
168 $ip_address = $_SERVER['REMOTE_ADDR'];
169
170 }
171
172 } else {
173
174 if (getenv('HTTP_X_FORWARDED_FOR')) {
175 $ip_address = getenv('HTTP_X_FORWARDED_FOR');
176
177 } elseif (getenv('HTTP_CLIENT_IP')) {
178 $ip_address = getenv('HTTP_CLIENT_IP');
179
180 } else {
181 $ip_address = getenv('REMOTE_ADDR');
182
183 }
184
185 }
186
187 $ip_address = ($disable && !$override) ? esc_html__('IP data disabled', 'simple-feed-stats') : $ip_address;
188
189 return $ip_address;
190
191 }
192
193
194
195 // custom key/value parameter
196 function sfs_custom_parameter() {
197
198 global $sfs_options;
199
200 $custom_key = '';
201 $custom_value = '';
202
203 if (
204 (isset($sfs_options['sfs_custom_key']) && !empty($sfs_options['sfs_custom_key'])) &&
205 (isset($sfs_options['sfs_custom_value']) && !empty($sfs_options['sfs_custom_value']))
206 ) {
207
208 $custom_key = $sfs_options['sfs_custom_key'];
209 $custom_value = $sfs_options['sfs_custom_value'];
210
211 }
212
213 return array($custom_key, $custom_value);
214
215 }
216
217
218
219 // get query-string parameters
220 function sfs_get_query_params() {
221
222 $params = array();
223
224 if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
225
226 $args = explode('&', htmlspecialchars_decode($_SERVER['QUERY_STRING'], ENT_QUOTES));
227
228 foreach($args as $key => $value) {
229
230 $string = explode('=', $value);
231
232 $a = isset($string[0]) ? sfs_clean($string[0]) : null;
233 $b = isset($string[1]) ? sfs_clean($string[1]) : null;
234
235 $params[$a] = $b;
236
237 }
238
239 }
240
241 return $params;
242
243 }
244
245
246
247 // ignored bots
248 function sfs_ignore_bots() {
249
250 $bots = array(
251 'aolbuild',
252 'adsbot-google',
253 'googlebot',
254 'googleproducer',
255 'google-site-verification',
256 'google-test',
257 'mediapartners-google',
258 'baidu',
259 'bingbot',
260 'bingpreview',
261 'duckduckgo',
262 'msnbot',
263 'yandex',
264 'sosospider',
265 'sosoimagespider',
266 'exabot',
267 'sogou',
268 'teoma',
269 'slurp',
270 'yandex',
271 'facebookexternalhit',
272 'feedfetcher-google',
273 );
274
275 return apply_filters('sfs_filter_bots', $bots);
276
277 }
278
279
280
281 /*
282 Default Tracking
283 Tracks all feed requests
284 Recommended for most users
285 */
286 function simple_feed_stats() {
287
288 global $sfs_options, $wpdb;
289
290 if (!is_feed()) return;
291
292 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_default_tracking') {
293
294 $protocol = is_ssl() ? 'https://' : 'http://';
295
296 $host = isset($_SERVER['HTTP_HOST']) ? sfs_clean($_SERVER['HTTP_HOST']) : 'n/a';
297 $request = isset($_SERVER['REQUEST_URI']) ? esc_url_raw($protocol . $host . $_SERVER['REQUEST_URI']) : 'n/a';
298 $referer = isset($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : 'n/a';
299 $qstring = isset($_SERVER['QUERY_STRING']) ? sfs_clean($_SERVER['QUERY_STRING']) : 'n/a';
300 $agent = isset($_SERVER['HTTP_USER_AGENT']) ? sfs_clean($_SERVER['HTTP_USER_AGENT']) : 'n/a';
301
302 if (isset($sfs_options['sfs_ignore_bots']) && $sfs_options['sfs_ignore_bots'] && preg_match('/'. implode('|', sfs_ignore_bots()) .'/i', $agent)) return;
303
304 $address = sfs_get_ip_address();
305 $logtime = sfs_get_date_time();
306
307 $feed_rdf = get_bloginfo('rdf_url');
308 $feed_atom = get_bloginfo('atom_url');
309 $feed_rss2 = get_bloginfo('rss2_url');
310 $feed_coms_atom = get_bloginfo('comments_atom_url');
311 $feed_coms = get_bloginfo('comments_rss2_url');
312
313 if (strpos($request, $feed_rdf) !== false) $type = 'RDF';
314 elseif (strpos($request, $feed_atom) !== false) $type = 'Atom';
315 elseif (strpos($request, $feed_rss2) !== false) $type = 'RSS2';
316 elseif (strpos($request, $feed_coms_atom) !== false) $type = 'Comments';
317 elseif (strpos($request, $feed_coms) !== false) $type = 'Comments';
318 else $type = 'Other';
319
320 $tracking = 'default';
321
322 $table = $wpdb->prefix .'simple_feed_stats';
323
324 $wpdb->insert($table, array(
325
326 'logtime' => $logtime,
327 'request' => $request,
328 'referer' => $referer,
329 'type' => $type,
330 'qstring' => $qstring,
331 'address' => $address,
332 'tracking' => $tracking,
333 'agent' => $agent,
334
335 ), array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'));
336
337 }
338
339 }
340 add_action('wp', 'simple_feed_stats');
341
342
343
344 /*
345 Custom Tracking
346 Tracks via embedded post image
347 Recommended if redirecting your feed to a service like Feedburner
348 */
349 function sfs_feed_tracking($content) {
350
351 global $sfs_options, $wp_query;
352
353 if (!is_feed()) return $content;
354
355 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_custom_tracking') {
356
357 $custom = sfs_custom_parameter();
358
359 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
360 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
361
362 if (get_option('permalink_structure')) {
363
364 $parse = isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI']) : array();
365
366 $path = isset($parse['path']) ? sfs_clean($parse['path']) : '';
367
368 if (strpos($path, '/comments/') !== false) {
369
370 $feed = 'comments';
371
372 } else {
373
374 $feed = 'rss2';
375
376 if (strpos($path, '/feed/rdf') !== false) $feed = 'rdf';
377 elseif (strpos($path, '/feed/atom') !== false) $feed = 'atom';
378
379 }
380
381 } else {
382
383 $feed = isset($_GET['feed']) ? sfs_clean($_GET['feed']) : 'rss2'; // get_query_var('feed') returns incorrect feed vars when permalinks enabled
384
385 if ($feed === 'comments-rss2' || $feed === 'comments-atom') $feed = 'comments';
386
387 }
388
389 $query = array('sfs_tracking' => 'true', 'feed_type' => $feed, $custom_key => $custom_val, 'v' => sfs_randomizer());
390
391 $url = add_query_arg($query, plugins_url('/simple-feed-stats/tracker.php'));
392
393 if (($wp_query->current_post === 0) || ($wp_query->current_comment === 0)) {
394
395 return '<img src="'. esc_url_raw($url) .'" width="1" height="1" alt=""> '. $content;
396
397 }
398
399 }
400
401 return $content;
402
403 }
404 add_filter('the_excerpt_rss', 'sfs_feed_tracking');
405 add_filter('comment_text_rss', 'sfs_feed_tracking');
406 add_filter('comment_text', 'sfs_feed_tracking');
407
408
409
410 /*
411 Alt Tracking
412 Tracks via embedded feed image
413 Experimental tracking method
414 */
415 function sfs_alt_tracking_rdf() {
416
417 global $sfs_options;
418
419 if (!is_feed()) return;
420
421 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') {
422
423 $custom = sfs_custom_parameter();
424
425 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
426 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
427
428 $query = array('sfs_tracking' => 'true', 'feed_type' => 'rdf', $custom_key => $custom_val, 'v' => sfs_randomizer());
429
430 $url = plugins_url('/simple-feed-stats/tracker.php?'. http_build_query($query, '', '&amp;'));
431
432 ?>
433
434 <image rdf:resource="<?php echo esc_url_raw($url); ?>">
435 <title><?php bloginfo_rss('name'); ?></title>
436 <url><?php echo esc_url_raw($url); ?></url>
437 <link><?php bloginfo_rss('url'); ?></link>
438 <description><?php bloginfo('description'); ?></description>
439 </image>
440
441 <?php }
442
443 }
444 add_action('rdf_header', 'sfs_alt_tracking_rdf');
445
446
447
448 function sfs_alt_tracking_rss() {
449
450 global $sfs_options;
451
452 if (!is_feed()) return;
453
454 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') {
455
456 $custom = sfs_custom_parameter();
457
458 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
459 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
460
461 $query = array('sfs_tracking' => 'true', 'feed_type' => 'rss2', $custom_key => $custom_val, 'v' => sfs_randomizer());
462
463 $url = plugins_url('/simple-feed-stats/tracker.php?'. http_build_query($query, '', '&amp;'));
464
465 ?>
466
467 <image>
468 <title><?php bloginfo_rss('name'); ?></title>
469 <url><?php echo esc_url_raw($url); ?></url>
470 <link><?php bloginfo_rss('url'); ?></link>
471 <width>1</width><height>1</height>
472 <description><?php bloginfo('description'); ?></description>
473 </image>
474
475 <?php }
476
477 }
478 add_action('rss2_head', 'sfs_alt_tracking_rss');
479
480
481
482 function sfs_alt_tracking_atom() {
483
484 global $sfs_options;
485
486 if (!is_feed()) return;
487
488 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') {
489
490 $custom = sfs_custom_parameter();
491
492 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
493 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
494
495 $query = array('sfs_tracking' => 'true', 'feed_type' => 'atom', $custom_key => $custom_val, 'v' => sfs_randomizer());
496
497 $url = plugins_url('/simple-feed-stats/tracker.php?'. http_build_query($query, '', '&amp;'));
498
499 ?>
500
501 <icon><?php echo esc_url_raw($url); ?></icon>
502
503 <?php }
504
505 }
506 add_action('atom_head', 'sfs_alt_tracking_atom');
507
508
509
510 function sfs_alt_tracking_comments_rss() {
511
512 global $sfs_options;
513
514 if (!is_feed()) return;
515
516 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') {
517
518 $custom = sfs_custom_parameter();
519
520 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
521 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
522
523 $query = array('sfs_tracking' => 'true', 'feed_type' => 'comments', $custom_key => $custom_val, 'v' => sfs_randomizer());
524
525 $url = plugins_url('/simple-feed-stats/tracker.php?'. http_build_query($query, '', '&amp;'));
526
527 ?>
528
529 <image>
530 <title><?php esc_html_e('Comments for ', 'simple-feed-stats') . bloginfo_rss('name'); ?></title>
531 <url><?php echo esc_url_raw($url); ?></url>
532 <link><?php bloginfo_rss('url'); ?></link>
533 <width>1</width><height>1</height>
534 <description><?php bloginfo('description'); ?></description>
535 </image>
536
537 <?php }
538
539 }
540 add_action('commentsrss2_head', 'sfs_alt_tracking_comments_rss');
541
542
543
544 function sfs_alt_tracking_comments_atom() {
545
546 global $sfs_options;
547
548 if (!is_feed()) return;
549
550 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') {
551
552 $custom = sfs_custom_parameter();
553
554 $custom_key = isset($custom[0]) ? sfs_clean($custom[0]) : 'custom_key';
555 $custom_val = isset($custom[1]) ? sfs_clean($custom[1]) : 'custom_value';
556
557 $query = array('sfs_tracking' => 'true', 'feed_type' => 'comments', $custom_key => $custom_val, 'v' => sfs_randomizer());
558
559 $url = plugins_url('/simple-feed-stats/tracker.php?'. http_build_query($query, '', '&amp;'));
560
561 ?>
562
563 <icon><?php echo esc_url_raw($url); ?></icon>
564
565 <?php }
566
567 }
568 add_action('comments_atom_head', 'sfs_alt_tracking_comments_atom');
569
570
571
572 // sfs feed tracker
573 function sfs_tracker() {
574
575 global $sfs_options, $wpdb;
576
577 $params = sfs_get_query_params();
578
579 if (isset($params['sfs_tracking']) && !empty($params['sfs_tracking'])) {
580
581 $address = sfs_get_ip_address();
582 $logtime = sfs_get_date_time();
583
584 $protocol = is_ssl() ? 'https://' : 'http://';
585
586 $host = isset($_SERVER['HTTP_HOST']) ? sfs_clean($_SERVER['HTTP_HOST']) : 'n/a';
587 $request = isset($_SERVER['REQUEST_URI']) ? esc_url_raw($protocol . $host . $_SERVER['REQUEST_URI']) : 'n/a';
588 $referer = isset($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : 'n/a';
589 $qstring = isset($_SERVER['QUERY_STRING']) ? sfs_clean($_SERVER['QUERY_STRING']) : 'n/a';
590 $agent = isset($_SERVER['HTTP_USER_AGENT']) ? sfs_clean($_SERVER['HTTP_USER_AGENT']) : 'n/a';
591
592 if (isset($sfs_options['sfs_ignore_bots']) && $sfs_options['sfs_ignore_bots'] && preg_match('/'. implode('|', sfs_ignore_bots()) .'/i', $agent)) exit;
593
594 $feed_type = isset($params['feed_type']) ? $params['feed_type'] : 'undefined';
595
596 if (isset($params['sfs_type']) && $params['sfs_type'] === 'open') $feed_type = 'open';
597
598 if ($feed_type == 'rdf') $type = 'RDF';
599 elseif ($feed_type == 'rss2') $type = 'RSS2';
600 elseif ($feed_type == 'atom') $type = 'Atom';
601 elseif ($feed_type == 'comments') $type = 'Comments';
602 elseif ($feed_type == 'open') $type = 'Open';
603 else $type = 'Other';
604
605 $tracking = 'default';
606
607 if (isset($sfs_options['sfs_tracking_method'])) {
608
609 if (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_custom_tracking') $tracking = 'custom';
610 elseif (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_alt_tracking') $tracking = 'alt';
611 elseif (isset($sfs_options['sfs_tracking_method']) && $sfs_options['sfs_tracking_method'] === 'sfs_open_tracking') $tracking = 'open';
612
613 }
614
615 $table = $wpdb->prefix .'simple_feed_stats';
616
617 $insert = $wpdb->insert($table, array(
618
619 'logtime' => $logtime,
620 'request' => $request,
621 'referer' => $referer,
622 'type' => $type,
623 'qstring' => $qstring,
624 'address' => $address,
625 'tracking' => $tracking,
626 'agent' => $agent,
627
628 ), array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'));
629
630 $open_tracker = isset($sfs_options['sfs_open_image_url']) ? $sfs_options['sfs_open_image_url'] : null;
631
632 $tracker_image = ('open' === $tracking && !empty($open_tracker)) ? $open_tracker : plugins_url('/simple-feed-stats/tracker.gif');
633
634 wp_redirect(esc_url_raw($tracker_image));
635
636 exit;
637
638 }
639
640 }
641
642
643
644 // clear cache link
645 function sfs_clear_cache_link() {
646
647 $nonce = wp_create_nonce('sfs_clear_cache');
648
649 $label = esc_html__('Clear cache', 'simple-feed-stats');
650
651 $href = add_query_arg(array('clear-cache-verify' => $nonce, 'cache' => 'clear'), admin_url('options-general.php?page=sfs-options'));
652
653 echo '<a class="sfs-clear-cache" href="'. esc_url($href) .'">'. esc_html($label) .'</a>';
654
655 }
656
657 // clear cache (single site only)
658 function sfs_clear_cache() {
659
660 if (isset($_GET['clear-cache-verify']) && wp_verify_nonce($_GET['clear-cache-verify'], 'sfs_clear_cache')) {
661
662 if (isset($_GET['cache']) && $_GET['cache'] === 'clear') {
663
664 if (current_user_can('activate_plugins')) {
665
666 sfs_delete_transients();
667 sfs_create_transients();
668
669 $location = admin_url('options-general.php?page=sfs-options&clear-cache=true');
670
671 wp_redirect($location);
672
673 exit;
674
675 }
676
677 }
678
679 }
680
681 }
682 add_action('admin_init', 'sfs_clear_cache');
683
684
685
686 // reset stats link
687 function sfs_reset_stats_link() {
688
689 $nonce = wp_create_nonce('sfs_reset_stats');
690
691 $label = esc_html__('Reset stats', 'simple-feed-stats');
692
693 $href = add_query_arg(array('reset-stats-verify' => $nonce, 'reset' => 'true'), admin_url('options-general.php?page=sfs-options'));
694
695 echo '<a class="sfs-reset-stats reset" href="'. esc_url($href) .'">'. esc_html($label) .'</a>';
696
697 }
698
699 // reset stats (single site only)
700 function sfs_reset_stats() {
701
702 if (isset($_GET['reset-stats-verify']) && wp_verify_nonce($_GET['reset-stats-verify'], 'sfs_reset_stats')) {
703
704 if (isset($_GET['reset']) && $_GET['reset'] === 'true') {
705
706 if (current_user_can('activate_plugins')) {
707
708 global $wpdb;
709
710 $truncate = $wpdb->query("TRUNCATE ". $wpdb->prefix ."simple_feed_stats");
711
712 sfs_delete_transients();
713 sfs_create_transients();
714
715 $location = admin_url('options-general.php?page=sfs-options&reset-stats=true');
716
717 wp_redirect($location);
718
719 exit;
720
721 }
722
723 }
724
725 }
726
727 }
728 add_action('admin_init', 'sfs_reset_stats');
729
730
731
732 // delete options (single site only)
733 function sfs_delete_options_on_deactivation() {
734
735 global $sfs_options;
736
737 if (isset($sfs_options['default_options']) && $sfs_options['default_options']) {
738
739 delete_option('sfs_options');
740 delete_option('sfs_alert');
741 delete_option('sfs_version');
742
743 }
744
745 }
746
747
748
749 // delete stats table (single site only)
750 function sfs_delete_table_on_deactivation() {
751
752 global $sfs_options, $wpdb;
753
754 if (isset($sfs_options['sfs_delete_table']) && $sfs_options['sfs_delete_table']) {
755
756 $result = $wpdb->query("DROP TABLE ". $wpdb->prefix ."simple_feed_stats");
757
758 sfs_delete_transients();
759
760 }
761
762 }
763
764
765
766 // date/time
767 function sfs_get_date_time() {
768
769 $date_format = get_option('date_format');
770
771 $time_format = get_option('time_format');
772
773 if (function_exists('current_datetime')) {
774
775 $format = $date_format .' @ '. $time_format;
776
777 $date = current_datetime()->format($format);
778
779 } else {
780
781 $date = date("{$date_format} {$time_format}", current_time('timestamp'));
782
783 }
784
785 return apply_filters('sfs_get_date_time', $date);
786
787 }
788
789
790
791 // cron: add on activate (supports multisite network activate)
792 function sfs_cron_activation() {
793
794 global $wpdb;
795
796 if (is_multisite() && is_network_admin()) {
797
798 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
799
800 if ($blog_ids) {
801
802 foreach ($blog_ids as $blog_id) {
803
804 switch_to_blog($blog_id);
805
806 if (!wp_next_scheduled('sfs_create_transients')) {
807
808 wp_schedule_event(time(), 'twicedaily', 'sfs_create_transients');
809
810 }
811
812 restore_current_blog();
813
814 }
815
816 }
817
818 } else {
819
820 if (!wp_next_scheduled('sfs_create_transients')) {
821
822 wp_schedule_event(time(), 'twicedaily', 'sfs_create_transients');
823
824 }
825
826 }
827
828 }
829 register_activation_hook(__FILE__, 'sfs_cron_activation');
830
831
832
833 // cron: remove on deactivate (supports multisite network activate)
834 function sfs_cron_cleanup() {
835
836 global $wpdb;
837
838 if (is_multisite() && is_network_admin()) {
839
840 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
841
842 if ($blog_ids) {
843
844 foreach ($blog_ids as $blog_id) {
845
846 switch_to_blog($blog_id);
847
848 $timestamp = wp_next_scheduled('sfs_create_transients');
849
850 wp_unschedule_event($timestamp,'sfs_create_transients');
851
852 restore_current_blog();
853
854 }
855
856 }
857
858 } else {
859
860 $timestamp = wp_next_scheduled('sfs_create_transients');
861
862 wp_unschedule_event($timestamp,'sfs_create_transients');
863
864 }
865
866 }
867 register_deactivation_hook(__FILE__, 'sfs_cron_cleanup');
868
869
870
871 // delete transients
872 function sfs_delete_transients() {
873
874 delete_transient('all_count');
875 delete_transient('feed_count');
876 delete_transient('rss2_count');
877 delete_transient('comments_count');
878
879 delete_transient('_transient_all_count');
880 delete_transient('_transient_feed_count');
881 delete_transient('_transient_rss2_count');
882 delete_transient('_transient_comments_count');
883
884 delete_transient('_transient_timeout_all_count');
885 delete_transient('_transient_timeout_feed_count');
886 delete_transient('_transient_timeout_rss2_count');
887 delete_transient('_transient_timeout_comment_count');
888
889 }
890
891
892
893 // create transients
894 function sfs_create_transients() {
895
896 global $sfs_options, $wpdb;
897
898 $all_stats = $daily_stats = $rss2_stats = $comment_stats = 0;
899
900 $count = (isset($sfs_options['sfs_strict_stats']) && $sfs_options['sfs_strict_stats']) ? 'COUNT(DISTINCT address)' : 'COUNT(*)';
901
902 $table_name = $wpdb->prefix .'simple_feed_stats';
903
904 $check_table = $wpdb->get_var("SHOW TABLES LIKE '$table_name'");
905
906 if ($check_table === $table_name) {
907
908 // all stats
909 $all_stats_query = "SELECT ". $count ." FROM ". $table_name;
910
911 $all_stats = $wpdb->get_row($all_stats_query, ARRAY_A);
912
913 $all_stats = is_array($all_stats) ? $all_stats[$count] : 0;
914
915
916 // daily stats
917 $daily_stats_query = "SELECT ". $count ." FROM ". $table_name ." WHERE cur_timestamp BETWEEN TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND NOW()";
918
919 $daily_stats = $wpdb->get_row($daily_stats_query, ARRAY_A);
920
921 $daily_stats = is_array($daily_stats) ? $daily_stats[$count] : 0;
922
923
924 // daily stats RSS2 only
925 $rss2_stats_query = "SELECT ". $count ." FROM ". $table_name ." WHERE type='RSS2' AND cur_timestamp BETWEEN TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND NOW()";
926
927 $rss2_stats = $wpdb->get_row($rss2_stats_query, ARRAY_A);
928
929 $rss2_stats = is_array($rss2_stats) ? $rss2_stats[$count] : 0;
930
931
932 // daily comment stats
933 $comment_stats_query = "SELECT ". $count ." FROM ". $table_name ." WHERE type='Comments' AND cur_timestamp BETWEEN TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND NOW()";
934
935 $comment_stats = $wpdb->get_row($comment_stats_query, ARRAY_A);
936
937 $comment_stats = is_array($comment_stats) ? $comment_stats[$count] : 0;
938
939 }
940
941 $duration = 60*60*24; // 12 hour cache 60*60*12 // 24 hour cache = 60*60*24
942
943 set_transient('all_count', $all_stats, $duration);
944 set_transient('feed_count', $daily_stats, $duration);
945 set_transient('rss2_count', $rss2_stats, $duration);
946 set_transient('comment_count', $comment_stats, $duration);
947
948 }
949 add_action('sfs_create_transients', 'sfs_create_transients');
950
951
952
953 // query database for stats
954 function sfs_query_database($query) {
955
956 global $sfs_options, $wpdb;
957
958 $table = $wpdb->prefix .'simple_feed_stats';
959
960 $count = (isset($sfs_options['sfs_strict_stats']) && $sfs_options['sfs_strict_stats']) ? 'COUNT(DISTINCT address)' : 'COUNT(*)';
961
962 $range = ($query === 'current_stats') ? " AND cur_timestamp BETWEEN TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND NOW()" : "";
963
964 $rdf_query = "SELECT ". $count ." FROM ". $table ." WHERE type='RDF' ". $range;
965 $rss2_query = "SELECT ". $count ." FROM ". $table ." WHERE type='RSS2' ". $range;
966 $atom_query = "SELECT ". $count ." FROM ". $table ." WHERE type='Atom' ". $range;
967 $comments_query = "SELECT ". $count ." FROM ". $table ." WHERE type='Comments' ". $range;
968 $open_query = "SELECT ". $count ." FROM ". $table ." WHERE type='Open' ". $range;
969 $other_query = "SELECT ". $count ." FROM ". $table ." WHERE type='Other' ". $range;
970
971 $rdf = $wpdb->get_row($rdf_query, ARRAY_A);
972 $rdf = is_array($rdf) ? $rdf[$count] : 0;
973
974 $rss2 = $wpdb->get_row($rss2_query, ARRAY_A);
975 $rss2 = is_array($rss2) ? $rss2[$count] : 0;
976
977 $atom = $wpdb->get_row($atom_query, ARRAY_A);
978 $atom = is_array($atom) ? $atom[$count] : 0;
979
980 $comments = $wpdb->get_row($comments_query, ARRAY_A);
981 $comments = is_array($comments) ? $comments[$count] : 0;
982
983 $open = $wpdb->get_row($open_query, ARRAY_A);
984 $open = is_array($open) ? $open[$count] : 0;
985
986 $other = $wpdb->get_row($other_query, ARRAY_A);
987 $other = is_array($other) ? $other[$count] : 0;
988
989 $stats = array($rdf, $rss2, $atom, $comments, $open, $other);
990
991 return $stats;
992
993 }