root/afridex/plugins/dailytop10/dailytop10.php @ 21

Revision 21, 17.5 kB (checked in by admin, 18 years ago)
Line 
1<?php
2/*
3Plugin Name: Daily Top 10 Posts
4Plugin URI: http://www.alleba.com/blog/2007/03/27/wordpress-plugin-daily-top-10-posts/
5Description: Tracks the number of pageviews per blog post for the current day and cumulatively with options to display sidebar widgets for both.  Features dashboard widgets too.
6Author: Andrew dela Serna
7Author URI: http://www.alleba.com/blog/
8Version: 0.4
9
10  Copyright 2007 Andrew dela Serna (email andrew@alleba.com)
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26INSTALLATION
27------------
281. Download the plugin from http://www.alleba.com/blog/wp-downloads/dailytop10.zip
292. Extract and upload dailytop10.php to 'yourserver.com/wp-content/plugins/'
303. Login to your Wordpress admin panel and browse to the Plugins section.
314. Activate The Daily Top 10 Posts plugin.
32
33INSTRUCTIONS
34------------
351. Go to Admin Panel > Design > Theme Editor and click on Single Post (single.php).
36
37         #Find this line:
38               
39                <?php the_time('F jS, Y') ?> //date
40               
41         #Right after it, insert this line:
42                <br /><?php if (function_exists('todays_overall_count')) { todays_overall_count($post->ID, 'Visited', 'times', 'so far today', '0', 'show'); } ?>
43               
44   #The line will display something like "Visited 300 times, 25 so far today" while viewing an individual post.
45   #You may edit the wording to suit your preference.
46   #If you wish to leave a word/phrase empty, use two quotes '' instead of just leaving it completely blank.
47   #Insert only one instance of this line to avoid double tracking.
48   #Change '0' to '1' if you wish to track unique sessions.
49   #Change 'show' to 'noshow' if you donot wish to display the post count information.
50
512. To display the number of views per post on the main index page, click on Main Index Template (index.php) in the Theme Editor panel.
52
53   #Find this line:
54   
55    <?php the_time('F jS, Y') ?>
56   
57   #Right after it, insert this line:
58               
59                <br /><?php if (function_exists('todays_overall_main')) { todays_overall_main($post->ID, 'Viewed', 'times', 'so far today'); } ?>
60         
61         #The line will display something like "Visited 300 times, 25 so far today" under each post heading
62         #while browsing your main page.
63   #You may edit the wording to suit your preference.
64   #If you wish to leave a word/phrase empty, use two quotes '' instead of just leaving it completely blank.
65   
663. To add the sidebar widget, you may add it directly in the widgets panel of your theme (Design > Widgets).  If your theme is not widget-ready, click on sidebar.php in the Theme Editor panel and do the following:
67
68         #Add the following code:
69         
70          <h3>Top Posts for Today</h3>
71    <?php if (function_exists('todays_count_widget')) { todays_count_widget('views', 'ul'); }?>
72   
73   #You may edit the word "views" to your liking (e.g. visits, pageviews or leave it empty '').
74   #The list format defaults to an unordered list (ul).  If you would like an ordered list, change it to 'ol'.
75
764. To add the sidebar widget to show your most popular posts overall, you may add it directly in the widgets panel of your theme (Design > Widgets).  If your theme is not widget-ready, click on sidebar.php in the Theme Editor panel and do the following:
77         #Add the following code:
78         
79          <h3>Overall Top Posts</h3>
80    <?php if (function_exists('todays_overall_count_widget')) { todays_overall_count_widget('views', 'ul'); } ?>
81   
82   #You may edit the word "views" to your liking (e.g. visits, pageviews or leave it empty '').
83   #The list format defaults to an unordered list (ul).  If you would like an ordered list, change it to 'ol'.
84
85CREDITS
86-------
871. Thanks to Abe (www.yugatech.com/blog) for the whole idea of this plugin.
88*/
89
90#SET TIMEZONE OFFSET (UTC/GMT).  To know your timezone, visit http://www.timeanddate.com/worldclock/ and click on the appropriate city.  If you live in the Philippines, the offset is +8.
91$OFFSET = 0;
92#If you wish not to offset the date in which post views are recorded, leave it as is (0).
93
94add_action('init', 'jal_install');
95
96$table_name = $wpdb->prefix . "dailytopten";
97$table_name_all = $wpdb->prefix . "dailytoptenall";
98$table_posts = $wpdb->prefix . "posts";
99
100function jal_install () {
101   global $wpdb;
102   global $table_name;
103   global $table_name_all;
104   
105   if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
106        $sql = "CREATE TABLE " . $table_name . " (
107          id mediumint(9) NOT NULL AUTO_INCREMENT,
108          time date DEFAULT '0000-00-00' NOT NULL,
109          postnum int NOT NULL,
110          postcount int DEFAULT '0' NOT NULL,
111          UNIQUE KEY id (id)
112          )
113        ;";
114       
115        require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
116      dbDelta($sql);
117  }
118    if($wpdb->get_var("SHOW TABLES LIKE '$table_name_all'") != $table_name_all) {
119        $sqall = "CREATE TABLE " . $table_name_all . " (
120          id mediumint(9) NOT NULL AUTO_INCREMENT,
121          postnum int NOT NULL,
122          postcount int DEFAULT '0' NOT NULL,
123          UNIQUE KEY id (id)
124          )
125        ;";
126        require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
127      dbDelta($sqall);
128   }
129  }
130function todays_date()
131{
132    global $OFFSET;
133    $format = "Y-m-d";
134    if ($offset) {
135    $nowtime = gmdate($format, time() + 3600*$OFFSET);
136    } else {
137    $nowtime = date($format, time());
138    }
139    return $nowtime;
140}
141function has_viewed($postno, $all) {
142        if ($all == 1) {
143        $temp = explode("&", $_SESSION["a"]);
144        if(!in_array($postno, $temp)){
145        $_SESSION["a"] .= $postno."&";
146        return 0;
147        } else {
148        return 1;       
149   }
150  } 
151   else if ($all == 2) {
152   $temp = explode("&", $_SESSION["n"]);
153        if(!in_array($postno, $temp)){
154        $_SESSION["n"] .= $postno."&";
155        return 0;
156        } else {
157        return 1;
158    }
159   }
160  }
161 
162function todays_count($postnum, $unique) {
163        global $wpdb;
164        global $table_name;
165        if ($unique) {
166        $viewed = has_viewed($postnum,2);       
167        }
168        $nowisnow = todays_date();
169        $checkpost = $wpdb->get_row("SELECT id, postnum, time, postcount FROM $table_name WHERE postnum = '$postnum'");
170        $postid = $checkpost->id;
171        $postnumb = $checkpost->postnum;
172        $posttime = $checkpost->time;
173        $postcount = $checkpost->postcount;
174        if (!$postid) {
175        $wpdb->query("INSERT INTO $table_name (time, postnum, postcount) VALUES ('$nowisnow', $postnum, 1)");
176        }
177        else if ($posttime == $nowisnow) {
178        if (!$viewed) {
179        $wpdb->query("UPDATE $table_name SET postcount = postcount+1, time = '$nowisnow'        WHERE postnum = '$postnum'");
180   }
181        }
182        else {
183        if (!$viewed) {
184        $wpdb->query("UPDATE $table_name SET postcount = 1, time = '$nowisnow'  WHERE postnum = '$postnum'");
185         }
186        }
187        $post_c = $wpdb->get_var("SELECT postcount FROM $table_name WHERE postnum = '$postnum'");
188        return $post_c;
189        }
190
191function todays_count_widget($views,$list) {
192        global $wpdb;
193        global $table_name;
194        global $table_posts;
195        $home_url = get_settings('home');
196        $perma = get_settings('permalink_structure');
197        $nowisnow = todays_date();
198        $todays_widget_temp = "";
199        trim($views)!='' ? ($views = " ".$views) : ($views = "");
200        ((strtolower($list) == 'ul') || (strtolower($list) == 'ol')) ? $list = strtolower($list) : $list = 'ul';
201        if ($list == 'ul') {$lbeg = '<ul>'; $lend = '</ul>';} else {$lbeg = '<ol>'; $lend = '</ol>';}
202        $todays_widget = $lbeg."\n";;
203        $widgets = $wpdb->get_results("SELECT * from $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish' ORDER BY postcount DESC LIMIT 10");
204        $widgets_num = $wpdb->get_var("SELECT COUNT(*) FROM $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish'");
205       
206        if ($widgets_num) {
207        foreach ($widgets as $widget) {
208        $postnum = $widget->postnum;
209        $postcount = $widget->postcount;
210  $id_post = $widget->ID;
211  $title_post = $widget->post_title;
212  if ($perma) {
213  $home_url_perma = get_permalink($id_post);
214  $todays_widget_temp .= "<li><a href=\"$home_url_perma\">$title_post</a> ($postcount views)</li>\n";
215  } else {
216  $todays_widget_temp .= "<li><a href=\"$home_url/?p=$id_post\" title=\"$text\">$title_post</a> ($postcount$views)</li>\n";
217          }
218         }
219  }
220        if (!$todays_widget_temp) {
221        $todays_widget_temp .= "<li>No posts viewed yet.</li>";
222        }
223        $todays_widget .= $todays_widget_temp;
224        $todays_widget .= $lend."\n";;
225        echo $todays_widget;
226  }
227 
228function todays_overall_main($postnum, $visited, $times, $sofar) {
229        global $wpdb;
230        global $table_name;
231        global $table_name_all;
232        $nowisnow = todays_date();
233        $show_overall_main= "";
234  $post_c = $wpdb->get_var("SELECT postcount FROM $table_name WHERE time = '$nowisnow' AND postnum = '$postnum'");
235  $post_d = $wpdb->get_var("SELECT postcount FROM $table_name_all WHERE postnum = '$postnum'");
236  if ($post_d>0) {
237        $show_overall_main = "$visited $post_d $times";
238        if ($post_c) {$show_overall_main .= ", $post_c $sofar";}
239  }
240        echo $show_overall_main;
241        }
242       
243function todays_overall_count($postnum, $visited, $times, $sofar, $unique, $show) {
244        global $wpdb;
245        global $table_name_all;
246        if ($unique) {
247        $viewed = has_viewed($postnum,1);
248        }
249        $checkpost = $wpdb->get_row("SELECT id, postnum, postcount FROM $table_name_all WHERE postnum = '$postnum'");
250        $postid = $checkpost->id;
251        $postnumb = $checkpost->postnum;
252        $postcount = $checkpost->postcount;
253        if (!$postid) {
254        $wpdb->query("INSERT INTO $table_name_all (postnum, postcount) VALUES ($postnum, 1)");
255        $show_overall = "Visited 1 times";
256        $show_today = todays_count($postnum, $unique);
257        }
258        else {
259        if (!$viewed) {
260        $wpdb->query("UPDATE $table_name_all SET postcount = postcount+1 WHERE postnum = '$postnum'");
261        $postcount_ap = $postcount+1;
262  } else {
263  $postcount_ap = $postcount;
264  }
265        $show_today = todays_count($postnum, $unique);
266        if ($postcount_ap>0) {
267        $show_overall = "$visited $postcount_ap $times";
268        if ($show_today) {$show_overall .= ", $show_today $sofar";}
269         }
270  }
271  if ($show != 'noshow') {
272        echo $show_overall;
273   }
274        }
275       
276function todays_overall_count_widget($views,$list) {
277        global $wpdb;
278        global $table_name_all;
279        global $table_posts;
280        $home_url = get_settings('home');
281        $perma = get_settings('permalink_structure');
282        $nowisnow = todays_date();
283        $todays_overall_widget_temp = "";
284        trim($views)!='' ? ($views = " ".$views) : ($views = "");
285        ((strtolower($list) == 'ul') || (strtolower($list) == 'ol')) ? $list = strtolower($list) : $list = 'ul';
286        if ($list == 'ul') {$lbeg = '<ul>'; $lend = '</ul>';} else {$lbeg = '<ol>'; $lend = '</ol>';}
287        $todays_overall_widget = $lbeg."\n";
288        $widgets = $wpdb->get_results("SELECT * from $table_name_all inner join $table_posts on $table_posts.ID=$table_name_all.postnum WHERE postcount > 0 and post_status = 'publish' ORDER BY postcount DESC LIMIT 10");
289        $widgets_num = $wpdb->get_var("SELECT COUNT(*) FROM $table_name_all inner join $table_posts on $table_posts.ID=$table_name_all.postnum WHERE postcount > 0 and post_status = 'publish'");
290       
291        if ($widgets_num) {
292        foreach ($widgets as $widget) {
293        $postnum = $widget->postnum;
294        $postcount = $widget->postcount;
295  $id_post = $widget->ID;
296  $title_post = $widget->post_title;
297  if ($perma) {
298  $home_url_perma = get_permalink($id_post);
299  $todays_overall_widget_temp .= "<li><a href=\"$home_url_perma\">$title_post</a> ($postcount views)</li>\n";
300  } else {
301  $todays_overall_widget_temp .= "<li><a href=\"$home_url/?p=$id_post\" title=\"$text\">$title_post</a> ($postcount$views)</li>\n";
302          }
303         }
304  }
305        if (!$todays_overall_widget_temp) {
306        $todays_overall_widget_temp .= "<li>No posts viewed yet.</li>\n";
307        }
308        $todays_overall_widget .= $todays_overall_widget_temp;
309        $todays_overall_widget .= $lend."\n";
310        echo $todays_overall_widget;
311  }
312
313function admin_todays_count_widget() {
314        global $wpdb;
315        global $table_name;
316        global $table_posts;
317        $home_url = get_settings('home');
318        $perma = get_settings('permalink_structure');
319        $nowisnow = todays_date();
320        $todays_widget_temp = "";
321        $widgets = $wpdb->get_results("SELECT * FROM $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish' ORDER BY postcount DESC LIMIT 10");
322        $widgets_num = $wpdb->get_var("SELECT COUNT(*) FROM $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish'");
323        if ($widgets_num) {
324        foreach ($widgets as $widget) {
325        $postnum = $widget->postnum;
326        $postcount = $widget->postcount;
327  $id_post = $widget->ID;
328  $title_post = $widget->post_title;
329  if ($perma) {
330  $home_url_perma = get_permalink($id_post);
331  $todays_widget_temp .= "<h5 style=\"font-size: 12px; display: inline;\"><a href=\"$home_url_perma\">$title_post</a></h5> <span style=\"font-size: 12px; color: #999999;\">| $postcount views</span><br>";
332  } else {
333  $todays_widget_temp .= "<h5 style=\"font-size: 12px; display: inline;\"><a href=\"$home_url/?p=$id_post\" title=\"$text\">$title_post</a></h5> <span style=\"font-size: 12px; color: #999999;\">| $postcount views</span><br>";
334          }
335         }
336  }
337        if (!$todays_widget_temp) {
338        $todays_widget_temp .= "No posts viewed yet.";
339        }
340        $todays_widget .= $todays_widget_temp;
341        echo $todays_widget;
342  }
343
344function admin_rightnow() {
345        global $wpdb;
346        global $table_name;
347        global $table_posts;
348        $home_url = get_settings('home');
349        $perma = get_settings('permalink_structure');
350        $nowisnow = todays_date();
351        //$todays_widget = "<ul>";
352        $rightnow_widget_temp = "";
353        $widgets = $wpdb->get_results("SELECT * FROM $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish' ORDER BY postcount DESC LIMIT 1");
354        $widgets_num = $wpdb->get_var("SELECT COUNT(*) FROM $table_name inner join $table_posts on $table_posts.ID=$table_name.postnum WHERE time = '$nowisnow' and postcount > 0 and post_status = 'publish'");
355        if ($widgets_num) {
356  $rightnow_widget_temp = "<h3>Top Post of the Day</h3><p>";
357        foreach ($widgets as $widget) {
358        $postnum = $widget->postnum;
359        $postcount = $widget->postcount;
360  $id_post = $widget->ID;
361  $title_post = $widget->post_title;
362  if ($perma) {
363  $home_url_perma = get_permalink($id_post);
364  $rightnow_widget_temp .= "The <a href=\"http://www.alleba.com/blog/2007/03/27/wordpress-plugin-daily-top-10-posts/\">most popular blog post</a> of the day is <a href=\"$home_url_perma\">$title_post</a>, viewed $postcount times so far. <a href=\"#topten\" class=\"rbutton\">more...</a></p>";
365  } else {
366  $rightnow_widget_temp .= "The <a href=\"http://www.alleba.com/blog/2007/03/27/wordpress-plugin-daily-top-10-posts/\">most popular blog post</a> of the day is <a href=\"$home_url/?p=$id_post\">$title_post</a>, viewed $postcount times so far. <a href=\"#topten\" class=\"rbutton\">more...</a></p>";
367          }
368   }
369  }
370        if (!$rightnow_widget_temp) {
371        $rightnow_widget_temp = "<h3>Top Post of the Day</h3><p>";
372        $rightnow_widget_temp .= "No posts viewed yet today.</p>";
373        }
374        $rightnow_widget .= $rightnow_widget_temp;
375        echo $rightnow_widget;
376  }
377add_action('activity_box_end', 'admin_rightnow');
378add_action('wp_dashboard_setup', 'dailytopten_register_dashboard_widget');
379function dailytopten_register_dashboard_widget() {
380        wp_register_sidebar_widget('dashboard_dailytopten', __('<a name="topten"></a>Top Blog Posts of the Day', 'dailytopten'), 'dashboard_dailytopten',
381                array(
382                'width' => 'half',
383                'height' => 'single',
384                )
385        );
386}
387 
388add_filter('wp_dashboard_widgets', 'dailytopten_add_dashboard_widget');
389function dailytopten_add_dashboard_widget($widgets) {
390        global $wp_registered_widgets;
391        if (!isset($wp_registered_widgets['dashboard_dailytopten'])) {
392                return $widgets;
393        }
394        array_splice($widgets, sizeof($widgets)-1, 0, 'dashboard_dailytopten');
395        return $widgets;
396}
397
398function dashboard_dailytopten($sidebar_args) {
399        global $wpdb;
400        extract($sidebar_args, EXTR_SKIP);
401        echo $before_widget;
402        echo $before_title;
403        echo $widget_name;
404        echo $after_title;
405        admin_todays_count_widget();
406        echo $after_widget;
407}
408
409function widget_dailytopten_init() {
410        if(!function_exists('register_sidebar_widget')) { return; }
411        function widget_dailytopten($args) {
412            extract($args);
413            echo $before_widget . $before_title . "Top Posts of the Day" . $after_title;
414            todays_count_widget('views', 'ul');
415            echo $after_widget;
416        }
417        register_sidebar_widget('Daily Top 10 Posts','widget_dailytopten');
418    }
419add_action('plugins_loaded', 'widget_dailytopten_init');
420
421function widget_dailytopten_overall_init() {
422        if(!function_exists('register_sidebar_widget')) { return; }
423        function widget_dailytopten_overall($args) {
424            extract($args);
425            echo $before_widget . $before_title . "Top Posts Overall" . $after_title;
426            todays_overall_count_widget('views', 'ul');
427            echo $after_widget;
428        }
429        register_sidebar_widget('Overall Top 10 Posts','widget_dailytopten_overall');
430    }
431add_action('plugins_loaded', 'widget_dailytopten_overall_init');
432
433?>
Note: See TracBrowser for help on using the browser.