Changeset 1312

Show
Ignore:
Timestamp:
08/28/08 05:13:07 (3 months ago)
Author:
_ck_
Message:

1.6.0 fixes for bbPress 1.0 compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bb-topic-views/trunk/bb-topic-views.php

    r435 r1312  
    22/* 
    33Plugin Name: bb-Topic-Views 
    4 Plugin URI: http://blog.wittmania.com/bb-topic-views 
     4Plugin URI: http://bbpress.org/plugins/topic/bb-topic-views/ 
    55Description: 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 
     6Author: Mike Wittmann, _ck_ 
    77Author URI: http://blog.wittmania.com/ 
    8 Version: 1.5 
     8Version: 1.6.0 
    99*/ 
    1010 
     
    1212$append_to_title = 1; 
    1313 
     14/*  stop editing here */ 
     15 
     16// only executes if option is set to 1 above 
     17if ($append_to_title && (is_front() || is_forum() || is_tags())) {add_filter('topic_title', 'view_count_append_to_title', 99);} 
     18 
     19add_filter('bb_head', 'update_view_count'); 
     20add_action('bb_init', 'views_session_check'); 
     21 
    1422//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) 
     23function 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 
     31if (!function_exists('is_tags')) {function is_tags() {return is_tag();}}        // older bbPress compatibility 
     32 
     33function view_count_append_to_title ($title) { 
    2334/*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  
    2435to use the "show_view_count" function to place the view count somewhere else instead.*/ 
    25 
     36 
    2637        global $topic; 
    2738         
     
    4051} 
    4152 
    42 function show_view_count () 
     53function show_view_count () { 
    4354/*Use this function directly to display the view count somehere other than at the end of the title.  If you are going that route, 
    4455you will probably want to comment out the display_view_count_title function above*/ 
    45 
     56 
    4657        global $topic; 
    4758         
     
    5061} 
    5162         
    52  
    53 function get_view_count ( $topic_id ) 
    54 
     63function get_view_count ( $topic_id ) { 
    5564        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         
    5772        $view_count = $bbdb->get_var("SELECT meta_value FROM $bbdb->topicmeta WHERE topic_id = $topic_id AND meta_key='views'"); 
     73         
     74        } 
     75                 
    5876                //If it already set, it just returns the value 
    5977 
     
    6583} 
    6684 
    67 function initialize_view_count( $topic_id ) //If the view count for a topic hasn't been set yet 
    68 {  
     85function initialize_view_count( $topic_id ) //If the view count for a topic hasn't been set yet 
     86  
    6987        global $bbdb, $topic; 
    7088         
    71         $view_count = $topic->topic_posts; //Sets the new record to the number of posts that have been made in a topic 
     89        $view_count = $topic->topic_posts;     //Sets the new record to the number of posts that have been made in a topic 
    7290                 
    7391        //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        }  
    75102         
    76103        return $view_count; 
    77104} 
    78105         
    79 function update_view_count() 
    80 
    81         global $bbdb, $topic; 
     106function update_view_count() { 
     107        global $bbdb, $topic, $topic_id; 
    82108         
    83109        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;} 
    89111         
    90112                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 
    99134                } 
    100         $_SESSION['last_topic_id'] = $topic_id; //Sets the session variable so it is there the next time we view a topics page 
    101         } 
    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 
     139function most_viewed_list ( $list_length = '10', $before_list = '<ul>', $after_list = '</ul>', $before_item = '<li>', $after_item = '</li>') { 
     140         
    106141        global $bbdb; 
    107142         
     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 
    108149        $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                 
    109153        $most_viewed = array_slice($most_viewed, 0, $list_length); 
    110154         
     
    123167} 
    124168 
    125 function most_viewed_table ( $list_length = '10') 
    126 
     169function most_viewed_table ( $list_length = '10') { 
    127170        global $bbdb; 
    128171         
     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 
    129178        $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                 
    130182        $most_viewed = array_slice($most_viewed, 0, $list_length); 
    131183         
     
    171223} 
    172224 
    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 above 
    184 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 } 
    189225?> 
  • bb-topic-views/trunk/readme.txt

    r436 r1312  
    11=== bb-Topic-Views === 
    22Plugin Name: bb-Topic-Views 
    3 Plugin URI: http://blog.wittmania.com/bb-topic-views 
    4 Tags: views, count, topic views 
     3Plugin URI: http://bbpress.org/plugins/topic/bb-topic-views/ 
     4Tags: views, count, topic views, _ck_ 
    55Author: wittmania 
    66Author URI: http://blog.wittmania.com 
    7 Stable Tag: 1.5 
     7Contributors:  _ck_ 
     8Stable Tag: 1.6 
     9Requires at least: 0.8 
     10Tested up to: 1.0 
    811 
    912bb-Topic-Views counts and displays the number of times each topic has been viewed. 
     
    119122== Example == 
    120123 
    121 You can see this plugin in action at http://blog.wittmania.com/bbpress 
     124You can see this plugin in action at http://blog.wittmania.com/bbpress or http://bbshowcase.org/forums/