Number of users currently viewing forum

123 views Asked by At

I am trying to input a feature that vBulletin has on my forum which is using BBpress. It's to display how many users are currently viewing a particular forum or thread. I am trying to edit the CBX Users Online plugin because it has a function to display how many users are currently viewing a current page so I am trying to figure out how to edit it for each individual forum instead of the current page.

This is the function that logs the user's visit on the current page:

public function log_visit($page_url = '', $page_title = '') {
    global $wpdb;

    if (empty($page_url))
        $page_url = sanitize_text_field($_SERVER['REQUEST_URI']);
        //$page_url = bbp_forum_permalink($forum_id);
    if (empty($page_title))
        $page_title = self::get_title();



    $referral = CBXUseronlineHelper::get_referral();

    $user_ip = CBXUseronlineHelper::get_ipaddress();


    $user_agent   = CBXUseronlineHelper::get_useragent();
    $current_user = wp_get_current_user();
    $bots         = CBXUseronlineHelper::get_bots();


    $bot_found = false;
    $user_id = '';

    foreach ($bots as $name => $lookfor)
    {
        if (stristr($user_agent, $lookfor) !== false)
        {

            $user_id   = $_COOKIE[CBX_USERONLINE_COOKIE_NAME];
            $user_name = $name;
            $username  = $lookfor;
            $user_type = 'bot';
            $bot_found = true;

            break;
        }
    }



    // If No Bot Is Found, Then We Check Members And Guests
    if (!$bot_found)
    {
        if ($current_user->ID)
        {
            // Check For Member
            $user_id   = $current_user->ID;
            $user_name = $current_user->display_name;
            $user_type = 'user';
            $where     = $wpdb->prepare("WHERE user_id = %d", $user_id);
        }
        elseif (isset($_COOKIE[CBX_USERONLINE_COOKIE_NAME])){
            $user_id   = $_COOKIE[CBX_USERONLINE_COOKIE_NAME];

            $user_name = (!empty($_COOKIE['comment_author_' . COOKIEHASH])) ? trim(strip_tags($_COOKIE['comment_author_' . COOKIEHASH])): __('Guest', 'cbxuseronline');
            $user_type = 'guest';
        }
    }
    else{
        return;
    }

    $mobile = (CBXUseronlineHelper::is_mobile())? 1: 0;


    // Current GMT Timestamp
    $timestamp = current_time('mysql');

    $cbxuseronline_tablename = CBXUseronlineHelper::get_tablename();

    $userid  = $user_id;

    $cbxuseronline_basics = get_option('cbxuseronline_basics');
    $refresh_time =  isset($cbxuseronline_basics['refreshtime'])? intval($cbxuseronline_basics['refreshtime']): 3600;


    // Purge table
    $real_purge = $wpdb->query( $wpdb->prepare( "DELETE FROM $cbxuseronline_tablename WHERE userid = %s OR timestamp < DATE_SUB(%s, INTERVAL %d SECOND)", $userid, $timestamp, $refresh_time ) );
    if($real_purge !== false){
        do_action('cbxuseronline_record');
    }



    // Insert Users
    $data = compact( 'timestamp', 'user_type', 'userid', 'user_name', 'user_ip', 'user_agent', 'page_title', 'page_url', 'referral', 'mobile' );
    $data = stripslashes_deep( $data );



    $wpdb->replace( $cbxuseronline_tablename, $data );

    // Count Users Online
    $cbxuseronline_mostonline_now = intval( $wpdb->get_var( "SELECT COUNT( * ) FROM $cbxuseronline_tablename" ) );

    $cbxuseronline_mostonline_old = get_option('cbxuseronline_mostonline');
    if($cbxuseronline_mostonline_old ===  FALSE || ($cbxuseronline_mostonline_now > intval($cbxuseronline_mostonline_old['count'])) ){

        update_option('cbxuseronline_mostonline', array(
            'count' => $cbxuseronline_mostonline_now,
            'date' => current_time( 'timestamp' )
        ));
    }
}

I'm pretty sure that this is the piece of code responsible for logging the user's visit on the current page:

$page_url = sanitize_text_field($_SERVER['REQUEST_URI']);

But I have tried to edit it to something like this:

$page_url = bbp_forum_permalink($forum_id);

but unfortunately that doesn't work.

Does anyone know what I'm doing wrong please?

Thanks in advance for any info / advice given.

enter image description here

0

There are 0 answers