Top 10 – Popular posts plugin for WordPress - Version 1.2

Version Description

Download this release

Release Info

Developer Ajay
Plugin Icon 128x128 Top 10 – Popular posts plugin for WordPress
Version 1.2
Comparing to
See all releases

Code changes from version 1.1 to 1.2

Files changed (6) hide show
  1. admin.inc.php +102 -2
  2. readme.txt +28 -15
  3. top-10-addcount.js.php +13 -0
  4. top-10-counter.js.php +6 -1
  5. top-10.php +164 -36
  6. uninstall.php +3 -0
admin.inc.php CHANGED
@@ -11,10 +11,13 @@ function tptn_options() {
11
 
12
  if($_POST['tptn_save']){
13
  $tptn_settings[title] = ($_POST['title']);
 
14
  $tptn_settings[limit] = ($_POST['limit']);
15
  $tptn_settings[count_disp_form] = ($_POST['count_disp_form']);
16
  $tptn_settings[add_to_content] = (($_POST['add_to_content']) ? true : false);
17
  $tptn_settings[exclude_pages] = (($_POST['exclude_pages']) ? true : false);
 
 
18
  $tptn_settings[show_credit] = (($_POST['show_credit']) ? true : false);
19
 
20
  update_option('ald_tptn_settings', $tptn_settings);
@@ -60,10 +63,10 @@ function tptn_options() {
60
  <p>
61
  <label>
62
  <?php _e('Format to display the count in: ','ald_tptn_plugin'); ?><br />
63
- <textarea name="count_disp_form" id="count_disp_form" cols="45" rows="5"><?php echo htmlspecialchars(stripslashes($tptn_settings[count_disp_form])); ?></textarea>
64
  </label>
65
  </p>
66
- <p><?php _e('Use <code>%totalcount%</code> to display the total count. e.g. the default options displays <code>(Visited 123 times)</code>','ald_tptn_plugin'); ?></p>
67
  <p>
68
  <label>
69
  <?php _e('Number of popular posts to display: ','ald_tptn_plugin'); ?>
@@ -76,12 +79,30 @@ function tptn_options() {
76
  <input type="textbox" name="title" id="title" value="<?php echo stripslashes($tptn_settings[title]); ?>">
77
  </label>
78
  </p>
 
 
 
 
 
 
79
  <p>
80
  <label>
81
  <input type="checkbox" name="exclude_pages" id="exclude_pages" <?php if ($tptn_settings[exclude_pages]) echo 'checked="checked"' ?> />
82
  <?php _e('Exclude Pages in display of Popular Posts? Number of views on Pages will continue to be counted.','ald_tptn_plugin'); ?>
83
  </label>
84
  </p>
 
 
 
 
 
 
 
 
 
 
 
 
85
  <p>
86
  <label>
87
  <input type="checkbox" name="add_to_content" id="add_to_content" <?php if ($tptn_settings[add_to_content]) echo 'checked="checked"' ?> />
@@ -128,6 +149,7 @@ add_action('admin_menu', 'tptn_adminmenu');
128
 
129
 
130
  /* Create a Dashboard Widget */
 
131
  function tptn_pop_dashboard() {
132
  global $wpdb, $siteurl, $tableposts, $id;
133
 
@@ -138,6 +160,33 @@ function tptn_pop_dashboard() {
138
  $sql = "SELECT postnumber, cntaccess , ID, post_type ";
139
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
140
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
142
 
143
  $results = $wpdb->get_results($sql);
@@ -155,8 +204,59 @@ function tptn_pop_dashboard() {
155
  function tptn_pop_dashboard_setup() {
156
  if (function_exists('wp_add_dashboard_widget')) {
157
  wp_add_dashboard_widget( 'tptn_pop_dashboard', __( 'Popular Posts' ), 'tptn_pop_dashboard' );
 
158
  }
159
  }
160
  add_action('wp_dashboard_setup', 'tptn_pop_dashboard_setup');
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  ?>
11
 
12
  if($_POST['tptn_save']){
13
  $tptn_settings[title] = ($_POST['title']);
14
+ $tptn_settings[title_daily] = ($_POST['title_daily']);
15
  $tptn_settings[limit] = ($_POST['limit']);
16
  $tptn_settings[count_disp_form] = ($_POST['count_disp_form']);
17
  $tptn_settings[add_to_content] = (($_POST['add_to_content']) ? true : false);
18
  $tptn_settings[exclude_pages] = (($_POST['exclude_pages']) ? true : false);
19
+ $tptn_settings[track_authors] = (($_POST['track_authors']) ? true : false);
20
+ $tptn_settings[pv_in_admin] = (($_POST['pv_in_admin']) ? true : false);
21
  $tptn_settings[show_credit] = (($_POST['show_credit']) ? true : false);
22
 
23
  update_option('ald_tptn_settings', $tptn_settings);
63
  <p>
64
  <label>
65
  <?php _e('Format to display the count in: ','ald_tptn_plugin'); ?><br />
66
+ <textarea name="count_disp_form" id="count_disp_form" cols="50" rows="5"><?php echo htmlspecialchars(stripslashes($tptn_settings[count_disp_form])); ?></textarea>
67
  </label>
68
  </p>
69
+ <p><?php _e('Use <code>%totalcount%</code> to display the total count and <code>%dailycount%</code> to display the daily count. e.g. the default options displays <code>(Visited 123 times, 23 visits today)</code>','ald_tptn_plugin'); ?></p>
70
  <p>
71
  <label>
72
  <?php _e('Number of popular posts to display: ','ald_tptn_plugin'); ?>
79
  <input type="textbox" name="title" id="title" value="<?php echo stripslashes($tptn_settings[title]); ?>">
80
  </label>
81
  </p>
82
+ <p>
83
+ <label>
84
+ <?php _e('Title of daily popular posts: ','ald_tptn_plugin'); ?>
85
+ <input type="textbox" name="title_daily" id="title_daily" value="<?php echo stripslashes($tptn_settings[title_daily]); ?>">
86
+ </label>
87
+ </p>
88
  <p>
89
  <label>
90
  <input type="checkbox" name="exclude_pages" id="exclude_pages" <?php if ($tptn_settings[exclude_pages]) echo 'checked="checked"' ?> />
91
  <?php _e('Exclude Pages in display of Popular Posts? Number of views on Pages will continue to be counted.','ald_tptn_plugin'); ?>
92
  </label>
93
  </p>
94
+ <p>
95
+ <label>
96
+ <input type="checkbox" name="track_authors" id="track_authors" <?php if ($tptn_settings[track_authors]) echo 'checked="checked"' ?> />
97
+ <?php _e('Track visits of authors on their own posts?','ald_tptn_plugin'); ?>
98
+ </label>
99
+ </p>
100
+ <p>
101
+ <label>
102
+ <input type="checkbox" name="pv_in_admin" id="pv_in_admin" <?php if ($tptn_settings[pv_in_admin]) echo 'checked="checked"' ?> />
103
+ <?php _e('Display page views on Edit posts/pages in WP-Admin? An extra column is added with the count','ald_tptn_plugin'); ?>
104
+ </label>
105
+ </p>
106
  <p>
107
  <label>
108
  <input type="checkbox" name="add_to_content" id="add_to_content" <?php if ($tptn_settings[add_to_content]) echo 'checked="checked"' ?> />
149
 
150
 
151
  /* Create a Dashboard Widget */
152
+ // Dashboard for Popular Posts
153
  function tptn_pop_dashboard() {
154
  global $wpdb, $siteurl, $tableposts, $id;
155
 
160
  $sql = "SELECT postnumber, cntaccess , ID, post_type ";
161
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
162
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
163
+ $sql .= "AND post_status = 'publish' ";
164
+ $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
165
+
166
+ $results = $wpdb->get_results($sql);
167
+
168
+ echo '<ul>';
169
+ if ($results) {
170
+ foreach ($results as $result) {
171
+ echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
172
+ }
173
+ }
174
+ if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
175
+ echo '</ul>';
176
+ }
177
+
178
+ // Dashboard for Daily Popular Posts
179
+ function tptn_pop_daily_dashboard() {
180
+ global $wpdb, $siteurl, $tableposts, $id;
181
+
182
+ $table_name = $wpdb->prefix . "top_ten_daily";
183
+ $tptn_settings = tptn_read_options();
184
+ $limit = $tptn_settings['limit'];
185
+
186
+ $sql = "SELECT postnumber, cntaccess , ID, post_type ";
187
+ $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
188
+ if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
189
+ $sql .= "AND post_status = 'publish' ";
190
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
191
 
192
  $results = $wpdb->get_results($sql);
204
  function tptn_pop_dashboard_setup() {
205
  if (function_exists('wp_add_dashboard_widget')) {
206
  wp_add_dashboard_widget( 'tptn_pop_dashboard', __( 'Popular Posts' ), 'tptn_pop_dashboard' );
207
+ wp_add_dashboard_widget( 'tptn_pop_daily_dashboard', __( 'Daily Popular' ), 'tptn_pop_daily_dashboard' );
208
  }
209
  }
210
  add_action('wp_dashboard_setup', 'tptn_pop_dashboard_setup');
211
 
212
+ /* Display page views on the Edit Posts / Pages screen */
213
+ // Add an extra column
214
+ function tptn_column($cols) {
215
+ $tptn_settings = tptn_read_options();
216
+
217
+ if ($tptn_settings[pv_in_admin]) $cols['tptn'] = __('Total / Today\'s Views','ald_tptn_plugin');
218
+ return $cols;
219
+ }
220
+
221
+ // Display page views for each column
222
+ function tptn_value($column_name, $id) {
223
+ $tptn_settings = tptn_read_options();
224
+ if (($column_name == 'tptn')&&($tptn_settings[pv_in_admin])) {
225
+ global $wpdb;
226
+
227
+ $table_name = $wpdb->prefix . "top_ten";
228
+
229
+ $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id");
230
+ $cntaccess = number_format((($resultscount) ? $resultscount->cntaccess : 0));
231
+
232
+ $cntaccess .= ' / ';
233
+
234
+ $table_name = $wpdb->prefix . "top_ten_daily";
235
+
236
+ $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id");
237
+ $cntaccess .= number_format((($resultscount) ? $resultscount->cntaccess : 0));
238
+ echo $cntaccess;
239
+ }
240
+ }
241
+
242
+ // Output CSS for width of new column
243
+ function tptn_css() {
244
+ ?>
245
+ <style type="text/css">
246
+ #tptn { width: 50px; }
247
+ </style>
248
+ <?php
249
+ }
250
+
251
+ // Actions/Filters for various tables and the css output
252
+ add_filter('manage_posts_columns', 'tptn_column');
253
+ add_action('manage_posts_custom_column', 'tptn_value', 10, 2);
254
+ add_filter('manage_pages_columns', 'tptn_column');
255
+ add_action('manage_pages_custom_column', 'tptn_value', 10, 2);
256
+ add_filter('manage_media_columns', 'tptn_column');
257
+ add_action('manage_media_custom_column', 'tptn_value', 10, 2);
258
+ add_filter('manage_link-manager_columns', 'tptn_column');
259
+ add_action('manage_link_custom_column', 'tptn_value', 10, 2);
260
+ add_action('admin_head', 'tptn_css');
261
+
262
  ?>
readme.txt CHANGED
@@ -1,21 +1,43 @@
1
  === Top 10 ===
2
- Tags: popular posts, top posts, counter
3
  Contributors: Ajay, Mark Ghosh
4
  Donate link: http://ajaydsouza.com/donate/
5
  Stable tag: trunk
6
  Requires at least: 2.5
7
- Tested up to: 2.7
8
 
9
 
10
- Count pageviews on posts and display a list of top posts
11
 
12
  == Description ==
13
 
14
- WordPress doesn't count page views by default. <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10</a> will count the number of page views on your single posts and store them in the database.
15
 
16
- You can then display the page view count on individual posts and pages as well as display a list of most popular posts based on page views.
17
 
18
- Includes a sidebar widget to display the popular posts. And, all settings can be configured from within your WordPress Admin area itself!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  == Installation ==
@@ -31,15 +53,6 @@ Includes a sidebar widget to display the popular posts. And, all settings can be
31
  5. Goto Appearance > Widgets to add the Popular Posts sidebar widget to your theme
32
 
33
 
34
- == Changelog ==
35
-
36
- * 1.1
37
- - Added the Popular Posts sidebar widget in your theme. Find it under <strong>Appearance > Widgets</strong>
38
- - Uses JavaScript by default to count. Hence, better support for different caching plugins
39
- - Change format to display count. Now, a single textarea instead of two text boxes.
40
- - Added WordPress 2.7 Dashboard widget to display popular posts on your Dashboard
41
- * 1.0.1 - Release
42
-
43
 
44
  == Frequently Asked Questions ==
45
 
1
  === Top 10 ===
2
+ Tags: popular posts, top posts, counter, daily popular, page views, statistics
3
  Contributors: Ajay, Mark Ghosh
4
  Donate link: http://ajaydsouza.com/donate/
5
  Stable tag: trunk
6
  Requires at least: 2.5
7
+ Tested up to: 2.7.1
8
 
9
 
10
+ Track daily and total visits on your blog posts and display the count as well as popular posts.
11
 
12
  == Description ==
13
 
14
+ WordPress doesn't count page views by default. <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10</a> will count the number of page views on your single posts on a daily as well as overall basis. You can then display the page view count on individual posts and pages as well as display a list of most popular posts based on page views.
15
 
16
+ Includes a sidebar widget to display the popular posts. And, all settings can be configured from within your WordPress Admin area itself! You can choose to disable tracking of author visits on their own posts.
17
 
18
+ = Features =
19
+ * Counts daily and total page views on single posts and pages
20
+ * Display the count on the single posts and pages
21
+ * Customize the text that can be displayed
22
+ * Display a list of daily and/or overall popular posts by page count. The number can be customized.
23
+ * Sidebar widgets available for daily popular and overall popular posts.
24
+ * View list of daily and/or overall popular posts from within the dashboard itself
25
+ * Clean uninstall if you choose to delete the plugin from within WP-Admin
26
+ * Works with caching plugins like WP-Super-Cache
27
+
28
+
29
+ = Changelog =
30
+
31
+ * 1.2
32
+ - Do not display Drafts in Related Posts anymore
33
+ - Option to disable tracking author visits on their own posts
34
+ - Display top posts for the current day
35
+ * 1.1
36
+ - Added the Popular Posts sidebar widget in your theme. Find it under <strong>Appearance > Widgets</strong>
37
+ - Uses JavaScript by default to count. Hence, better support for different caching plugins
38
+ - Change format to display count. Now, a single textarea instead of two text boxes.
39
+ - Added WordPress 2.7 Dashboard widget to display popular posts on your Dashboard
40
+ * 1.0.1 - Release
41
 
42
 
43
  == Installation ==
53
  5. Goto Appearance > Widgets to add the Popular Posts sidebar widget to your theme
54
 
55
 
 
 
 
 
 
 
 
 
 
56
 
57
  == Frequently Asked Questions ==
58
 
top-10-addcount.js.php CHANGED
@@ -16,6 +16,7 @@ tptn_inc_count();
16
  function tptn_inc_count() {
17
  global $wpdb;
18
  $table_name = $wpdb->prefix . "top_ten";
 
19
 
20
  $id = intval($_GET['top_ten_id']);
21
  if($id > 0) {
@@ -30,6 +31,18 @@ function tptn_inc_count() {
30
  if ($test == 0) {
31
  $wpdb->query("insert into $table_name (postnumber, cntaccess) values('$id', '1')");
32
  }
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
34
  }
35
 
16
  function tptn_inc_count() {
17
  global $wpdb;
18
  $table_name = $wpdb->prefix . "top_ten";
19
+ $top_ten_daily = $wpdb->prefix . "top_ten_daily";
20
 
21
  $id = intval($_GET['top_ten_id']);
22
  if($id > 0) {
31
  if ($test == 0) {
32
  $wpdb->query("insert into $table_name (postnumber, cntaccess) values('$id', '1')");
33
  }
34
+ // Now update daily count
35
+ $results = $wpdb->get_results("select postnumber, cntaccess from $top_ten_daily where postnumber = '$id'");
36
+ $test = 0;
37
+ if ($results) {
38
+ foreach ($results as $result) {
39
+ $wpdb->query("update $top_ten_daily set cntaccess = cntaccess + 1 where postnumber = $result->postnumber");
40
+ $test = 1;
41
+ }
42
+ }
43
+ if ($test == 0) {
44
+ $wpdb->query("insert into $top_ten_daily (postnumber, cntaccess) values('$id', '1')");
45
+ }
46
  }
47
  }
48
 
top-10-counter.js.php CHANGED
@@ -16,6 +16,7 @@ function tptn_disp_count() {
16
  global $wpdb;
17
 
18
  $table_name = $wpdb->prefix . "top_ten";
 
19
  $tptn_settings = tptn_read_options();
20
  $count_disp_form = htmlspecialchars(stripslashes($tptn_settings[count_disp_form]));
21
 
@@ -24,9 +25,13 @@ function tptn_disp_count() {
24
 
25
  $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id");
26
  $cntaccess = number_format((($resultscount) ? $resultscount->cntaccess : 0));
27
-
28
  $count_disp_form = str_replace("%totalcount%", $cntaccess, $count_disp_form);
29
 
 
 
 
 
 
30
  echo 'document.write("'.$count_disp_form.'")';
31
  }
32
  }
16
  global $wpdb;
17
 
18
  $table_name = $wpdb->prefix . "top_ten";
19
+ $table_name_daily = $wpdb->prefix . "top_ten_daily";
20
  $tptn_settings = tptn_read_options();
21
  $count_disp_form = htmlspecialchars(stripslashes($tptn_settings[count_disp_form]));
22
 
25
 
26
  $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id");
27
  $cntaccess = number_format((($resultscount) ? $resultscount->cntaccess : 0));
 
28
  $count_disp_form = str_replace("%totalcount%", $cntaccess, $count_disp_form);
29
 
30
+ // Now process daily count
31
+ $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name_daily WHERE postnumber = $id");
32
+ $cntaccess = number_format((($resultscount) ? $resultscount->cntaccess : 0));
33
+ $count_disp_form = str_replace("%dailycount%", $cntaccess, $count_disp_form);
34
+
35
  echo 'document.write("'.$count_disp_form.'")';
36
  }
37
  }
top-10.php CHANGED
@@ -1,9 +1,9 @@
1
  <?php
2
  /*
3
  Plugin Name: Top 10
4
- Version: 1.1
5
  Plugin URI: http://ajaydsouza.com/wordpress/plugins/top-10/
6
- Description: Count 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
  */
@@ -18,7 +18,7 @@ if (!function_exists('add_action')) {
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__)));
@@ -35,14 +35,21 @@ 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
- $output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>';
42
- echo $output;
 
 
 
 
 
43
  }
44
  }
45
 
 
46
  function tptn_pc_content($content) {
47
  global $single, $post;
48
  $tptn_settings = tptn_read_options();
@@ -57,6 +64,7 @@ function tptn_pc_content($content) {
57
  }
58
  add_filter('the_content', 'tptn_pc_content');
59
 
 
60
  function echo_tptn_post_count() {
61
  global $post;
62
  $id = intval($post->ID);
@@ -65,7 +73,6 @@ function echo_tptn_post_count() {
65
  echo $output;
66
  }
67
 
68
-
69
  // Function to show popular posts
70
  function tptn_show_pop_posts() {
71
  global $wpdb, $siteurl, $tableposts, $id;
@@ -73,9 +80,10 @@ function tptn_show_pop_posts() {
73
  $tptn_settings = tptn_read_options();
74
  $limit = $tptn_settings['limit'];
75
 
76
- $sql = "SELECT postnumber, cntaccess , ID, post_type ";
77
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
78
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
 
79
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
80
 
81
  $results = $wpdb->get_results($sql);
@@ -91,27 +99,23 @@ function tptn_show_pop_posts() {
91
  echo '</ul>';
92
  echo '</div>';
93
  }
94
- // Create a WordPress Widget for Popular Posts
95
- function widget_tptn_pop($args) {
96
- global $wpdb, $siteurl, $tableposts, $id;
97
-
98
- extract($args); // extracts before_widget,before_title,after_title,after_widget
99
 
100
- $table_name = $wpdb->prefix . "top_ten";
 
 
 
101
  $tptn_settings = tptn_read_options();
102
  $limit = $tptn_settings['limit'];
103
 
104
- $sql = "SELECT postnumber, cntaccess , ID, post_type ";
105
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
106
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
 
107
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
108
 
109
  $results = $wpdb->get_results($sql);
110
 
111
- $title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts'));
112
-
113
- echo $before_widget;
114
- echo $before_title.$title.$after_title;
115
  echo '<ul>';
116
  if ($results) {
117
  foreach ($results as $result) {
@@ -120,27 +124,24 @@ function widget_tptn_pop($args) {
120
  }
121
  if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
122
  echo '</ul>';
123
-
124
- echo $after_widget;
125
- }
126
-
127
- function init_tptn(){
128
- register_sidebar_widget(__('Popular Posts'), 'widget_tptn_pop');
129
  }
130
- add_action("plugins_loaded", "init_tptn");
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
- exclude_pages => true, // Exclude Pages
141
- count_disp_form => '(Visited %totalcount% times)', // Format to display the count
142
- title => $title, // Add before the content
143
- limit => '10', // How many posts to display?
 
 
 
144
  );
145
  return $tptn_settings;
146
  }
@@ -148,6 +149,12 @@ function tptn_default_options() {
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();
@@ -174,6 +181,8 @@ function tptn_install() {
174
  global $tptn_db_version;
175
 
176
  $table_name = $wpdb->prefix . "top_ten";
 
 
177
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
178
 
179
  $sql = "CREATE TABLE " . $table_name . " (
@@ -189,6 +198,21 @@ function tptn_install() {
189
  add_option("tptn_db_version", $tptn_db_version);
190
  }
191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  // Upgrade table code
193
  $installed_ver = get_option( "tptn_db_version" );
194
 
@@ -201,6 +225,16 @@ function tptn_install() {
201
  PRIMARY KEY (accessedid)
202
  );";
203
 
 
 
 
 
 
 
 
 
 
 
204
  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
205
  dbDelta($sql);
206
 
@@ -210,12 +244,106 @@ function tptn_install() {
210
  }
211
  if (function_exists('register_activation_hook')) {
212
  register_activation_hook(__FILE__,'tptn_install');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  }
214
 
 
 
 
 
 
 
215
  // This function adds an Options page in WP Admin
216
  if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
217
  require_once(ALD_TPTN_DIR . "/admin.inc.php");
218
  }
219
 
220
-
221
  ?>
1
  <?php
2
  /*
3
  Plugin Name: Top 10
4
+ Version: 1.2
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
  */
18
  }
19
  }
20
 
21
+ $tptn_db_version = "2.0";
22
 
23
  function ald_tptn_init() {
24
  load_plugin_textdomain('myald_tptn_plugin', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)));
35
  function tptn_add_viewed_count() {
36
  global $post, $wpdb, $single;
37
  $table_name = $wpdb->prefix . "top_ten";
38
+ $tptn_settings = tptn_read_options();
39
 
40
+ $current_user = wp_get_current_user();
41
+ $post_author = ( $current_user->ID == $post->post_author ? true : false );
42
+
43
+ if((is_single() || is_page())) {
44
+ if (!(($post_author)&&(!$tptn_settings['track_authors']))) {
45
+ $id = intval($post->ID);
46
+ $output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-addcount.js.php?top_ten_id='.$id.'"></script>';
47
+ echo $output;
48
+ }
49
  }
50
  }
51
 
52
+ // Function to add count to content
53
  function tptn_pc_content($content) {
54
  global $single, $post;
55
  $tptn_settings = tptn_read_options();
64
  }
65
  add_filter('the_content', 'tptn_pc_content');
66
 
67
+ // Function to manually display count
68
  function echo_tptn_post_count() {
69
  global $post;
70
  $id = intval($post->ID);
73
  echo $output;
74
  }
75
 
 
76
  // Function to show popular posts
77
  function tptn_show_pop_posts() {
78
  global $wpdb, $siteurl, $tableposts, $id;
80
  $tptn_settings = tptn_read_options();
81
  $limit = $tptn_settings['limit'];
82
 
83
+ $sql = "SELECT postnumber, cntaccess , ID, post_type, post_status ";
84
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
85
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
86
+ $sql .= "AND post_status = 'publish' ";
87
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
88
 
89
  $results = $wpdb->get_results($sql);
99
  echo '</ul>';
100
  echo '</div>';
101
  }
 
 
 
 
 
102
 
103
+ // Function to show daily popular posts
104
+ function tptn_show_daily_pop_posts() {
105
+ global $wpdb, $siteurl, $tableposts, $id;
106
+ $table_name = $wpdb->prefix . "top_ten_daily";
107
  $tptn_settings = tptn_read_options();
108
  $limit = $tptn_settings['limit'];
109
 
110
+ $sql = "SELECT postnumber, cntaccess , ID, post_type, post_status ";
111
  $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
112
  if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
113
+ $sql .= "AND post_status = 'publish' ";
114
  $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
115
 
116
  $results = $wpdb->get_results($sql);
117
 
118
+ echo '<div id="crp_related">'.$tptn_settings['title_daily'];
 
 
 
119
  echo '<ul>';
120
  if ($results) {
121
  foreach ($results as $result) {
124
  }
125
  if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
126
  echo '</ul>';
127
+ echo '</div>';
 
 
 
 
 
128
  }
 
 
129
 
130
  // Default Options
131
  function tptn_default_options() {
132
+ $title = __('<h3>Popular Posts</h3>','ald_tptn_plugin');
133
+ $title_daily = __('<h3>Daily Popular</h3>','ald_tptn_plugin');
134
 
135
  $tptn_settings = Array (
136
+ show_credit => true, // Add link to plugin page of my blog in top posts list
137
+ add_to_content => true, // Add post count to content (only on single pages)
138
+ exclude_pages => true, // Exclude Pages
139
+ track_authors => false, // Track Authors visits
140
+ pv_in_admin => true, // Add an extra column on edit posts/pages to display page views?
141
+ count_disp_form => '(Visited %totalcount% times, %dailycount% visits today)', // Format to display the count
142
+ title => $title, // Title of Popular Posts
143
+ title_daily => $title_daily, // Title of Daily Popular
144
+ limit => '10', // How many posts to display?
145
  );
146
  return $tptn_settings;
147
  }
149
  // Function to read options from the database
150
  function tptn_read_options() {
151
 
152
+ // Upgrade table code
153
+ global $tptn_db_version;
154
+ $installed_ver = get_option( "tptn_db_version" );
155
+
156
+ if( $installed_ver != $tptn_db_version ) tptn_install();
157
+
158
  $tptn_settings_changed = false;
159
 
160
  $defaults = tptn_default_options();
181
  global $tptn_db_version;
182
 
183
  $table_name = $wpdb->prefix . "top_ten";
184
+ $table_name_daily = $wpdb->prefix . "top_ten_daily";
185
+
186
  if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
187
 
188
  $sql = "CREATE TABLE " . $table_name . " (
198
  add_option("tptn_db_version", $tptn_db_version);
199
  }
200
 
201
+ if($wpdb->get_var("show tables like '$table_name_daily'") != $table_name_daily) {
202
+
203
+ $sql = "CREATE TABLE " . $table_name_daily . " (
204
+ accessedid int NOT NULL AUTO_INCREMENT,
205
+ postnumber int NOT NULL,
206
+ cntaccess int NOT NULL,
207
+ PRIMARY KEY (accessedid)
208
+ );";
209
+
210
+ require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
211
+ dbDelta($sql);
212
+
213
+ add_option("tptn_db_version", $tptn_db_version);
214
+ }
215
+
216
  // Upgrade table code
217
  $installed_ver = get_option( "tptn_db_version" );
218
 
225
  PRIMARY KEY (accessedid)
226
  );";
227
 
228
+ require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
229
+ dbDelta($sql);
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
+ PRIMARY KEY (accessedid)
236
+ );";
237
+
238
  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
239
  dbDelta($sql);
240
 
244
  }
245
  if (function_exists('register_activation_hook')) {
246
  register_activation_hook(__FILE__,'tptn_install');
247
+ register_activation_hook(__FILE__, 'tptn_cron_install');
248
+ }
249
+
250
+ /* Schedule daily cron to clean up the Daily Table */
251
+ add_action('tptn_cron_hook', 'tptn_daily_clean');
252
+ function tptn_cron_install() {
253
+ if (!wp_next_scheduled('tptn_cron_hook')) {
254
+ wp_schedule_event(mktime(0,0), 'daily', 'tptn_cron_hook');
255
+ }
256
+ }
257
+ function tptn_daily_clean() {
258
+ global $wpdb;
259
+ $table_name_daily = $wpdb->prefix . "table_name_daily";
260
+
261
+ $sql = "TRUNCATE TABLE $table_name_daily";
262
+ $wpdb->query($sql);
263
+ }
264
+
265
+ if (function_exists('register_deactivation_hook')) {
266
+ register_deactivation_hook(__FILE__, 'tptn_cron_deinstall');
267
+ }
268
+ function tptn_cron_deinstall() {
269
+ wp_clear_scheduled_hook('tptn_cron_hook');
270
+ }
271
+
272
+ // Create a WordPress Widget for Popular Posts
273
+ function widget_tptn_pop($args) {
274
+ global $wpdb, $siteurl, $tableposts, $id;
275
+
276
+ extract($args); // extracts before_widget,before_title,after_title,after_widget
277
+
278
+ $table_name = $wpdb->prefix . "top_ten";
279
+ $tptn_settings = tptn_read_options();
280
+ $limit = $tptn_settings['limit'];
281
+
282
+ $sql = "SELECT postnumber, cntaccess , ID, post_type ";
283
+ $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
284
+ if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
285
+ $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
286
+
287
+ $results = $wpdb->get_results($sql);
288
+
289
+ $title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts'));
290
+
291
+ echo $before_widget;
292
+ echo $before_title.$title.$after_title;
293
+ echo '<ul>';
294
+ if ($results) {
295
+ foreach ($results as $result) {
296
+ echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
297
+ }
298
+ }
299
+ if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
300
+ echo '</ul>';
301
+
302
+ echo $after_widget;
303
+ }
304
+
305
+ // Create a WordPress Widget for Daily Popular Posts
306
+ function widget_tptn_pop_daily($args) {
307
+ global $wpdb, $siteurl, $tableposts, $id;
308
+
309
+ extract($args); // extracts before_widget,before_title,after_title,after_widget
310
+
311
+ $table_name = $wpdb->prefix . "top_ten_daily";
312
+ $tptn_settings = tptn_read_options();
313
+ $limit = $tptn_settings['limit'];
314
+
315
+ $sql = "SELECT postnumber, cntaccess , ID, post_type ";
316
+ $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
317
+ if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
318
+ $sql .= "ORDER BY cntaccess DESC LIMIT $limit";
319
+
320
+ $results = $wpdb->get_results($sql);
321
+
322
+ $title = (($tptn_settings['title_daily']) ? strip_tags($tptn_settings['title_daily']) : __('Daily Popular'));
323
+
324
+ echo $before_widget;
325
+ echo $before_title.$title.$after_title;
326
+ echo '<ul>';
327
+ if ($results) {
328
+ foreach ($results as $result) {
329
+ echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
330
+ }
331
+ }
332
+ if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
333
+ echo '</ul>';
334
+
335
+ echo $after_widget;
336
  }
337
 
338
+ function init_tptn(){
339
+ register_sidebar_widget(__('Popular Posts','ald_tptn_plugin'), 'widget_tptn_pop');
340
+ register_sidebar_widget(__('Daily Popular','ald_tptn_plugin'), 'widget_tptn_pop_daily');
341
+ }
342
+ add_action("plugins_loaded", "init_tptn");
343
+
344
  // This function adds an Options page in WP Admin
345
  if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
346
  require_once(ALD_TPTN_DIR . "/admin.inc.php");
347
  }
348
 
 
349
  ?>
uninstall.php CHANGED
@@ -4,9 +4,12 @@ if ( !defined('ABSPATH') && !defined('WP_UNINSTALL_PLUGIN') ) {
4
  }
5
  global $wpdb;
6
  $table_name = $wpdb->prefix . "top_ten";
 
7
 
8
  $sql = "DROP TABLE $table_name";
9
  $wpdb->query($sql);
 
 
10
  delete_option('ald_tptn_settings');
11
  delete_option('tptn_db_version');
12
  ?>
4
  }
5
  global $wpdb;
6
  $table_name = $wpdb->prefix . "top_ten";
7
+ $table_name_daily = $wpdb->prefix . "table_name_daily";
8
 
9
  $sql = "DROP TABLE $table_name";
10
  $wpdb->query($sql);
11
+ $sql = "DROP TABLE $table_name_daily";
12
+ $wpdb->query($sql);
13
  delete_option('ald_tptn_settings');
14
  delete_option('tptn_db_version');
15
  ?>