root/wp-stats/trunk/wp-stats.php

Revision 53030, 26.8 kB (checked in by GamerZ, 4 days ago)

Fixed Translation Issue

Line 
1 <?php
2 /*
3 Plugin Name: WP-Stats
4 Plugin URI: http://lesterchan.net/portfolio/programming/php/
5 Description: Display your WordPress blog statistics. Ranging from general total statistics, some of my plugins statistics and top 10 statistics.
6 Version: 2.31
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 */
10
11
12 /* 
13     Copyright 2008  Lester Chan  (email : lesterchan@gmail.com)
14
15     This program is free software; you can redistribute it and/or modify
16     it under the terms of the GNU General Public License as published by
17     the Free Software Foundation; either version 2 of the License, or
18     (at your option) any later version.
19
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU General Public License for more details.
24
25     You should have received a copy of the GNU General Public License
26     along with this program; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 */
29
30
31 ### Use WordPress 2.6 Constants
32 if (!defined('WP_CONTENT_DIR')) {
33     define( 'WP_CONTENT_DIR', ABSPATH.'wp-content');
34 }
35 if (!defined('WP_CONTENT_URL')) {
36     define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
37 }
38 if (!defined('WP_PLUGIN_DIR')) {
39     define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
40 }
41 if (!defined('WP_PLUGIN_URL')) {
42     define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
43 }
44
45
46 ### Create Text Domain For Translations
47 add_action('init', 'stats_textdomain');
48 function stats_textdomain() {
49     if (!function_exists('wp_print_styles')) {
50         load_plugin_textdomain('wp-stats', 'wp-content/plugins/wp-stats');
51     } else {
52         load_plugin_textdomain('wp-stats', false, 'wp-stats');
53     }
54 }
55
56
57 ### Function: WP-Stats Menu
58 add_action('admin_menu', 'stats_menu');
59 function stats_menu() {
60     if (function_exists('add_submenu_page')) {
61         add_submenu_page('index.php'__('WP-Stats', 'wp-stats'),  __('WP-Stats', 'wp-stats'), 1, 'wp-stats/wp-stats.php', 'display_stats');
62     }
63     if (function_exists('add_options_page')) {
64         add_options_page(__('Stats', 'wp-stats'), __('Stats', 'wp-stats'), 'manage_options', 'wp-stats/stats-options.php');
65     }
66 }
67
68
69 ### Function: WP-Stats Manager CSS
70 add_action('wp_head', 'stats_css');
71 function stats_css() {   
72     if(!function_exists('pagenavi_css')) {
73         echo "\n".'<!-- Start Of Script Generated By WP-Stats 2.31 -->'."\n";
74         if(@file_exists(TEMPLATEPATH.'/stats-css.css')) {
75             echo '<link rel="stylesheet" href="'.get_stylesheet_directory_uri().'/stats-css.css" type="text/css" media="screen" />'."\n";   
76         } else {
77             echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/wp-stats/stats-css.css" type="text/css" media="screen" />'."\n";
78         }   
79         echo '<!-- End Of Script Generated By WP-Stats 2.31 -->'."\n";
80     }
81 }
82
83
84 ### Display WP-Stats Admin Page
85 function display_stats() {
86     $stats_page = stats_page();
87     echo "<div class=\"wrap\">\n$stats_page</div>\n";
88 }
89
90
91 ### Function: Get Total Authors
92 function get_totalauthors($display = true) {
93     global $wpdb;
94     $totalauthors = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users LEFT JOIN $wpdb->usermeta ON $wpdb->usermeta.user_id = $wpdb->users.ID WHERE $wpdb->users.user_activation_key = '' AND $wpdb->usermeta.meta_key = '".$wpdb->prefix."user_level' AND (meta_value+0.00) > 1");
95     if($display) {
96         echo number_format($totalauthors);
97     } else {
98         return number_format($totalauthors);
99     }
100 }
101
102
103 ### Function: Get Total Posts
104 function get_totalposts($display = true) {
105     global $wpdb;
106     $totalposts = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
107     if($display) {
108         echo number_format($totalposts);
109     } else {
110         return number_format($totalposts);
111     }
112 }
113
114
115 ### Function: Get Total Pages
116 function get_totalpages($display = true) {
117     global $wpdb;
118     $totalpages = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish'");
119     if($display) {
120         echo number_format($totalpages);
121     } else {
122         return number_format($totalpages);
123     }
124 }
125
126
127 ### Function: Get Total Comments
128 function get_totalcomments($display = true) {
129     global $wpdb;
130     $totalcomments = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = '1'");
131     if($display) {
132         echo number_format($totalcomments);
133     } else {
134         return number_format($totalcomments);
135     }
136 }
137
138
139 ### Function: Get Total Comments Poster
140 function get_totalcommentposters($display = true) {
141     global $wpdb;
142     $totalcommentposters = $wpdb->get_var("SELECT COUNT(DISTINCT comment_author) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_type = ''");
143     if($display) {
144         echo number_format($totalcommentposters);
145     } else {
146         return number_format($totalcommentposters);
147     }
148 }
149
150
151 ### Function: Get Total Links
152 function get_totallinks($display = true) {
153     global $wpdb;
154     $totallinks = $wpdb->get_var("SELECT COUNT(link_id) FROM $wpdb->links");
155     if($display) {
156         echo number_format($totallinks);
157     } else {
158         return number_format($totallinks);
159     }
160 }
161
162
163 ### Function: Get Recent Posts
164 function get_recentposts($mode = '', $limit = 10, $display = true) {
165     global $wpdb, $post;
166     $where = '';
167     $temp = '';
168     if(!empty($mode) && $mode != 'both') {
169         $where = "post_type = '$mode'";
170     } else {
171         $where = '1=1';
172     }
173     $recentposts = $wpdb->get_results("SELECT $wpdb->posts.*, $wpdb->users.* FROM $wpdb->posts LEFT JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->posts.post_author WHERE user_activation_key = '' AND post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND post_password = '' ORDER  BY post_date DESC LIMIT $limit");
174     if($recentposts) {
175         foreach ($recentposts as $post) {
176             $post_title = get_the_title();
177             $post_date = get_the_time(get_option('date_format').' @ '.get_option('time_format'));
178             $display_name = stripslashes($post->display_name);
179             $temp .= "<li>$post_date - <a href=\"".get_permalink()."\" title=\"".sprintf(__('View post %s', 'wp-stats'), $post_title)."\">$post_title</a> ($display_name)</li>\n";
180         }
181     } else {
182         $temp = '<li>'.__('N/A', 'wp-stats').'</li>';
183     }
184     if($display) {
185         echo $temp;
186     } else {
187         return $temp;
188     }
189 }
190
191
192 ### Function: Get Recent Comments
193 function get_recentcomments($mode = '', $limit = 10, $display = true) {
194     global $wpdb, $post;
195     $where = '';
196     $temp = '';
197     if(!empty($mode) && $mode != 'both') {
198         $where = "post_type = '$mode'";
199     } else {
200         $where = '1=1';
201     }
202     $recentcomments = $wpdb->get_results("SELECT * FROM $wpdb->posts INNER JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND comment_type = '' AND post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND post_password = '' ORDER  BY comment_date DESC LIMIT $limit");
203     if($recentcomments) {
204         foreach ($recentcomments as $post) {
205             $post_title = get_the_title();
206             $comment_author = htmlspecialchars(stripslashes($post->comment_author));
207             $comment_date = mysql2date(get_option('date_format').' @ '.get_option('time_format'), $post->comment_date);
208             $temp .= "<li>$comment_date - $comment_author (<a href=\"".get_permalink()."#comment-".$post->comment_ID."\" title=\"".sprintf(__('View comments in post %s', 'wp-stats'), $post_title)."\">$post_title</a>)</li>\n";
209         }
210     } else {
211         $temp = '<li>'.__('N/A', 'wp-stats').'</li>';
212     }
213     if($display) {
214         echo $temp;
215     } else {
216         return $temp;
217     }
218 }
219
220
221 ### Function: Get Top Commented Posts
222 function get_mostcommented($mode = '', $limit = 10, $chars = 0, $display = true) {
223     global $wpdb, $post;
224     $where = '';
225     $temp = '';
226     if(!empty($mode) && $mode != 'both') {
227         $where = "post_type = '$mode'";
228     } else {
229         $where = '1=1';
230     }
231     $mostcommenteds = $wpdb->get_results("SELECT $wpdb->posts.*, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->posts LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND post_password = '' GROUP BY $wpdb->comments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");
232     if($mostcommenteds) {
233         if($chars > 0) {
234             foreach ($mostcommenteds as $post) {
235                 $post_title = get_the_title();
236                 $comment_total = number_format_i18n($post->comment_total);
237                 $temp .= "<li><a href=\"".get_permalink()."\" title=\"".sprintf(__('View comments in post %s', 'wp-stats'), $post_title)."\">".snippet_text($post_title, $chars)."</a> - $comment_total ".__('comments', 'wp-stats')."</li>";
238             }
239         } else {
240             foreach ($mostcommenteds as $post) {
241                 $post_title = get_the_title();
242                 $comment_total = number_format_i18n($post->comment_total);
243                 $temp .= "<li><a href=\"".get_permalink()."\" title=\"".sprintf(__('View comments in post %s', 'wp-stats'), $post_title)."\">$post_title</a> - $comment_total ".__('comments', 'wp-stats')."</li>";
244             }
245         }
246     } else {
247         $temp = '<li>'.__('N/A', 'wp-stats').'</li>';
248     }
249     if($display) {
250         echo $temp;
251     } else {
252         return $temp;
253     }
254 }
255
256
257 ### Function: Get Author Stats
258 function get_authorsstats($mode = '', $display = true) {
259     global $wpdb, $wp_rewrite;
260     $where = '';
261     $temp = '';
262     if(!empty($mode) && $mode != 'both') {
263         $where = "post_type = '$mode'";
264     } else {
265         $where = '1=1';
266     }
267     $posts = $wpdb->get_results("SELECT COUNT($wpdb->posts.ID) AS 'posts_total', $wpdb->users.display_name, $wpdb->users.user_nicename FROM $wpdb->posts LEFT JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->posts.post_author WHERE user_activation_key = '' AND $where AND post_status = 'publish' GROUP BY $wpdb->posts.post_author");
268     if($posts) {
269         $using_permalink = get_option('permalink_structure');
270         $permalink = $wp_rewrite->get_author_permastruct();
271         foreach ($posts as $post) {
272                 $post_author = strip_tags(stripslashes($post->user_nicename));
273                 $author_link = str_replace('%author%', $post_author, $permalink);
274                 $display_name = strip_tags(stripslashes($post->display_name));
275                 $posts_total = number_format_i18n($post->posts_total);               
276                 if($using_permalink) {
277                     $temp .= "<li><a href=\"".get_option('home').$author_link."\" title=\"".sprintf(__('View posts posted by %s', 'wp-stats'), $display_name)."\">$display_name</a> ($posts_total)</li>\n";
278                 } else {
279                     $temp .= "<li><a href=\"".get_option('siteurl')."/?author_name=$post_author\" title=\"".sprintf(__('View posts posted by %s', 'wp-stats'), $display_name)."\">$display_name</a> ($posts_total)</li>\n";
280                 }
281         }
282     } else {
283         $temp = '<li>'.__('N/A', 'wp-stats').'</li>';
284     }
285     if($display) {
286         echo $temp;
287     } else {
288         return $temp;
289     }
290 }
291
292
293 ### Function: Get Comments' Members Stats
294 // Treshhold = Number Of Posts User Must Have Before It Will Display His Name Out
295 // 5 = Default Treshhold; -1 = Disable Treshhold
296 function get_commentmembersstats($threshhold = -1, $limit = 0, $display = true) {
297     global $wpdb;
298     $temp = '';
299     $limit_sql = '';
300     if($limit > 0) {
301         $limit_sql = "LIMIT $limit";
302     }
303     $comments = $wpdb->get_results("SELECT comment_author, COUNT(comment_ID) AS 'comment_total' FROM $wpdb->comments INNER  JOIN $wpdb->posts ON $wpdb->comments.comment_post_ID = $wpdb->posts.ID WHERE comment_approved = '1' AND comment_type = '' AND post_date < '".current_time('mysql')."' AND post_status = 'publish' AND post_password = '' GROUP BY comment_author ORDER BY comment_total DESC $limit_sql");
304     if($comments) {
305         foreach ($comments as $comment) {
306                 $comment_total = intval($comment->comment_total);
307                 // If Total Comments Is Below Threshold
308                 if($comment_total >= $threshhold) {
309                     $comment_author = strip_tags(stripslashes($comment->comment_author));
310                     $comment_author_link = urlencode($comment_author);
311                     $temp .= "<li><a href=\"".stats_page_link($comment_author_link)."\" title=\"".sprintf(__('View all comments posted by %s', 'wp-stats'), $comment_author)."\">$comment_author</a> (".number_format_i18n($comment_total).")</li>\n";
312                 }
313         }
314     } else {
315         $temp = '<li>'.__('N/A', 'wp-stats').'</li>';
316     }
317     if($display) {
318         echo $temp;
319     } else {
320         return $temp;
321     }
322 }
323
324
325 ### Function: Get Post Categories Stats
326 function get_postcats($display = true) {
327     global $wpdb;
328     $temp = '';
329     $defaults = array('type' => 'post', 'style' => 'list', 'show_count' => 1);
330     $categories = get_categories($defaults);
331     if (empty($categories)){
332         $temp .= '<li>'.__('No categories', 'wp-stats').'</li>';
333     } else {
334         $temp .= walk_category_tree($categories, 0, $defaults);
335     }
336     if($display) {
337         echo $temp;
338     } else {
339         return $temp;
340     }
341 }
342
343
344 ### Function: Get Links Categories Stats
345 function get_linkcats($display = true) {
346     global $wpdb;
347     $temp = '';
348     $cats = get_categories('type=link');
349     if ($cats) {
350         foreach ($cats as $cat) {
351             $temp .= '<li>'.$cat->cat_name.' ('.number_format_i18n($cat->count).")</li>\n";
352         }
353     }
354     if($display) {
355         echo $temp;
356     } else {
357         return $temp;
358     }
359 }
360
361
362 ### Function: Get Tags List
363 function get_tags_list($display = true) {
364     global $wpdb;
365     $temp = '';
366     $tags = get_tags('orderby=count&order=DESC');
367     if ($tags) {
368         foreach ($tags as $tag) {
369             $temp .= '<li><a href="'.clean_url(get_tag_link($tag->term_id)).'" title="'.sprintf(__('%s topics', 'wp-stats'), number_format_i18n($tag->count)).'">'.$tag->name.'</a> ('.number_format_i18n($tag->count).")</li>\n";
370         }
371     }
372     if($display) {
373         echo $temp;
374     } else {
375         return $temp;
376     }
377 }
378
379
380 ### Function: Snippet Text
381 if(!function_exists('snippet_text')) {
382     function snippet_text($text, $length = 0) {
383         $text = html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
384          if (strlen($text) > $length) {
385             return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
386          } else {
387             return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
388          }
389     }
390 }
391
392
393 ### Function: Short Code For Inserting Stats Into Page
394 add_shortcode('page_stats', 'stats_page_shortcode');
395 function stats_page_shortcode($atts) {
396     return stats_page();
397 }
398
399
400 ### Function: Stats Page
401 function stats_page_link($author, $page = 0) {
402     $stats_url = get_option('stats_url');
403     if($page > 1) {
404         $page = "&amp;stats_page=$page";
405     } else {
406         $page = '';
407     }
408     if(strpos($stats_url, '?') !== false) {
409         $stats_url = "$stats_url&amp;stats_author=$author$page";
410     } else {
411         $stats_url = "$stats_url?stats_author=$author$page";
412     }
413     return $stats_url;
414 }
415
416
417 ### Function: Statistics Page
418 function stats_page() {
419     global $wpdb, $post;   
420     // Variables Variables Variables
421     $comment_author = urldecode(strip_tags(stripslashes(trim($_GET['stats_author']))));
422     $page = intval($_GET['stats_page']);
423     $temp_stats = '';
424     $temp_post = $post;
425     $stats_mostlimit = intval(get_option('stats_mostlimit'));
426     $stats_display = get_option('stats_display');
427
428     // Default wp-stats.php Page
429     if(empty($comment_author)) {
430         // General Stats
431         if($stats_display['total_stats'] == 1) {
432             $temp_stats .= '<h2 id="GeneralStats">'.__('General Stats', 'wp-stats').'</h2>'."\n";
433             $temp_stats .= '<p><strong>'.__('Total Stats', 'wp-stats').'</strong></p>'."\n";
434             $temp_stats .= '<ul>'."\n";
435             $temp_stats .= '<li><strong>'.get_totalauthors(false).'</strong> '.__('authors to this blog.', 'wp-stats').'</li>'."\n";
436             $temp_stats .= '<li><strong>'.get_totalposts(false).'</strong> '.__('posts were posted.', 'wp-stats').'</li>'."\n";
437             $temp_stats .= '<li><strong>'.get_totalpages(false).'</strong> '.__('pages were created.', 'wp-stats').'</li>'."\n";
438             $temp_stats .= '<li><strong>'.wp_count_terms('post_tag').'</strong> '.__('tags were created.', 'wp-stats').'</li>'."\n";       
439             $temp_stats .= '<li><strong>'.get_totalcomments(false).'</strong> '.__('comments were posted.', 'wp-stats').'</li>'."\n";
440             $temp_stats .= '<li><strong>'.get_totalcommentposters(false).'</strong> '.__('different nicknames were represented in the comments.', 'wp-stats').'</li>'."\n";
441             $temp_stats .= '<li><strong>'.get_totallinks(false).'</strong> '.__('links were added.', 'wp-stats').'</li>'."\n";
442             $temp_stats .= '<li><strong>'.wp_count_terms('category').'</strong> '.__('post categories were needed.', 'wp-stats').'</li>'."\n";
443             $temp_stats .= '<li><strong>'.wp_count_terms('link_category').'</strong> '.__('link categories were needed.', 'wp-stats').'</li>'."\n";
444             if(function_exists('akismet_spam_count')) {
445                 $temp_stats .= '<li><strong>'.number_format(get_option('akismet_spam_count')).'</strong> '.__('spam blocked.', 'wp-stats').'</li>'."\n";
446             }
447             // WP-Stats: General Stats Filter
448             $temp_stats = apply_filters('wp_stats_page_general', $temp_stats);
449             $temp_stats .= '</ul>'."\n";
450         }
451
452         // Plugin Stats
453         $temp_stats .= '<h2 id="PluginsStats">'.__('Plugins Stats', 'wp-stats').'</h2>'."\n";
454                 
455         // WP-Stats: Plugins Stats Filter
456         $temp_stats = apply_filters('wp_stats_page_plugins', $temp_stats);
457
458         // Top Recent Stats
459         $temp_stats .= '<h2 id="TopRecentStats">'.sprintf(__('Top %s Recent Stats', 'wp-stats'), $stats_mostlimit).'</h2>'."\n";
460
461         // Recent Posts
462         if($stats_display['recent_posts'] == 1) {
463             $temp_stats .= '<p><strong>'.$stats_mostlimit.' '.__('Recent Posts', 'wp-stats').'</strong></p>'."\n";
464             $temp_stats .= '<ul>'."\n";
465             $temp_stats .=