Page 1 of 2

Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 6:35 am
by fediddy
I am having a hard time trying to figure this out.

I have setup the following Membership Groups the Paid Membership Pro plugin in wordpress.

Suicide Kings Car Club - Free ↔ pmpro_role_1
SKIE - Inland Empire - 1 Year Membership ↔ pmpro_role_2
SKWA - Washington - 1 Year Membership ↔ pmpro_role_3
SKLA - Los Angeles - 1 Year Membership ↔ pmpro_role_4
SKNC - Northern California - 1 Year Membership ↔ pmpro_role_5
SKCV - Coachella / Valley - 1 Year Membership ↔ pmpro_role_7
I have setup the following Roles in PHPBB.

[qoute]
Group ID = 8 ↔ Group Name = Free - Suicide Kings Car Club
Group ID = 9 ↔ Group Name = Suicide Kings Inland Empire Chapter
Group ID = 10 ↔ Group Name = Suicide Kings Washington Chapter
Group ID = 11 ↔ Group Name = Suicide Kings Los Angeles Chapter
Group ID = 12 ↔ Group Name = Suicide Kings Northern California Chapter
Group ID = 13 ↔ Group Name = Suicide Kings Coachella Valley Chapter
[/qoute]

What I want to happen. All new users will sign up using the "Suicide Kings Car Club - Free ↔ pmpro_role_1" Membership options. Which will then automatically register them on PHPBB and into the Group ID = 8 ↔ Group Name = Free - Suicide Kings Car Club Group. THIS CURRENTLY WORKS.

What I want it to do next is when an Actual Club Member signs up and pays for their specific Membership . They will automatically get updated in PHPBB to the appropriate ROLE. When a membership expires, after warnings and what not, The member will be switched back to "Suicide Kings Car Club - Free ↔ pmpro_role_1" membership on Wordpress and will be switched over to "Group ID = 8 ↔ Group Name = Free - Suicide Kings Car Club" on phpbb.

I used the following code in wpRoles_phpbbGroups.php
Line 39 is what I came up with. What am i doing wrong?
"$wpRoles_phpbbGroups = ['pmpro_role_1' => 8, 'pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];"

Code: Select all

<?php defined( 'ABSPATH' ) or die( 'forbidden' );
// (C) 2023 - axew3.com

# The groups/roles integration can be so complex
# It is substantially quite hard to put here any possible combination of things expecting that it will fit the required for any possible scenario.
# This file can be edited as more like, and can be copied into the 'wp-content/plugins/wp-w3all-custom/wpRoles_phpbbGroups.php' folder
# so that when the plugin will update, custom modifications will not be lost


# Vars
# $w3all_roles_to_groups_on_wpupdate_profile (declared on file class.wp.w3all-phpbb.php -> function phpbb_update_profile )
# $w3all_roles_to_groups_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )

# $w3all_groups_to_roles_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )
# are used to determine which part of code may is not executed, due to options values or because one option could conflict with the other, during some WP profile update process


# phpBB: https://www.phpbb.com/support/docs/en/3.3/ug/adminguide/groups_types/
# some default phpBB group
// group_id 2 = registered
// group_id 4 = global moderator
// group_id 5 = administrator

# WordPress: https://wordpress.org/support/article/roles-and-capabilities/

# used to determine roles against groups
$wp_normal_roles_ary  = ['subscriber', 'contributor', 'author', 'customer'];
$wp_editors_roles_ary = ['editor', 'shop_manager'];
$wp_admins_roles_ary  = ['administrator'];

/*
# Custom additions arrays (NOT used, just a test way)
$wpRolesphpBBGroups = ['administrator' => 5, 'subscriber' => 2, 'contributor' => 2, 'author' => 2, 'editor' => 4];
# shop_manager and customer, refers to Woocommerce capabilities/roles
$wooAndcustom_wpRolesphpBBGroups = ['customer' => 2, 'shop_manager' => 4, 'example_another_role' => 2];
# an array to manage more complex switches between WP Roles and phpBB Groups
$wpRoles_phpBBGroups = array_merge($wpRolesphpBBGroups,$wooAndcustom_wpRolesphpBBGroups);
*/
$wpRoles_phpbbGroups = ['pmpro_role_1' => 8, 'pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];
############################################
### START
# Roles -> Groups switches WHEN ON --> wp_profile_update (see public static function phpbb_update_profile on class.wp.w3all-phpbb.php)
# -> for the updated WP user
# synchronous

if( isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
  // **** MemberPress note: do not know if it is a bug of the version 10 Legacy i am testing on, or a default behavior, but when an user is set as NoRole in WordPress,
  // WP set/leave the user as subscriber by the way.
  // So that it is necessary to detect that the user may have no active mepr subscriptions/memberships and deactivate or move the user into another group in phpBB if the case

  // ** it is possible to add memberships from external wp plugins for more complex roles/groups/memberships switches
  // like for MemberPress memberships or more Woocommerce Roles or something else

  // has been set as NoRole in WP?
     if( empty($wpu->roles) )
      { // if no role for this site deactivate in phpBB
        $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '1' WHERE user_id = '$uid'");
      }

  // if old user's data role do not match, then there is an user role change: update/activate in phpBB based on Roles/Groups array
  // check that the user effectively have a role
   if( empty($old_user_data->roles[0]) && !empty($wpu->roles)
       OR !empty($wpu->roles) && $old_user_data->roles[0] != $wpu->roles[0] ) // **
   { // retrieve all groups which the user belong to (for more complex roles/groups switches)
     # $uid_groups = $w3all_phpbb_connection->get_results("SELECT * FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = $uid");

          // on duplicate key is not possible, there is no index into the user_group table: remove the record if it exist before to insert again
          // even if the user_group table can contain duplicated values
          // *** Note: maybe (?) the phpBB user should be removed from any other group which may belong to into phpBB OR should be added into more groups?

       if( $wpu->roles[0] == 'author' OR $wpu->roles[0] == 'subscriber' OR $wpu->roles[0] == 'contributor' OR $wpu->roles[0] == 'customer' )
       { # AUTHOR, SUBSCRIBER, CONTRIBUTOR, CUSTOMER set as registered
         if( $ugid != 2 )
         {
          $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '2' WHERE user_id = '$uid'");
          $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('2','$ugid')");
          $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('2','$uid','0','0')");
         }

       } elseif( $wpu->roles[0] == 'editor' OR $wpu->roles[0] == 'shop_manager' )
         { # EDITOR, SHOP MANAGER set as global moderator
          if( $ugid != 4 )
          {
           $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '4' WHERE user_id = '$uid'");
           $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('4','$ugid')");
           $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('4','$uid','0','0')");
          }

         } elseif( $wpu->roles[0] == 'administrator' && $ugid != 5 )
           { # ADMINISTRATOR set as administrator
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '5' WHERE user_id = '$uid'");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('5','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('5','$uid','0','0')");
             //$w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('2','4','5','$ugid')");
             //$w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('2','$uid','0','0'),('4','$uid','0','0'),('5','$uid','0','0')");
            }
    }

}

# Roles -> Groups switches ON wp_profile_update
### END
############################################


############################################
### START
# WP Roles -> phpBB Groups switches WHEN ON --> verify_credentials
# -> for the current logged in user (and data changed in WP, not due to an explicit WP profile update action, but due to some plugin Role update that fire on the background, may due to a subscribed membership or something else
# asynchronous
# Do not execute this if on WP admin profile update action -> function phpbb_update_profile -> class.wp.w3all-phpbb.php

if( isset($w3all_roles_to_groups_on_verify_credentials) && !isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
    # some default phpBB groups
    // group_id 2 = registered
    // group_id 4 = global moderator
    // group_id 5 = administrator

   // NoRole in WP?
     if( empty($current_user->roles) )
      { // if no role for this site deactivate in phpBB
        $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '1' WHERE user_id = '$uid'");
      }

   if( !empty($current_user->roles) )
   {
       if( $current_user->roles[0] == 'author' OR $current_user->roles[0] == 'subscriber' OR $current_user->roles[0] == 'contributor' OR $current_user->roles[0] == 'customer' )
       { # AUTHOR, SUBSCRIBER, CONTRIBUTOR, CUSTOMER set as registered
         if( $phpbb_user_session[0]->group_id != 2 )
         {
          $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '2' WHERE user_id = '$uid'");
          $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('2','$ugid')");
          $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('2','$uid','0','0')");
         }
       } elseif( $current_user->roles[0] == 'editor' OR $current_user->roles[0] == 'shop_manager' )
         { # EDITOR, SHOP MANAGER set as global moderator
          if( $phpbb_user_session[0]->group_id != 4 )
          {
           $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '4' WHERE user_id = '$uid'");
           $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('4','$ugid')");
           $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('4','$uid','0','0')");
          }

         } elseif( $current_user->roles[0] == 'administrator' && $phpbb_user_session[0]->group_id != 5 )
           { # ADMINISTRATOR set as administrator, global mod and registered
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '5' WHERE user_id = '$uid'");
             //$w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('5','$ugid')");
             //$w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('5','$uid','0','0')");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('2','4','5','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('2','$uid','0','0'),('4','$uid','0','0'),('5','$uid','0','0')");
           }
  }
}

# Roles -> Groups switches ON verify_credentials
### END
############################################


############################################
### START
# Groups -> Roles switches WHEN ON --> verify_credentials
# -> for the current logged in user (and data changed in phpBB)
# asynchronous

if( isset($w3all_groups_to_roles_on_verify_credentials) )
{
    # some default phpBB groups
    // group_id 2 = registered
    // group_id 4 = global moderator
    // group_id 5 = administrator

        if($phpbb_user_session[0]->group_id == 2 && !in_array($current_user->roles[0],$wp_normal_roles_ary))
        {
          $usr = new WP_User($current_user->ID);
          $usr->remove_role($current_user->roles[0]); # should remove all roles instead and not only the primary? Or should add to existent?
          $usr->set_role('subscriber');
          $refresh_u = true;

        } elseif($phpbb_user_session[0]->group_id == 4 && !in_array($current_user->roles[0],$wp_editors_roles_ary))
          {
            $usr = new WP_User($current_user->ID);
            $usr->remove_role($current_user->roles[0]); # should remove all roles instead and not only the primary? Or should add to existent?
            $usr->set_role('editor');
            $refresh_u = true;

          } elseif($phpbb_user_session[0]->group_id == 5 && $current_user->roles[0] != 'administrator')
            {
             # $usr = new WP_User($current_user->ID);
             # $usr->remove_role($current_user->roles[0]);
             # $usr->set_role('administrator');
             # $refresh_u = true;
            }


    if(isset($refresh_u))
    {
      #if ( defined( 'WP_ADMIN' ) )
      #{
       if ( !function_exists( 'refresh_user_details' ) ) {
          require_once ABSPATH . '/wp-admin/includes/ms.php';
        }
       refresh_user_details($current_user->ID);
      #}
      clean_user_cache($current_user->ID);
    }

}

# Groups -> Roles switches ON verify_credentials
### END
############################################


/*
# ABOUT memberpress

# function that return specified membership state
  if( class_exists('MeprUser') ){
   if(current_user_can('mepr-active','rules:111')){
    # the user belong and is active on Rule ID 111
   }
  }


# function that return all active mepr memberships
function w3all_mepr_active_umemberships( $user_id = false ){

    if( class_exists('MeprUser') ){

        if( ! $user_id ){
           return false;
        }
     # ... code to get all mepr user's active memberships
    } else {
        return false;
    }
}

# END ABOUT memberpress
*/

Re: Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 2:28 pm
by axew3
Done on fly, an example for all to give an idea.

Since you only want updates in phpBB, when roles changed in WP (let say so in one way) i simplified the code like this (NOT TESTED, if there is a php error correct it but should be ok) but maybe work exactly like you want or surely give you the idea (the code can be changed as more you like because you should consider it totally independent by the 'plugin core', and you have to consider only the fact that a piece code fire when an user's update occur on profile page, while the other always fire, when the user normally navigate the site from page to page and do not click into the Update profile button).
May read inline hints and let know if any answer (because surely i missed something?)... let see

Code: Select all

<?php defined( 'ABSPATH' ) or die( 'forbidden' );
// (C) 2023 - axew3.com

# The groups/roles integration can be so complex
# It is substantially quite hard to put here any possible combination of things expecting that it will fit the required for any possible scenario.
# This file can be edited as more like, and can be copied into the 'wp-content/plugins/wp-w3all-custom/wpRoles_phpbbGroups.php' folder
# so that when the plugin will update, custom modifications will not be lost


# Vars
# $w3all_roles_to_groups_on_wpupdate_profile (declared on file class.wp.w3all-phpbb.php -> function phpbb_update_profile )
# $w3all_roles_to_groups_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )

# $w3all_groups_to_roles_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )
# are used to determine which part of code may is not executed, due to options values or because one option could conflict with the other, during some WP profile update process


# phpBB: https://www.phpbb.com/support/docs/en/3.3/ug/adminguide/groups_types/
# some default phpBB group
// group_id 2 = registered
// group_id 4 = global moderator
// group_id 5 = administrator

# WordPress: https://wordpress.org/support/article/roles-and-capabilities/

# used to determine roles against groups
//$wp_normal_roles_ary  = ['subscriber', 'contributor', 'author', 'customer'];
//$wp_editors_roles_ary = ['editor', 'shop_manager'];
//$wp_admins_roles_ary  = ['administrator'];

/*
# Custom additions arrays (NOT used, just a test way)
$wpRolesphpBBGroups = ['administrator' => 5, 'subscriber' => 2, 'contributor' => 2, 'author' => 2, 'editor' => 4];
# shop_manager and customer, refers to Woocommerce capabilities/roles
$wooAndcustom_wpRolesphpBBGroups = ['customer' => 2, 'shop_manager' => 4, 'example_another_role' => 2];
# an array to manage more complex switches between WP Roles and phpBB Groups
$wpRoles_phpBBGroups = array_merge($wpRolesphpBBGroups,$wooAndcustom_wpRolesphpBBGroups);
*/

$ugid = $phpbb_user_session[0]->group_id;
$uid = $phpbb_user_session[0]->user_id;

//$wpRoles_phpbbGroups = ['pmpro_role_1' => 8, 'pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];
// To check instead, that users that do not belong to these 'active/valid' roles, are switched to default suicide in phpBB
// yes it can be done in more complex ways leaving the pmpro_role_1. I just simplified, so that
// if the user do not belong to one of these, we'll change the phpBB in accord

$wpRoles_phpbbGroups = ['pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];

############################################
### START
# Roles -> Groups switches WHEN ON --> wp_profile_update (see public static function phpbb_update_profile on class.wp.w3all-phpbb.php)
# -> for the updated WP user
# synchronous

if( isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
  // has been set as NoRole in WP?
  // may you can remove this, or leave
     if( empty($wpu->roles) )
      { // if no role for this site deactivate in phpBB
        $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '1' WHERE user_id = '$uid'");
      }

  // if old user's data role do not match, then there is an user role change: update/activate in phpBB based on Roles/Groups array
  // check that the user effectively have a role
   if( empty($old_user_data->roles[0]) && !empty($wpu->roles)
       OR !empty($wpu->roles) && $old_user_data->roles[0] != $wpu->roles[0] ) // **
   { // retrieve all groups which the user belong to (for more complex roles/groups switches)
     # $uid_groups = $w3all_phpbb_connection->get_results("SELECT * FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = $uid");


            // This should check if the current WP user belong or not to one role on the created array of WP roles
            // if the user do not belong anymore to one of these, set as suicide in phpBB
            // i just removed the first you added, into the array above, to make it easy
           
           // if the user is not an admin, or an editor
           // and there is no subscriptions that match his first role ($wpu->roles[0])
           
           // ADD some other WP ROLE to be excluded by default
           if( ! isset($wpRoles_phpbbGroups[$wpu->roles[0]]) 
               && $wpu->roles[0] != 'administrator' 
               && $wpu->roles[0] != 'editor' 
               && $wpu->roles[0] != 'shop_manager' )
            
           { // move the user to phpBB group 8 
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '8' WHERE user_id = '$uid'");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('8','$uid','0','0')");
           }  elseif ( isset($wpRoles_phpbbGroups[$wpu->roles[0]]) && $phpbb_user_session[0]->group_id != $wpRoles_phpbbGroups[$wpu->roles[0]] )
             { // update the user because there is active membership
             	
           	  // not sure is $wpRoles_phpbbGroups[$wpu->roles[0]] require to be set like var
           	  // before to use it on query, like so: $idg = $wpRoles_phpbbGroups[$wpu->roles[0]];
           	  // then use '$idg' in place of $wpRoles_phpbbGroups[$wpu->roles[0]] as it is here
           	  $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '".$wpRoles_phpbbGroups[$wpu->roles[0]]."' WHERE user_id = '$uid'");
              $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
              $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('".$wpRoles_phpbbGroups[$wpu->roles[0]]."','$uid','0','0')");
  
             }
    }

}

# Roles -> Groups switches ON wp_profile_update
### END
############################################





############################################
### START
# Groups -> Roles switches WHEN ON --> verify_credentials
# -> for the current logged in user (and data changed in phpBB)
# asynchronous

if( isset($w3all_roles_to_groups_on_verify_credentials) && !isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
    # some default phpBB groups
    // group_id 2 = registered
    // group_id 4 = global moderator
    // group_id 5 = administrator
    
           $wpu = wp_get_current_user();
    
           if( ! isset($wpRoles_phpbbGroups[$wpu->roles[0]])
               && $wpu->roles[0] != 'administrator' 
               && $wpu->roles[0] != 'editor' 
               && $wpu->roles[0] != 'shop_manager'
             )
           
           {
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '8' WHERE user_id = '$uid'");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('8','$uid','0','0')");
           
           } elseif ( isset($wpRoles_phpbbGroups[$wpu->roles[0]]) && $phpbb_user_session[0]->group_id != $wpRoles_phpbbGroups[$wpu->roles[0]] )
             {
           	  // not sure is $wpRoles_phpbbGroups[$wpu->roles[0]] require to be set like var
           	  // before to use it on query, like so: $idg = $wpRoles_phpbbGroups[$wpu->roles[0]];
           	  // then use '$idg' in place of $wpRoles_phpbbGroups[$wpu->roles[0]] as it is here
           	  $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '".$wpRoles_phpbbGroups[$wpu->roles[0]]."' WHERE user_id = '$uid'");
              $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
              $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('".$wpRoles_phpbbGroups[$wpu->roles[0]]."','$uid','0','0')");
  
             }


}

# Groups -> Roles switches ON verify_credentials
### END
############################################
[EDITED]

Re: Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 3:10 pm
by fediddy
I will try it in a few minutes.


Question for you.

How do you handle higher up members like the President and Vice President of each chapter?


Also, what about administrators and moderators of both phpbb and wordpress?

Thank you so much!!!

Re: Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 7:22 pm
by fediddy
Got a critical error switching user Felix from Northern California to Inland empire

Got an email
Howdy!

WordPress has a built-in feature that detects when a plugin or theme causes a fatal error on your site, and notifies you with this automated email.

In this case, WordPress caught an error with one of your plugins, wp-w3all-custom.

First, visit your website (https://www.suicidekingscarclub.com/) and check for any visible issues. Next, visit the page where the error was caught (https://www.suicidekingscarclub.com/wp- ... r-edit.php) and check for any visible issues.

Please contact your host for assistance with investigating this issue further.

If your site appears broken and you can't access your dashboard normally, WordPress now has a special "recovery mode". This lets you safely login to your dashboard and investigate further.

https://www.suicidekingscarclub.com/log ... 2AsnjAPKpa

To keep your site safe, this link will expire in 1 day. Don't worry about that, though: a new link will be emailed to you if the error occurs again after it expires.

When seeking help with this issue, you may be asked for some of the following information:
WordPress version 6.3.2
Active theme: Astra (version 4.3.1)
Current plugin: (version )
PHP version 8.1.21

Error Details
=============
An error of type E_PARSE was caused in line 70 of the file /home/u711450871/domains/suicidekingscarclub.com/public_html/wp-content/plugins/wp-w3all-custom/wpRoles_phpbbGroups.php. Error message: syntax error, unexpected token "&&"

Re: Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 8:49 pm
by axew3
(NOT TESTED, if there is a php error correct it

Code: Select all

<?php defined( 'ABSPATH' ) or die( 'forbidden' );
// (C) 2023 - axew3.com

# The groups/roles integration can be so complex
# It is substantially quite hard to put here any possible combination of things expecting that it will fit the required for any possible scenario.
# This file can be edited as more like, and can be copied into the 'wp-content/plugins/wp-w3all-custom/wpRoles_phpbbGroups.php' folder
# so that when the plugin will update, custom modifications will not be lost


# Vars
# $w3all_roles_to_groups_on_wpupdate_profile (declared on file class.wp.w3all-phpbb.php -> function phpbb_update_profile )
# $w3all_roles_to_groups_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )

# $w3all_groups_to_roles_on_verify_credentials (declared on file class.wp.w3all-phpbb.php -> function verify_phpbb_credentials )
# are used to determine which part of code may is not executed, due to options values or because one option could conflict with the other, during some WP profile update process


# phpBB: https://www.phpbb.com/support/docs/en/3.3/ug/adminguide/groups_types/
# some default phpBB group
// group_id 2 = registered
// group_id 4 = global moderator
// group_id 5 = administrator

# WordPress: https://wordpress.org/support/article/roles-and-capabilities/

# used to determine roles against groups
//$wp_normal_roles_ary  = ['subscriber', 'contributor', 'author', 'customer'];
//$wp_editors_roles_ary = ['editor', 'shop_manager'];
//$wp_admins_roles_ary  = ['administrator'];

/*
# Custom additions arrays (NOT used, just a test way)
$wpRolesphpBBGroups = ['administrator' => 5, 'subscriber' => 2, 'contributor' => 2, 'author' => 2, 'editor' => 4];
# shop_manager and customer, refers to Woocommerce capabilities/roles
$wooAndcustom_wpRolesphpBBGroups = ['customer' => 2, 'shop_manager' => 4, 'example_another_role' => 2];
# an array to manage more complex switches between WP Roles and phpBB Groups
$wpRoles_phpBBGroups = array_merge($wpRolesphpBBGroups,$wooAndcustom_wpRolesphpBBGroups);
*/


//$wpRoles_phpbbGroups = ['pmpro_role_1' => 8, 'pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];
// To check instead, that users that do not belong to these 'active/valid' roles, are switched to default suicide in phpBB
// yes it can be done in more complex ways leaving the pmpro_role_1. I just simplified, so that
// if the user do not belong to one of these, we'll change the phpBB in accord

$wpRoles_phpbbGroups = ['pmpro_role_2' => 9, 'pmpro_role_3' => 10, 'pmpro_role_4' => 11, 'pmpro_role_5' => 12, 'pmpro_role_7' => 13, ];

$ugid = $phpbb_user_session[0]->group_id;
$uid = $phpbb_user_session[0]->user_id;

############################################
### START
# Roles -> Groups switches WHEN ON --> wp_profile_update (see public static function phpbb_update_profile on class.wp.w3all-phpbb.php)
# -> for the updated WP user
# synchronous

if( isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
  // has been set as NoRole in WP?
  // may you can remove this, or leave
     if( empty($wpu->roles) )
      { // if no role for this site deactivate in phpBB
        $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '1' WHERE user_id = '$uid'");
      }

  // if old user's data role do not match, then there is an user role change: update/activate in phpBB based on Roles/Groups array
  // check that the user effectively have a role
   if( empty($old_user_data->roles[0]) && !empty($wpu->roles)
       OR !empty($wpu->roles) && $old_user_data->roles[0] != $wpu->roles[0] ) // **
   { // retrieve all groups which the user belong to (for more complex roles/groups switches)
     # $uid_groups = $w3all_phpbb_connection->get_results("SELECT * FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = $uid");


            // This should check if the current WP user belong or not to one role on the created array of WP roles
            // if the user do not belong anymore to one of these, set as suicide in phpBB
            // i just removed the first you added, into the array above, to make it easy
           
           // if the user is not an admin, or an editor
           // and there is no subscriptions that match his first role ($wpu->roles[0])
           
           // ADD some other WP ROLE to be excluded by default
           if( ! isset($wpRoles_phpbbGroups[$wpu->roles[0]]) 
               && $wpu->roles[0] != 'administrator' 
               && $wpu->roles[0] != 'editor' 
               && $wpu->roles[0] != 'shop_manager' )
            
           { // move the user to phpBB group 8 
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '8' WHERE user_id = '$uid'");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('8','$uid','0','0')");
           }  elseif ( isset($wpRoles_phpbbGroups[$wpu->roles[0]]) && $phpbb_user_session[0]->group_id != $wpRoles_phpbbGroups[$wpu->roles[0]] )
             { // update the user because there is active membership
             	
           	  // not sure is $wpRoles_phpbbGroups[$wpu->roles[0]] require to be set like var
           	  // before to use it on query, like so: $idg = $wpRoles_phpbbGroups[$wpu->roles[0]];
           	  // then use '$idg' in place of $wpRoles_phpbbGroups[$wpu->roles[0]] as it is here
           	  $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '".$wpRoles_phpbbGroups[$wpu->roles[0]]."' WHERE user_id = '$uid'");
              $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
              $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('".$wpRoles_phpbbGroups[$wpu->roles[0]]."','$uid','0','0')");
  
             }
    }

}

# Roles -> Groups switches ON wp_profile_update
### END
############################################





############################################
### START
# Groups -> Roles switches WHEN ON --> verify_credentials
# -> for the current logged in user (and data changed in phpBB)
# asynchronous

if( isset($w3all_roles_to_groups_on_verify_credentials) && !isset($w3all_roles_to_groups_on_wpupdate_profile) )
{
    # some default phpBB groups
    // group_id 2 = registered
    // group_id 4 = global moderator
    // group_id 5 = administrator
    
           $wpu = wp_get_current_user();
    
           if( ! isset($wpRoles_phpbbGroups[$wpu->roles[0]])
               && $wpu->roles[0] != 'administrator' 
               && $wpu->roles[0] != 'editor' 
               && $wpu->roles[0] != 'shop_manager'
             )
           
           {
             $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '8' WHERE user_id = '$uid'");
             $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
             $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('8','$uid','0','0')");
           
           } elseif ( isset($wpRoles_phpbbGroups[$wpu->roles[0]]) && $phpbb_user_session[0]->group_id != $wpRoles_phpbbGroups[$wpu->roles[0]] )
             {
           	  // not sure is $wpRoles_phpbbGroups[$wpu->roles[0]] require to be set like var
           	  // before to use it on query, like so: $idg = $wpRoles_phpbbGroups[$wpu->roles[0]];
           	  // then use '$idg' in place of $wpRoles_phpbbGroups[$wpu->roles[0]] as it is here
           	  $w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', user_permissions = '', group_id = '".$wpRoles_phpbbGroups[$wpu->roles[0]]."' WHERE user_id = '$uid'");
              $w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('8','9','10','11','12','$ugid')");
              $w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('".$wpRoles_phpbbGroups[$wpu->roles[0]]."','$uid','0','0')");
  
             }


}

# Groups -> Roles switches ON verify_credentials
### END
############################################
:

Re: Paid Membership Pro Wordpress & PHPBB Roles & Groups Integration.

Posted: Sat Oct 14, 2023 11:32 pm
by fediddy
I now get a critical error while logging in. But if i hit refresh, it works. Changing the role via the plugin manually does not update it in phpbb