Show Number of Notifications | WordPress.org

[ad_1]

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 site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer