Created
December 11, 2012 10:02
-
-
Save benmay/4257453 to your computer and use it in GitHub Desktop.
simple downloader tracker / counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Download counter | |
Plugin URI: | |
Description: Very simple download counter | |
Author: Ben May | |
Version: 0.1 | |
Author URI: | |
*/ | |
Class Dstdl{ | |
var $db; | |
var $table; | |
function __construct() | |
{ | |
global $wpdb; | |
$this->db = $wpdb; | |
$this->table = $this->db->prefix . 'file_downloads'; | |
// Check to install. | |
add_action( 'init', array(&$this, 'install' ) ); | |
// Watch for DLs | |
add_action( 'init', array(&$this, 'dl' ) ); | |
// Admin page for reports. | |
add_action( 'admin_menu', array(&$this, 'add_menu') ); | |
} | |
function add_menu() | |
{ | |
add_submenu_page( | |
'index.php', | |
'File download hits', | |
'File downloads', | |
'administrator', | |
'dst_download_stats', | |
array( &$this, 'reports' ) | |
); | |
} | |
function reports() | |
{ | |
$download_data = $this->db->get_results( "select * from $this->table order by count desc"); | |
echo '<div class="wrap">'; | |
echo '<h2>File Downloads</h2>'; | |
echo '<p>Sorting from most downloads to least</p>'; | |
echo '<table class="widefat fixed" cellspacing="0">'; | |
echo '<thead> | |
<tr> | |
<tr> | |
<th> Post </th> | |
<th> Type </th> | |
<th> Downloads </th> | |
</tr> | |
</tr> | |
</thead> <tbody> '; | |
foreach( $download_data as $dl ){ | |
$post = get_post( $dl->post_id ); | |
echo ' <tr> | |
<th>'.$post->post_title.'</th> | |
<td>'.$dl->note.'</td> | |
<td>'.$dl->count.'</td> | |
</tr>'; | |
} | |
echo '</table>'; | |
echo '</div>'; | |
} | |
function dl() | |
{ | |
// If URL ain't set, not doing much! | |
if( ! isset( $_GET[ 'dst_download_file' ] ) ) | |
return; | |
// Don't want to count bots in the download hits. | |
if( ! $this->is_bot() ) { | |
// Santize our vars, protect against monsters | |
$file = esc_url_raw( $_GET[ 'dst_download_file' ] ); | |
$pid = intval( $_GET['dst_post_id'] ); | |
$note = sanitize_text_field( $_GET[ 'dst_note' ] ); | |
// Insert to DB, if exists, update count | |
$this->db->query( " INSERT INTO $this->table | |
SET url='$file', | |
post_id='$pid', | |
note='$note', | |
count=1 | |
ON DUPLICATE KEY UPDATE count=count+1" ); | |
} | |
// Redirect 'em to the file they wanted. | |
wp_redirect( $_GET[ 'dst_download_file' ] ); | |
die; | |
} | |
public function download_url( $file, $post_id='', $note='' ) | |
{ | |
echo self::get_download_url( $file, $post_id, $note ); | |
} | |
public function get_download_url( $file, $post_id='', $note='' ) | |
{ | |
$vars = urldecode( $file ); | |
if( $post_id ) | |
$vars .= "&dst_post_id=$post_id"; | |
if( $note ) | |
$vars .= "&dst_note=$note"; | |
return get_bloginfo('url') . "?dst_download_file=$vars"; | |
} | |
function install() | |
{ | |
$result = $this->db->get_results("SHOW TABLES LIKE '".$this->table."'" ); | |
if( empty( $result ) ) { | |
$sql = "CREATE TABLE IF NOT EXISTS `{$this->table}` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`post_id` int(11) NOT NULL, | |
`url` varchar(512) NOT NULL, | |
`note` varchar(512) NOT NULL, | |
`count` int(11) NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `combination_key` (`url`,`note`,`post_id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"; | |
$this->db->query( $sql ); | |
} | |
} | |
function is_bot(){ | |
$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", | |
"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", | |
"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", | |
"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", | |
"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", | |
"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", | |
"Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot", | |
"Butterfly","Twitturls","Me.dium","Twiceler"); | |
foreach($botlist as $bot) | |
if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false) | |
return true; | |
return false; | |
} | |
} | |
new Dstdl(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment