PluginProbe
WebberZone Top 10 — Popular Posts / 1.6
WebberZone Top 10 — Popular Posts v1.6
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.6, at top-10.php

444 lines 15.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.6
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>
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 // Pre-2.6 compatibility
16 if ( ! defined( 'WP_CONTENT_URL' ) )
17 define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
18 if ( ! defined( 'WP_CONTENT_DIR' ) )
19 define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
20 if ( ! defined( 'WP_PLUGIN_URL' ) )
21 define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
22 if ( ! defined( 'WP_PLUGIN_DIR' ) )
23 define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
24
25 // Guess the location
26 $tptn_path = WP_PLUGIN_DIR.'/'.plugin_basename(dirname(__FILE__));
27 $tptn_url = WP_PLUGIN_URL.'/'.plugin_basename(dirname(__FILE__));
28
29
30
31 if (!function_exists('add_action')) {
32 $wp_root = '../../..';
33 if (file_exists($wp_root.'/wp-load.php')) {
34 require_once($wp_root.'/wp-load.php');
35 } else {
36 require_once($wp_root.'/wp-config.php');
37 }
38 }
39
40 global $tptn_db_version;
41 $tptn_db_version = "2.8";
42
43 function ald_tptn_init() {
44 //* Begin Localization Code */
45 $tc_localizationName = TPTN_LOCAL_NAME;
46 $tc_comments_locale = get_locale();
47 $tc_comments_mofile = ALD_TPTN_DIR . "/languages/" . $tc_localizationName . "-". $tc_comments_locale.".mo";
48 load_textdomain($tc_localizationName, $tc_comments_mofile);
49 //* End Localization Code */
50 }
51 add_action('init', 'ald_tptn_init');
52
53 /*********************************************************************
54 * Main Function (Do not edit) *
55 ********************************************************************/
56 // Update post views
57 add_filter('the_content','tptn_add_viewed_count');
58 function tptn_add_viewed_count($content) {
59 global $post, $wpdb, $single,$tptn_url,$tptn_path;
60 $table_name = $wpdb->prefix . "top_ten";
61 $tptn_settings = tptn_read_options();
62
63 $current_user = wp_get_current_user();
64 $post_author = ( $current_user->ID == $post->post_author ? true : false );
65
66 if((is_single() || is_page())) {
67 if (!(($post_author)&&(!$tptn_settings['track_authors']))) {
68 $id = intval($post->ID);
69 $output = '<script type="text/javascript" src="'.$tptn_url.'/top-10-addcount.js.php?top_ten_id='.$id.'"></script>';
70 return $content.$output;
71 }
72 else {
73 return $content;
74 }
75 }
76 else {
77 return $content;
78 }
79 }
80
81
82 // Function to add count to content
83 function tptn_pc_content($content) {
84 global $single, $post,$tptn_url,$tptn_path;
85 $tptn_settings = tptn_read_options();
86 $id = intval($post->ID);
87
88 if((is_single())&&($tptn_settings['add_to_content'])) {
89 $output = '<script type="text/javascript" src="'.$tptn_url.'/top-10-counter.js.php?top_ten_id='.$id.'"></script>';
90 return $content.$output;
91 } elseif((is_page())&&($tptn_settings['count_on_pages'])) {
92 $output = '<script type="text/javascript" src="'.$tptn_url.'/top-10-counter.js.php?top_ten_id='.$id.'"></script>';
93 return $content.$output;
94 } else {
95 return $content;
96 }
97 }
98 add_filter('the_content', 'tptn_pc_content');
99
100 // Function to manually display count
101 function echo_tptn_post_count() {
102 global $post,$tptn_url,$tptn_path;
103 $id = intval($post->ID);
104
105 $output = '<script type="text/javascript" src="'.$tptn_url.'/top-10-counter.js.php?top_ten_id='.$id.'"></script>';
106 echo $output;
107 }
108
109 // Function to return popular posts
110 function tptn_pop_posts( $daily = false , $widget = false ) {
111 global $wpdb, $siteurl, $tableposts, $id;
112 if ($daily) $table_name = $wpdb->prefix . "top_ten_daily";
113 else $table_name = $wpdb->prefix . "top_ten";
114 $tptn_settings = tptn_read_options();
115 $limit = $tptn_settings['limit'];
116
117 if (!$daily) {
118 $sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status, post_content ";
119 $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
120 if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
121 $sql .= "AND post_status = 'publish' ";
122 $sql .= "ORDER BY sumCount DESC LIMIT $limit";
123 } else {
124 $daily_range = $tptn_settings[daily_range] - 1;
125 $current_time = gmdate( 'Y-m-d', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
126 $current_date = strtotime ( '-'.$daily_range. ' DAY' , strtotime ( $current_time ) );
127 $current_date = date ( 'Y-m-j' , $current_date );
128
129 $sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status, post_content ";
130 $sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ;
131 if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' ";
132 $sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' ";
133 $sql .= "GROUP BY postnumber ";
134 $sql .= "ORDER BY sumCount DESC LIMIT $limit";
135 }
136 $results = $wpdb->get_results($sql);
137 $output = '';
138
139 if (!$widget) {
140 if (!$daily) {
141 $output .= '<div id="tptn_related">'.$tptn_settings['title'];
142 } else {
143 $output .= '<div id="tptn_related_daily">'.$tptn_settings['title_daily'];
144 }
145 }
146
147 if ($results) {
148 $output .= $tptn_settings['before_list'];
149 foreach ($results as $result) {
150 $title = trim(stripslashes(get_the_title($result->postnumber)));
151 $output .= $tptn_settings['before_list_item'];
152
153 if (($tptn_settings['post_thumb_op']=='inline')||($tptn_settings['post_thumb_op']=='thumbs_only')) {
154 $output .= '<a href="'.get_permalink($result->postnumber).'" rel="bookmark">';
155 if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail($result->postnumber))) {
156 $output .= get_the_post_thumbnail( $result->postnumber, array($tptn_settings[thumb_width],$tptn_settings[thumb_height]), array('title' => $title,'alt' => $title, 'class' => 'tptn_thumb'));
157 } else {
158 $postimage = get_post_meta($result->postnumber, $tptn_settings[thumb_meta], true); // Check
159 if ((!$postimage)&&($tptn_settings['scan_images'])) {
160 preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $result->post_content, $matches );
161 // any image there?
162 if( isset( $matches ) && $matches[1][0] ) {
163 $postimage = $matches[1][0]; // we need the first one only!
164 }
165 }
166 if (!$postimage) $postimage = $tptn_settings[thumb_default];
167 $output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" width="'.$tptn_settings[thumb_width].'" height="'.$tptn_settings[thumb_height].'" class="tptn_thumb" />';
168 }
169 $output .= '</a> ';
170 }
171 if (($tptn_settings['post_thumb_op']=='inline')||($tptn_settings['post_thumb_op']=='text_only')) {
172 $output .= '<a href="'.get_permalink($result->postnumber).'" rel="bookmark">'.$title.'</a>';
173 }
174 if ($tptn_settings['show_excerpt']) {
175 $output .= '<span class="tptn_excerpt"> '.tptn_excerpt($result->post_content,$tptn_settings['excerpt_length']).'</span>';
176 }
177 if ($tptn_settings['disp_list_count']) $output .= ' ('.$result->sumCount.')';
178 $output .= $tptn_settings['after_list_item'];
179 }
180 if ($tptn_settings['show_credit']) $output .= '<li>Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></li>';
181 $output .= $tptn_settings['after_list'];
182 }
183 if (!$widget) $output .= '</div>';
184
185 return $output;
186 }
187
188 // Function to show popular posts
189 function tptn_show_pop_posts() {
190 echo tptn_pop_posts(false,false);
191 }
192
193 // Function to show daily popular posts
194 function tptn_show_daily_pop_posts() {
195 global $tptn_url;
196 $tptn_settings = tptn_read_options();
197 if ($tptn_settings['d_use_js']) {
198 echo '<script type="text/javascript" src="'.$tptn_url.'/top-10-daily.js.php?widget=1"></script>';
199 } else {
200 echo tptn_pop_posts(true,false);
201 }
202 }
203
204 // Create a WordPress Widget for Daily Popular Posts
205 function widget_tptn_pop_daily($args) {
206 global $wpdb, $siteurl, $tableposts, $id,$tptn_url;
207
208 extract($args); // extracts before_widget,before_title,after_title,after_widget
209
210 $tptn_settings = tptn_read_options();
211 $title = (($tptn_settings['title_daily']) ? strip_tags($tptn_settings['title_daily']) : __('Daily Popular',TPTN_LOCAL_NAME));
212
213 echo $before_widget;
214 echo $before_title.$title.$after_title;
215
216 if ($tptn_settings['d_use_js']) {
217 echo '<script type="text/javascript" src="'.$tptn_url.'/top-10-daily.js.php?widget=1"></script>';
218 } else {
219 echo tptn_pop_posts(true,true);
220 }
221
222 echo $after_widget;
223 }
224
225 // Create a WordPress Widget for Popular Posts
226 function widget_tptn_pop($args) {
227 global $wpdb, $siteurl, $tableposts, $id;
228
229 extract($args); // extracts before_widget,before_title,after_title,after_widget
230
231 $tptn_settings = tptn_read_options();
232 $title = (($tptn_settings['title']) ? strip_tags($tptn_settings['title']) : __('Popular Posts',TPTN_LOCAL_NAME));
233
234 echo $before_widget;
235 echo $before_title.$title.$after_title;
236 echo tptn_pop_posts(false,true);
237 echo $after_widget;
238 }
239
240 // Default Options
241 function tptn_default_options() {
242 global $tptn_url;
243 $title = __('<h3>Popular Posts</h3>',TPTN_LOCAL_NAME);
244 $title_daily = __('<h3>Daily Popular</h3>',TPTN_LOCAL_NAME);
245 $thumb_default = $tptn_url.'/default.png';
246
247 $tptn_settings = Array (
248 show_credit => true, // Add link to plugin page of my blog in top posts list
249 add_to_content => true, // Add post count to content (only on single posts)
250 exclude_pages => true, // Exclude Pages
251 count_on_pages => true, // Display on pages
252 track_authors => false, // Track Authors visits
253 pv_in_admin => true, // Add an extra column on edit posts/pages to display page views?
254 disp_list_count => true, // Display count in popular lists?
255 d_use_js => false, // Use JavaScript for displaying daily posts
256 count_disp_form => '(Visited %totalcount% times, %dailycount% visits today)', // Format to display the count
257 title => $title, // Title of Popular Posts
258 title_daily => $title_daily, // Title of Daily Popular
259 limit => '10', // How many posts to display?
260 daily_range => '1', // Daily Popular will contain posts of how many days?
261 before_list => '<ul>', // Before the entire list
262 after_list => '</ul>', // After the entire list
263 before_list_item => '<li>', // Before each list item
264 after_list_item => '</li>', // After each list item
265 post_thumb_op => 'text_only', // Display only text in posts
266 thumb_height => '100', // Height of thumbnails
267 thumb_width => '100', // Width of thumbnails
268 thumb_meta => 'post-image', // Meta field that is used to store the location of default thumbnail image
269 thumb_default => $thumb_default, // Default thumbnail image
270 scan_images => false, // Scan post for images
271 show_excerpt => false, // Show description in list item
272 excerpt_length => '10', // Length of characters
273 );
274 return $tptn_settings;
275 }
276
277 // Function to read options from the database
278 function tptn_read_options() {
279
280 // Upgrade table code
281 global $tptn_db_version;
282 $installed_ver = get_option( "tptn_db_version" );
283
284 if( $installed_ver != $tptn_db_version ) tptn_install();
285
286 $tptn_settings_changed = false;
287
288 $defaults = tptn_default_options();
289
290 $tptn_settings = array_map('stripslashes',(array)get_option('ald_tptn_settings'));
291 unset($tptn_settings[0]); // produced by the (array) casting when there's nothing in the DB
292
293 foreach ($defaults as $k=>$v) {
294 if (!isset($tptn_settings[$k])) {
295 $tptn_settings[$k] = $v;
296 $tptn_settings_changed = true;
297 }
298 }
299 if ($tptn_settings_changed == true)
300 update_option('ald_tptn_settings', $tptn_settings);
301
302 return $tptn_settings;
303
304 }
305
306 // Create tables to store pageviews
307 function tptn_install() {
308 global $wpdb;
309 global $tptn_db_version;
310
311 $table_name = $wpdb->prefix . "top_ten";
312 $table_name_daily = $wpdb->prefix . "top_ten_daily";
313
314 if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
315
316 $sql = "CREATE TABLE " . $table_name . " (
317 accessedid int NOT NULL AUTO_INCREMENT,
318 postnumber int NOT NULL,
319 cntaccess int NOT NULL,
320 PRIMARY KEY (accessedid)
321 );";
322
323 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
324 dbDelta($sql);
325
326 add_option("tptn_db_version", $tptn_db_version);
327 }
328
329 if($wpdb->get_var("show tables like '$table_name_daily'") != $table_name_daily) {
330
331 $sql = "CREATE TABLE " . $table_name_daily . " (
332 accessedid int NOT NULL AUTO_INCREMENT,
333 postnumber int NOT NULL,
334 cntaccess int NOT NULL,
335 dp_date date NOT NULL,
336 PRIMARY KEY (accessedid)
337 );";
338
339 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
340 dbDelta($sql);
341
342 add_option("tptn_db_version", $tptn_db_version);
343 }
344
345 // Upgrade table code
346 $installed_ver = get_option( "tptn_db_version" );
347
348 if( $installed_ver != $tptn_db_version ) {
349
350 $sql = "CREATE TABLE " . $table_name . " (
351 accessedid int NOT NULL AUTO_INCREMENT,
352 postnumber int NOT NULL,
353 cntaccess int NOT NULL,
354 PRIMARY KEY (accessedid)
355 );";
356
357 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
358 dbDelta($sql);
359
360 $sql = "DROP TABLE $table_name_daily";
361 $wpdb->query($sql);
362
363 $sql = "CREATE TABLE " . $table_name_daily . " (
364 accessedid int NOT NULL AUTO_INCREMENT,
365 postnumber int NOT NULL,
366 cntaccess int NOT NULL,
367 dp_date date NOT NULL,
368 PRIMARY KEY (accessedid)
369 );";
370
371 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
372 dbDelta($sql);
373
374 update_option( "tptn_db_version", $tptn_db_version );
375 }
376
377 }
378 if (function_exists('register_activation_hook')) {
379 register_activation_hook(__FILE__,'tptn_install');
380 }
381
382
383 // Function to delete all rows in the posts table
384 function tptn_trunc_count($daily = false) {
385 global $wpdb;
386 $table_name = $wpdb->prefix . "top_ten";
387 if ($daily) $table_name .= "_daily";
388
389 $sql = "TRUNCATE TABLE $table_name";
390 $wpdb->query($sql);
391 }
392
393 function init_tptn(){
394 register_sidebar_widget(__('Popular Posts',TPTN_LOCAL_NAME), 'widget_tptn_pop');
395 register_sidebar_widget(__('Daily Popular',TPTN_LOCAL_NAME), 'widget_tptn_pop_daily');
396 }
397 add_action("plugins_loaded", "init_tptn");
398
399 // Function to create an excerpt for the post
400 function tptn_excerpt($content,$excerpt_length){
401 $out = strip_tags($content);
402 $blah = explode(' ',$out);
403 if (!$excerpt_length) $excerpt_length = 10;
404 if(count($blah) > $excerpt_length){
405 $k = $excerpt_length;
406 $use_dotdotdot = 1;
407 }else{
408 $k = count($blah);
409 $use_dotdotdot = 0;
410 }
411 $excerpt = '';
412 for($i=0; $i<$k; $i++){
413 $excerpt .= $blah[$i].' ';
414 }
415 $excerpt .= ($use_dotdotdot) ? '...' : '';
416 $out = $excerpt;
417 return $out;
418 }
419
420 // This function adds an Options page in WP Admin
421 if (is_admin() || strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
422 require_once(ALD_TPTN_DIR . "/admin.inc.php");
423
424 // Add meta links
425 function tptn_plugin_actions( $links, $file ) {
426 $plugin = plugin_basename(__FILE__);
427
428 // create link
429 if ($file == $plugin) {
430 $links[] = '<a href="' . admin_url( 'options-general.php?page=tptn_options' ) . '">' . __('Settings', TPTN_LOCAL_NAME ) . '</a>';
431 $links[] = '<a href="http://ajaydsouza.org">' . __('Support', TPTN_LOCAL_NAME ) . '</a>';
432 $links[] = '<a href="http://ajaydsouza.com/donate/">' . __('Donate', TPTN_LOCAL_NAME ) . '</a>';
433 }
434 return $links;
435 }
436 global $wp_version;
437 if ( version_compare( $wp_version, '2.8alpha', '>' ) )
438 add_filter( 'plugin_row_meta', 'tptn_plugin_actions', 10, 2 ); // only 2.8 and higher
439 else add_filter( 'plugin_action_links', 'tptn_plugin_actions', 10, 2 );
440
441 }
442
443
444 ?>