Editor and contributor roles not correct after adding function

** RESTATED **

I am creating a bridge. The work involves a default installation of WordPress and a plugin to bridge a forum and WordPress. The forum provides all the account information. The secondary usergroup of the forum is matched to the WP role. Now I am trying to retrieve the “role” selected in the options for that logged in user and add_role for the string. Inserting wp_die shows the correct role is being given to the logged in user. However, the wrong capabilities are being added. Everyone is getting admin.

Read More
// returns an array
var_dump( $XF->options['xf_user_role'] );

//returns string() "role such as administrator" (in quotes)
var_dump( $XF->options['xf_user_role'][$user_group] );

** UPDATED **:

Here is the latest partial code. There is no reason to really show filluserData

class XF_User_Data {

public static function fillUserData($visitor, $classObj, $user_id) {
    // doing something and calls set_user_roles
}

public static function set_user_roles( &$classObj, $user_group_id, $secondary_group_ids ) {
    /**
     * Function set_user_roles(&$classObj, $user_group_id, $secondary_group_ids)
     * 
     * This function sets the current user role based on the secondary group
     */
    global $XF, $wp_roles;
    $user_groups = explode( ',', $secondary_group_ids );
    $user_groups[] = $user_group_id;

    /**
     * Loop through the user_roles and find the WP role equivalent for each
     */
    $user_roles = array();
    foreach( $user_groups as $user_group ) {
        $user_roles[] = $XF->options['xf_user_role'][$user_group];

        /**
         * wp_die( 'This is the role:' . var_dump( $XF->options['xf_user_role'] ) );
         * 
         * Returns:
         *
         * array(7) { [3]=> string(13) "administrator" [4]=> string(6) "author" [2]=> string(10) "subscriber" [1]=> string(10) "subscriber" [5]=> string(6) "author" [7]=> string(11) "contributor" [6]=> string(6) "editor" }
         */

        /**
         * wp_die( var_dump( $XF->options['xf_user_role'][$user_group] ) ); 
         *
         * Returns for user Ellie who has secondary group related to author
         * string(6) "author"
         * 
         * Returns Albert who has secondary group set to editor
         * string(6) "editor"
         *
         * Returns Pascal who has secondary group related to contributor
         * string(11) "contributor"
         *
         * Returns Gracie NO secondary group for registered members.
         * NULL
         *           
         */

        $new_roles =  $XF->options['xf_user_role'][$user_group];

        // wp_die( var_dump( $XF->options['xf_user_role'][$user_group] ) );

        if( $new_roles == 'administrator' ) {

            $capabilities = array ( 
                'activate_plugins','delete_others_pages','delete_others_posts','delete_pages','delete_plugins','delete_posts','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_dashboard','edit_files','edit_others_pages','edit_others_posts','edit_pages','edit_posts','edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','edit_theme_options','export','import','list_users','manage_categories','manage_links','manage_options','moderate_comments','promote_users','publish_pages','publish_posts','read_private_pages','read_private_posts','read','remove_users','switch_themes','upload_files','create_product'
            );

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            foreach ( $capabilities as $cap ) {

                $user->add_cap( $cap ); 

            }

            break;

        }               

        elseif( $role == 'editor' ) {

            $capabilities = array ( 
                'delete_others_pages','delete_others_posts','delete_pages','delete_post','delete_private_pages','delete_private_posts','delete_published_pages','delete_published_posts','edit_others_pages','edit_others_posts','edit_pages','edit_posts', 'edit_private_pages','edit_private_posts','edit_published_pages','edit_published_posts','manage_categories','manage_links','moderate_comments','publish_pages','publish_posts','read','read_private_pages','read_private_posts'
            );

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            foreach ( $capabilities as $cap ) {

                $user->add_cap( $cap ); 

            }

            break;

        }

        elseif( $new_roles == 'author' ) {          

            $capabilities = array(
                'delete_posts', 'delete_published_posts','edit_posts', 'edit_published_posts','publish_posts', 'read', 'upload_files','upload_files'
                );

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            foreach ( $capabilities as $cap ) {

                $user->add_cap( $cap ); 

            }

            break;

        }   

        elseif( $new_roles == 'contributor' ) {

            $capabilities = array( 'delete_posts', 'edit_posts', 'read' );

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            foreach ( $capabilities as $cap ) {

                $user->add_cap( $cap ); 

            }

            break;

        }

        elseif( $new_roles == '' || $new_roles == NULL || $new_roles == 'subscriber' ) {

            $capabilities = array( 'read' );

            $user_id = $XF->visitor->get('user_id');

            $user = new WP_User( $user_id );

            foreach ( $capabilities as $cap ) {

                $user->add_cap( $cap ); 

            }

            break;

        }

    }

}

public static function create_new_wp_user( $new_role, $user_id ) {

    if( null == username_exists( $user_id ) ) {

    ini_set('memory_limit', '128M');

      // Generate the password and create the user
      $password = wp_generate_password( 12, false );
      $user_id = wp_create_user( $email_address, $password, $email_address );

      // Set the nickname
      wp_update_user(
        array(
          'ID'          =>    $user_id,
          'nickname'    =>    $email_address
        )
      );

      // Set the role
      $user = new WP_User( $user_id );
      $user->set_role( $new_role );

      // Email the user
      wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );

      return;

      } // end if

}

}

The wrong capabilities are being added to the user roles. For example, everyone is getting administrator capabilities.

What is wrong with this code?

PS. I am a neophyte and know just enough to be dangerous 😉

Related posts