root/wp-since-last-visit/trunk/wp-last-visit.php

Revision 671, 7.6 kB (checked in by aking, 3 years ago)

initial add.

  • Property svn:executable set to *
Line 
1 <?php
2
3 // WP Since Last Visit
4 // version 2.2, 2004-07-05
5 //
6 // Copyright (c) 2002-2004 Alex King
7 // http://www.alexking.org/software/wordpress/
8 //
9 // This is an add-on for WordPress
10 // http://wordpress.org/
11 //
12 // **********************************************************************
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 // *****************************************************************
17
18 /*
19 Plugin Name: WP Since Last Visit
20 Plugin URI: http://www.alexking.org/software/wordpress/
21 Description: Indicate new posts and comments since your last visit.
22 Author: Alex King
23 Author URI: http://www.alexking.org/
24 */
25
26 // Modified by Dougal Campbell <URL:http://dougal.gunters.org/> 20030911
27 //   replaced mysql_*() calls with ezSQL calls
28 //   modified cookie code to derive info from $siteurl
29 //   renamed cookies with 'wp' instead of 'b2'
30 //
31 // include this file or paste the functions into your page
32
33 // set $path and $domain as appropriate to your server. In most cases,
34 // you'll want $path to match your WordPress install directory. If your
35 // site is accessible by multiple subdomain names, like "example.com"
36 // and "www.example.com", then set $domain to ".example.com"
37
38 // By default, we'll try to derive them from the site url defined
39 // in the Base Settings options:
40 $urlinfo = parse_url($siteurl);
41
42 $path = $urlinfo['path'];
43 $domain = $urlinfo['host'];
44
45 if (!isset($time_difference)) {
46     $time_difference = 0;
47 }
48 if (!isset($tableposts)) {
49     $tableposts = 'wp_posts';
50 }
51 if (!isset($tablecomments)) {
52     $tablecomments = 'wp_comments';
53 }
54
55 if (!empty($_GET["type"]) && $_GET["type"] == "js") {
56     $wplastvisit_output = "javascript";
57 }
58
59 if (isset($wplastvisit_output)) { // will output stuff for the HTML source
60     switch ($wplastvisit_output) {
61         case "javascript":
62             include("../../wp-blog-header.php");
63             header("Content-type: text/javascript");
64 ?>
65 //    var slvIndicator = 'New';
66     var slvIndicator = '<img src="<?php bloginfo('siteurl'); ?>/ak-img/new.gif" alt="New" title="New since your last visit." />';
67    
68     function slvBanner() {
69         var posts = slvGetCookie('wplastvisit_posts');
70         var comments = slvGetCookie('wplastvisit_comments');
71         if (posts == null || comments == null) {
72             return false;
73         }
74         var banner = '';
75         if (posts == 1) {
76             banner += posts + ' new post and ';
77         }
78         else {
79             banner += posts + ' new posts and ';
80         }
81         if (comments == 1) {
82             banner += comments + ' new comment since your last visit.';
83         }
84         else {
85             banner += comments + ' new comments since your last visit.';
86         }
87         document.write(banner);
88     }
89    
90     function slvShowNewIndicator(date) {
91         if (parseInt(date) > parseInt(slvGetCookie('wplastvisit'))) {
92             document.write(slvIndicator);
93         }
94     }
95    
96     function slvGetCookie(name) {
97
98     /**
99      * Read the JavaScript cookies tutorial at:
100      *   http://www.netspade.com/articles/javascript/cookies.xml
101      */
102
103         var dc = document.cookie;
104         var prefix = name + "=";
105         var begin = dc.indexOf("; " + prefix);
106         if (begin == -1)
107         {
108             begin = dc.indexOf(prefix);
109             if (begin != 0) return null;
110         }
111         else
112         {
113             begin += 2;
114         }
115         var end = document.cookie.indexOf(";", begin);
116         if (end == -1)
117         {
118             end = dc.length;
119         }
120         return unescape(dc.substring(begin + prefix.length, end));
121     }   
122 <?php
123         break;
124     }
125 }
126 else { // standard
127     
128     function ak_last_visit($use_session = 1, $hours = 1) {
129         global $path, $domain, $time_difference;
130         global $tableposts, $tablecomments;
131         if ($use_session == 1 &&
132             isset($_COOKIE["wpthisvisit"]) &&
133             isset($_COOKIE["wplastvisit"])) {
134             if ($_COOKIE["wpthisvisit"] >
135                 (time() + ($time_difference * 3600)) - ($hours * 3600)) {
136                 setcookie("wpthisvisit"
137                          ,time() + ($time_difference * 3600)
138                          ,time() + 30000000
139                          ,$path
140                          ,$domain
141                          );
142                 $ak_last_visit = $_COOKIE["wplastvisit"];
143             }
144             else {
145                 setcookie("wpthisvisit"
146                          ,time() + ($time_difference * 3600)
147                          ,time() + 30000000
148                          ,$path
149                          ,$domain
150                          );
151                 setcookie("wplastvisit"
152                          ,$_COOKIE["wpthisvisit"]
153                          ,time() + 30000000
154                          ,$path
155                          ,$domain
156                          );
157                 $ak_last_visit = $_COOKIE["wpthisvisit"];
158             }
159         }
160         else {
161             setcookie("wpthisvisit"
162                      ,time() + ($time_difference * 3600)
163                      ,time() + 30000000
164                      ,$path
165                      ,$domain
166                      );
167             setcookie("wplastvisit"
168                      ,time() + ($time_difference * 3600)
169                      ,time() + 30000000
170                      ,$path
171                      ,$domain
172                      );
173             if (isset($_COOKIE["wplastvisit"])) {
174                 $ak_last_visit = $_COOKIE["wplastvisit"];
175             }
176             else {
177                 $ak_last_visit = time() + ($time_difference * 3600);
178             }
179         }
180         if (isset($ak_last_visit)) {
181             $last_visit = date("Y-m-d H:i:s", $ak_last_visit);
182             $result = mysql_query("SELECT COUNT(ID) AS count "
183                                  ."FROM $tableposts "
184                                  ."WHERE post_date > '$last_visit' "
185                                  ."AND post_date <= '"
186                                  .date("Y-m-d H:i:s"
187                                       ,(time() + ($time_difference * 3600))
188                                       )
189                                  ."' AND post_status = 'publish'"
190                                  );
191             if ($result) {
192                 while ($data = mysql_fetch_object($result)) {
193                     $posts = $data->count;
194                 }
195             }
196             setcookie("wplastvisit_posts"
197                      ,$posts
198                      ,time() + 30000000
199                      ,$path
200                      ,$domain
201                      );
202             $result = mysql_query("SELECT COUNT(comment_ID) AS count "
203                                  ."FROM $tablecomments "
204                                  ."LEFT JOIN $tableposts "
205                                  ."ON $tablecomments.comment_post_ID=$tableposts.ID "
206                                  ."WHERE comment_date > '$last_visit' "
207                                  ."AND comment_approved = '1' "
208                                  ."AND comment_date <= '"
209                                  .date("Y-m-d H:i:s"
210                                       ,(time() + ($time_difference * 3600))
211                                       )
212                                  ."' AND $tableposts.post_status = 'publish'"
213                                  );
214             if ($result) {
215                 while ($data = mysql_fetch_object($result)) {
216                     $comments = $data->count;
217                 }
218             }
219             setcookie("wplastvisit_comments"
220                      ,$comments
221                      ,time() + 30000000
222                      ,$path
223                      ,$domain
224                      );
225         }
226     }
227     
228     function mysql2timestamp($m) {
229         return mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
230     }
231     
232     function ak_comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
233         global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post, $wpdb, $tablecomments, $HTTP_COOKIE_VARS, $cookiehash;
234         global $querystring_start, $querystring_equal, $querystring_separator, $siteurl;
235         $data = $wpdb->get_row("SELECT COUNT(comment_ID) AS number, MAX(comment_date) AS max_comment_date FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1';");
236         $number = $data->number;
237         $max_date = $data->max_comment_date;
238         if (0 == $number && 'closed' == $post->comment_status && 'closed' == $post->ping_status) {
239             echo $none;
240             return;
241         } else {
242             if (!empty($post->post_password)) { // if there's a password
243                 if ($HTTP_COOKIE_VARS['wp-postpass_'.$cookiehash] != $post->post_password) {  // and it doesn't match the cookie
244                     echo("Enter your password to view comments");
245                     return;
246                 }
247             }
248             echo '<a href="';
249             if ($wpcommentsjavascript) {
250                 echo $siteurl.'/'.$wpcommentspopupfile.$querystring_start.'p'.$querystring_equal.$id.$querystring_separator.'c'.$querystring_equal.'1';
251                 //echo get_permalink();
252                 echo '" onclick="wpopen(this.href); return false"';
253             } else {
254                 // if comments_popup_script() is not in the template, display simple comment link
255                 comments_link();
256                 echo '"';
257             }
258             if (!empty($CSSclass)) {
259                 echo ' class="'.$CSSclass.'"';
260             }
261             echo '>';
262             comments_number($zero, $one, $more, $number);
263             echo '</a> ';
264             echo '<script type="text/javascript">slvShowNewIndicator('.mysql2timestamp($max_date).');</script>';
265         }
266     }
267
268     ak_last_visit();
269
270 }
271
272 ?>
Note: See TracBrowser for help on using the browser.