| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Top 10 |
| 4 |
Version: 1.1 |
| 5 |
Plugin URI: http://ajaydsouza.com/wordpress/plugins/top-10/ |
| 6 |
Description: Count visits per post and display the most popular posts based on the number of views. Based on the plugin by <a href="http://weblogtoolscollection.com">Mark Ghosh</a>. <a href="options-general.php?page=tptn_options">Configure...</a> |
| 7 |
Author: Ajay D'Souza |
| 8 |
Author URI: http://ajaydsouza.com/ |
| 9 |
*/ |
| 10 |
|
| 11 |
//if (!defined('ABSPATH')) die("Aren't you supposed to come here via WP-Admin?"); |
| 12 |
if (!function_exists('add_action')) { |
| 13 |
$wp_root = '../../..'; |
| 14 |
if (file_exists($wp_root.'/wp-load.php')) { |
| 15 |
require_once($wp_root.'/wp-load.php'); |
| 16 |
} else { |
| 17 |
require_once($wp_root.'/wp-config.php'); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
$tptn_db_version = "1.0"; |
| 22 |
|
| 23 |
function ald_tptn_init() { |
| 24 |
load_plugin_textdomain('myald_tptn_plugin', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__))); |
| 25 |
} |
| 26 |
add_action('init', 'ald_tptn_init'); |
| 27 |
|
| 28 |
define('ALD_TPTN_DIR', dirname(__FILE__)); |
| 29 |
|
| 30 |
/********************************************************************* |
| 31 |
* Main Function (Do not edit) * |
| 32 |
********************************************************************/ |
| 33 |
// Update post views |
| 34 |
add_action('wp_head','tptn_add_viewed_count'); |
| 35 |
function tptn_add_viewed_count() { |
| 36 |
global $post, $wpdb, $single; |
| 37 |
$table_name = $wpdb->prefix . "top_ten"; |
| 38 |
|
| 39 |
if(is_single() || is_page()) { |
| 40 |
$id = intval($post->ID); |
| 41 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>'; |
| 42 |
echo $output; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
function tptn_pc_content($content) { |
| 47 |
global $single, $post; |
| 48 |
$tptn_settings = tptn_read_options(); |
| 49 |
$id = intval($post->ID); |
| 50 |
|
| 51 |
if(($single)&&($tptn_settings['add_to_content'])) { |
| 52 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 53 |
return $content.$output; |
| 54 |
} else { |
| 55 |
return $content; |
| 56 |
} |
| 57 |
} |
| 58 |
add_filter('the_content', 'tptn_pc_content'); |
| 59 |
|
| 60 |
function echo_tptn_post_count() { |
| 61 |
global $post; |
| 62 |
$id = intval($post->ID); |
| 63 |
|
| 64 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 65 |
echo $output; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
// Function to show popular posts |
| 70 |
function tptn_show_pop_posts() { |
| 71 |
global $wpdb, $siteurl, $tableposts, $id; |
| 72 |
$table_name = $wpdb->prefix . "top_ten"; |
| 73 |
$tptn_settings = tptn_read_options(); |
| 74 |
$limit = $tptn_settings['limit']; |
| 75 |
|
| 76 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 77 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 78 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 79 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 80 |
|
| 81 |
$results = $wpdb->get_results($sql); |
| 82 |
|
| 83 |
echo '<div id="crp_related">'.$tptn_settings['title']; |
| 84 |
echo '<ul>'; |
| 85 |
if ($results) { |
| 86 |
foreach ($results as $result) { |
| 87 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 88 |
} |
| 89 |
} |
| 90 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 91 |
echo '</ul>'; |
| 92 |
echo '</div>'; |
| 93 |
} |
| 94 |
// Create a WordPress Widget for Popular Posts |
| 95 |
function widget_tptn_pop($args) { |
| 96 |
global $wpdb, $siteurl, $tableposts, $id; |
| 97 |
|
| 98 |
extract($args); // extracts before_widget,before_title,after_title,after_widget |
| 99 |
|
| 100 |
$table_name = $wpdb->prefix . "top_ten"; |
| 101 |
$tptn_settings = tptn_read_options(); |
| 102 |
$limit = $tptn_settings['limit']; |
| 103 |
|
| 104 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 105 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 106 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 107 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 108 |
|
| 109 |
$results = $wpdb->get_results($sql); |
| 110 |
|
| 111 |
$title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts')); |
| 112 |
|
| 113 |
echo $before_widget; |
| 114 |
echo $before_title.$title.$after_title; |
| 115 |
echo '<ul>'; |
| 116 |
if ($results) { |
| 117 |
foreach ($results as $result) { |
| 118 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 119 |
} |
| 120 |
} |
| 121 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 122 |
echo '</ul>'; |
| 123 |
|
| 124 |
echo $after_widget; |
| 125 |
} |
| 126 |
|
| 127 |
function init_tptn(){ |
| 128 |
register_sidebar_widget(__('Popular Posts'), 'widget_tptn_pop'); |
| 129 |
} |
| 130 |
add_action("plugins_loaded", "init_tptn"); |
| 131 |
|
| 132 |
|
| 133 |
// Default Options |
| 134 |
function tptn_default_options() { |
| 135 |
$title = __('<h3>Popular Posts</h3>'); |
| 136 |
|
| 137 |
$tptn_settings = Array ( |
| 138 |
show_credit => true, // Add link to plugin page of my blog in top posts list |
| 139 |
add_to_content => true, // Add post count to content (only on single pages) |
| 140 |
exclude_pages => true, // Exclude Pages |
| 141 |
count_disp_form => '(Visited %totalcount% times)', // Format to display the count |
| 142 |
title => $title, // Add before the content |
| 143 |
limit => '10', // How many posts to display? |
| 144 |
); |
| 145 |
return $tptn_settings; |
| 146 |
} |
| 147 |
|
| 148 |
// Function to read options from the database |
| 149 |
function tptn_read_options() { |
| 150 |
|
| 151 |
$tptn_settings_changed = false; |
| 152 |
|
| 153 |
$defaults = tptn_default_options(); |
| 154 |
|
| 155 |
$tptn_settings = array_map('stripslashes',(array)get_option('ald_tptn_settings')); |
| 156 |
unset($tptn_settings[0]); // produced by the (array) casting when there's nothing in the DB |
| 157 |
|
| 158 |
foreach ($defaults as $k=>$v) { |
| 159 |
if (!isset($tptn_settings[$k])) { |
| 160 |
$tptn_settings[$k] = $v; |
| 161 |
$tptn_settings_changed = true; |
| 162 |
} |
| 163 |
} |
| 164 |
if ($tptn_settings_changed == true) |
| 165 |
update_option('ald_tptn_settings', $tptn_settings); |
| 166 |
|
| 167 |
return $tptn_settings; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
// Create tables to store pageviews |
| 172 |
function tptn_install() { |
| 173 |
global $wpdb; |
| 174 |
global $tptn_db_version; |
| 175 |
|
| 176 |
$table_name = $wpdb->prefix . "top_ten"; |
| 177 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name) { |
| 178 |
|
| 179 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 180 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 181 |
postnumber int NOT NULL, |
| 182 |
cntaccess int NOT NULL, |
| 183 |
PRIMARY KEY (accessedid) |
| 184 |
);"; |
| 185 |
|
| 186 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 187 |
dbDelta($sql); |
| 188 |
|
| 189 |
add_option("tptn_db_version", $tptn_db_version); |
| 190 |
} |
| 191 |
|
| 192 |
// Upgrade table code |
| 193 |
$installed_ver = get_option( "tptn_db_version" ); |
| 194 |
|
| 195 |
if( $installed_ver != $tptn_db_version ) { |
| 196 |
|
| 197 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 198 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 199 |
postnumber int NOT NULL, |
| 200 |
cntaccess int NOT NULL, |
| 201 |
PRIMARY KEY (accessedid) |
| 202 |
);"; |
| 203 |
|
| 204 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 205 |
dbDelta($sql); |
| 206 |
|
| 207 |
update_option( "tptn_db_version", $tptn_db_version ); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
if (function_exists('register_activation_hook')) { |
| 212 |
register_activation_hook(__FILE__,'tptn_install'); |
| 213 |
} |
| 214 |
|
| 215 |
// This function adds an Options page in WP Admin |
| 216 |
if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { |
| 217 |
require_once(ALD_TPTN_DIR . "/admin.inc.php"); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
?> |