PluginProbe
WP-PostViews / 1.01
WP-PostViews v1.01
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 / postviews.php

postviews.php in WP-PostViews 1.01, at postviews.php

131 lines 4.8 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://www.lesterchan.net/portfolio/programming.php
5 Description: Enables You To Display How Many Time A Post Had Been Viewed.
6 Version: 1.01
7 Author: GaMerZ
8 Author URI: http://www.lesterchan.net
9 */
10
11
12 /* Copyright 2006 Lester Chan (email : gamerz84@hotmail.com)
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29
30 ### Function: Calculate Post Views
31 add_action('loop_start', 'process_postviews');
32 function process_postviews() {
33 global $id;
34 $post_views = intval(post_custom('views'));
35 if(empty($_COOKIE[USER_COOKIE])) {
36 if(is_single() || is_page()) {
37 if($post_views > 0) {
38 update_post_meta($id, 'views', ($post_views+1));
39 } else {
40 add_post_meta($id, 'views', 1);
41 }
42 }
43 }
44 }
45
46
47 ### Function: Display The Post Views
48 function the_views($text_views = 'Views', $display = true) {
49 $post_views = intval(post_custom('views'));
50 if($display) {
51 echo number_format($post_views).' '.$text_views;
52 } else {
53 return $post_views;
54 }
55 }
56
57
58 ### Function: Display Most Viewed Page/Post
59 if(!function_exists('get_most_viewed')) {
60 function get_most_viewed($mode = '', $limit = 10, $chars = 0) {
61 global $wpdb, $post;
62 $where = '';
63 if($mode == 'post') {
64 $where = 'post_status = \'publish\'';
65 } elseif($mode == 'page') {
66 $where = 'post_status = \'static\'';
67 } else {
68 $where = '(post_status = \'publish\' OR post_status = \'static\')';
69 }
70 $most_viewed = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_status, post_date, CAST(meta_value AS UNSIGNED) 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 meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
71 if($most_viewed) {
72 if($chars > 0) {
73 foreach ($most_viewed as $post) {
74 $post_title = htmlspecialchars(stripslashes($post->post_title));
75 $post_views = intval($post->views);
76 $post_views = number_format($post_views);
77 echo "<li><a href=\"".get_permalink()."\">".snippet_chars($post_title, $chars)."</a> - $post_views ".__('Views')."</li>";
78 }
79 } else {
80 foreach ($most_viewed as $post) {
81 $post_title = htmlspecialchars(stripslashes($post->post_title));
82 $post_views = intval($post->views);
83 $post_views = number_format($post_views);
84 echo "<li><a href=\"".get_permalink()."\">$post_title</a> - $post_views ".__('Views')."</li>";
85 }
86 }
87 } else {
88 echo '<li>'.__('N/A').'</li>';
89 }
90 }
91 }
92
93
94 ### Added by Paolo Tagliaferri (http://www.vortexmind.net - webmaster@vortexmind.net)
95 function get_timespan_most_viewed($mode = '', $limit = 10,$days = 7) {
96 global $wpdb, $post;
97 $limit_date = current_time('timestamp') - ($days*86400);
98 $limit_date = date("Y-m-d H:i:s",$limit_date);
99 $where = '';
100 if($mode == 'post') {
101 $where = 'post_status = \'publish\'';
102 } elseif($mode == 'page') {
103 $where = 'post_status = \'static\'';
104 } else {
105 $where = '(post_status = \'publish\' OR post_status = \'static\')';
106 }
107 $most_viewed = $wpdb->get_results("SELECT $wpdb->posts.ID, post_title, post_name, post_status, post_date, CAST(meta_value AS UNSIGNED) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
108 if($most_viewed) {
109 echo "<ul>";
110 foreach ($most_viewed as $post) {
111 $post_title = htmlspecialchars(stripslashes($post->post_title));
112 $post_views = intval($post->views);
113 $post_views = number_format($post_views);
114 echo "<li><a href=\"".get_permalink()."\">$post_title</a> - $post_views ".__('Views')."</li>";
115 }
116 echo "</ul>";
117 } else {
118 _e('N/A');
119 }
120 }
121
122
123 ### Function: Display Total Views
124 if(!function_exists('get_totalviews')) {
125 function get_totalviews() {
126 global $wpdb;
127 $total_views = $wpdb->get_var("SELECT SUM(CAST(meta_value AS UNSIGNED)) FROM $wpdb->postmeta WHERE meta_key = 'views'");
128 echo number_format($total_views);
129 }
130 }
131 ?>