w3all Public functions - groups roles switches

brianp6
User w
User w
Posts: 6
Joined: Thu Aug 15, 2024 8:10 pm

w3all Public functions - groups roles switches

Post by brianp6 »

Hi,

Now that I understand how w3all works now that you helped me (and fixed a bug) with the WooCommerce Memberships question, I have another one.

I develop and manage a club membership & management system for (primarily) classic car and bike clubs. My system is called Crossmember (https://crossmember.co.uk).

One of my clubs uses WordPress and w3all to help synchronise WP users with phpBB. I will be bringing this club off WooCommerce memberships and onto Crossmember in the very near future (about 4 weeks time).

Rather than go through the “verify credentials” each time, what I would really like to do is call an API / Public Function in w3all supplying as parameters the WP user ID and the phpBB group ID therefore directly integrating with w3all to update the phpBB group that the user is in.
I am sure this would be quite straight forward as all the code is already there in your plugin, and would remove the need for the custom files in such direct integrations.

What do you think?

[TITLE EDITED]
User avatar
axew3
w3all User
w3all User
Posts: 2862
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: W3all Public functions

Post by axew3 »

If i have understand what you mean (sorry). I will maybe completely understand the best (to me) how to going on here.

Code: Select all

private static function verify_phpbb_credentials(){
himself, do not waste too much and essentially works with a single query to phpBB practically 98% of the times.
As basic, and without activating (and even if) pm options etc, it is very fast. Surely it could be needed into a site with a really but really high traffic, but sure it is required in your case?

If yes, and if anyway you want.
First thoughts:
Ok, it can be assuming so, that in phpBB, users cannot update their data about email and password?
Because there will not be updated data about the user, if user's data changed into phpBB in the while, if not using the phpBB extension.
So you'll maybe use it?
Having a remember me cookie always set, maybe yes, it is ok, or the user navigating from phpBB to wp and viceversa could result to be logged in into one and not into another, because in the while that the user navigate the WP side, the phpBB session is not updated.
Or the query could be reduced to get the phpBB userID, the user Group and then update the session, that's what it do normally, adding some data more

Code: Select all

// remember me auto login
   $phpbb_user_session = $w3all_phpbb_connection->get_results("SELECT *
    FROM ". $w3all_config["table_prefix"] ."sessions_keys
    JOIN ". $w3all_config["table_prefix"] ."users ON ". $w3all_config["table_prefix"] ."users.user_id = ".$w3all_config["table_prefix"] ."sessions_keys.user_id
    AND ". $w3all_config["table_prefix"] ."sessions_keys.key_id = '".md5($phpbb_k)."'
    AND ". $w3all_config["table_prefix"] ."users.user_id = ". $phpbb_u ."
    JOIN ". $w3all_config["table_prefix"] ."groups ON ". $w3all_config["table_prefix"] ."groups.group_id = ". $w3all_config["table_prefix"] ."users.group_id
      LEFT JOIN ". $w3all_config["table_prefix"] ."profile_fields_data ON ". $w3all_config["table_prefix"] ."profile_fields_data.user_id = ". $phpbb_u ."
      LEFT JOIN ". $w3all_config["table_prefix"] ."banlist ON ". $w3all_config["table_prefix"] ."banlist.ban_userid = ". $phpbb_u ." AND ban_exclude = 0
     GROUP BY ". $w3all_config["table_prefix"] ."sessions_keys.user_id");
About a function that should automatically switch the WP user to a specific WP role, based on the passed phpBB group.
So the function could provide also another param, the WP role where the user will be switched to, if we already know it at the time we go to call the function, that would be easier instead to have inside the function, an array of switches.
Maybe the third param could be both an array, so used for the switches, or a specific role, if we already know.

It depend where the code will be used.
So, which code will call this function?
Where you want to hook it? (can you please better explain this?)
Because if the switch need to happen due to a phpBB group change into phpBB, then you need to know the phpBB user's group, and the query of the verify_phpbb_credentials() can be yes reduced, but how it will gain in milliseconds? Anyway yes, it can be also faster removing data to be retrieved and not executing all the verify_phpbb_credentials().

Or, the function can be called from phpBB, so to be synchronous, calling it when an user change the group into phpBB?
The code could be easily added into the phpBB WordPress extension.

These are first thoughts at moment

ps i created lately a script for a site where something like this happen but with:
phpBB groups changes:
user's subscribed to specifics newsletters into Sendy service, and addition into specifics memberpress memberships, when via admin they go to move an user from a group to another.
An headache that finally worked very well.
So to say, that it can be maybe a function that can be called from phpBB and be synchronous.

Let figure out how all could work in code, a function that can be called to accomplish with these tasks, passing to it the mentioned params.
Do you mean this? A function that can be called from phpBB?

If the call is internal, fired by some WP event, the code little change: it will provide a working function that will maybe accomplish with any scenario

Code: Select all

function w3all_wp_phpbb_user_groupSwitch_phpbb_wp( $u_name_email_id = '', $phpGroupId = '', $switchGroupsVals = '' ) {

[EDITED]
User avatar
axew3
w3all User
w3all User
Posts: 2862
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: W3all Public functions

Post by axew3 »

So (please read all the above, even if i now my Eng is a nightmare) to not execute the verify_phpbb_credentials each time, it seem to me that would be just required to change the function

Code: Select all

public static function wp_w3all_phpbb_init() {
into the file class.wp.w3all-phpbb.php
that look like this:

Code: Select all

public static function wp_w3all_phpbb_init() {

  global $phpbb_config,$w3all_get_phpbb_avatar_yn;

  if(empty($phpbb_config)){
    self::wp_w3all_phpbb_conn_init();
  }

  if(empty($phpbb_config)){
    return;
  }

  if ( ! defined("WPW3ALL_NOT_ULINKED") ):
    self::verify_phpbb_credentials();
  endif;

  if ( $w3all_get_phpbb_avatar_yn > 0 ):
    self::init_w3all_avatars();
  endif;

}
should be changed into:

Code: Select all

public static function wp_w3all_phpbb_init() {

  global $phpbb_config,$w3all_get_phpbb_avatar_yn;

  if(empty($phpbb_config)){
    self::wp_w3all_phpbb_conn_init();
  }

  if(empty($phpbb_config)){
    return;
  }

  if ( ! defined("WPW3ALL_NOT_ULINKED") ):
  
  if ( !is_user_logged_in() ) {
    self::verify_phpbb_credentials();
  }
  
  endif;

  if ( $w3all_get_phpbb_avatar_yn > 0 ):
    self::init_w3all_avatars();
  endif;

}
this changed part

Code: Select all

  if ( !is_user_logged_in() ) {
    self::verify_phpbb_credentials();
  }
let execute verify_phpbb_credentials() only when the WP user result to be not logged, so that if a valid phpBB cookie is found, because the user logged in into phpBB, the user will be logged in also in WP. In this case, while the user is navigating WP, the phpBB session will not be updated, nor any user's data when the user come into WP as logged, but only when will do explicit actions, like update the WP profile. IT IS SO MANDATORY to not allow the email or password change into phpBB in this case, or it is REQUIRED to use the phpBB extension in conjunction.
Should also maybe be released always a remember me cookie for phpBB, even if the remember me option is not set, logging in into WP or phpBB (or maybe allowing to do so (login) only in WP or phpBB?)

It will be maybe provided an option on 2.9.0, to be added into the wp_config.php file, or better into the plugin admin, like:
WPW3ALL_SOFT_PHPBB_VERIFY_CREDENTIALS
that when active, will switch the code flow to be like above mentioned.


[EDITED]
User avatar
axew3
w3all User
w3all User
Posts: 2862
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: W3all Public functions

Post by axew3 »

Since it is long time i was thinking to the same, and the idea of the endpoint/function that can be used by internals WP plugins code or from an external script with a cURL call seem to me a very good feature also for this aspect (while for the user addition it already exist) i am coding a simple but complete function that will accomplish easier with all tasks.

2.9.0 will provide SOFT verify_credentials option and
and a new endpopint that will work for both external cURL calls, or internal WP hooks.

PS the soft verify_phpbb_credentials() option will provide the fact, that it can be activated with an option that will update the user session in phpBB if active, that seem to me useful.
User avatar
axew3
w3all User
w3all User
Posts: 2862
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: W3all Public functions

Post by axew3 »

the function, that's also an API (so it can be called internally from WP or externally via cURL) will accept 2 params, and mainly will works with emails and not IDS, even it will accept also IDs. The reason is that the integration works by email and there is no correspondence between an userID in phpBB an the same (linked by email) userdID into WP. So the user email is the faster way to work with these kind of things.

the function declaration look like this:

Code: Select all

w3all_wp_phpbb_user_groupSwitch_phpbb_wp( $u_list_email_ids = '', $phpbbGroups_wpRoles_schema = '' ){
the $u_list_email_ids first param can be filled with how many users wanted to be processed, passing them as pairs like this:

Code: Select all

userEmail:phpbbGroup
(the phpBB group can be by name or ID) separated by comma if multiple users, like so:

Code: Select all

$u_list_email_ids ='jdddddddd@gmail.com:REGISTERED,pacos@peco.com:4,yhhhhhyhyhy7@gmail.com:2';
where so it mean:
jdddddddd@gmail.com -> REGISTERED (phpBB group id 2)
pacos@peco.com -> 4 (phpBB group GLOBAL_MODERATORS id 4)
yhhhhhyhyhy7@gmail.com -> 2 ( REGISTERED phpBB group id 2)

the second param, require to be filled and set as more we like,
using pairs:
user_GroupID_or_GroupNAME:WP_role_to_switch_to
so an example mixing phpBB groups names or ID would look like this:

Code: Select all

$switchUsersGroupsValsSchema = '2:subscriber,4:editor,REGISTERED:subscriber,5:administrator';
where so it mean:
group ID 2 in phpBB -> wp subscriber
group ID 4 in phpBB -> wp editor
group NAME REGISTERED in phpBB -> wp subscriber
group ID 5 in phpBB -> wp administrator

passing these values to the function

Code: Select all

jdddddddd@gmail.com:REGISTERED,pacos@peco.com:4,yhhhhhyhyhy7@gmail.com:2
2:subscriber,4:editor,REGISTERED:subscriber,5:administrator
the result will be two arrays that will look like this:

Code: Select all

Array
(
    [jdddddddd@gmail.com] => REGISTERED
    [pacos@peco.com] => 4
    [yhhhhhyhyhy7@gmail.com] => 2
)
Array
(
    [2] => subscriber
    [4] => editor
    [REGISTERED] => subscriber
    [5] => administrator
)
that can be used to execute the subsequent code into the function and switch single or multiple users into related and proper wp assigned role, based on the phpBB group. Note that the schema can be in this way changed as more like, so calling the function it will automatically fit any possible scenario.
This will be an add-on of the plugin, also added to be compatible with any http request and will be provided using cURL also into the phpBB WP extension.
So that groups/roles changes between phpBB/wp twill be synchronous.
It can be synchronous also using it as function into the WP side, hooking it into proper WP filter or action, adding needed values.

Maybe you intended the contrary, so to switch WP users to certain phpBB groups? Which will be also added, after this completed.
brianp6
User w
User w
Posts: 6
Joined: Thu Aug 15, 2024 8:10 pm

Re: W3all Public functions

Post by brianp6 »

Hi,

Yes, it was my intention to switch WP users into the correct phpbb group.
In my membership system I have perhaps 3 new WP user roles that I define:
club_member
club_overdue
club_lapsed

I do not use the standard WP roles.

So, when a new member joins the club, the user is created in the website programatically using the WP function wp_create_user(). I believe that your plugin will pick this up from a WP action hook?? I am assuming that you will see this and create the user in the phpbb side with the default configured phpbb group in the w3all settings.

If the user goes overdue, or lapses their membership, I want to call a function that will give you their email address and the number of the phpbb group to move the user into.

This is because overdue or lapsed members will have a reduced visibility of the forum compared to full members.

Of course if they re-subscribe to the club, then I would also call your new function giving their email address and the number of the phpbb group to give them back full visibilty.

I hope that makes sense?

Many thanks.
Post Reply