w3all Public functions - groups roles switches

Post a reply


This question is a means of preventing automated form submissions by spambots.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

If you wish to attach one or more files enter the details below.

Maximum filesize per attachment: 1 MiB.

Expand view Topic review: w3all Public functions - groups roles switches

Re: w3all Public functions - groups roles switches

by axew3 » Wed Sep 04, 2024 6:21 am

Functions

w3all_wp_USERS_roles_switch
this function works also like a little API: it can be fired using cURL with specific parameters, or can be used as internal function in WP, called into any action or filter

Code: Select all

w3all_wp_USERS_roles_switch( $phpbb_u_emails__wp_roles );
I realized after, that WordPress by default, remove the previous role and set the new. It do not allow by default to set multiple users Roles for an user.

It will be very easy to edit the function code so to fit any requirement where there are plugins that sets for the user multiple Roles, anyway by default the function that switch WP users Roles will follow WP default rules, and take only one param

Code: Select all

function w3all_wp_USERS_roles_switch( $phpbb_u_emails__wp_roles )
the

Code: Select all

$phpbb_u_emails__wp_roles
explained with example, need to contain the following values, supposing it is called to change the Role for the user Thomas (thomas@thom.com) to subscriber and Marco (marco@mar.com) to editor:

Code: Select all

$phpbb_u_emails__wp_roles = 'marco@mar.com[editor],thomas@thom.com[subscriber]';
if multiple users, separated by comma, the param require:
the user email followed by square brackets which must contain the name of the role to which the user must be assigned.

As said, the function code is ready to set for the user/s multiple roles and could be easily done passing the param like this, supposing that instead, the function is called to add marco to the editors and administrators roles:

Code: Select all

$phpbb_u_emails__wp_roles = 'marco@mar.com[editor|administrator],thomas@thom.com[subscriber]';
But actually, since the code will go to process before the role editor and then administrator, the result will be that the user will result set as Administrator because it is the second/last role processed (the previous one will be replaced by the next one).
Passing something like this so:

Code: Select all

$phpbb_u_emails__wp_roles = 'thomas@thom.com[subscriber|administrator|editor]';
the WP user with email thomas@thom.com, will be set as editor, the last passed param, and not to all the 3 Roles.
It is ready to be adapted and work with any other external plugin, we'll see after how to add the one and the other, multiple roles or not (or also something else) for users when it is required.



w3all_phpbb_USER_groupSwitch

Code: Select all

function w3all_phpbb_USER_groupSwitch( $wpu_email_id = '', $phpbbGroupsAdd = '', $phpbbGroupDefault = '', $phpbbGroupsRemove = '' ){

     # required - $wpu_email_id - the WP user email or user id
     # required - $phpbbGroupDefault - The default phpBB user group ID which the user will be set to
     # required - $phpbbGroupAdd - phpBB Groups IDs where the user will be added (single or separated by comma phpBB Groups IDs)

     # $phpbbGroupRemove NOT USED, see below
     # $phpbbGroupRemove - phpBB Groups ids where the user will be removed from (single or separated by comma)
i tested that NOT removing all groups which the user belong to into the table user_group (that's with no index) before to insert a new group, a duplicate record insertion occur.
It is really obscure at moment why (i noted the same into another contest, when wp_insert_user is used on 'similar loop scenario', but i do not have still reversed the thing: a specific bug into the custom WP ezSQL lib?) this happen, but anyway and by the way so:

the param $phpbbGroupsRemove = '' will be ignored,
and the user, before to be added to the indicated/passed groups IDs
WILL BE REMOVED from any of the other existent old groups which the user belong to in phpBB.

Well, this could be also easily done in different way, collecting all Groups which the user belong to, before to delete, then re-adding if the case (or removing some).
Let see this aspect before to release 2.9.0.
How would be better?
Collect then re-add old and news, if the case
OR just remove all olds Groups which the user belong to, and then adding only the news, as it is at this moment?

P.s think that as it is this in this moment maybe is the best. Because it is supposed (?) that is known, into which phpBB group/s we want the user to be added to, based on actual WP roles or something else. Supposing this, the easy function will always work fine as is.

Re: W3all Public functions

by axew3 » Mon Sep 02, 2024 11:51 am

It is easy to say 4 params, when then you figure out that:
if it is not removed any and each userID record from the user_group table, before to insert the new one, or more then one it is the same, a duplicate record insertion happen!
And i have not find out a valid work around for this.
Why it happen is a mystery to me at moment, but i noted the same into some other aspect, when inserting in WP a new user (in this case the duplicate insertion is done into WP).
I suspect then, or there is a bug some way into the library used by wordpress, that is a (custom?) ezSQL libraryor there is something wrong somewhere else or even, i still do not have understand something.

Anyway so, to fix it, the function will work this way with 3 params:

Code: Select all

function w3all_wp_phpbb_USER_groupSwitch_wp_phpbb( $wpu_email_id = '', $phpbbGroupsAdd = '', $phpbbGroupDefault = '' ){
when it is executed, the function REMOVE the phpBB user from any and each group which the user belong to, then will re-add following values of:
$phpbbGroupsAdd groupID or IDs separated by comma which the user will be added to
$phpbbGroupDefault the default group which the user will be set to

P.s
it has been an hard fight to understand why the double insertion happen.
That i have not win at moment.
I will maybe open a topic where someone could apply the code and test what i really mean.
It is very strange behavior that have not a possible reason to be for me.

Re: W3all Public functions

by axew3 » Sun Sep 01, 2024 5:51 pm

4 params:

Code: Select all

function w3all_wp_phpbb_USER_groupSwitch_wp_phpbb( $wpu_email_id = '', $phpbbGroupsAdd = '', $phpbbGroupsRemove = '', $phpbbGroupDefault = '' ){

     # $wpu_email_id - the WP user email or user id
     # $phpbbGroupDefault - The default phpBB user group into which the user will be set
     # $phpbbGroupAdd - phpBB Groups ids where the user will be added (single or separated by comma)
     # $phpbbGroupRemove - phpBB Groups ids where the user will be removed from (single or separated by comma)

Re: W3all Public functions

by axew3 » Sun Sep 01, 2024 7:17 am

I liked to add

Code: Select all

function w3all_wp_phpbb_USER_groupSwitch_wp_phpbb( $wpu_email_id = '', $phpbbGroupsAdd = '', $phpbbGroupsRemove = '' ){
which get 3 params:

wpu_email_id email or id of the WP user
phpbbGroupsAdd list of groups ID/s where the user need to be added to (separated by comma)
phpbbGroupsRemove list of groups ID/s where the user need to be removed from (separated by comma)

but will be also another function named instead

Code: Select all

function w3all_wp_phpbb_USERS_groupSwitch_wp_phpbb( $wp_u_list_ids_emails = '', $wp_u_list_ids_emails_and_roles = '', $wpRoles_phpbbGroups_schema = '' ){
these two params

Code: Select all

$wp_u_list_ids_emails_and_roles = '', $wpRoles_phpbbGroups_schema = ''
can be used as necessary, the one or the other will be processed as needed based on, if the one or the other is empty or not.

that's more complex because will manage multiple users groups switches based on these two params.
So this second, since the first can be used also into a loop to achieve the same exact result (on filters and hooks)
i am not sure if i will be able to complete it as i intended before 2.9.1.
It will be probably released as basic on 2.9.0 then completed on 2.9.1.

Re: W3all Public functions

by axew3 » Thu Aug 22, 2024 10:30 am

2.9.0
I hope just in these days to release it!

Re: W3all Public functions

by brianp6 » Tue Aug 20, 2024 7:27 pm

Thank you, looks good.
Do you know when you are planning for these latest changes to be released?

many thanks,
Brian.

Top