| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Top 10 |
| 4 |
Version: 1.4 |
| 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 |
define('ALD_TPTN_DIR', dirname(__FILE__)); |
| 13 |
define('TPTN_LOCAL_NAME', 'tptn'); |
| 14 |
|
| 15 |
if (!function_exists('add_action')) { |
| 16 |
$wp_root = '../../..'; |
| 17 |
if (file_exists($wp_root.'/wp-load.php')) { |
| 18 |
require_once($wp_root.'/wp-load.php'); |
| 19 |
} else { |
| 20 |
require_once($wp_root.'/wp-config.php'); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
global $tptn_db_version; |
| 25 |
$tptn_db_version = "2.8"; |
| 26 |
|
| 27 |
function ald_tptn_init() { |
| 28 |
//* Begin Localization Code */ |
| 29 |
$tc_localizationName = TPTN_LOCAL_NAME; |
| 30 |
$tc_comments_locale = get_locale(); |
| 31 |
$tc_comments_mofile = ALD_TPTN_DIR . "/languages/" . $tc_localizationName . "-". $tc_comments_locale.".mo"; |
| 32 |
load_textdomain($tc_localizationName, $tc_comments_mofile); |
| 33 |
//* End Localization Code */ |
| 34 |
} |
| 35 |
add_action('init', 'ald_tptn_init'); |
| 36 |
|
| 37 |
/********************************************************************* |
| 38 |
* Main Function (Do not edit) * |
| 39 |
********************************************************************/ |
| 40 |
// Update post views |
| 41 |
add_action('wp_head','tptn_add_viewed_count'); |
| 42 |
function tptn_add_viewed_count() { |
| 43 |
global $post, $wpdb, $single; |
| 44 |
$table_name = $wpdb->prefix . "top_ten"; |
| 45 |
$tptn_settings = tptn_read_options(); |
| 46 |
|
| 47 |
$current_user = wp_get_current_user(); |
| 48 |
$post_author = ( $current_user->ID == $post->post_author ? true : false ); |
| 49 |
|
| 50 |
if((is_single() || is_page())) { |
| 51 |
if (!(($post_author)&&(!$tptn_settings['track_authors']))) { |
| 52 |
$id = intval($post->ID); |
| 53 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>'; |
| 54 |
echo $output; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Function to add count to content |
| 60 |
function tptn_pc_content($content) { |
| 61 |
global $single, $post; |
| 62 |
$tptn_settings = tptn_read_options(); |
| 63 |
$id = intval($post->ID); |
| 64 |
|
| 65 |
if((is_single())&&($tptn_settings['add_to_content'])) { |
| 66 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 67 |
return $content.$output; |
| 68 |
} elseif((is_page())&&($tptn_settings['count_on_pages'])) { |
| 69 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 70 |
return $content.$output; |
| 71 |
} else { |
| 72 |
return $content; |
| 73 |
} |
| 74 |
} |
| 75 |
add_filter('the_content', 'tptn_pc_content',9001); |
| 76 |
|
| 77 |
// Function to manually display count |
| 78 |
function echo_tptn_post_count() { |
| 79 |
global $post; |
| 80 |
$id = intval($post->ID); |
| 81 |
|
| 82 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 83 |
echo $output; |
| 84 |
} |
| 85 |
|
| 86 |
// Function to show popular posts |
| 87 |
function tptn_show_pop_posts() { |
| 88 |
global $wpdb, $siteurl, $tableposts, $id; |
| 89 |
$table_name = $wpdb->prefix . "top_ten"; |
| 90 |
$tptn_settings = tptn_read_options(); |
| 91 |
$limit = $tptn_settings['limit']; |
| 92 |
|
| 93 |
$sql = "SELECT postnumber, cntaccess , ID, post_type, post_status "; |
| 94 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 95 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 96 |
$sql .= "AND post_status = 'publish' "; |
| 97 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 98 |
|
| 99 |
$results = $wpdb->get_results($sql); |
| 100 |
|
| 101 |
echo '<div id="tptn_related">'.$tptn_settings['title']; |
| 102 |
echo '<ul>'; |
| 103 |
if ($results) { |
| 104 |
foreach ($results as $result) { |
| 105 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a>'; |
| 106 |
if ($tptn_settings['disp_list_count']) echo ' ('.$result->cntaccess.')'; |
| 107 |
echo '</li>'; |
| 108 |
} |
| 109 |
} |
| 110 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 111 |
echo '</ul>'; |
| 112 |
echo '</div>'; |
| 113 |
} |
| 114 |
|
| 115 |
// Function to show daily popular posts |
| 116 |
function tptn_show_daily_pop_posts() { |
| 117 |
global $wpdb, $siteurl, $tableposts, $id; |
| 118 |
$table_name = $wpdb->prefix . "top_ten_daily"; |
| 119 |
$tptn_settings = tptn_read_options(); |
| 120 |
$limit = $tptn_settings['limit']; |
| 121 |
|
| 122 |
$output = ''; |
| 123 |
if ($tptn_settings['d_use_js']) { |
| 124 |
$output .= '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-daily.js.php"></script>'; |
| 125 |
} else { |
| 126 |
$daily_range = $tptn_settings[daily_range]. ' DAY'; |
| 127 |
$current_date = $wpdb->get_var("SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $daily_range), INTERVAL 1 DAY) "); |
| 128 |
|
| 129 |
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status "; |
| 130 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 131 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 132 |
$sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' "; |
| 133 |
$sql .= "GROUP BY postnumber "; |
| 134 |
$sql .= "ORDER BY sumCount DESC LIMIT $limit"; |
| 135 |
|
| 136 |
$results = $wpdb->get_results($sql); |
| 137 |
|
| 138 |
$output .= '<div id="tptn_related">'.$tptn_settings['title_daily']; |
| 139 |
$output .= '<ul>'; |
| 140 |
if ($results) { |
| 141 |
foreach ($results as $result) { |
| 142 |
$output .= '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a>'; |
| 143 |
if ($tptn_settings['disp_list_count']) $output .= ' ('.$result->sumCount.')'; |
| 144 |
$output .= '</li>'; |
| 145 |
} |
| 146 |
} |
| 147 |
if ($tptn_settings['show_credit']) $output .= '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 148 |
$output .= '</ul>'; |
| 149 |
$output .= '</div>'; |
| 150 |
} |
| 151 |
echo $output; |
| 152 |
} |
| 153 |
|
| 154 |
// Default Options |
| 155 |
function tptn_default_options() { |
| 156 |
$title = __('<h3>Popular Posts</h3>',TPTN_LOCAL_NAME); |
| 157 |
$title_daily = __('<h3>Daily Popular</h3>',TPTN_LOCAL_NAME); |
| 158 |
|
| 159 |
$tptn_settings = Array ( |
| 160 |
show_credit => true, // Add link to plugin page of my blog in top posts list |
| 161 |
add_to_content => true, // Add post count to content (only on single posts) |
| 162 |
exclude_pages => true, // Exclude Pages |
| 163 |
count_on_pages => true, // Display on pages |
| 164 |
track_authors => false, // Track Authors visits |
| 165 |
pv_in_admin => true, // Add an extra column on edit posts/pages to display page views? |
| 166 |
disp_list_count => true, // Display count in popular lists? |
| 167 |
d_use_js => false, // Use JavaScript for displaying daily posts |
| 168 |
count_disp_form => '(Visited %totalcount% times, %dailycount% visits today)', // Format to display the count |
| 169 |
title => $title, // Title of Popular Posts |
| 170 |
title_daily => $title_daily, // Title of Daily Popular |
| 171 |
limit => '10', // How many posts to display? |
| 172 |
daily_range => '1', // Daily Popular will contain posts of how many days? |
| 173 |
); |
| 174 |
return $tptn_settings; |
| 175 |
} |
| 176 |
|
| 177 |
// Function to read options from the database |
| 178 |
function tptn_read_options() { |
| 179 |
|
| 180 |
// Upgrade table code |
| 181 |
global $tptn_db_version; |
| 182 |
$installed_ver = get_option( "tptn_db_version" ); |
| 183 |
|
| 184 |
if( $installed_ver != $tptn_db_version ) tptn_install(); |
| 185 |
|
| 186 |
$tptn_settings_changed = false; |
| 187 |
|
| 188 |
$defaults = tptn_default_options(); |
| 189 |
|
| 190 |
$tptn_settings = array_map('stripslashes',(array)get_option('ald_tptn_settings')); |
| 191 |
unset($tptn_settings[0]); // produced by the (array) casting when there's nothing in the DB |
| 192 |
|
| 193 |
foreach ($defaults as $k=>$v) { |
| 194 |
if (!isset($tptn_settings[$k])) { |
| 195 |
$tptn_settings[$k] = $v; |
| 196 |
$tptn_settings_changed = true; |
| 197 |
} |
| 198 |
} |
| 199 |
if ($tptn_settings_changed == true) |
| 200 |
update_option('ald_tptn_settings', $tptn_settings); |
| 201 |
|
| 202 |
return $tptn_settings; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
// Create tables to store pageviews |
| 207 |
function tptn_install() { |
| 208 |
global $wpdb; |
| 209 |
global $tptn_db_version; |
| 210 |
|
| 211 |
$table_name = $wpdb->prefix . "top_ten"; |
| 212 |
$table_name_daily = $wpdb->prefix . "top_ten_daily"; |
| 213 |
|
| 214 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name) { |
| 215 |
|
| 216 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 217 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 218 |
postnumber int NOT NULL, |
| 219 |
cntaccess int NOT NULL, |
| 220 |
PRIMARY KEY (accessedid) |
| 221 |
);"; |
| 222 |
|
| 223 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 224 |
dbDelta($sql); |
| 225 |
|
| 226 |
add_option("tptn_db_version", $tptn_db_version); |
| 227 |
} |
| 228 |
|
| 229 |
if($wpdb->get_var("show tables like '$table_name_daily'") != $table_name_daily) { |
| 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 |
dp_date date NOT NULL, |
| 236 |
PRIMARY KEY (accessedid) |
| 237 |
);"; |
| 238 |
|
| 239 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 240 |
dbDelta($sql); |
| 241 |
|
| 242 |
add_option("tptn_db_version", $tptn_db_version); |
| 243 |
} |
| 244 |
|
| 245 |
// Upgrade table code |
| 246 |
$installed_ver = get_option( "tptn_db_version" ); |
| 247 |
|
| 248 |
if( $installed_ver != $tptn_db_version ) { |
| 249 |
|
| 250 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 251 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 252 |
postnumber int NOT NULL, |
| 253 |
cntaccess int NOT NULL, |
| 254 |
PRIMARY KEY (accessedid) |
| 255 |
);"; |
| 256 |
|
| 257 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 258 |
dbDelta($sql); |
| 259 |
|
| 260 |
$sql = "DROP TABLE $table_name_daily"; |
| 261 |
$wpdb->query($sql); |
| 262 |
|
| 263 |
$sql = "CREATE TABLE " . $table_name_daily . " ( |
| 264 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 265 |
postnumber int NOT NULL, |
| 266 |
cntaccess int NOT NULL, |
| 267 |
dp_date date NOT NULL, |
| 268 |
PRIMARY KEY (accessedid) |
| 269 |
);"; |
| 270 |
|
| 271 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 272 |
dbDelta($sql); |
| 273 |
|
| 274 |
update_option( "tptn_db_version", $tptn_db_version ); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
if (function_exists('register_activation_hook')) { |
| 279 |
register_activation_hook(__FILE__,'tptn_install'); |
| 280 |
//register_activation_hook(__FILE__, 'tptn_cron_install'); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
// Function to delete all rows in the daily posts table |
| 285 |
function tptn_trunc_count() { |
| 286 |
global $wpdb; |
| 287 |
$table_name_daily = $wpdb->prefix . "top_ten_daily"; |
| 288 |
|
| 289 |
$sql = "TRUNCATE TABLE $table_name_daily"; |
| 290 |
$wpdb->query($sql); |
| 291 |
} |
| 292 |
|
| 293 |
// Create a WordPress Widget for Daily Popular Posts |
| 294 |
function widget_tptn_pop_daily($args) { |
| 295 |
global $wpdb, $siteurl, $tableposts, $id; |
| 296 |
|
| 297 |
extract($args); // extracts before_widget,before_title,after_title,after_widget |
| 298 |
|
| 299 |
$table_name = $wpdb->prefix . "top_ten_daily"; |
| 300 |
$tptn_settings = tptn_read_options(); |
| 301 |
$limit = $tptn_settings['limit']; |
| 302 |
|
| 303 |
$title = (($tptn_settings['title_daily']) ? strip_tags($tptn_settings['title_daily']) : __('Daily Popular',TPTN_LOCAL_NAME)); |
| 304 |
echo $before_widget; |
| 305 |
echo $before_title.$title.$after_title; |
| 306 |
|
| 307 |
if ($tptn_settings['d_use_js']) { |
| 308 |
echo '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-daily.js.php?widget=1"></script>'; |
| 309 |
} else { |
| 310 |
$daily_range = $tptn_settings[daily_range]. ' DAY'; |
| 311 |
$current_date = $wpdb->get_var("SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $daily_range), INTERVAL 1 DAY) "); |
| 312 |
|
| 313 |
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status "; |
| 314 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 315 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 316 |
$sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' "; |
| 317 |
$sql .= "GROUP BY postnumber "; |
| 318 |
$sql .= "ORDER BY sumCount DESC LIMIT $limit"; |
| 319 |
|
| 320 |
$results = $wpdb->get_results($sql); |
| 321 |
|
| 322 |
echo '<ul>'; |
| 323 |
if ($results) { |
| 324 |
foreach ($results as $result) { |
| 325 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a>'; |
| 326 |
if ($tptn_settings['disp_list_count']) echo ' ('.$result->sumCount.')'; |
| 327 |
echo '</li>'; |
| 328 |
} |
| 329 |
} |
| 330 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 331 |
echo '</ul>'; |
| 332 |
} |
| 333 |
|
| 334 |
echo $after_widget; |
| 335 |
} |
| 336 |
|
| 337 |
// Create a WordPress Widget for Popular Posts |
| 338 |
function widget_tptn_pop($args) { |
| 339 |
global $wpdb, $siteurl, $tableposts, $id; |
| 340 |
|
| 341 |
extract($args); // extracts before_widget,before_title,after_title,after_widget |
| 342 |
|
| 343 |
$table_name = $wpdb->prefix . "top_ten"; |
| 344 |
$tptn_settings = tptn_read_options(); |
| 345 |
$limit = $tptn_settings['limit']; |
| 346 |
|
| 347 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 348 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 349 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 350 |
$sql .= "ORDER BY cntaccess DESC LIMIT $limit"; |
| 351 |
|
| 352 |
$results = $wpdb->get_results($sql); |
| 353 |
|
| 354 |
$title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts',TPTN_LOCAL_NAME)); |
| 355 |
|
| 356 |
echo $before_widget; |
| 357 |
echo $before_title.$title.$after_title; |
| 358 |
echo '<ul>'; |
| 359 |
if ($results) { |
| 360 |
foreach ($results as $result) { |
| 361 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a>'; |
| 362 |
if ($tptn_settings['disp_list_count']) echo ' ('.$result->cntaccess.')'; |
| 363 |
echo '</li>'; |
| 364 |
} |
| 365 |
} |
| 366 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 367 |
echo '</ul>'; |
| 368 |
|
| 369 |
echo $after_widget; |
| 370 |
} |
| 371 |
|
| 372 |
function init_tptn(){ |
| 373 |
register_sidebar_widget(__('Popular Posts',TPTN_LOCAL_NAME), 'widget_tptn_pop'); |
| 374 |
register_sidebar_widget(__('Daily Popular',TPTN_LOCAL_NAME), 'widget_tptn_pop_daily'); |
| 375 |
} |
| 376 |
add_action("plugins_loaded", "init_tptn"); |
| 377 |
|
| 378 |
// This function adds an Options page in WP Admin |
| 379 |
if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { |
| 380 |
require_once(ALD_TPTN_DIR . "/admin.inc.php"); |
| 381 |
} |
| 382 |
|
| 383 |
?> |