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.php

statify.php in Statify 0.8, at statify.php

570 lines 11.3 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 if ( !class_exists('WP') ) {
13 header('Status: 403 Forbidden');
14 header('HTTP/1.1 403 Forbidden');
15 exit();
16 }
17 class Statify
18 {
19 private static $base;
20 private static $stats;
21 private static $days = 14;
22 private static $limit = 3;
23 private static $today = 0;
24 public static function init()
25 {
26 if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) or (defined('DOING_CRON') && DOING_CRON) or (defined('DOING_AJAX') && DOING_AJAX) or (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ) {
27 return;
28 }
29 self::$base = plugin_basename(__FILE__);
30 Statify_Table::init();
31 if ( is_admin() ) {
32 add_action(
33 'wpmu_new_blog',
34 array(
35 __CLASS__,
36 'install_later'
37 )
38 );
39 add_action(
40 'delete_blog',
41 array(
42 __CLASS__,
43 'uninstall_later'
44 )
45 );
46 add_action(
47 'wp_dashboard_setup',
48 array(
49 __CLASS__,
50 'init_dashboard'
51 )
52 );
53 add_filter(
54 'plugin_row_meta',
55 array(
56 __CLASS__,
57 'init_meta'
58 ),
59 10,
60 2
61 );
62 add_filter(
63 'plugin_action_links_' .self::$base,
64 array(
65 __CLASS__,
66 'init_action'
67 )
68 );
69 } else {
70 add_action(
71 'template_redirect',
72 array(
73 __CLASS__,
74 'db_push'
75 )
76 );
77 }
78 }
79 public static function init_action($data)
80 {
81 if ( !current_user_can('manage_options') ) {
82 return $data;
83 }
84 return array_merge(
85 $data,
86 array(
87 sprintf(
88 '<a href="%s">%s</a>',
89 add_query_arg(
90 array(
91 'edit' => 'statify_dashboard#statify_dashboard'
92 ),
93 admin_url('/')
94 ),
95 __('Settings')
96 )
97 )
98 );
99 }
100 public static function init_meta($links, $file)
101 {
102 if ( self::$base == $file ) {
103 return array_merge(
104 $links,
105 array(
106 '<a href="http://flattr.com/thing/148966/Statify-Plugin-fur-Datenschutz-konforme-Statistik-in-WordPress" target="_blank">Plugin flattern</a>',
107 '<a href="https://plus.google.com/110569673423509816572" target="_blank">Auf Google+ folgen</a>'
108 )
109 );
110 }
111 return $links;
112 }
113 public static function init_dashboard()
114 {
115 if ( !current_user_can('level_2') ) {
116 return;
117 }
118 self::_prepare_stats();
119 wp_add_dashboard_widget(
120 'statify_dashboard',
121 'Statify',
122 array(
123 __CLASS__,
124 'front_view'
125 ),
126 array(
127 __CLASS__,
128 'back_view'
129 )
130 );
131 add_action(
132 'admin_print_styles',
133 array(
134 __CLASS__,
135 'add_style'
136 )
137 );
138 add_action(
139 'admin_print_scripts',
140 array(
141 __CLASS__,
142 'add_js'
143 )
144 );
145 }
146 public static function add_style()
147 {
148 $plugin = get_plugin_data(__FILE__);
149 wp_register_style(
150 'statify',
151 plugins_url('/css/dashboard.css', __FILE__),
152 array(),
153 $plugin['Version']
154 );
155 wp_enqueue_style('statify');
156 }
157 public static function add_js() {
158 if ( (!$stats = self::$stats) or empty($stats['visits']) ) {
159 return;
160 }
161 $plugin = get_plugin_data(__FILE__);
162 wp_register_script(
163 'statify',
164 plugins_url('/js/dashboard.js', __FILE__),
165 array(),
166 $plugin['Version']
167 );
168 wp_register_script(
169 'google_jsapi',
170 'http://www.google.com/jsapi',
171 false
172 );
173 wp_enqueue_script('google_jsapi');
174 wp_enqueue_script('statify');
175 wp_localize_script(
176 'statify',
177 'statify',
178 $stats['visits']
179 );
180 }
181 private static function get_options()
182 {
183 if ( $options = wp_cache_get('statify') ) {
184 return $options;
185 }
186 $options = wp_parse_args(
187 get_option('statify'),
188 array(
189 'days'=> self::$days,
190 'limit'=> self::$limit,
191 'today' => self::$today
192 )
193 );
194 wp_cache_set(
195 'statify',
196 $options
197 );
198 return $options;
199 }
200 public static function front_view()
201 {
202 if ( (!$stats = self::$stats) or empty($stats['visits']) ) {
203 echo '<p>Heutige Werte werden erst gesammelt.</p>';
204 return;
205 } ?>
206 <div id="statify_chart"></div>
207 <?php if ( !empty($stats['target']) ) { ?>
208 <div class="table referrer">
209 <p class="sub">Top Referrer</p>
210 <div>
211 <table>
212 <?php if ( empty($stats['referrer']) ) { ?>
213 <tr>
214 <td>
215 Keine
216 </td>
217 </tr>
218 <?php } else { ?>
219 <?php foreach ($stats['referrer'] as $referrer) { ?>
220 <tr class="first">
221 <td class="first b">
222 <a href="<?php echo esc_url($referrer['url']) ?>" target="_blank"><?php echo intval($referrer['count']) ?></a>
223 </td>
224 <td class="t">
225 <a href="<?php echo esc_url($referrer['url']) ?>" target="_blank"><?php echo esc_url($referrer['host']) ?></a>
226 </td>
227 </tr>
228 <?php } ?>
229 <?php } ?>
230 </table>
231 </div>
232 </div>
233 <div class="table target">
234 <p class="sub">Top Ziele</p>
235 <div>
236 <table>
237 <?php foreach ($stats['target'] as $target) { ?>
238 <tr class="first">
239 <td class="b">
240 <a href="<?php echo esc_url($target['url']) ?>" target="_blank"><?php echo intval($target['count']) ?></a>
241 </td>
242 <td class="last t">
243 <a href="<?php echo home_url($target['url']) ?>" target="_blank"><?php echo esc_url($target['url']) ?></a>
244 </td>
245 </tr>
246 <?php } ?>
247 </table>
248 </div>
249 </div>
250 <?php } ?>
251 <?php }
252 public static function back_view()
253 {
254 if ( !current_user_can('manage_options') ) {
255 return;
256 }
257 if ( !empty($_POST['statify']) ) {
258 check_admin_referer('_statify');
259 update_option(
260 'statify',
261 array(
262 'days'=> (int)@$_POST['statify']['days'],
263 'limit'=> (int)@$_POST['statify']['limit'],
264 'today'=> (int)@$_POST['statify']['today']
265 )
266 );
267 delete_transient('statify');
268 }
269 $options = self::get_options();
270 wp_nonce_field('_statify'); ?>
271 <table class="form-table">
272 <tr>
273 <td>
274 <select name="statify[days]" id="statify_days">
275 <?php foreach(array(7, 10, 14, 20, 21, 28, 30) as $num) { ?>
276 <option <?php selected($options['days'], $num); ?>><?php echo $num; ?></option>
277 <?php } ?>
278 </select>
279 <label for="statify_days">Anzahl der Tage für Statistiken</label>
280 </td>
281 </tr>
282 <tr>
283 <td>
284 <select name="statify[limit]" id="statify_limit">
285 <?php foreach(range(0, 12) as $num) { ?>
286 <option <?php selected($options['limit'], $num); ?>><?php echo $num; ?></option>
287 <?php } ?>
288 </select>
289 <label for="statify_limit">Anzahl der Einträge in Listen</label>
290 </td>
291 </tr>
292 <tr>
293 <td>
294 <input type="checkbox" name="statify[today]" id="statify_today" value="1" <?php checked($options['today'], 1) ?> />
295 <label for="statify_today">Referrer und Ziele nur vom aktuellen Tag zeigen</label>
296 </td>
297 </tr>
298 </table>
299 <?php
300 }
301 public static function install()
302 {
303 global $wpdb;
304 if ( is_multisite() && !empty($_GET['networkwide']) ) {
305 $ids = $wpdb->get_col(
306 $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
307 );
308 foreach ($ids as $id) {
309 switch_to_blog( (int)$id );
310 self::_install_backend();
311 }
312 restore_current_blog();
313 } else {
314 self::_install_backend();
315 }
316 }
317 public static function install_later($id) {
318 if ( !is_plugin_active_for_network(self::$base) ) {
319 return;
320 }
321 switch_to_blog( (int)$id );
322 self::_install_backend();
323 restore_current_blog();
324 }
325 private static function _install_backend()
326 {
327 add_option(
328 'statify',
329 array(),
330 '',
331 'no'
332 );
333 delete_transient('statify');
334 Statify_Table::init();
335 Statify_Table::create();
336 }
337 public static function uninstall()
338 {
339 global $wpdb;
340 if ( is_multisite() && !empty($_GET['networkwide']) ) {
341 $old = $wpdb->blogid;
342 $ids = $wpdb->get_col(
343 $wpdb->prepare("SELECT blog_id FROM `$wpdb->blogs`")
344 );
345 foreach ($ids as $id) {
346 switch_to_blog($id);
347 self::_uninstall_backend();
348 }
349 switch_to_blog($old);
350 } else {
351 self::_uninstall_backend();
352 }
353 }
354 public static function uninstall_later($id) {
355 if ( !is_plugin_active_for_network(self::$base) ) {
356 return;
357 }
358 switch_to_blog( (int)$id );
359 self::_uninstall_backend();
360 restore_current_blog();
361 }
362 private static function _uninstall_backend()
363 {
364 delete_option('statify');
365 delete_transient('statify');
366 Statify_Table::init();
367 Statify_Table::drop();
368 }
369 public static function update()
370 {
371 self::_update_backend();
372 }
373 private static function _update_backend()
374 {
375 delete_transient('statify');
376 }
377 public static function db_push()
378 {
379 if ( is_feed() or is_trackback() or is_robots() or is_preview() or is_user_logged_in() or is_404() or self::_is_bot() ) {
380 return;
381 }
382 global $wpdb, $wp_rewrite;
383 $data = array();
384 $home = home_url();
385 $data['created'] = strftime('%Y-%m-%d', current_time('timestamp'));
386 if ( !empty($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $home) === false ) {
387 $data['referrer'] = esc_url_raw($_SERVER['HTTP_REFERER']);
388 }
389 $data['target'] = str_replace(
390 $home,
391 '',
392 esc_url_raw(
393 home_url(
394 ( empty($_SERVER['REQUEST_URI']) ? '/' : $_SERVER['REQUEST_URI'] )
395 )
396 )
397 );
398 if ( $wp_rewrite->permalink_structure && !is_search() ) {
399 $data['target'] = preg_replace('/\?.*/', '', $data['target']);
400 }
401 $wpdb->insert(
402 $wpdb->statify,
403 $data
404 );
405 }
406 private static function _is_bot()
407 {
408 if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
409 return true;
410 }
411 if ( !preg_match('/(?:Windows|Mac OS X|Macintosh|Linux)/', $_SERVER['HTTP_USER_AGENT']) ) {
412 return true;
413 }
414 return false;
415 }
416 private static function _clean_data()
417 {
418 if ( get_transient('statify_cron') ) {
419 return;
420 }
421 global $wpdb;
422 $options = self::get_options();
423 $wpdb->query(
424 $wpdb->prepare(
425 "DELETE FROM `$wpdb->statify` WHERE created <= SUBDATE(CURDATE(), %d)",
426 $options['days']
427 )
428 );
429 $wpdb->query(
430 "OPTIMIZE TABLE `$wpdb->statify`"
431 );
432 set_transient(
433 'statify_cron',
434 'ilovesweta',
435 60 * 60 * 12
436 );
437 }
438 private static function _get_stats()
439 {
440 global $wpdb;
441 $options = self::get_options();
442 return array(
443 'visits' => $wpdb->get_results(
444 $wpdb->prepare(
445 "SELECT DATE_FORMAT(`created`, '%%d.%%m') as `date`, COUNT(`created`) as `count` FROM `$wpdb->statify` GROUP BY `created` ORDER BY `created` DESC LIMIT %d",
446 $options['days']
447 ),
448 ARRAY_A
449 ),
450 'target' => $wpdb->get_results(
451 $wpdb->prepare(
452 sprintf(
453 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
454 ( $options['today'] ? 'WHERE created = DATE(NOW())' : '' ),
455 $options['limit']
456 )
457 ),
458 ARRAY_A
459 ),
460 'referrer' => $wpdb->get_results(
461 $wpdb->prepare(
462 sprintf(
463 "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",
464 ( $options['today'] ? 'AND created = DATE(NOW())' : '' ),
465 $options['limit']
466 )
467 ),
468 ARRAY_A
469 )
470 );
471 }
472 private static function _prepare_stats()
473 {
474 if ( ($stats = get_transient('statify')) && self::$stats = $stats ) {
475 return;
476 }
477 self::_clean_data();
478 if ( !$stats = self::_get_stats() ) {
479 return;
480 }
481 if ( !$visits = $stats['visits'] ) {
482 return;
483 }
484 if ( $visits[0]['date'] == date('d.m', current_time('timestamp')) ) {
485 $visits[0]['date'] = 'Heute';
486 }
487 $output = array(
488 'created' => array(),
489 'count' => array()
490 );
491 foreach($visits as $item) {
492 array_push($output['created'], $item['date']);
493 array_push($output['count'], $item['count']);
494 }
495 $stats['visits'] = array(
496 'created' => implode(',', $output['created']),
497 'count' => implode(',', $output['count'])
498 );
499 set_transient(
500 'statify',
501 $stats,
502 60 * 15);
503 self::$stats = $stats;
504 }
505 }
506 class Statify_Table
507 {
508 public function init()
509 {
510 global $wpdb;
511 $table = 'statify';
512 $wpdb->tables[] = $table;
513 $wpdb->$table = $wpdb->get_blog_prefix() . $table;
514 }
515 public function create()
516 {
517 global $wpdb;
518 if ( $wpdb->get_var("SHOW TABLES LIKE '$wpdb->statify'") == $wpdb->statify ) {
519 return;
520 }
521 require_once(ABSPATH. 'wp-admin/includes/upgrade.php');
522 dbDelta(
523 "CREATE TABLE `$wpdb->statify` (
524 `id` bigint(20) unsigned NOT NULL auto_increment,
525 `created` date NOT NULL default '0000-00-00',
526 `referrer` varchar(255) NOT NULL default '',
527 `target` varchar(255) NOT NULL default '',
528 PRIMARY KEY(`id`),
529 KEY `referrer` (`referrer`),
530 KEY `target` (`target`),
531 KEY `created` (`created`)
532 );"
533 );
534 }
535 public function drop()
536 {
537 global $wpdb;
538 $wpdb->query("DROP TABLE IF EXISTS `$wpdb->statify`");
539 }
540 }
541 add_action(
542 'plugins_loaded',
543 array(
544 'Statify',
545 'init'
546 )
547 );
548 register_activation_hook(
549 __FILE__,
550 array(
551 'Statify',
552 'install'
553 )
554 );
555 register_uninstall_hook(
556 __FILE__,
557 array(
558 'Statify',
559 'uninstall'
560 )
561 );
562 if ( function_exists('register_update_hook') ) {
563 register_update_hook(
564 __FILE__,
565 array(
566 'Statify',
567 'update'
568 )
569 );
570 }