Top 10 – Popular posts plugin for WordPress - Version 1.1

Version Description

Download this release

Release Info

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

Code changes from version 1.0.1 to 1.1

admin.inc.php CHANGED
@@ -12,9 +12,9 @@ function tptn_options() {
12
  if($_POST['tptn_save']){
13
  $tptn_settings[title] = ($_POST['title']);
14
  $tptn_settings[limit] = ($_POST['limit']);
15
- $tptn_settings[before_count] = ($_POST['before_count']);
16
- $tptn_settings[after_count] = ($_POST['after_count']);
17
  $tptn_settings[add_to_content] = (($_POST['add_to_content']) ? true : false);
 
18
  $tptn_settings[show_credit] = (($_POST['show_credit']) ? true : false);
19
 
20
  update_option('ald_tptn_settings', $tptn_settings);
@@ -59,17 +59,11 @@ function tptn_options() {
59
  </legend>
60
  <p>
61
  <label>
62
- <?php _e('Text to display before the post count: ','ald_tptn_plugin'); ?>
63
- <input type="textbox" name="before_count" id="before_count" value="<?php echo stripslashes($tptn_settings[before_count]); ?>">
64
  </label>
65
  </p>
66
- <p>
67
- <label>
68
- <?php _e('Text to display after the post count: ','ald_tptn_plugin'); ?>
69
- <input type="textbox" name="after_count" id="after_count" value="<?php echo stripslashes($tptn_settings[after_count]); ?>">
70
- </label>
71
- </p>
72
- <p><?php _e('e.g. the default options displays <code>(Visited 123 times)</code>','ald_tptn_plugin'); ?></p>
73
  <p>
74
  <label>
75
  <?php _e('Number of popular posts to display: ','ald_tptn_plugin'); ?>
@@ -82,6 +76,12 @@ function tptn_options() {
82
  <input type="textbox" name="title" id="title" value="<?php echo stripslashes($tptn_settings[title]); ?>">
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"' ?> />
@@ -105,7 +105,7 @@ function tptn_options() {
105
 
106
  }
107
 
108
-
109
  function tptn_adminmenu() {
110
  if (function_exists('current_user_can')) {
111
  // In WordPress 2.x
@@ -124,8 +124,39 @@ function tptn_adminmenu() {
124
  add_options_page(__("Top 10", 'myald_tptn_plugin'), __("Top 10", 'myald_tptn_plugin'), 9, 'tptn_options', 'tptn_options');
125
  }
126
  }
 
127
 
128
 
129
- add_action('admin_menu', 'tptn_adminmenu');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  ?>
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);
59
  </legend>
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
  <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"' ?> />
105
 
106
  }
107
 
108
+ /* Add menu item in WP-Admin */
109
  function tptn_adminmenu() {
110
  if (function_exists('current_user_can')) {
111
  // In WordPress 2.x
124
  add_options_page(__("Top 10", 'myald_tptn_plugin'), __("Top 10", 'myald_tptn_plugin'), 9, 'tptn_options', 'tptn_options');
125
  }
126
  }
127
+ 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
+
134
+ $table_name = $wpdb->prefix . "top_ten";
135
+ $tptn_settings = tptn_read_options();
136
+ $limit = $tptn_settings['limit'];
137
+
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);
144
+
145
+ echo '<ul>';
146
+ if ($results) {
147
+ foreach ($results as $result) {
148
+ echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
149
+ }
150
+ }
151
+ if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
152
+ echo '</ul>';
153
+ }
154
+
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
  ?>
readme.txt CHANGED
@@ -3,17 +3,19 @@ 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.0
7
  Tested up to: 2.7
8
 
9
 
10
- Count page views on posts and display a list of top posts
11
 
12
  == Description ==
13
 
14
- WordPress doesn't count page views by default. This plugin 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 pages as well as display a list of most popular posts based on page views.
 
 
17
 
18
 
19
  == Installation ==
@@ -26,9 +28,16 @@ You can then display the page view count on individual pages as well as display
26
 
27
  4. Goto Settings > Top 10 to configure
28
 
 
 
29
 
30
  == Changelog ==
31
 
 
 
 
 
 
32
  * 1.0.1 - Release
33
 
34
 
@@ -36,7 +45,7 @@ You can then display the page view count on individual pages as well as display
36
 
37
  = What are the requirements for this plugin? =
38
 
39
- WordPress 2.0 or above
40
 
41
 
42
  = Can I customize what is displayed? =
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 ==
28
 
29
  4. Goto Settings > Top 10 to configure
30
 
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
 
45
 
46
  = What are the requirements for this plugin? =
47
 
48
+ WordPress 2.5 or above
49
 
50
 
51
  = Can I customize what is displayed? =
top-10_ajax.php → top-10-addcount.js.php RENAMED
@@ -1,4 +1,6 @@
1
  <?php
 
 
2
 
3
  if (!function_exists('add_action')) {
4
  $wp_root = '../../..';
1
  <?php
2
+ //"top-10-addcount.js.php" Add count to database
3
+ Header("content-type: application/x-javascript");
4
 
5
  if (!function_exists('add_action')) {
6
  $wp_root = '../../..';
top-10-counter.js.php CHANGED
@@ -17,16 +17,17 @@ function tptn_disp_count() {
17
 
18
  $table_name = $wpdb->prefix . "top_ten";
19
  $tptn_settings = tptn_read_options();
20
- $before_count = $tptn_settings['before_count'];
21
- $after_count = $tptn_settings['after_count'];
22
 
23
  $id = intval($_GET['top_ten_id']);
24
  if($id > 0) {
25
 
26
  $resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id");
27
- $cntaccess = (($resultscount) ? $resultscount->cntaccess : 0);
28
 
29
- echo 'document.write("'.$before_count.$cntaccess.$after_count.'")';
 
 
30
  }
31
  }
32
  tptn_disp_count();
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
 
22
  $id = intval($_GET['top_ten_id']);
23
  if($id > 0) {
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
  }
33
  tptn_disp_count();
top-10.php CHANGED
@@ -1,9 +1,9 @@
1
  <?php
2
  /*
3
  Plugin Name: Top 10
4
- Version: 1.0.1
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
  */
@@ -38,63 +38,18 @@ function tptn_add_viewed_count() {
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;
@@ -103,7 +58,10 @@ function tptn_pc_content($content) {
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
 
@@ -115,7 +73,12 @@ function tptn_show_pop_posts() {
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>';
@@ -126,19 +89,56 @@ function tptn_show_pop_posts() {
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
  );
@@ -156,9 +156,10 @@ function tptn_read_options() {
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);
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
  */
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();
49
  $id = intval($post->ID);
50
 
51
  if(($single)&&($tptn_settings['add_to_content'])) {
52
+ $output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>';
 
 
 
 
53
  return $content.$output;
54
  } else {
55
  return $content;
58
  add_filter('the_content', 'tptn_pc_content');
59
 
60
  function echo_tptn_post_count() {
61
+ global $post;
62
+ $id = intval($post->ID);
63
+
64
+ $output = '<script type="text/javascript" src="'.get_bloginfo('wpurl').'/wp-content/plugins/top-10/top-10-counter.js.php?top_ten_id='.$id.'"></script>';
65
  echo $output;
66
  }
67
 
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);
82
 
83
  echo '<div id="crp_related">'.$tptn_settings['title'];
84
  echo '<ul>';
89
  }
90
  if ($tptn_settings['show_credit']) echo '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
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) {
118
+ echo '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a> ('.$result->cntaccess.')</li>';
119
+ }
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
  );
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
  }
164
  if ($tptn_settings_changed == true)
165
  update_option('ald_tptn_settings', $tptn_settings);