topic_id );
//Builds the text to be appended to the title
$count_display =" (".$view_count . " views)";
//Makes this plugin play nice with the Page Links plugin by putting the pages links (if they exist) on a new line
if (function_exists('page_links_add_links')) {
$count_display .= "
";
}
$title .= $count_display;
return $title;
}
function show_view_count () {
/*Use this function directly to display the view count somehere other than at the end of the title. If you are going that route,
you will probably want to comment out the display_view_count_title function above*/
global $topic;
$view_count = get_view_count ( $topic->topic_id );
echo $view_count;
}
function get_view_count ( $topic_id=0 ) {
global $bbdb, $topic, $bb_topic_cache; $view_count=0;
if (empty($topic_id)) {if (!empty($topic->topic_id)) {$topic_id=$topic->topic_id;} else {return 0;}}
if (isset($topic->views) && $topic->topic_id===$topic_id) {$view_count=$topic->views;} // bypass db for cached data
elseif (defined('BACKPRESS_PATH')) { // bbPress 1.0
$temp_topic = wp_cache_get( $topic_id, 'bb_topic' );
if (!empty($temp_topic->views)) {$view_count= $temp_topic->views;} // bypass db for cached data
else {$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' ");}
} else { // bbPress 0.9
if (!empty($bb_topic_cache[$topic_id]->views)) {$view_count= $bb_topic_cache[$topic_id]->views;} // bypass db for cached data
else {$view_count = $bbdb->get_var("SELECT meta_value FROM $bbdb->topicmeta WHERE topic_id = $topic_id AND meta_key='views'");}
}
// If it already set, it just returns the value
if ($view_count<=0) { //If the view count hasn't bee initialized yet, this will initialize the value before it is returned
$view_count = initialize_view_count ( $topic_id );
}
return $view_count;
}
function initialize_view_count( $topic_id ) { //If the view count for a topic hasn't been set yet
global $bbdb, $topic;
$view_count = $topic->topic_posts; //Sets the new record to the number of posts that have been made in a topic
//Adds the record to the DB so it isn't zero any longer
/* // this is broken somehow - occasionally inserts multiple topic_ids of zero because bbpress doesn't keep unique index on meta
if (defined('BACKPRESS_PATH')) { // bbPress 1.0
@$bbdb->query("INSERT INTO $bbdb->meta (object_id, object_type, meta_key, meta_value) VALUES ($topic_id, 'bb_topic', 'views', $view_count)");
} else { // bbPress 0.7 - 0.9
@$bbdb->query("INSERT INTO $bbdb->topicmeta (topic_id, meta_key, meta_value) VALUES ($topic_id, 'views', $view_count)");
}
*/
bb_delete_meta( $topic_id, 'views', 0, 'topic' );
bb_update_meta( $topic_id, 'views', $view_count, 'topic' );
return $view_count;
}
function update_view_count() {
global $bbdb, $topic, $topic_id;
if (is_topic()) {
if (empty($topic_id)) {if (empty($topic)) {return;} else {$topic_id=$topic->topic_id;}} // should never happen in bb_head but does for some reason in 1.0 ?
if (empty($topic->views)) {$view_count=0;} else {$view_count=$topic->views;}
if ($view_count>=1) {
$last_topic_id = $_SESSION['last_topic_id']; // Pulls the session variable for the last topic we viewed
if ($topic_id != $last_topic_id) { //Makes sure we aren't viewing a different page of the same topic.
// Add 1 to $view_count and update the DB
$topic->views++;
if (defined('BACKPRESS_PATH')) { // bbPress 1.0
@$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");
} else { // 0.7 - 0.9
@$bbdb->query("UPDATE $bbdb->topicmeta SET meta_value=meta_value+1 WHERE topic_id = $topic_id AND meta_key='views' LIMIT 1");
}
}
} else {
initialize_view_count ( $topic_id); // Initializes the value so the next time it is displayed it will not be zero
}
$_SESSION['last_topic_id'] = $topic_id; //Sets the session variable so it is there the next time we view a topics page
}
}
function most_viewed_list ( $list_length = '10', $before_list = '
| Topic | Views | Posts | Last Poster | Freshness |
|---|---|---|---|---|