PluginProbe
WebberZone Top 10 — Popular Posts / 1.4.1
WebberZone Top 10 — Popular Posts v1.4.1
4.5.2 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 All 118 releases
top-10 / top-10.php

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

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