<?php
/*
Plugin Name: Top 10 posts, Views per post
Version: 1.2
Plugin URI: http://weblogtoolscollection.com/
Description: Show Top 10 posts on your blog and count visits per post
Author: Mark Ghosh (LaughingLizard)
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.

*/

// 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) {
// Polyglot begins
            
if (function_exists('lang_picker_respect_more')) {
                
$result->postnumber lang_picker_respect_more($result->postnumber);
            } 
// Polyglot ends
            
echo '<li><a href="'.get_permalink($result->postnumber).'">'.lang_picker_respect_more(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;
}
?>