| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Top 10 |
| 4 |
Version: 1.2 |
| 5 |
Plugin URI: http://ajaydsouza.com/wordpress/plugins/top-10/ |
| 6 |
Description: Count daily and total 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 = "2.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 |
$tptn_settings = tptn_read_options(); |
| 39 |
|
| 40 |
$current_user = wp_get_current_user(); |
| 41 |
$post_author = ( $current_user->ID == $post->post_author ? true : false ); |
| 42 |
|
| 43 |
if((is_single() || is_page())) { |
| 44 |
if (!(($post_author)&&(!$tptn_settings['track_authors']))) { |
| 45 |
$id = intval($post->ID); |
| 46 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>'; |
| 47 |
echo $output; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Function to add count to content |
| 53 |
function tptn_pc_content($content) { |
| 54 |
global $single, $post; |
| 55 |
$tptn_settings = tptn_read_options(); |
| 56 |
$id = intval($post->ID); |
| 57 |
|
| 58 |
if(($single)&&($tptn_settings['add_to_content'])) { |
| 59 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 60 |
return $content.$output; |
| 61 |
} else { |
| 62 |
return $content; |
| 63 |
} |
| 64 |
} |
| 65 |
add_filter('the_content', 'tptn_pc_content'); |
| 66 |
|
| 67 |
// Function to manually display count |
| 68 |
function echo_tptn_post_count() { |
| 69 |
global $post; |
| 70 |
$id = intval($post->ID); |
| 71 |
|
| 72 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 73 |
echo $output; |
| 74 |
} |
| 75 |
|
| 76 |
// Function to show popular posts |
| 77 |
function tptn_show_pop_posts() { |
| 78 |
global $wpdb, $siteurl, $tableposts, $id; |
| 79 |
$table_name = $wpdb->prefix . "top_ten"; |
| 80 |
$tptn_settings = tptn_read_options(); |
| 81 |
$limit = $tptn_settings['limit']; |
| 82 |
|
| 83 |
$sql = "SELECT postnumber, cntaccess , ID, post_type, post_status "; |
| 84 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 85 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 86 |
$sql .= "AND post_status = 'publish' "; |
| 87 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 88 |
|
| 89 |
$results = $wpdb->get_results($sql); |
| 90 |
|
| 91 |
echo '<div id="crp_related">'.$tptn_settings['title']; |
| 92 |
echo '<ul>'; |
| 93 |
if ($results) { |
| 94 |
foreach ($results as $result) { |
| 95 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 96 |
} |
| 97 |
} |
| 98 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 99 |
echo '</ul>'; |
| 100 |
echo '</div>'; |
| 101 |
} |
| 102 |
|
| 103 |
// Function to show daily popular posts |
| 104 |
function tptn_show_daily_pop_posts() { |
| 105 |
global $wpdb, $siteurl, $tableposts, $id; |
| 106 |
$table_name = $wpdb->prefix . "top_ten_daily"; |
| 107 |
$tptn_settings = tptn_read_options(); |
| 108 |
$limit = $tptn_settings['limit']; |
| 109 |
|
| 110 |
$sql = "SELECT postnumber, cntaccess , ID, post_type, post_status "; |
| 111 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 112 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 113 |
$sql .= "AND post_status = 'publish' "; |
| 114 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 115 |
|
| 116 |
$results = $wpdb->get_results($sql); |
| 117 |
|
| 118 |
echo '<div id="crp_related">'.$tptn_settings['title_daily']; |
| 119 |
echo '<ul>'; |
| 120 |
if ($results) { |
| 121 |
foreach ($results as $result) { |
| 122 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 123 |
} |
| 124 |
} |
| 125 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 126 |
echo '</ul>'; |
| 127 |
echo '</div>'; |
| 128 |
} |
| 129 |
|
| 130 |
// Default Options |
| 131 |
function tptn_default_options() { |
| 132 |
$title = __('<h3>Popular Posts</h3>','ald_tptn_plugin'); |
| 133 |
$title_daily = __('<h3>Daily Popular</h3>','ald_tptn_plugin'); |
| 134 |
|
| 135 |
$tptn_settings = Array ( |
| 136 |
show_credit => true, // Add link to plugin page of my blog in top posts list |
| 137 |
add_to_content => true, // Add post count to content (only on single pages) |
| 138 |
exclude_pages => true, // Exclude Pages |
| 139 |
track_authors => false, // Track Authors visits |
| 140 |
pv_in_admin => true, // Add an extra column on edit posts/pages to display page views? |
| 141 |
count_disp_form => '(Visited %totalcount% times, %dailycount% visits today)', // Format to display the count |
| 142 |
title => $title, // Title of Popular Posts |
| 143 |
title_daily => $title_daily, // Title of Daily Popular |
| 144 |
limit => '10', // How many posts to display? |
| 145 |
); |
| 146 |
return $tptn_settings; |
| 147 |
} |
| 148 |
|
| 149 |
// Function to read options from the database |
| 150 |
function tptn_read_options() { |
| 151 |
|
| 152 |
// Upgrade table code |
| 153 |
global $tptn_db_version; |
| 154 |
$installed_ver = get_option( "tptn_db_version" ); |
| 155 |
|
| 156 |
if( $installed_ver != $tptn_db_version ) tptn_install(); |
| 157 |
|
| 158 |
$tptn_settings_changed = false; |
| 159 |
|
| 160 |
$defaults = tptn_default_options(); |
| 161 |
|
| 162 |
$tptn_settings = array_map('stripslashes',(array)get_option('ald_tptn_settings')); |
| 163 |
unset($tptn_settings[0]); // produced by the (array) casting when there's nothing in the DB |
| 164 |
|
| 165 |
foreach ($defaults as $k=>$v) { |
| 166 |
if (!isset($tptn_settings[$k])) { |
| 167 |
$tptn_settings[$k] = $v; |
| 168 |
$tptn_settings_changed = true; |
| 169 |
} |
| 170 |
} |
| 171 |
if ($tptn_settings_changed == true) |
| 172 |
update_option('ald_tptn_settings', $tptn_settings); |
| 173 |
|
| 174 |
return $tptn_settings; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
// Create tables to store pageviews |
| 179 |
function tptn_install() { |
| 180 |
global $wpdb; |
| 181 |
global $tptn_db_version; |
| 182 |
|
| 183 |
$table_name = $wpdb->prefix . "top_ten"; |
| 184 |
$table_name_daily = $wpdb->prefix . "top_ten_daily"; |
| 185 |
|
| 186 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name) { |
| 187 |
|
| 188 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 189 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 190 |
postnumber int NOT NULL, |
| 191 |
cntaccess int NOT NULL, |
| 192 |
PRIMARY KEY (accessedid) |
| 193 |
);"; |
| 194 |
|
| 195 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 196 |
dbDelta($sql); |
| 197 |
|
| 198 |
add_option("tptn_db_version", $tptn_db_version); |
| 199 |
} |
| 200 |
|
| 201 |
if($wpdb->get_var("show tables like '$table_name_daily'") != $table_name_daily) { |
| 202 |
|
| 203 |
$sql = "CREATE TABLE " . $table_name_daily . " ( |
| 204 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 205 |
postnumber int NOT NULL, |
| 206 |
cntaccess int NOT NULL, |
| 207 |
PRIMARY KEY (accessedid) |
| 208 |
);"; |
| 209 |
|
| 210 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 211 |
dbDelta($sql); |
| 212 |
|
| 213 |
add_option("tptn_db_version", $tptn_db_version); |
| 214 |
} |
| 215 |
|
| 216 |
// Upgrade table code |
| 217 |
$installed_ver = get_option( "tptn_db_version" ); |
| 218 |
|
| 219 |
if( $installed_ver != $tptn_db_version ) { |
| 220 |
|
| 221 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 222 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 223 |
postnumber int NOT NULL, |
| 224 |
cntaccess int NOT NULL, |
| 225 |
PRIMARY KEY (accessedid) |
| 226 |
);"; |
| 227 |
|
| 228 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 229 |
dbDelta($sql); |
| 230 |
|
| 231 |
$sql = "CREATE TABLE " . $table_name_daily . " ( |
| 232 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 233 |
postnumber int NOT NULL, |
| 234 |
cntaccess int NOT NULL, |
| 235 |
PRIMARY KEY (accessedid) |
| 236 |
);"; |
| 237 |
|
| 238 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 239 |
dbDelta($sql); |
| 240 |
|
| 241 |
update_option( "tptn_db_version", $tptn_db_version ); |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
if (function_exists('register_activation_hook')) { |
| 246 |
register_activation_hook(__FILE__,'tptn_install'); |
| 247 |
register_activation_hook(__FILE__, 'tptn_cron_install'); |
| 248 |
} |
| 249 |
|
| 250 |
/* Schedule daily cron to clean up the Daily Table */ |
| 251 |
add_action('tptn_cron_hook', 'tptn_daily_clean'); |
| 252 |
function tptn_cron_install() { |
| 253 |
if (!wp_next_scheduled('tptn_cron_hook')) { |
| 254 |
wp_schedule_event(mktime(0,0), 'daily', 'tptn_cron_hook'); |
| 255 |
} |
| 256 |
} |
| 257 |
function tptn_daily_clean() { |
| 258 |
global $wpdb; |
| 259 |
$table_name_daily = $wpdb->prefix . "table_name_daily"; |
| 260 |
|
| 261 |
$sql = "TRUNCATE TABLE $table_name_daily"; |
| 262 |
$wpdb->query($sql); |
| 263 |
} |
| 264 |
|
| 265 |
if (function_exists('register_deactivation_hook')) { |
| 266 |
register_deactivation_hook(__FILE__, 'tptn_cron_deinstall'); |
| 267 |
} |
| 268 |
function tptn_cron_deinstall() { |
| 269 |
wp_clear_scheduled_hook('tptn_cron_hook'); |
| 270 |
} |
| 271 |
|
| 272 |
// Create a WordPress Widget for Popular Posts |
| 273 |
function widget_tptn_pop($args) { |
| 274 |
global $wpdb, $siteurl, $tableposts, $id; |
| 275 |
|
| 276 |
extract($args); // extracts before_widget,before_title,after_title,after_widget |
| 277 |
|
| 278 |
$table_name = $wpdb->prefix . "top_ten"; |
| 279 |
$tptn_settings = tptn_read_options(); |
| 280 |
$limit = $tptn_settings['limit']; |
| 281 |
|
| 282 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 283 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 284 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 285 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 286 |
|
| 287 |
$results = $wpdb->get_results($sql); |
| 288 |
|
| 289 |
$title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts')); |
| 290 |
|
| 291 |
echo $before_widget; |
| 292 |
echo $before_title.$title.$after_title; |
| 293 |
echo '<ul>'; |
| 294 |
if ($results) { |
| 295 |
foreach ($results as $result) { |
| 296 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 297 |
} |
| 298 |
} |
| 299 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 300 |
echo '</ul>'; |
| 301 |
|
| 302 |
echo $after_widget; |
| 303 |
} |
| 304 |
|
| 305 |
// Create a WordPress Widget for Daily Popular Posts |
| 306 |
function widget_tptn_pop_daily($args) { |
| 307 |
global $wpdb, $siteurl, $tableposts, $id; |
| 308 |
|
| 309 |
extract($args); // extracts before_widget,before_title,after_title,after_widget |
| 310 |
|
| 311 |
$table_name = $wpdb->prefix . "top_ten_daily"; |
| 312 |
$tptn_settings = tptn_read_options(); |
| 313 |
$limit = $tptn_settings['limit']; |
| 314 |
|
| 315 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 316 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 317 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 318 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 319 |
|
| 320 |
$results = $wpdb->get_results($sql); |
| 321 |
|
| 322 |
$title = (($tptn_settings['title_daily']) ? strip_tags($tptn_settings['title_daily']) : __('Daily Popular')); |
| 323 |
|
| 324 |
echo $before_widget; |
| 325 |
echo $before_title.$title.$after_title; |
| 326 |
echo '<ul>'; |
| 327 |
if ($results) { |
| 328 |
foreach ($results as $result) { |
| 329 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 330 |
} |
| 331 |
} |
| 332 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 333 |
echo '</ul>'; |
| 334 |
|
| 335 |
echo $after_widget; |
| 336 |
} |
| 337 |
|
| 338 |
function init_tptn(){ |
| 339 |
register_sidebar_widget(__('Popular Posts','ald_tptn_plugin'), 'widget_tptn_pop'); |
| 340 |
register_sidebar_widget(__('Daily Popular','ald_tptn_plugin'), 'widget_tptn_pop_daily'); |
| 341 |
} |
| 342 |
add_action("plugins_loaded", "init_tptn"); |
| 343 |
|
| 344 |
// This function adds an Options page in WP Admin |
| 345 |
if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { |
| 346 |
require_once(ALD_TPTN_DIR . "/admin.inc.php"); |
| 347 |
} |
| 348 |
|
| 349 |
?> |