• Resolved Halil ESEN

    (@halilesen)


    Hello,
    I want to show forum notifications in WordPress. However, it is not possible to do as in phpBB. So I thought of doing it like this: https://i.hizliresim.com/bd3ax3f.jpg

    I will do the same for mobile. I will add it to the dropdown menu icon and the forum link when the menu opens.

    For a private use when I’m in the forum I can display it like this:
    <!-- IF NOTIFICATIONS_COUNT > 0 --><span class="blablax <!-- IF NOTIFICATIONS_COUNT > 9 -->blablaxxl<!-- ENDIF --> <!-- IF NOTIFICATIONS_COUNT > 99 -->blablaxxxl<!-- ENDIF -->">{NOTIFICATIONS_COUNT}</span><!-- ENDIF -->

    In short the following code is required:
    <!-- IF NOTIFICATIONS_COUNT > 0 -->{NOTIFICATIONS_COUNT}<!-- ENDIF -->

    So how can I show these notification numbers in WordPress?

    Thanks.

Viewing 15 replies - 1 through 15 (of 57 total)
  • Plugin Author axew3

    (@axewww)

    So,
    the main query on verify_credentials on class.wp.w3all-phpbb.php that retrieve user’s data based on session, present at the last line this:
    GROUP BY ". $w3all_config["table_prefix"] ."sessions_keys.user_id");
    that limit the retrieved number of notifications to only one, even if there are more for the user, so that we can already know that there is almost one notification for the user, just declaring as global the var $w3all_phpbb_usession
    everywhere in WP:

    
    global $w3all_phpbb_usession;
    print_r(unserialize($w3all_phpbb_usession->notification_data));

    but in this case it will return only array data of one record, and serialized as it is stored into the phpBB db table.

    If we change the last query line into this:
    ); removing GROUP BY ". $w3all_config["table_prefix"] ."sessions_keys.user_id
    the resulting array will contain many rows, containing lot of data that is not handy to be managed. Iterations on the retrieved big array would be necessary.
    It is not a good and efficient way, the query is not fast.

    While it can be an efficient way, to add an option (so a single fast and separate query) that will retrieve notifications data for the logged in users.

    It will be done like this into next 2.6.6, so that you can start to add as now explained, and after update the plugin, expecting all will work fine (i will not add the option code that will be added into plugin code, that is not important for the result now for you).

    OPEN wp_w3all.php and search for this line (+- on top):
    $w3all_phpbb_connection = $phpbb_config = $w3all_phpbb_usession = $w3all_wp_email_exist_inphpbb = $w3all_oninsert_wp_user = $w3all_wpusers_delete_ary = $w3all_wpusers_delete_once = $phpbb_online_udata = $w3all_widget_phpbb_onlineStats_exec = ''; // $w3all_oninsert_wp_user used to check if WP is creating an user, switch to 1 before wp_insert_user fire so to avoid user's email check into phpBB

    CHANGE IT BY ADDING the following var:
    $w3all_phpbb_unotifications
    so you’ll have :
    $w3all_phpbb_unotifications = $w3all_phpbb_connection = $phpbb_config = $w3all_phpbb_usession = $w3all_wp_email_exist_inphpbb = $w3all_oninsert_wp_user = $w3all_wpusers_delete_ary = $w3all_wpusers_delete_once = $phpbb_online_udata = $w3all_widget_phpbb_onlineStats_exec = ''; // $w3all_oninsert_wp_user used to check if WP is creating an user, switch to 1 before wp_insert_user fire so to avoid user's email check into phpBB
    SAVE.

    NOW OPEN file class.wp.w3all-phpbb.php
    and add the same var where into verify_credentials function, where global vars are declared:
    global $w3all_phpbb_connection,$phpbb_config,$w3all_phpbb_usession,$w3all_config,$w3all_oninsert_wp_user,$wpdb,$w3cookie_domain,$w3all_anti_brute_force_yn,$w3all_bruteblock_phpbbulist,$w3all_phpbb_lang_switch_yn,$w3all_useragent,$wp_w3all_forum_folder_wp,$w3all_add_into_wp_u_capability;

    so it become:
    global $w3all_phpbb_unotifications, $w3all_phpbb_connection,$phpbb_config,$w3all_phpbb_usession,$w3all_config,$w3all_oninsert_wp_user,$wpdb,$w3cookie_domain,$w3all_anti_brute_force_yn,$w3all_bruteblock_phpbbulist,$w3all_phpbb_lang_switch_yn,$w3all_useragent,$wp_w3all_forum_folder_wp,$w3all_add_into_wp_u_capability;

    then we just have to add a query to retrieve all notifications data, if there are because the main query say, that ALMOST one new notification (but maybe more) exists, so we go to get them like this:

    SEARCH FOR this line (inside verify_credentials):
    if(isset($phpbb_user_session[0])){

    JUST AFTER ADD THE FOLLOWING CODE:

    if(!empty($phpbb_user_session[0]->notification_data)){ // there is almost an unread notification for this user
     		// so we go to retrieve all unread for this user session, populating related global var
     		$w3all_phpbb_unotifications = $w3all_phpbb_connection->get_results("SELECT *
         FROM ". $w3all_config["table_prefix"] ."notifications
         WHERE ". $w3all_config["table_prefix"] ."notifications.user_id =  ".$phpbb_user_session[0]->user_id."
         AND ". $w3all_config["table_prefix"] ."notifications.notification_read = 0");
     	}

    declaring as global the var $w3all_phpbb_unotifications
    everywhere into wordpress plugins or custom code or templates that run after the init hook (+- all while it’s the one where verify_credentials run and take precedence over all), will get data as required:

    global $w3all_phpbb_unotifications;
     	echo'<pre>';
     	print_r($w3all_phpbb_unotifications);
    // display only the notification count:
    echo count($w3all_phpbb_unotifications);
     	exit;

    well there is much to say. The query can retrieve efficiently only data you want (maybe only the count and not all notifications data, like titles and authors etc)
    Look that in this case the resulting array is like this:

    Array
    (
        [0] => stdClass Object
            (
                [notification_id] => 10961
                [notification_type_id] => 1
                [item_id] => 1671
                [item_parent_id] => 2
                [user_id] => 48
                [notification_read] => 0
                [notification_time] => 1648454678
                [notification_data] => a:4:{s:9:"poster_id";s:1:"1";s:11:"topic_title";s:9:"test code";s:13:"post_username";s:7:"guester";s:10:"forum_name";s:15:"phpBB WordPress";}
            )
    
        [1] => stdClass Object
            (
                [notification_id] => 10976
                [notification_type_id] => 1
                [item_id] => 1689
                [item_parent_id] => 2
                [user_id] => 48
                [notification_read] => 0
                [notification_time] => 1663444164
                [notification_data] => a:4:{s:9:"poster_id";i:2554;s:11:"topic_title";s:20:"Post by master sdvvd";s:13:"post_username";s:0:"";s:10:"forum_name";s:15:"phpBB WordPress";}
            )
    
        [2] => stdClass Object
            (
                [notification_id] => 10953
                [notification_type_id] => 10
                [item_id] => 1674
                [item_parent_id] => 3
                [user_id] => 48
                [notification_read] => 0
                [notification_time] => 1648909048
                [notification_data] => a:4:{s:9:"poster_id";i:1;s:11:"topic_title";s:5:"dfsdf";s:13:"post_username";s:7:"dssdsdf";s:10:"forum_name";s:31:"Incoming/Outgoing and Free Time";}
            )

    so that
    notification_data require to be unserialized
    and note that notification_type_id
    refer to the type of notification, which corresponding values in phpBB are stored into the table:
    phpbb_notification_types

    so that for notification.type.pm that refer to a PM notification into this table for example, correspond to notification_type_id -> 11.

    It is easy to add more complex code and get results you want everywhere.
    Cheers!

    [EDITED CODE: NOTE that a VAR NAME ON CODES SNIPPETS CHANGED]
    take code from the post, not from notification email arrived to you, so you’ll not have to update nothing on next 2.6.6 (vars names will be the same))

    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    Thread Starter Halil ESEN

    (@halilesen)

    The following code shows the number of notifications when i make the edits you said in files a and b:

    <?php global $w3all_phpbb_unotifications; echo count($w3all_phpbb_unotifications); ?>

    The first version of this code, that is, the part that shows all notification codes, is beyond me when I don’t have the code to set it. That’s enough for now. ??

    Thank you so much.

    But I still have some problems. First how do I add this code to the primary menu? How do I assign a css class to it code?

    Thread Starter Halil ESEN

    (@halilesen)

    <?php global $w3all_phpbb_unotifications;
     	echo'<span class="bildirimvarbaksana"> ';
     	echo count($w3all_phpbb_unotifications);
     	echo' </span>';?>

    Okey?

    Plugin Author axew3

    (@axewww)

    activate:

    ACTIVATE related option into plugin admin preferences where:
    Activate notify Read/Unread Private Messages into Admin Tool Bar

    then into wp_w3all.php file, change the function
    function wp_w3all_toolbar_new_phpbbpm

    function wp_w3all_toolbar_new_phpbbpm( $wp_admin_bar ) {
        global $w3all_phpbb_wptoolbar_pm_yn,$w3all_url_to_cms,$w3all_phpbb_usession;
    
      if ( !empty($w3all_phpbb_usession) && $w3all_phpbb_wptoolbar_pm_yn == 1 ) {
            if($w3all_phpbb_usession->user_unread_privmsg > 0){
            $hrefmode = $w3all_url_to_cms.'/ucp.php?i=pm&folder=inbox';
            $args_meta = array( 'class' => 'w3all_phpbb_pmn' );
            $args = array(
                    'id'    => 'w3all_phpbb_pm',
                    'title' => __( 'You have ', 'wp-w3all-phpbb-integration' ) . $w3all_phpbb_usession->user_unread_privmsg .' '. __( 'unread forum PM', 'wp-w3all-phpbb-integration' ),
                    'href'  => $hrefmode,
                    'meta' => $args_meta );
    
           $wp_admin_bar->add_node( $args );
           unset($phpbb_user_session);
         }
      } else { return false; }
    }


    into this (for example), note changes:

    function wp_w3all_toolbar_new_phpbbpm( $wp_admin_bar ) {
        global $w3all_phpbb_unotifications,$w3all_phpbb_wptoolbar_pm_yn,$w3all_url_to_cms,$w3all_phpbb_usession;
    
      if ( !empty($w3all_phpbb_unotifications) && $w3all_phpbb_wptoolbar_pm_yn == 1 ) {
            if(!empty($w3all_phpbb_unotifications)){
            $hrefmode = $w3all_url_to_cms.'/ucp.php?i=pm&folder=inbox';
            $args_meta = array( 'class' => 'w3all_phpbb_pmn' );
            $nnumber = count($w3all_phpbb_unotifications);
            $args = array(
                    'id'    => 'w3all_phpbb_pm',
                    'title' => __( 'You have ', 'wp-w3all-phpbb-integration' ) . $nnumber .' '. __( 'unread forum notifications', 'wp-w3all-phpbb-integration' ),
                    'href'  => $hrefmode,
                    'meta' => $args_meta );
    
           $wp_admin_bar->add_node( $args );
         }
      } else { return false; }
    }

    Assuming that for main menu you mean this.
    The admin bar menu.
    The code will be arranged to be smarter, and the name of the function will change into function wp_w3all_toolbar_new_phpbb_notifications
    and will display in several ways, all important notifications (listed by title and type maybe). I will aim to make it simpler and fast to be used into any contest.

    NOTE that unset($phpbb_user_session); last line on function has been removed.
    It can be safe to unset there, but maybe it could be called after into some other part of the code flow, so better to remove the unset(). It will be changed into something else, on next 2.6.6.

    the function wp_w3all_toolbar_new_phpbbpm( $wp_admin_bar ) {
    is hooked using:
    add_action( 'admin_bar_menu', 'wp_w3all_toolbar_new_phpbbpm', 999 );
    two times to fire inside the code that run only in wp admin and also into front end.
    The call to the hook about this will be so moved into the part of the code that run both in admin and frontend, so once and not two times.

    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    Plugin Author axew3

    (@axewww)

    PSSS i have forget to say:
    ACTIVATE related option into plugin admin preferences where:
    Activate notify Read/Unread Private Messages into Admin Tool Bar

    Thread Starter Halil ESEN

    (@halilesen)

    No, I didn’t mean WP Toolbar. I meant the site menu that the theme uses. Since these jobs are all assigned with php, I just asked how to add it next to the “Forums” item/link. (in the picture in the first post)

    Plugin Author axew3

    (@axewww)

    Well yes, add your code just into a function or php code of the theme in any way it work, assigning values as required, you have all!
    Or yes, also directly on template files!
    Declare as global and use it!

    Thread Starter Halil ESEN

    (@halilesen)

    Thank you so much. I got what I wanted. Let’s talk in the previous topic ??

    Plugin Author axew3

    (@axewww)

    could be simple as:

    
    global $w3all_phpbb_unotifications;
    if(empty($w3all_phpbb_unotifications)){
     $nnum = 0; } else { $nnum = count($w3all_phpbb_unotifications); }

    or:

    
    global $w3all_phpbb_unotifications;
    $nnum = empty($w3all_phpbb_unotifications) ? 0 : count($w3all_phpbb_unotifications);

    that’s one line

    [CODE EDITED]

    is never a good idea to write code directly on posts replies!

    • This reply was modified 2 years, 2 months ago by axew3.
    Thread Starter Halil ESEN

    (@halilesen)

    Since php cannot be added to the menus in the WordPress panel, I thought it would be better to add a custom menu with JS.

    add_filter('wp_nav_menu_items','add_custom_in_menu', 10, 2);
    
    function add_custom_in_menu( $items, $args ) 
    {
        if( $args->theme_location == 'primary' ) // only for primary menu
        {
            $items_array = array();
            while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) )
            {
                $items_array[] = substr($items, 0, $item_pos);
                $items = substr($items, $item_pos);
            }
            $items_array[] = $items;
            array_splice($items_array, 2, 0, '<li id="menu-item-9" class="menu-item menu-item-type-custom menu-item-object-custom"><a href="https://obelde.com/forum/"><i class="fas fa-comment"></i> Forumlar</a></li>');
    
            $items = implode('', $items_array);
        }
        return $items;
    }

    However, this time, I could not add php code to the menu created in the array_splice section. How can I add php code there (before )? How should I convert?

    Plugin Author axew3

    (@axewww)

    Look that the suggested query into verify_credentials is wrong, it do not return an array but an object. May it is the problem, so change the added query:

    if(!empty($phpbb_user_session[0]->notification_data)){ // there is almost an unread notification for this user
     		// so we go to retrieve all unreads for this user session
     		$w3all_phpbb_unotifications = $w3all_phpbb_connection->get_results("SELECT *
         FROM ". $w3all_config["table_prefix"] ."notifications
         WHERE ". $w3all_config["table_prefix"] ."notifications.user_id =  ".$phpbb_user_session[0]->user_id."
         AND ". $w3all_config["table_prefix"] ."notifications.notification_read = 0");
     	}

    CHANGE INTO

    if(!empty($phpbb_user_session[0]->notification_data)){ // there is almost an unread notification for this user
     		// so we go to retrieve all unreads for this user session
     		$w3all_phpbb_unotifications = $w3all_phpbb_connection->get_results("SELECT *
         FROM ". $w3all_config["table_prefix"] ."notifications
         WHERE ". $w3all_config["table_prefix"] ."notifications.user_id =  ".$phpbb_user_session[0]->user_id."
         AND ". $w3all_config["table_prefix"] ."notifications.notification_read = 0",ARRAY_A);
     	}

    note ,ARRAY_A added on the end, that now will return an array and not an object.
    Even count() was wrongly applied, and supposed to fail in the other way, it will return error because it require to pass to it a countable array, and not an object!

    • This reply was modified 2 years, 2 months ago by axew3.
    Thread Starter Halil ESEN

    (@halilesen)

    excuse me. I didn’t understand much from these codes. I made the first changes you mentioned in files wp_w3all.php and class.wp.w3all-phpbb.php. Then I generated the following code:

    <?php global $w3all_phpbb_unotifications; echo'<span class="bildirimvarbaksana">'; echo count($w3all_phpbb_unotifications); echo'</span>'; ?>

    It works as intended. What I’m trying to do now is add it next to a menu item. Demo: Home | Forums (1) | …

    Since php code is not added to the menu item in the WordPress panel, I tried to add the menu item specifically with JS. It worked. However, my php code is not not work to the place I mentioned in my previous post.

    What do you recommend? I didn’t quite understand the purpose of the codes you wrote next.

    Thread Starter Halil ESEN

    (@halilesen)

    there is a problem. It always shows the number 1. But there are no notifications. What’s more, it shows for guests as well.

    Plugin Author axew3

    (@axewww)

    1) yes, count() into the query result, works both as ARRAY_A or not, because the returning value is by the way an array, containing objects or arrays. So if only you use count() to know the number of notifications it will be ok also the first query you added, without ARRAY_A, and you have to change nothing.

    2) it show also for not registered because you have to wrap the code, to fire only for registered users, so in WP it is like this:

    // wordpress
    if ( is_user_logged_in() ) {
     // code for logged in user here
    }

    3) to have a javascript var available all over and in the global scope, you have to declare the var outside any other function/code, may into the header template file (so to output it in the <head>....</head>, like this:

    
    global $w3all_phpbb_unotifications;
    echo'<script>var notificationsCount = '.count($w3all_phpbb_unotifications).'</script>';

    so into any other point and template file, you’ll have the javascript var notificationsCount all over.

    so the complete snippet, could be something like this, but it depend by your code, that i do not know:

    if ( is_user_logged_in() ) {
     if(!empty($w3all_phpbb_unotifications)) {
      echo'<script>var notificationsCount = '.count($w3all_phpbb_unotifications).';</script>';
     } else { echo'<script>var notificationsCount = 0;</script>'; }
    }

    it is really basic, may instead to assign to 0 the var, you could check instead after, if the var notificationsCount is defined or not (based on the fact that the code may will run only if the user is logged in)

    if ( is_user_logged_in() ) {
     if(!empty($w3all_phpbb_unotifications)) {
      echo'<script>var notificationsCount = '.count($w3all_phpbb_unotifications).';</script>';
     }
    }

    anyway, as you can imagine, we are far away from the scope of the plugin support here, so i hope that you’ll find the way to resolve following hints above

    PS NOTE
    EDITED CODE that was missing ; on last line into a snippet
    } else { echo'<script>var notificationsCount = 0;</script>'; }
    the ; was missing

    • This reply was modified 2 years, 2 months ago by axew3.
    • This reply was modified 2 years, 2 months ago by axew3.
    Thread Starter Halil ESEN

    (@halilesen)

    I made the here. But it always shows a number. Both for guests even if there are no notifications, and when there are too many notifications.

Viewing 15 replies - 1 through 15 (of 57 total)
  • The topic ‘Show Number of Notifications’ is closed to new replies.