Changeset 1312
- Timestamp:
- 08/28/08 05:13:07 (3 months ago)
- Files:
-
- bb-topic-views/trunk/bb-topic-views.php (modified) (7 diffs)
- bb-topic-views/trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
bb-topic-views/trunk/bb-topic-views.php
r435 r1312 2 2 /* 3 3 Plugin Name: bb-Topic-Views 4 Plugin URI: http://b log.wittmania.com/bb-topic-views4 Plugin URI: http://bbpress.org/plugins/topic/bb-topic-views/ 5 5 Description: Counts the number of times a topic has been viewed, and allows the administrator to display the count in various places. 6 Author: Mike Wittmann 6 Author: Mike Wittmann, _ck_ 7 7 Author URI: http://blog.wittmania.com/ 8 Version: 1. 58 Version: 1.6.0 9 9 */ 10 10 … … 12 12 $append_to_title = 1; 13 13 14 /* stop editing here */ 15 16 // only executes if option is set to 1 above 17 if ($append_to_title && (is_front() || is_forum() || is_tags())) {add_filter('topic_title', 'view_count_append_to_title', 99);} 18 19 add_filter('bb_head', 'update_view_count'); 20 add_action('bb_init', 'views_session_check'); 21 14 22 //Force bbpress to open a session, if it hasn't already, which will help to avoid double-counting views (see below) 15 function views_session_check () 16 { 17 if( !isset( $_SESSION ) ) { 18 session_start(); 19 } 20 } 21 22 function view_count_append_to_title ($title) 23 function views_session_check () { 24 if( !isset( $_SESSION ) && is_topic()) { // only start session if not already stared and it's a topic page 25 // eaccelerator_set_session_handlers(); // todo: detect and work with memcaches 26 // @session_cache_limiter('public'); // allows back button to work without losing form data - update: bad idea, causes other problems 27 @session_start(); 28 } 29 } 30 31 if (!function_exists('is_tags')) {function is_tags() {return is_tag();}} // older bbPress compatibility 32 33 function view_count_append_to_title ($title) { 23 34 /*This function appends the view count to the end of the title on the front, forum, and tags pages. You can comment this out if you want 24 35 to use the "show_view_count" function to place the view count somewhere else instead.*/ 25 { 36 26 37 global $topic; 27 38 … … 40 51 } 41 52 42 function show_view_count () 53 function show_view_count () { 43 54 /*Use this function directly to display the view count somehere other than at the end of the title. If you are going that route, 44 55 you will probably want to comment out the display_view_count_title function above*/ 45 { 56 46 57 global $topic; 47 58 … … 50 61 } 51 62 52 53 function get_view_count ( $topic_id ) 54 { 63 function get_view_count ( $topic_id ) { 55 64 global $bbdb; 56 65 66 if (bb_get_option('bb_db_version')>1600) { // bbPress 1.0 67 68 $view_count = $bbdb->get_var("SELECT meta_value FROM $bbdb->meta WHERE object_type='bb_topic' AND object_id = $topic_id AND meta_key='views' "); 69 70 } else { 71 57 72 $view_count = $bbdb->get_var("SELECT meta_value FROM $bbdb->topicmeta WHERE topic_id = $topic_id AND meta_key='views'"); 73 74 } 75 58 76 //If it already set, it just returns the value 59 77 … … 65 83 } 66 84 67 function initialize_view_count( $topic_id ) //If the view count for a topic hasn't been set yet68 {85 function initialize_view_count( $topic_id ) { //If the view count for a topic hasn't been set yet 86 69 87 global $bbdb, $topic; 70 88 71 $view_count = $topic->topic_posts; //Sets the new record to the number of posts that have been made in a topic89 $view_count = $topic->topic_posts; //Sets the new record to the number of posts that have been made in a topic 72 90 73 91 //Adds the record to the DB so it isn't zero any longer 74 $bbdb->query("INSERT INTO $bbdb->topicmeta ( meta_id, topic_id, meta_key, meta_value) VALUES ( NULL , $topic_id, 'views', $view_count )"); 92 93 if (bb_get_option('bb_db_version')>1600) { // bbPress 1.0 94 95 @$bbdb->query("INSERT INTO $bbdb->meta (object_id, object_type, meta_key, meta_value) VALUES ($topic_id, 'bb_topic', 'views', $view_count)"); 96 97 } else { // bbPress 0.7 - 0.9 98 99 @$bbdb->query("INSERT INTO $bbdb->topicmeta (topic_id, meta_key, meta_value) VALUES ($topic_id, 'views', $view_count)"); 100 101 } 75 102 76 103 return $view_count; 77 104 } 78 105 79 function update_view_count() 80 { 81 global $bbdb, $topic; 106 function update_view_count() { 107 global $bbdb, $topic, $topic_id; 82 108 83 109 if (is_topic()) { 84 $topic_id = get_topic_id(); 85 86 $row = $bbdb->get_row("SELECT * FROM $bbdb->topicmeta WHERE topic_id = $topic_id AND meta_key='views'"); 87 $view_count = $row->meta_value; 88 $meta_id = $row->meta_id; 110 if (empty($topic->views)) {$view_count=0;} else {$view_count=$topic->views;} 89 111 90 112 if ($view_count>=1) { 91 $last_topic_id = $_SESSION['last_topic_id']; //Pulls the session variable for the last topic we viewed 92 if ($topic_id != $last_topic_id) { //Makes sure we aren't viewing a different page of the same topic. 93 //Add 1 to $view_count and update the DB 94 $view_count = ($view_count + 1); 95 $bbdb->query("UPDATE $bbdb->topicmeta SET meta_value = $view_count WHERE meta_id = $meta_id"); 96 } 97 }else{ 98 initialize_view_count ( $topic_id); // Initializes the value so the next time it is displayed it will not be zero 113 114 $last_topic_id = $_SESSION['last_topic_id']; // Pulls the session variable for the last topic we viewed 115 if ($topic_id != $last_topic_id) { //Makes sure we aren't viewing a different page of the same topic. 116 117 // Add 1 to $view_count and update the DB 118 $topic->views++; 119 120 if (bb_get_option('bb_db_version')>1600) { // bbPress 1.0 121 122 @$bbdb->query("UPDATE $bbdb->meta SET meta_value=meta_value+1 WHERE object_type='bb_topic' AND object_id=$topic_id AND meta_key='views' LIMIT 1"); 123 124 } else { // 0.7 - 0.9 125 126 @$bbdb->query("UPDATE $bbdb->topicmeta SET meta_value=meta_value+1 WHERE topic_id=$topic_id AND meta_key='views' LIMIT 1"); 127 128 } 129 130 } 131 132 } else { 133 initialize_view_count ( $topic_id); // Initializes the value so the next time it is displayed it will not be zero 99 134 } 100 $_SESSION['last_topic_id'] = $topic_id;//Sets the session variable so it is there the next time we view a topics page101 } 102 } 103 104 function most_viewed_list ( $list_length = '10', $before_list = '<ul>', $after_list = '</ul>', $before_item = '<li>', $after_item = '</li>') 105 { 135 $_SESSION['last_topic_id'] = $topic_id; //Sets the session variable so it is there the next time we view a topics page 136 } 137 } 138 139 function most_viewed_list ( $list_length = '10', $before_list = '<ul>', $after_list = '</ul>', $before_item = '<li>', $after_item = '</li>') { 140 106 141 global $bbdb; 107 142 143 if (bb_get_option('bb_db_version')>1600) { // bbPress 1.0 144 145 $most_viewed = (array) $bbdb->get_results("SELECT object_id as topic_id, meta_value FROM $bbdb->meta WHERE object_type='bb_topic' AND meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC"); 146 147 } else { 148 108 149 $most_viewed = (array) $bbdb->get_results("SELECT topic_id, meta_value FROM $bbdb->topicmeta WHERE meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC"); 150 151 } 152 109 153 $most_viewed = array_slice($most_viewed, 0, $list_length); 110 154 … … 123 167 } 124 168 125 function most_viewed_table ( $list_length = '10') 126 { 169 function most_viewed_table ( $list_length = '10') { 127 170 global $bbdb; 128 171 172 if (bb_get_option('bb_db_version')>1600) { // bbPress 1.0 173 174 $most_viewed = (array) $bbdb->get_results("SELECT object_id as topic_id, meta_value FROM $bbdb->meta WHERE object_type='bb_topic' AND meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC"); 175 176 } else { 177 129 178 $most_viewed = (array) $bbdb->get_results("SELECT topic_id, meta_value FROM $bbdb->topicmeta WHERE meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC"); 179 180 } 181 130 182 $most_viewed = array_slice($most_viewed, 0, $list_length); 131 183 … … 171 223 } 172 224 173 if (!function_exists('is_tags')) {174 function is_tags()175 {176 return is_tag();177 }178 }179 180 add_action('bb_init', 'views_session_check');181 add_filter('bb_head', 'update_view_count');182 183 // only executes if option is set to 1 above184 if (is_front() || is_forum() || is_tags()) {185 if ($append_to_title == 1) {186 add_filter('topic_title', 'view_count_append_to_title', 99);187 }188 }189 225 ?> bb-topic-views/trunk/readme.txt
r436 r1312 1 1 === bb-Topic-Views === 2 2 Plugin Name: bb-Topic-Views 3 Plugin URI: http://b log.wittmania.com/bb-topic-views4 Tags: views, count, topic views 3 Plugin URI: http://bbpress.org/plugins/topic/bb-topic-views/ 4 Tags: views, count, topic views, _ck_ 5 5 Author: wittmania 6 6 Author URI: http://blog.wittmania.com 7 Stable Tag: 1.5 7 Contributors: _ck_ 8 Stable Tag: 1.6 9 Requires at least: 0.8 10 Tested up to: 1.0 8 11 9 12 bb-Topic-Views counts and displays the number of times each topic has been viewed. … … 119 122 == Example == 120 123 121 You can see this plugin in action at http://blog.wittmania.com/bbpress 124 You can see this plugin in action at http://blog.wittmania.com/bbpress or http://bbshowcase.org/forums/
