Plugin Directory

Changeset 1332098


Ignore:
Timestamp:
01/20/2016 01:04:56 PM (10 years ago)
Author:
jezza101
Message:

Added member tracking. The plugin can now give a unique count of member views.

Location:
bbpress-simple-view-counts/trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • bbpress-simple-view-counts/trunk/bbpress-simple-view-counts.php

    r764794 r1332098  
    55Description: Counts and shows views in bbPress 2 forum
    66Author: jezza101
    7 Version: 0.1
     7Version: 0.2
    88Author URI: http://www.blogercise.com
    99*/
     
    1111
    1212/*
    13 Date      Version     History
    14 ------    -----       --------
    15 
     13Date       Version     History
     14------     -----       --------
     1512/01/2016 0.2         Added member counts
    1616
    1717
    1818*/
    1919
     20//this plugin is maintained!
     21//contact me via my webpage, twitter @jezza101 or the forum support page on wordpress.org
     22
     23const SVC_VERSION = 0.2;
     24
     25
    2026class bbpress_simple_view_counts {
     27
     28
     29    private $options;
     30    private $pluginConfigPage;
    2131
    2232    public function __construct() {
    2333
    24        //FILTERS NEEDED TO SHOW THE VIEWS IN THE FRONT END
    25        add_action ('bbp_theme_after_topic_started_by', array($this,'show_views_forum_page'),60);
    26        add_filter('bbp_get_single_topic_description', array($this,'show_views_topic_page'), 100, 2);
     34        $this->options = get_option('svc-options');
     35        //print_r($this->options);die;
     36
     37        //FILTERS NEEDED TO SHOW THE VIEWS IN THE FRONT END
     38        add_action ('bbp_theme_after_topic_started_by', array($this,'show_views_forum_page'),60);
     39        add_filter('bbp_get_single_topic_description', array($this,'show_views_topic_page'), 100, 2);
     40
     41
     42//        $options['version'] = SVC_VERSION;
     43//        $options['hitCount'] = '1';
     44//        $options['memberCount'] ='1';
     45//        update_option('SVC-options',$options);
     46
     47        if (is_admin()) {
     48
     49            require_once(WP_PLUGIN_DIR .'/bbpress-simple-view-counts/admin-options.php');
     50            $this->pluginConfigPage =  new PluginConfigPage();
     51
     52            register_activation_hook(__FILE__, array($this, 'activate'));
     53            add_action('admin_menu', array($this, 'register_my_custom_menu_page'));
     54
     55            //handle upgrades
     56            if (empty($this->options['version']) || SVC_VERSION > $this->options['version']){
     57                //do upgrade
     58                $this->upgrade();
     59            }
     60        }
     61    }
     62
     63
     64
     65    function register_my_custom_menu_page()
     66    {
     67        add_submenu_page('edit.php?post_type=forum', 'View Counts', 'View Count Options', 'administrator',
     68            'bbPress_simple_view_count', array($this->pluginConfigPage,'show_options_page'));
     69    }
     70
     71
     72
     73    function show_views_forum_page() {
     74
     75        //FORUM PAGE
     76        //ie the forum pages lists all the posts in the forum, let's add a view count
     77
     78        $post_id = get_the_ID();
     79        $count   = get_post_meta( $post_id, 'bbp_svc_viewcounts', true );
     80
     81        if (!empty($count)){
     82            echo '<br><span class="bbp-topic-started-by">Views: '.$count.'</span>';
     83        }
     84        return;
     85    }
     86
     87    function show_views_topic_page( $content, $reply_id ) {
     88
     89        $post_id = get_the_ID();
     90
     91        //HITS
     92        IF ($this->options['hitCount']==1) {
     93            //get previous count and add one!
     94            $hitCount = get_post_meta($post_id, 'bbp_svc_viewcounts', true);
     95            $hitCount = $hitCount + 1;
     96            update_post_meta($post_id, 'bbp_svc_viewcounts', $hitCount) ;
     97
     98            if ($hitCount==1){
     99                $text = ". This post has been viewed 1 time";
     100            }
     101            else if (!empty($hitCount)){
     102                $text = ". This post has been viewed $hitCount times";
     103            }
     104        }
     105
     106
     107        //MEMBER VIEWS
     108        IF ($this->options['memberCount']==1) {
     109
     110            $user_ID = get_current_user_id();
     111
     112            if ($user_ID > 0) {
     113
     114                //record users who have seen this page
     115                $memberList = get_post_meta($post_id, 'bbp_svc_membercounts', true);
     116
     117                $memberArray = explode('.',$memberList);
     118                $memberCount = sizeof($memberArray) -1;
     119                if(array_search($user_ID,$memberArray)===FALSE)
     120                {
     121                    $memberList = $memberList . $user_ID  . '.';
     122                    update_post_meta($post_id, 'bbp_svc_membercounts', $memberList) ;
     123                    $memberCount ++;
     124                }
     125                If($memberCount==1) {
     126                    $text .= ". The post has been read by " . $memberCount . " member.";
     127                }else{
     128                    $text .= ". The post has been read by " . $memberCount . " members.";
     129                }
     130            }
     131        }
     132
     133        //echo"$text";
     134
     135        //SHOW THE COUNTS
     136
     137        //inject our view count into the topic, let's replace the full stop at the end of the details.
     138        //Better way to do this?
     139        if (!empty($text)){
     140            $content = str_replace(".",$text,$content);
     141        }
     142
     143        //echo "<p>P:$post_id C:$count";
     144        return $content ;
    27145
    28146    }
    29147
    30148
    31     function show_views_forum_page() {
     149    public static function activate() {
    32150
    33       //FORUM PAGE
    34       //ie the forum pages lists all the posts in the forum, let's add a view count
     151        //same as upgrade for now...
     152        $this->upgrade();
    35153
    36       $post_id = get_the_ID();
    37       $count   = get_post_meta( $post_id, 'bbp_svc_viewcounts', true );
     154    }
    38155
    39       if (!empty($count)){
    40               echo '<br><span class="bbp-topic-started-by">Views: '.$count.'</span>';
    41                   }
    42       return;
    43      }
     156    function upgrade(){
    44157
    45      function show_views_topic_page( $content, $reply_id ) {
     158        $options  = get_option("svc-options");
    46159
     160        //IF NO VERSION IS FOUND THEN THIS IS A NEW INSTALL
     161        if (empty($options['version'])){
    47162
    48          //First let's update the view count
    49          $post_id = get_the_ID();
     163            $options['version'] = SVC_VERSION;
     164            $options['hitCount'] = '1';
     165            $options['memberCount'] ='';
    50166
    51          //get previous count and add one!
    52          $count = get_post_meta( $post_id, 'bbp_svc_viewcounts', true );
    53          $count = $count + 1 ;
     167            update_option('SVC-options',$options);
     168        }
    54169
    55          //save the new count
    56          update_post_meta($post_id, 'bbp_svc_viewcounts', $count) ;
     170        //ELSE
     171        //add new upgrades here
     172        if (SVC_VERSION == 0.3){
     173            //upgrade to next version
    57174
    58 
    59          //inject our view count into the topic, let's replace the full stop at the end of the details.
    60          //Better way to do this?
    61          if ($count==1){
    62                   $content = str_replace(".",". This post has been viewed 1 time",$content);
    63          }
    64          else if (!empty($count)){
    65                   $content = str_replace(".",". This post has been viewed $count times",$content);
    66          }
    67 
    68          //echo "<p>P:$post_id C:$count";
    69 
    70          return $content;
    71      }
    72 
     175        }
     176    }
    73177}
    74178
  • bbpress-simple-view-counts/trunk/readme.txt

    r764794 r1332098  
    44Tags: bbpress, views, reads, forum
    55Requires at least: 3.0.1
    6 Tested up to: 3.6
    7 Stable tag: 0.1
     6Tested up to: 4.4.1
     7Stable tag: 0.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1616once on the forum listing page and once at the top of each post.
    1717
    18 The count is currently incremented on each refresh of the page.  Counts are recorded using the WordPress post meta API.
     18You can select whether you want a simple hit count, or a unique count of member views.  Or both. 
     19 * The hit count literally counts every time the page is displayed.
     20 * The member count increases each time a member views the page for the first time.  It is a unique count.  This tracks users using their
     21 WordPress user ID so if you mess with the user table the tracking will likely break.
    1922
    2023Note that this is a new plugin and has not been tested on high volume sites so please perform your own tests
    2124before implementing.  Feedback on performance greatly appreciated.  Please post in the plugin's WP forum area or
    22 mention @jezza101 in a tweet.
     25mention @jezza101 in a tweet.  This plugin is maintained, I use it on my own forums,  but unless I have a request or it
     26breaks I will not be adding new features as it works how I want it to work.
    2327
    2428== Installation ==
    2529Upload and install the plugin as normal. 
    2630
    27 There are no configuration options at present.
     31The options link is under the main Forum menu.  Use this to select if you want to track hit count, member count or both.
    2832
    2933== Frequently Asked Questions ==
     
    3438might be.
    3539
     40Tracking member views is likely to be more resource intensive, but only if you have a lot of members.
     41
    3642= The plugin doesn't work quite how I'd like it, can I make a suggestion? =
    37 Sure, get in touch.  This version is really a proof of concept and if it is popular I will expand it.
     43Sure, get in touch.  The plugin works as I want it to so it is unlikely I will change it unless it breaks.  But sure, feel free to make a
     44request, the member tracking was added by request!
    3845
    3946= It's hardcoded in English, can you make it language friendly? =
    40 Yes, this will be added in a future version.  I just need to work out how...
     47Yes, this will be added in a future version.  Please do let me know if this is important to you and I will prioritise this.
     48
     49= Can you add a unique count? =
     50After careful consideration I have decided not to do this.  Tracking unique views is a whole different ball game and is not easy to do.  Even forum software
     51such as vBulletin shows a hit count.  I want this plugin to stay simple.  If you need to track unique views you are better off using Googe Analytics or
     52WordPress Stats.
     53
     54= Does it work with caching plugins? =
     55I have no idea, I don't use any.  But let me know.  I suspect the only way to make this work would be to regnerate the page on each load but that would defeat
     56the point of the page cache.
    4157
    4258
    4359== Changelog ==
    4460
     61= 0.2 =
     62Added member tracking.  The plugin can now give a unique count of member views.
     63
    4564= 0.1 =
    4665* Initial release
Note: See TracChangeset for help on using the changeset viewer.