PluginProbe
WebberZone Top 10 — Popular Posts / 1.3
WebberZone Top 10 — Popular Posts v1.3
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / top-10.php

top-10.php in WebberZone Top 10 — Popular Posts 1.3, at top-10.php

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