<?php
/*
Plugin Name: Top 10 posts, Views per post
Version: 1.2.tbble1
Plugin URI: http://weblogtoolscollection.com/
Description: Show Top 10 posts on your blog and count visits per post, updated for sidebar widget support
Author: Mark Ghosh (LaughingLizard), widgeting by TBBle
Author URI: http://weblogtoolscollection.com

Copyright (c) 2004
Released under the GPL license
http://www.gnu.org/licenses/gpl.txt

    This file is part of WordPress.
    WordPress is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    INSTALL: 
    Place this code where you want your top10 posts to show up (outside the wp-loop, anywhere in your templates):
    <?php show_pop_posts(); ?>
    
    Place this code where you want to show the number of visits per post (inside the wp-loop):
    (So for wp 1.5+ I have it right before the <?php endwhile; else: ?> in single.php)
    <?php show_post_count(); ?>

    PS: if you get an error, click on one of your posts to make that the most popular and things will catch on from there.

*/

function top10_init () {
    
// Make sure that the plugin is installed, if not, create the tables
    
$top10posts get_option('top10posts'); 
    if (
$top10posts == '') { 
        
$wpdb->query("create table mostAccessed (accessedid int not null auto_increment, postnumber int not null,cntaccess int not null,primary key(accessedid))");
        
update_option('top10posts'"top10postsver1");
    }
    
    if (
$top10posts == 'top10postsver1') { 
        
$wpdb->query("delete from mostAccessed where postnumber = 0");
        
$wpdb->query("alter table mostAccessed add accessedid int not null auto_increment, drop primary key, add primary key (accessedid)");
        
update_option('top10posts'"top10postsver12");
    }
    
add_action('shutdown','add_viewed_count');
    
    function 
add_viewed_count() {
        global 
$id$wpdb$single;
        if (
$single && isset($id) && $id 0) {    
            
$results $wpdb->get_results("select postnumber, cntaccess from mostAccessed where postnumber = '$id'");
            
$test 0;
            if (
$results) {
                foreach (
$results as $result) {
                    
$wpdb->query("update mostAccessed set cntaccess = cntaccess + 1 where postnumber = $result->postnumber");
                    
$test 1;
                }
            }
            if (
$test == 0) {
                
$wpdb->query("insert into mostAccessed(postnumber, cntaccess) values('$id', '1')");
            }
        }
    }
    
    function 
show_pop_posts() {
        global 
$wpdb$siteurl$tableposts$id;
        
$results $wpdb->get_results("select postnumber, cntaccess from mostAccessed ORDER BY cntaccess DESC LIMIT 10");
        echo 
"<ul>";
        if (
$results) {
            foreach (
$results as $result) {
                echo 
'<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
            }
        }
        echo 
"</ul>";
    }
    
    function 
show_post_count($beforecount='(Visited '$aftercount=' times)') {
        global 
$wpdb$id;
        
$resultscount $wpdb->get_row("select postnumber, cntaccess from mostAccessed WHERE postnumber = $id");
        echo 
$beforecount.$resultscount->cntaccess.$aftercount;
    }
    
// Below this point is stuff that relies on widget support
    
if ( !function_exists('register_sidebar_widget') )
        return;
    function 
widget_top10($args) {
        
$options get_option('widget_top10');
        
$title $options['title'];
        global 
$wpdb$siteurl$tableposts$id;
        
extract($args);
        echo 
$before_widget;
        echo 
$before_title;
//        _e('Top 10 Posts');
        
echo $title;
        echo 
$after_title;
        
$results $wpdb->get_results("select postnumber, cntaccess from mostAccessed ORDER BY cntaccess DESC LIMIT 10");
        echo 
"<ul>";
        if (
$results) {
            foreach (
$results as $result) {
                echo 
'<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
            }
        }
        echo 
"</ul>";
        echo 
$after_widget;
    }
    function 
widget_top10_control() {
        
$options get_option('widget_top10');
        if ( !
is_array($options) )
            
$options = array('title'=>'Top 10 Posts');
        if ( 
$_POST['top10-submit'] ) {

            
// Remember to sanitize and format use input appropriately.
            
$options['title'] = strip_tags(stripslashes($_POST['top10-title']));
            
update_option('widget_top10'$options);
        }
        
$title htmlspecialchars($options['title'], ENT_QUOTES);
        echo 
'<p style="text-align:right;"><label for="top10-title">Title: <input style="width: 200px;" id="top10-title" name="top10-title" type="text" value="'.$title.'" /></label></p>';
        echo 
'<input type="hidden" id="top10-submit" name="top10-submit" value="1" />';
    }
    
register_sidebar_widget('Top 10 posts''widget_top10');
    
register_widget_control('Top 10 posts''widget_top10_control'300100);
}
add_action('plugins_loaded''top10_init');
?>