| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Top 10 |
| 4 |
Version: 1.0 |
| 5 |
Plugin URI: http://ajaydsouza.com/wordpress/plugins/top-10/ |
| 6 |
Description: Count visits per post and display the top 10 posts. 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 |
if(defined('WP_CACHE') && WP_CACHE) { ?> |
| 42 |
<!-- Start of Top 10 JS --> |
| 43 |
<?php wp_print_scripts(array('sack')); ?> |
| 44 |
<script type="text/javascript"> |
| 45 |
//<![CDATA[ |
| 46 |
top_ten_count = new sack("<?php bloginfo( 'wpurl' ); ?>/wp-content/plugins/top-10/top-10_ajax.php"); |
| 47 |
top_ten_count.setVar( "top_ten_id", <?php echo $id ?> ); |
| 48 |
top_ten_count.method = 'GET'; |
| 49 |
top_ten_count.onError = function() { alert('Ajax error' )}; |
| 50 |
top_ten_count.runAJAX(); |
| 51 |
top_ten_count = null; |
| 52 |
//]]> |
| 53 |
</script> |
| 54 |
<!-- Start of Top 10 JS --> |
| 55 |
<?php } else { |
| 56 |
$results = $wpdb->get_results("select postnumber, cntaccess from $table_name where postnumber = '$id'"); |
| 57 |
$test = 0; |
| 58 |
if ($results) { |
| 59 |
foreach ($results as $result) { |
| 60 |
$wpdb->query("update $table_name set cntaccess = cntaccess + 1 where postnumber = $result->postnumber"); |
| 61 |
$test = 1; |
| 62 |
} |
| 63 |
} |
| 64 |
if ($test == 0) { |
| 65 |
$wpdb->query("insert into $table_name (postnumber, cntaccess) values('$id', '1')"); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Function to show post count |
| 72 |
function tptn_show_post_count() { |
| 73 |
global $wpdb, $post; |
| 74 |
$id = intval($post->ID); |
| 75 |
$table_name = $wpdb->prefix . "top_ten"; |
| 76 |
$tptn_settings = tptn_read_options(); |
| 77 |
$before_count = $tptn_settings['before_count']; |
| 78 |
$after_count = $tptn_settings['after_count']; |
| 79 |
|
| 80 |
$resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id"); |
| 81 |
$cntaccess = (($resultscount) ? $resultscount->cntaccess : 0); |
| 82 |
|
| 83 |
return $before_count.$cntaccess.$after_count; |
| 84 |
} |
| 85 |
|
| 86 |
function tptn_pc_content($content) { |
| 87 |
|
| 88 |
global $single, $post; |
| 89 |
$tptn_settings = tptn_read_options(); |
| 90 |
$id = intval($post->ID); |
| 91 |
|
| 92 |
if(($single)&&($tptn_settings['add_to_content'])) { |
| 93 |
if(defined('WP_CACHE') && WP_CACHE) { |
| 94 |
$output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>'; |
| 95 |
} else { |
| 96 |
$output = tptn_show_post_count(); |
| 97 |
} |
| 98 |
return $content.$output; |
| 99 |
} else { |
| 100 |
return $content; |
| 101 |
} |
| 102 |
} |
| 103 |
add_filter('the_content', 'tptn_pc_content'); |
| 104 |
|
| 105 |
function echo_tptn_post_count() { |
| 106 |
$output = tptn_show_post_count(); |
| 107 |
echo $output; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
// Function to show popular posts |
| 112 |
function tptn_show_pop_posts() { |
| 113 |
global $wpdb, $siteurl, $tableposts, $id; |
| 114 |
$table_name = $wpdb->prefix . "top_ten"; |
| 115 |
$tptn_settings = tptn_read_options(); |
| 116 |
$limit = $tptn_settings['limit']; |
| 117 |
|
| 118 |
$results = $wpdb->get_results("select postnumber, cntaccess from $table_name ORDER BY cntaccess DESC LIMIT $limit"); |
| 119 |
|
| 120 |
echo '<div id="crp_related">'.$tptn_settings['title']; |
| 121 |
echo '<ul>'; |
| 122 |
if ($results) { |
| 123 |
foreach ($results as $result) { |
| 124 |
echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>'; |
| 125 |
} |
| 126 |
} |
| 127 |
if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>'; |
| 128 |
echo '</ul>'; |
| 129 |
echo '</div><br/><br/>'; |
| 130 |
|
| 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 |
before_count => '(Visited ', // Text to display before the count |
| 141 |
after_count => ' times)', // Text to display after 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 |
if ($tptn_settings_changed == true) |
| 164 |
update_option('ald_tptn_settings', $tptn_settings); |
| 165 |
|
| 166 |
return $tptn_settings; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
// Create tables to store pageviews |
| 171 |
function tptn_install() { |
| 172 |
global $wpdb; |
| 173 |
global $tptn_db_version; |
| 174 |
|
| 175 |
$table_name = $wpdb->prefix . "top_ten"; |
| 176 |
if($wpdb->get_var("show tables like '$table_name'") != $table_name) { |
| 177 |
|
| 178 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 179 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 180 |
postnumber int NOT NULL, |
| 181 |
cntaccess int NOT NULL, |
| 182 |
PRIMARY KEY (accessedid) |
| 183 |
);"; |
| 184 |
|
| 185 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 186 |
dbDelta($sql); |
| 187 |
|
| 188 |
add_option("tptn_db_version", $tptn_db_version); |
| 189 |
} |
| 190 |
|
| 191 |
// Upgrade table code |
| 192 |
$installed_ver = get_option( "tptn_db_version" ); |
| 193 |
|
| 194 |
if( $installed_ver != $tptn_db_version ) { |
| 195 |
|
| 196 |
$sql = "CREATE TABLE " . $table_name . " ( |
| 197 |
accessedid int NOT NULL AUTO_INCREMENT, |
| 198 |
postnumber int NOT NULL, |
| 199 |
cntaccess int NOT NULL, |
| 200 |
PRIMARY KEY (accessedid) |
| 201 |
);"; |
| 202 |
|
| 203 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 204 |
dbDelta($sql); |
| 205 |
|
| 206 |
update_option( "tptn_db_version", $tptn_db_version ); |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
if (function_exists('register_activation_hook')) { |
| 211 |
register_activation_hook(__FILE__,'tptn_install'); |
| 212 |
} |
| 213 |
|
| 214 |
// This function adds an Options page in WP Admin |
| 215 |
if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) { |
| 216 |
require_once(ALD_TPTN_DIR . "/admin.inc.php"); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
?> |