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