PluginProbe
WP-PostViews / 1.31
WP-PostViews v1.31
2.0.1 2.0.0 1.01 1.02 1.10 1.11 1.20 1.30 1.31 1.40 1.50 1.66 1.67 1.68 1.69 1.70 1.71 1.72 1.73 1.74 1.75 1.76 1.76.1 1.77 1.78 All 29 releases
wp-postviews / wp-postviews.php

wp-postviews.php in WP-PostViews 1.31, at wp-postviews.php

419 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-PostViews
4 Plugin URI: http://lesterchan.net/portfolio/programming/php/
5 Description: Enables you to display how many times a post/page had been viewed.
6 Version: 1.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 ### Load WP-Config File If This File Is Called Directly
32 if (!function_exists('add_action')) {
33 $wp_root = '../../..';
34 if (file_exists($wp_root.'/wp-load.php')) {
35 require_once($wp_root.'/wp-load.php');
36 } else {
37 require_once($wp_root.'/wp-config.php');
38 }
39 }
40
41
42 ### Use WordPress 2.6 Constants
43 if (!defined('WP_CONTENT_DIR')) {
44 define( 'WP_CONTENT_DIR', ABSPATH.'wp-content');
45 }
46 if (!defined('WP_CONTENT_URL')) {
47 define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
48 }
49 if (!defined('WP_PLUGIN_DIR')) {
50 define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
51 }
52 if (!defined('WP_PLUGIN_URL')) {
53 define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
54 }
55
56
57 ### Create Text Domain For Translations
58 add_action('init', 'postviews_textdomain');
59 function postviews_textdomain() {
60 if (!function_exists('wp_print_styles')) {
61 load_plugin_textdomain('wp-postviews', 'wp-content/plugins/wp-postviews');
62 } else {
63 load_plugin_textdomain('wp-postviews', false, 'wp-postviews');
64 }
65 }
66
67
68 ### Function: Post Views Option Menu
69 add_action('admin_menu', 'postviews_menu');
70 function postviews_menu() {
71 if (function_exists('add_options_page')) {
72 add_options_page(__('Post Views', 'wp-postviews'), __('Post Views', 'wp-postviews'), 'manage_options', 'wp-postviews/postviews-options.php') ;
73 }
74 }
75
76
77 ### Function: Calculate Post Views
78 add_action('wp_head', 'process_postviews');
79 function process_postviews() {
80 global $user_ID, $post;
81 if(is_single() || is_page()) {
82 $id = intval($post->ID);
83 $views_options = get_option('views_options');
84 $post_views = get_post_custom($id);
85 $post_views = intval($post_views['views'][0]);
86 $should_count = false;
87 switch(intval($views_options['count'])) {
88 case 0:
89 $should_count = true;
90 break;
91 case 1:
92 if(empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) {
93 $should_count = true;
94 }
95 break;
96 case 2:
97 if(intval($user_ID) > 0) {
98 $should_count = true;
99 }
100 break;
101 }
102 if(intval($views_options['exclude_bots']) == 1) {
103 $bots = array('Google Bot' => 'googlebot', 'Google Bot' => 'google', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'jeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot', 'Technorati' => 'technorati', 'Yahoo' => 'yahoo', 'Findexa' => 'findexa', 'NextLinks' => 'findlinks', 'Gais' => 'gaisbo', 'WiseNut' => 'zyborg', 'WhoisSource' => 'surveybot', 'Bloglines' => 'bloglines', 'BlogSearch' => 'blogsearch', 'PubSub' => 'pubsub', 'Syndic8' => 'syndic8', 'RadioUserland' => 'userland', 'Gigabot' => 'gigabot', 'Become.com' => 'become.com');
104 $useragent = $_SERVER['HTTP_USER_AGENT'];
105 foreach ($bots as $name => $lookfor) {
106 if (stristr($useragent, $lookfor) !== false) {
107 $should_count = false;
108 break;
109 }
110 }
111 }
112 if($should_count) {
113 if(defined('WP_CACHE') && WP_CACHE) {
114 echo "\n".'<!-- Start Of Script Generated By WP-PostViews 1.31 -->'."\n";
115 wp_print_scripts(array('sack'));
116 echo '<script type="text/javascript">'."\n";
117 echo '/* <![CDATA[ */'."\n";
118 echo "\t".'var postviews_ajax_url = \''.WP_PLUGIN_URL.'/wp-postviews/wp-postviews.php'."';\n";
119 echo "\t".'postviews_count = new sack(postviews_ajax_url);'."\n";
120 echo "\t".'postviews_count.setVar("postviews_id", '.$id.');'."\n";
121 echo "\t".'postviews_count.method = \'GET\';'."\n";
122 echo "\t".'postviews_count.runAJAX();'."\n";
123 echo "\t".'postviews_count = null;'."\n";
124 echo '/* ]]> */'."\n";
125 echo '</script>'."\n";
126 echo '<!-- End Of Script Generated By WP-PostViews 1.31 -->'."\n";
127 } else {
128 if(!update_post_meta($id, 'views', ($post_views+1))) {
129 add_post_meta($id, 'views', 1, true);
130 }
131 }
132 }
133 }
134 }
135
136
137 ### Function: Display The Post Views
138 function the_views($display = true) {
139 $post_views = intval(post_custom('views'));
140 $views_options = get_option('views_options');
141 $output = str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']);
142 if($display) {
143 echo $output;
144 } else {
145 return $output;
146 }
147 }
148
149
150 ### Function: Display Most Viewed Page/Post
151 if(!function_exists('get_most_viewed')) {
152 function get_most_viewed($mode = '', $limit = 10, $chars = 0, $display = true) {
153 global $wpdb, $post;
154 $views_options = get_option('views_options');
155 $where = '';
156 $temp = '';
157 $output = '';
158 if(!empty($mode) && $mode != 'both') {
159 $where = "post_type = '$mode'";
160 } else {
161 $where = '1=1';
162 }
163 $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
164 if($most_viewed) {
165 foreach ($most_viewed as $post) {
166 $post_views = number_format_i18n(intval($post->views));
167 $post_title = get_the_title();
168 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password);
169 $post_content = get_the_content();
170 if($chars > 0) {
171 $temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), $post_views)."</li>\n";
172 } else {
173 $temp = stripslashes($views_options['most_viewed_template']);
174 $temp = str_replace("%VIEW_COUNT%", $post_views, $temp);
175 $temp = str_replace("%POST_TITLE%", $post_title, $temp);
176 $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
177 $temp = str_replace("%POST_CONTENT%", $post_content, $temp);
178 $temp = str_replace("%POST_URL%", get_permalink(), $temp);
179 }
180 $output .= $temp;
181 }
182 } else {
183 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
184 }
185 if($display) {
186 echo $output;
187 } else {
188 return $output;
189 }
190 }
191 }
192
193
194 ### Function: Display Most Viewed Page/Post By Category ID
195 if(!function_exists('get_most_viewed_category')) {
196 function get_most_viewed_category($category_id = 0, $mode = '', $limit = 10, $chars = 0, $display = true) {
197 global $wpdb, $post;
198 $views_options = get_option('views_options');
199 $where = '';
200 $temp = '';
201 $output = '';
202 if(is_array($category_id)) {
203 $category_sql = "$wpdb->term_taxonomy.term_id IN (".join(',', $category_id).')';
204 } else {
205 $category_sql = "$wpdb->term_taxonomy.term_id = $category_id";
206 }
207 if(!empty($mode) && $mode != 'both') {
208 $where = "post_type = '$mode'";
209 } else {
210 $where = '1=1';
211 }
212 $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_date < '".current_time('mysql')."' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $category_sql AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
213 if($most_viewed) {
214 foreach ($most_viewed as $post) {
215 $post_views = number_format_i18n(intval($post->views));
216 $post_title = get_the_title();
217 $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password);
218 $post_content = get_the_content();
219 if($chars > 0) {
220 $temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> - ".sprintf(__ngettext('%s view', '%s views', $post_views, 'wp-postviews'), $post_views)."</li>\n";
221 } else {
222 $temp = stripslashes($views_options['most_viewed_template']);
223 $temp = str_replace("%VIEW_COUNT%", $post_views, $temp);
224 $temp = str_replace("%POST_TITLE%", $post_title, $temp);
225 $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
226 $temp = str_replace("%POST_CONTENT%", $post_content, $temp);
227 $temp = str_replace("%POST_URL%", get_permalink(), $temp);
228 }
229 $output .= $temp;
230 }
231 } else {
232 $output = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";
233 }
234 if($display) {
235 echo $output;
236 } else {
237 return $output;
238 }
239 }
240 }
241
242
243 ### Function: Display Total Views
244 if(!function_exists('get_totalviews')) {
245 function get_totalviews($display = true) {
246 global $wpdb;
247 $total_views = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'");
248 if($display) {
249 echo number_format_i18n($total_views);
250 } else {
251 return number_format_i18n($total_views);
252 }
253 }
254 }
255
256
257 ### Function: Snippet Text
258 if(!function_exists('snippet_text')) {
259 function snippet_text($text, $length = 0) {
260 $text = html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
261 if (strlen($text) > $length) {
262 return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
263 } else {
264 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
265 }
266 }
267 }
268
269
270 ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
271 function views_post_excerpt($post_excerpt, $post_content, $post_password) {
272 if(!empty($post_password)) {
273 if(!isset($_COOKIE['wp-postpass_'.COOKIEHASH]) || $_COOKIE['wp-postpass_'.COOKIEHASH] != $post_password) {
274 return __('There is no excerpt because this is a protected post.', 'wp-postviews');
275 }
276 }
277 if(empty($post_excerpt)) {
278 return snippet_text($post_content, 200);
279 } else {
280 return $post_excerpt;
281 }
282 }
283
284
285 ### Function: Modify Default WordPress Listing To Make It Sorted By Post Views
286 function views_fields($content) {
287 global $wpdb;
288 $content .= ", ($wpdb->postmeta.meta_value+0) AS views";
289 return $content;
290 }
291 function views_join($content) {
292 global $wpdb;
293 $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
294 return $content;
295 }
296 function views_where($content) {
297 global $wpdb;
298 $content .= " AND $wpdb->postmeta.meta_key = 'views'";
299 return $content;
300 }
301 function views_orderby($content) {
302 $orderby = trim(addslashes(get_query_var('v_orderby')));
303 if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
304 $orderby = 'desc';
305 }
306 $content = " views $orderby";
307 return $content;
308 }
309
310
311 ### Function: Views Public Variables
312 add_filter('query_vars', 'views_variables');
313 function views_variables($public_query_vars) {
314 $public_query_vars[] = 'v_sortby';
315 $public_query_vars[] = 'v_orderby';
316 return $public_query_vars;
317 }
318
319
320 ### Function: Sort Ratings Posts
321 add_action('pre_get_posts', 'views_sorting');
322 function views_sorting() {
323 if(get_query_var('v_sortby') == 'views') {
324 add_filter('posts_fields', 'views_fields');
325 add_filter('posts_join', 'views_join');
326 add_filter('posts_where', 'views_where');
327 add_filter('posts_orderby', 'views_orderby');
328 }
329 }
330
331
332 ### Function: Plug Into WP-Stats
333 if(strpos(get_option('stats_url'), $_SERVER['REQUEST_URI']) || strpos($_SERVER['REQUEST_URI'], 'stats-options.php') || strpos($_SERVER['REQUEST_URI'], 'wp-stats/wp-stats.php')) {
334 add_filter('wp_stats_page_admin_plugins', 'postviews_page_admin_general_stats');
335 add_filter('wp_stats_page_admin_most', 'postviews_page_admin_most_stats');
336 add_filter('wp_stats_page_plugins', 'postviews_page_general_stats');
337 add_filter('wp_stats_page_most', 'postviews_page_most_stats');
338 }
339
340
341 ### Function: Add WP-PostViews General Stats To WP-Stats Page Options
342 function postviews_page_admin_general_stats($content) {
343 $stats_display = get_option('stats_display');
344 if($stats_display['views'] == 1) {
345 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" checked="checked" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
346 } else {
347 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_views" value="views" />&nbsp;&nbsp;<label for="wpstats_views">'.__('WP-PostViews', 'wp-postviews').'</label><br />'."\n";
348 }
349 return $content;
350 }
351
352
353 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page Options
354 function postviews_page_admin_most_stats($content) {
355 $stats_display = get_option('stats_display');
356 $stats_mostlimit = intval(get_option('stats_mostlimit'));
357 if($stats_display['viewed_most'] == 1) {
358 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most" value="viewed_most" checked="checked" />&nbsp;&nbsp;<label for="wpstats_viewed_most">'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), $stats_mostlimit).'</label><br />'."\n";
359 } else {
360 $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_viewed_most" value="viewed_most" />&nbsp;&nbsp;<label for="wpstats_viewed_most">'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), $stats_mostlimit).'</label><br />'."\n";
361 }
362 return $content;
363 }
364
365
366 ### Function: Add WP-PostViews General Stats To WP-Stats Page
367 function postviews_page_general_stats($content) {
368 $stats_display = get_option('stats_display');
369 if($stats_display['views'] == 1) {
370 $content .= '<p><strong>'.__('WP-PostViews', 'wp-postviews').'</strong></p>'."\n";
371 $content .= '<ul>'."\n";
372 $content .= '<li>'.sprintf(__ngettext('<strong>%s</strong> view was generated..', '<strong>%s</strong> views were generated.', get_totalviews(false), 'wp-postviews'), get_totalviews(false)).'</li>'."\n";
373 $content .= '</ul>'."\n";
374 }
375 return $content;
376 }
377
378
379 ### Function: Add WP-PostViews Top Most/Highest Stats To WP-Stats Page
380 function postviews_page_most_stats($content) {
381 $stats_display = get_option('stats_display');
382 $stats_mostlimit = intval(get_option('stats_mostlimit'));
383 if($stats_display['viewed_most'] == 1) {
384 $content .= '<p><strong>'.sprintf(__ngettext('%s Most Viewed Post', '%s Most Viewed Posts', $stats_mostlimit, 'wp-postviews'), $stats_mostlimit).'</strong></p>'."\n";
385 $content .= '<ul>'."\n";
386 $content .= get_most_viewed('post', $stats_mostlimit, 0, false);
387 $content .= '</ul>'."\n";
388 }
389 return $content;
390 }
391
392
393 ### Function: Increment Post Views
394 increment_views();
395 function increment_views() {
396 global $wpdb;
397 $post_id = intval($_GET['postviews_id']);
398 if($post_id > 0) {
399 $post_views = get_post_custom($post_id);
400 $post_views = intval($post_views['views'][0]);
401 if(!update_post_meta($post_id, 'views', ($post_views+1))) {
402 add_post_meta($post_id, 'views', 1, true);
403 }
404 }
405 }
406
407
408 ### Function: Post Views Options
409 add_action('activate_wp-postviews/wp-postviews.php', 'views_init');
410 function views_init() {
411 // Add Options
412 $views_options = array();
413 $views_options['count'] = 1;
414 $views_options['exclude_bots'] = 0;
415 $views_options['template'] = __('%VIEW_COUNT% views', 'wp-postviews');
416 $views_options['most_viewed_template'] = '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% '.__('views', 'wp-postviews').'</li>';
417 add_option('views_options', $views_options, 'Post Views Options');
418 }
419 ?>