PluginProbe
Statify / 0.8
Statify v0.8
2.0.2 2.0.1 trunk 0.7 0.8 0.9 1.0 1.2.8 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 All 32 releases
statify / statify.original.php

statify.original.php in Statify 0.8, at statify.original.php

1,017 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Statify
4 Description: Kompakte, begreifliche und datenschutzkonforme Statistik für WordPress.
5 Author: Sergej M&uuml;ller
6 Author URI: http://wpseo.de
7 Plugin URI: http://playground.ebiene.de/statify-wordpress-statistik/
8 Version: 0.8
9 */
10
11
12 /* Sicherheitsabfrage */
13 if ( !class_exists('WP') ) {
14 header('Status: 403 Forbidden');
15 header('HTTP/1.1 403 Forbidden');
16 exit();
17 }
18
19
20 /**
21 * Statify
22 *
23 * @since 0.1
24 */
25
26 class Statify
27 {
28
29
30 /* Save me */
31 private static $base;
32 private static $stats;
33 private static $days = 14;
34 private static $limit = 3;
35 private static $today = 0;
36
37
38 /**
39 * Konstruktor der Klasse
40 *
41 * @since 0.1
42 * @change 0.6
43 */
44
45 public static function init()
46 {
47 /* Filter */
48 if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) or (defined('DOING_CRON') && DOING_CRON) or (defined('DOING_AJAX') && DOING_AJAX) or (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ) {
49 return;
50 }
51
52 /* Plugin-Base */
53 self::$base = plugin_basename(__FILE__);
54
55 /* Tabelle Init */
56 Statify_Table::init();
57
58 /* BE */
59 if ( is_admin() ) {
60 add_action(
61 'wpmu_new_blog',
62 array(
63 __CLASS__,
64 'install_later'
65 )
66 );
67 add_action(
68 'delete_blog',
69 array(
70 __CLASS__,
71 'uninstall_later'
72 )
73 );
74 add_action(
75 'wp_dashboard_setup',
76 array(
77 __CLASS__,
78 'init_dashboard'
79 )
80 );
81 add_filter(
82 'plugin_row_meta',
83 array(
84 __CLASS__,
85 'init_meta'
86 ),
87 10,
88 2
89 );
90 add_filter(
91 'plugin_action_links_' .self::$base,
92 array(
93 __CLASS__,
94 'init_action'
95 )
96 );
97
98 /* FE */
99 } else {
100 add_action(
101 'template_redirect',
102 array(
103 __CLASS__,
104 'db_push'
105 )
106 );
107 }
108 }
109
110
111 /**
112 * Hinzufügen der Action-Links (Einstellungen links)
113 *
114 * @since 0.8
115 * @change 0.8
116 */
117
118 public static function init_action($data)
119 {
120 /* Rechte? */
121 if ( !current_user_can('manage_options') ) {
122 return $data;
123 }
124
125 return array_merge(
126 $data,
127 array(
128 sprintf(
129 '<a href="%s">%s</a>',
130 add_query_arg(
131 array(
132 'edit' => 'statify_dashboard#statify_dashboard'
133 ),
134 admin_url('/')
135 ),
136 __('Settings')
137 )
138 )
139 );
140 }
141
142
143 /**
144 * Links in der Plugins-Verwaltung
145 *
146 * @since 0.5
147 * @change 0.8
148 *
149 * @param array $links Array mit Links
150 * @param string $file Name des Plugins
151 * @return array $links Array mit erweitertem Link
152 */
153
154 public static function init_meta($links, $file)
155 {
156 if ( self::$base == $file ) {
157 return array_merge(
158 $links,
159 array(
160 '<a href="http://flattr.com/thing/148966/Statify-Plugin-fur-Datenschutz-konforme-Statistik-in-WordPress" target="_blank">Plugin flattern</a>',
161 '<a href="https://plus.google.com/110569673423509816572" target="_blank">Auf Google+ folgen</a>'
162 )
163 );
164 }
165
166 return $links;
167 }
168
169
170 /**
171 * Anzeige des Dashboard-Widgets
172 *
173 * @since 0.1
174 * @change 0.8
175 */
176
177 public static function init_dashboard()
178 {
179 /* Filter */
180 if ( !current_user_can('level_2') ) {
181 return;
182 }
183
184 /* Vorbereiten */
185 self::_prepare_stats();
186
187 /* Widget */
188 wp_add_dashboard_widget(
189 'statify_dashboard',
190 'Statify',
191 array(
192 __CLASS__,
193 'front_view'
194 ),
195 array(
196 __CLASS__,
197 'back_view'
198 )
199 );
200
201 /* CSS laden */
202 add_action(
203 'admin_print_styles',
204 array(
205 __CLASS__,
206 'add_style'
207 )
208 );
209
210 /* JS laden */
211 add_action(
212 'admin_print_scripts',
213 array(
214 __CLASS__,
215 'add_js'
216 )
217 );
218 }
219
220
221 /**
222 * Ausgabe der Stylesheets
223 *
224 * @since 0.1
225 * @change 0.8
226 */
227
228 public static function add_style()
229 {
230 /* PLugin-Info */
231 $plugin = get_plugin_data(__FILE__);
232
233 /* CSS registrieren */
234 wp_register_style(
235 'statify',
236 plugins_url('/css/dashboard.css', __FILE__),
237 array(),
238 $plugin['Version']
239 );
240
241 /* CSS ausgeben */
242 wp_enqueue_style('statify');
243 }
244
245
246 /**
247 * Ausgabe von JavaScript
248 *
249 * @since 0.1
250 * @change 0.6
251 */
252
253 public static function add_js() {
254 /* Leer? */
255 if ( (!$stats = self::$stats) or empty($stats['visits']) ) {
256 return;
257 }
258
259 /* PLugin-Info */
260 $plugin = get_plugin_data(__FILE__);
261
262 /* Registrieren */
263 wp_register_script(
264 'statify',
265 plugins_url('/js/dashboard.js', __FILE__),
266 array(),
267 $plugin['Version']
268 );
269 wp_register_script(
270 'google_jsapi',
271 'http://www.google.com/jsapi',
272 false
273 );
274
275 /* Einbinden */
276 wp_enqueue_script('google_jsapi');
277 wp_enqueue_script('statify');
278
279 /* Übergeben */
280 wp_localize_script(
281 'statify',
282 'statify',
283 $stats['visits']
284 );
285 }
286
287
288 /**
289 * Rückgabe der Optionen
290 *
291 * @since 0.4
292 * @change 0.8
293 *
294 * @return array $options Array mit Optionen
295 */
296
297 private static function get_options()
298 {
299 /* Im Cache */
300 if ( $options = wp_cache_get('statify') ) {
301 return $options;
302 }
303
304 /* Zusammenführen */
305 $options = wp_parse_args(
306 get_option('statify'),
307 array(
308 'days' => self::$days,
309 'limit' => self::$limit,
310 'today' => self::$today
311 )
312 );
313
314 /* Ins Cache */
315 wp_cache_set(
316 'statify',
317 $options
318 );
319
320 return $options;
321 }
322
323
324 /**
325 * Ausgabe der Frontseite
326 *
327 * @since 0.1
328 * @change 0.6
329 */
330
331 public static function front_view()
332 {
333 /* Leer? */
334 if ( (!$stats = self::$stats) or empty($stats['visits']) ) {
335 echo '<p>Heutige Werte werden erst gesammelt.</p>';
336 return;
337 } ?>
338
339 <div id="statify_chart"></div>
340
341 <?php if ( !empty($stats['target']) ) { ?>
342 <div class="table referrer">
343 <p class="sub">Top Referrer</p>
344 <div>
345 <table>
346 <?php if ( empty($stats['referrer']) ) { ?>
347 <tr>
348 <td>
349 Keine
350 </td>
351 </tr>
352 <?php } else { ?>
353 <?php foreach ($stats['referrer'] as $referrer) { ?>
354 <tr class="first">
355 <td class="first b">
356 <a href="<?php echo esc_url($referrer['url']) ?>" target="_blank"><?php echo intval($referrer['count']) ?></a>
357 </td>
358 <td class="t">
359 <a href="<?php echo esc_url($referrer['url']) ?>" target="_blank"><?php echo esc_url($referrer['host']) ?></a>
360 </td>
361 </tr>
362 <?php } ?>
363 <?php } ?>
364 </table>
365 </div>
366 </div>
367
368 <div class="table target">
369 <p class="sub">Top Ziele</p>
370 <div>
371 <table>
372 <?php foreach ($stats['target'] as $target) { ?>
373 <tr class="first">
374 <td class="b">
375 <a href="<?php echo esc_url($target['url']) ?>" target="_blank"><?php echo intval($target['count']) ?></a>
376 </td>
377 <td class="last t">
378 <a href="<?php echo home_url($target['url']) ?>" target="_blank"><?php echo esc_url($target['url']) ?></a>
379 </td>
380 </tr>
381 <?php } ?>
382 </table>
383 </div>
384 </div>
385 <?php } ?>
386 <?php }
387
388
389 /**
390 * Ausgabe der Backseite
391 *
392 * @since 0.4
393 * @change 0.8
394 */
395
396 public static function back_view()
397 {
398 /* Rechte */
399 if ( !current_user_can('manage_options') ) {
400 return;
401 }
402
403 /* Speichern */
404 if ( !empty($_POST['statify']) ) {
405 /* Formular-Referer */
406 check_admin_referer('_statify');
407
408 /* Save */
409 update_option(
410 'statify',
411 array(
412 'days' => (int)@$_POST['statify']['days'],
413 'limit' => (int)@$_POST['statify']['limit'],
414 'today' => (int)@$_POST['statify']['today']
415 )
416 );
417
418 /* Entleeren */
419 delete_transient('statify');
420 }
421
422 /* Optionen */
423 $options = self::get_options();
424
425 /* Security */
426 wp_nonce_field('_statify'); ?>
427
428 <table class="form-table">
429 <tr>
430 <td>
431 <select name="statify[days]" id="statify_days">
432 <?php foreach(array(7, 10, 14, 20, 21, 28, 30) as $num) { ?>
433 <option <?php selected($options['days'], $num); ?>><?php echo $num; ?></option>
434 <?php } ?>
435 </select>
436 <label for="statify_days">Anzahl der Tage für Statistiken</label>
437 </td>
438 </tr>
439
440 <tr>
441 <td>
442 <select name="statify[limit]" id="statify_limit">
443 <?php foreach(range(0, 12) as $num) { ?>
444 <option <?php selected($options['limit'], $num); ?>><?php echo $num; ?></option>
445 <?php } ?>
446 </select>
447 <label for="statify_limit">Anzahl der Einträge in Listen</label>
448 </td>
449 </tr>
450
451 <tr>
452 <td>
453 <input type="checkbox" name="statify[today]" id="statify_today" value="1" <?php checked($options['today'], 1) ?> />
454 <label for="statify_today">Referrer und Ziele nur vom aktuellen Tag zeigen</label>
455 </td>
456 </tr>
457 </table>
458
459 <?php
460 }
461
462
463 /**
464 * Installation des Plugins auch für MU-Blogs
465 *
466 * @since 0.1
467 * @change 0.6
468 */
469
470 public static function install()
471 {
472 /* Global */
473 global $wpdb;
474
475 /* Multisite & Network */
476 if ( is_multisite() && !empty($_GET['networkwide']) ) {
477 /* Blog-IDs */
478 $ids = $wpdb->get_col(
479 $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
480 );
481
482 /* Loopen */
483 foreach ($ids as $id) {
484 switch_to_blog( (int)$id );
485 self::_install_backend();
486 }
487
488 /* Wechsel zurück */
489 restore_current_blog();
490
491 } else {
492 self::_install_backend();
493 }
494 }
495
496
497 /**
498 * Installation des Plugins bei einem neuen MU-Blog
499 *
500 * @since 0.6
501 * @change 0.6
502 */
503
504 public static function install_later($id) {
505 /* Kein Netzwerk-Plugin */
506 if ( !is_plugin_active_for_network(self::$base) ) {
507 return;
508 }
509
510 /* Wechsel */
511 switch_to_blog( (int)$id );
512
513 /* Installieren */
514 self::_install_backend();
515
516 /* Wechsel zurück */
517 restore_current_blog();
518 }
519
520
521 /**
522 * Eigentliche Installation der Option und der Tabelle
523 *
524 * @since 0.6
525 * @change 0.6
526 */
527
528 private static function _install_backend()
529 {
530 /* Option */
531 add_option(
532 'statify',
533 array(),
534 '',
535 'no'
536 );
537
538 /* Reset */
539 delete_transient('statify');
540
541 /* Tabelle setzen */
542 Statify_Table::init();
543
544 /* Tabelle anlegen */
545 Statify_Table::create();
546 }
547
548
549 /**
550 * Uninstallation des Plugins pro MU-Blog
551 *
552 * @since 0.6
553 * @change 0.6
554 */
555
556 public static function uninstall()
557 {
558 /* Global */
559 global $wpdb;
560
561 /* Multisite & Network */
562 if ( is_multisite() && !empty($_GET['networkwide']) ) {
563 /* Alter Blog */
564 $old = $wpdb->blogid;
565
566 /* Blog-IDs */
567 $ids = $wpdb->get_col(
568 $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
569 );
570
571 /* Loopen */
572 foreach ($ids as $id) {
573 switch_to_blog($id);
574 self::_uninstall_backend();
575 }
576
577 /* Wechsel zurück */
578 switch_to_blog($old);
579 } else {
580 self::_uninstall_backend();
581 }
582 }
583
584
585 /**
586 * Uninstallation des Plugins bei MU & Network-Plugin
587 *
588 * @since 0.6
589 * @change 0.6
590 */
591
592 public static function uninstall_later($id) {
593 /* Kein Netzwerk-Plugin */
594 if ( !is_plugin_active_for_network(self::$base) ) {
595 return;
596 }
597
598 /* Wechsel */
599 switch_to_blog( (int)$id );
600
601 /* Installieren */
602 self::_uninstall_backend();
603
604 /* Wechsel zurück */
605 restore_current_blog();
606 }
607
608
609 /**
610 * Eigentliche Deinstallation des Plugins
611 *
612 * @since 0.6
613 * @change 0.6
614 */
615
616 private static function _uninstall_backend()
617 {
618 /* Option */
619 delete_option('statify');
620
621 /* Transient */
622 delete_transient('statify');
623
624 /* Tabelle setzen */
625 Statify_Table::init();
626
627 /* Tabelle anlegen */
628 Statify_Table::drop();
629 }
630
631
632 /**
633 * Update des Plugins
634 *
635 * @since 0.6
636 * @change 0.6
637 */
638
639 public static function update()
640 {
641 /* Updaten */
642 self::_update_backend();
643 }
644
645
646 /**
647 * Eigentlicher Update des Plugins
648 *
649 * @since 0.6
650 * @change 0.6
651 */
652
653 private static function _update_backend()
654 {
655 /* Transient */
656 delete_transient('statify');
657 }
658
659
660
661 /**
662 * Speicherung der Werte in die DB
663 *
664 * @since 0.1
665 * @change 0.6
666 */
667
668 public static function db_push()
669 {
670 /* Filter */
671 if ( is_feed() or is_trackback() or is_robots() or is_preview() or is_user_logged_in() or is_404() or self::_is_bot() ) {
672 return;
673 }
674
675 /* Global */
676 global $wpdb, $wp_rewrite;
677
678 /* Init */
679 $data = array();
680 $home = home_url();
681
682 /* Timestamp */
683 $data['created'] = strftime('%Y-%m-%d', current_time('timestamp'));
684
685 /* Referrer */
686 if ( !empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $home) === false ) {
687 $data['referrer'] = esc_url_raw($_SERVER['HTTP_REFERER']);
688 }
689
690 /* Referrer */
691 $data['target'] = str_replace(
692 $home,
693 '',
694 esc_url_raw(
695 home_url(
696 ( empty($_SERVER['REQUEST_URI']) ? '/' : $_SERVER['REQUEST_URI'] )
697 )
698 )
699 );
700
701 /* Parameter entfernen */
702 if ( $wp_rewrite->permalink_structure && !is_search() ) {
703 $data['target'] = preg_replace('/\?.*/', '', $data['target']);
704 }
705
706 /* Insert */
707 $wpdb->insert(
708 $wpdb->statify,
709 $data
710 );
711 }
712
713
714 /**
715 * Prüfung auf Bots
716 *
717 * @since 0.1
718 * @change 0.6
719 *
720 * @return boolean TRUE, wenn Bot
721 */
722
723 private static function _is_bot()
724 {
725 /* Leer? */
726 if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
727 return true;
728 }
729
730 /* Parsen */
731 if ( !preg_match('/(?:Windows|Mac OS X|Macintosh|Linux)/', $_SERVER['HTTP_USER_AGENT']) ) {
732 return true;
733 }
734
735 return false;
736 }
737
738
739 /**
740 * Bereinigung der veralteten Werte
741 *
742 * @since 0.3
743 * @change 0.6
744 */
745
746 private static function _clean_data()
747 {
748 /* Überspringen? */
749 if ( get_transient('statify_cron') ) {
750 return;
751 }
752
753 /* Global */
754 global $wpdb;
755
756 /* Optionen */
757 $options = self::get_options();
758
759 /* Löschen */
760 $wpdb->query(
761 $wpdb->prepare(
762 "DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(CURDATE(), %d)",
763 $options['days']
764 )
765 );
766
767 /* DB optimieren */
768 $wpdb->query(
769 "OPTIMIZE TABLE `$wpdb->statify`"
770 );
771
772 /* Merken */
773 set_transient(
774 'statify_cron',
775 'ilovesweta',
776 60 * 60 * 12
777 );
778 }
779
780
781 /**
782 * Lesevorgang der Datenbank
783 *
784 * @since 0.1
785 * @change 0.8
786 *
787 * @return array Array mit ausgelesenen Daten
788 */
789
790 private static function _get_stats()
791 {
792 /* GLobal */
793 global $wpdb;
794
795 /* Optionen */
796 $options = self::get_options();
797
798 return array(
799 'visits' => $wpdb->get_results(
800 $wpdb->prepare(
801 "SELECT DATE_FORMAT(`created`, '%%d.%%m') as `date`, COUNT(`created`) as `count` FROM `$wpdb->statify` GROUP BY `created` ORDER BY `created` DESC LIMIT %d",
802 $options['days']
803 ),
804 ARRAY_A
805 ),
806 'target' => $wpdb->get_results(
807 $wpdb->prepare(
808 sprintf(
809 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
810 ( $options['today'] ? 'WHERE created = DATE(NOW())' : '' ),
811 $options['limit']
812 )
813 ),
814 ARRAY_A
815 ),
816 'referrer' => $wpdb->get_results(
817 $wpdb->prepare(
818 sprintf(
819 "SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' %s GROUP BY `host` ORDER BY `count` DESC LIMIT %d",
820 ( $options['today'] ? 'AND created = DATE(NOW())' : '' ),
821 $options['limit']
822 )
823 ),
824 ARRAY_A
825 )
826 );
827 }
828
829
830 /**
831 * Verarbeitung und Caching der DB-Werte
832 *
833 * @since 0.1
834 * @change 0.7
835 */
836
837 private static function _prepare_stats()
838 {
839 /* Im Cache? */
840 if ( ($stats = get_transient('statify')) && self::$stats = $stats ) {
841 return;
842 }
843
844 /* Bereinigen */
845 self::_clean_data();
846
847 /* Daten holen */
848 if ( !$stats = self::_get_stats() ) {
849 return;
850 }
851
852 /* Zwischenspeichern */
853 if ( !$visits = $stats['visits'] ) {
854 return;
855 }
856
857 /* Heute? */
858 if ( $visits[0]['date'] == date('d.m', current_time('timestamp')) ) {
859 $visits[0]['date'] = 'Heute';
860 }
861
862 /* Init */
863 $output = array(
864 'created' => array(),
865 'count' => array()
866 );
867
868 /* Zeilen loopen */
869 foreach($visits as $item) {
870 array_push($output['created'], $item['date']);
871 array_push($output['count'], $item['count']);
872 }
873
874 /* Zusammenfassen */
875 $stats['visits'] = array(
876 'created' => implode(',', $output['created']),
877 'count' => implode(',', $output['count'])
878 );
879
880 /* Cache */
881 set_transient(
882 'statify',
883 $stats,
884 60 * 15 // 15 Minuten
885 );
886
887 /* Lokal speichern */
888 self::$stats = $stats;
889 }
890 }
891
892
893 /**
894 * Statify Table
895 *
896 * @since 0.6
897 */
898
899 class Statify_Table
900 {
901
902
903 /**
904 * Definition der Tabelle
905 *
906 * @since 0.6
907 * @change 0.6
908 */
909
910 public function init()
911 {
912 /* Global */
913 global $wpdb;
914
915 /* Name */
916 $table = 'statify';
917
918 /* Als Array */
919 $wpdb->tables[] = $table;
920
921 /* Mit Prefix */
922 $wpdb->$table = $wpdb->get_blog_prefix() . $table;
923 }
924
925
926 /**
927 * Anlegen der Tabelle
928 *
929 * @since 0.6
930 * @change 0.6
931 */
932
933 public function create()
934 {
935 /* Global */
936 global $wpdb;
937
938 /* Existenz prüfen */
939 if ( $wpdb->get_var("SHOW TABLES LIKE '$wpdb->statify'") == $wpdb->statify ) {
940 return;
941 }
942
943 /* Einbinden */
944 require_once(ABSPATH. 'wp-admin/includes/upgrade.php');
945
946 /* Anlegen */
947 dbDelta(
948 "CREATE TABLE `$wpdb->statify` (
949 `id` bigint(20) unsigned NOT NULL auto_increment,
950 `created` date NOT NULL default '0000-00-00',
951 `referrer` varchar(255) NOT NULL default '',
952 `target` varchar(255) NOT NULL default '',
953 PRIMARY KEY (`id`),
954 KEY `referrer` (`referrer`),
955 KEY `target` (`target`),
956 KEY `created` (`created`)
957 );"
958 );
959 }
960
961
962 /**
963 * Löschung der Tabelle
964 *
965 * @since 0.6
966 * @change 0.6
967 */
968
969 public function drop()
970 {
971 /* Global */
972 global $wpdb;
973
974 /* Remove */
975 $wpdb->query("DROP TABLE IF EXISTS `$wpdb->statify`");
976 }
977 }
978
979
980 /* Fire */
981 add_action(
982 'plugins_loaded',
983 array(
984 'Statify',
985 'init'
986 )
987 );
988
989
990 /* Install */
991 register_activation_hook(
992 __FILE__,
993 array(
994 'Statify',
995 'install'
996 )
997 );
998
999 /* Uninstall */
1000 register_uninstall_hook(
1001 __FILE__,
1002 array(
1003 'Statify',
1004 'uninstall'
1005 )
1006 );
1007
1008 /* Update */
1009 if ( function_exists('register_update_hook') ) {
1010 register_update_hook(
1011 __FILE__,
1012 array(
1013 'Statify',
1014 'update'
1015 )
1016 );
1017 }