w3all Public functions - groups roles switches

User avatar
axew3
w3all User
w3all User
Posts: 2883
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Re: w3all Public functions - groups roles switches

Post by axew3 »

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.
Post Reply