Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wasn't sure if starting a new question was appropriate, but the questions are completely different from my original ones and code has changed since originally asking for help. This is still the same project but I'm trying to fix code so it works. Thanks to those who explained using get_userdata in the plugin caused roles being added to everyone and not just the currently logged in user.

Plugin Concept: Login a user from a forum into WordPress, assign role based on the secondary user group of the forum.

So far - I can almost get this working by using get_userdata() and calling a class which assigns the roles. Unfortunately this assigns all roles to the user if the current user goes to a post belonging to a different author.

Here is the current code. This adds the role to the database for the user_id. It's just adding all roles because - as someone suggested - get_userdata is for all users.

In plugin file /includes/users.php

if ( !function_exists('get_currentuserinfo') ) :

/**
 *
 * @return void|boolean
 */

function get_currentuserinfo() {
    global $current_user, $user_ID, $XF;

    if ( ! empty( $current_user ) ) {
        // same as get_currentuserinfo() in pluggable
    }

    if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) {
        wp_set_current_user( 0 );
        return false;
    }

    $visitor = $XF->visitor;

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

    //This is the faked Wordpress login and logout routines 
    if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[LOGGED_IN_COOKIE] ) && $visitor->get('user_id') != 0 ) {

        global $wpdb;

        /* Logged in XF session but no WP session */
        if (!function_exists('wp_set_auth_cookie')) {

            wp_set_auth_cookie( $visitor->get('user_id') );

        }
    }

    elseif( !empty( $_COOKIE[LOGGED_IN_COOKIE] ) && $visitor->get('user_id') == 0 ) {

        /* we have a logged in WP session but no XF session */
        wp_clear_auth_cookie();
    }

    /* If no XenForo user is logged in */
    if ( $visitor->get('user_id') == 0 ) {
        $current_user = null;
        wp_set_current_user( 0 );
        return false;
    }

    $current_user = get_userdata( $visitor->get('user_id'), true );
    wp_set_current_user( $visitor->get('user_id') );
}

endif;

if ( !function_exists('get_userdata') ) :

/**
 * Retrieve user info by $user_id
 *
 * @param unknown $user_id
 * @return boolean|unknown|WP_User
 */

function get_userdata( $user_id ) {
    global $wpdb, $XF, $WP_User;

    if ( !is_numeric( $user_id ) )
        return false;

    if ( empty( $user_id ) )
        return false;

    $user_id = absint( $user_id );
    if ( !$user_id )
        return false;

    /**
     * Check $user is in cache object
     */

    $user = wp_cache_get( $user_id, 'users' );

    if ( $user ) {
        return get_user_by( 'id', $user_id );
    }

    /**
     * Initialize new $user
     */

    $user = new WP_User;

    XFUserData::fillUserData($XF->visitor, $user, $user_id);

    update_user_caches( $user );

    /**
     * This is a check for has_prop. Will remove later.
     */

    if ( $user->has_prop( 'twitter' ) ) {
        echo '';
    }

    return get_user_by( 'id', $user_id );
}

endif;

In file /public/xfuserdata.php

class XFUserData {

public static function fillUserData($visitor, $classObj, $user_id) {
    global $XF;

        if( $user_id != 0 ) {

            $userModel = $XF->getModelFromCache('XenForo_Model_User');

            $user = $userModel->getUserById( $user_id, array('join' => XenForo_Model_User::FETCH_USER_FULL) );

            $classObj->userObj = $user;

            if( $user_id == $visitor->get('user_id') ) {

                // Calls set_user_roles to establish WordPress Roles

                XFUserData::get_user_roles($classObj, (string)$visitor->get('user_group_id'), $visitor->get('secondary_group_ids'));

                // $classObj->ID = 

                return $classObj;

            } else {

                // Fill in permission based fields
                // $user['user_id']

                XFUserData::get_user_roles($classObj, (string)$user['user_group_id'], $user['secondary_group_ids']);


                // $classObj->ID =

                return $classObj;

            }
        }

    }

public static function get_user_roles( &$classObj, $user_group_id, $secondary_group_ids )  {
    /**
     * Function get_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];

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

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

        $user = new WP_User( $user_id );

            echo '<pre>Before Conditional<br />';
            echo 'This is the user_id ' . $user_id . '<br />';
            print_r( $classObj->userObj );
            echo '</pre>';

        if( $new_roles === 'administrator' ) {
            $user->set_role('administrator');
        }

        elseif( $new_roles === 'editor' ) {
            $user->set_role('editor');

            echo '<pre>Editor Area<br />';
            echo 'This is user_id ' . $user_id . '<br />';
            var_dump( $user );
            echo '</pre>';
        }

        elseif( $new_roles === 'author' ) {
            $user->set_role('author');
        }

        elseif( $new_roles === 'contributor' ) {
            $user->set_role('contributor');
        }

        elseif( $new_roles === 'subscriber' || $new_roles === '' || $new_roles === null ) {
            $user->set_role('subscriber');
        }

        return false;
    }

    return;
}

My hope was to simply rename get_usermeta to get_xf_usermeta ... but after the past 10 plus hours of trying all types of changes, I'm at a loss.

Changing the function name almost works. The role is available for just that single user (var_dump shows this) but the role is never added to the database as it is when get_userdata is the function name.

Clearly I'm missing a step to get the role added to the database when the get_userdata is no longer used.

Does anyone have any suggestions? The codex and a plugin development book I have just don't go into this type of detail for me (or I'm completely misunderstanding) and I'm too new to this but after weeks of struggling I've been asking here.

I appreciate any direction and suggestions.

UPDATE This is not exactly right but the wp_capabilities are now written to the database. Unfortunately the user gets the "You do not have sufficient permissions to access this page." despite having editor role and capabilities.

This is the change in xfuserdata.php

            elseif( $new_roles === 'editor' ) {

            $capabilities = array (
                "editor",'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->add_role('editor');
            update_user_meta( $user_id, 'wp_capabilities', $capabilities );

        }

UPDATE

I finally solved this problem after watching Nacin's WordPress TV talk regarding Roles & Capabilities as well as looking at many different options. If you see a better way then please let me know. But first recognize add_cap and add_role failed in this class's method. I have no idea why but update_user_meta works as long as the array is done with a true.

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

        $user = new WP_User( $user_id );

        if( $new_roles === 'administrator' ) {


            if ( is_multisite() && is_super_admin() ) { 

                grant_super_admin($user_id);

            } else {

            $capabilities = array ( 'administrator' => true );

            update_user_meta( $user_id, 'wp_capabilities', $capabilities );

            }
            return;
        }

        elseif( $new_roles === 'editor' ) {

            $capabilities = array ( 'editor' => true );
            update_user_meta( $user_id, 'wp_capabilities', $capabilities );
            return;

        }

        elseif( $new_roles === 'author' ) {

            $capabilities = array ( 'author' => true );
            update_user_meta( $user_id, 'wp_capabilities', $capabilities );
            return;

        }

        elseif( $new_roles === 'contributor' ) {

            $capabilities = array ( 'contributor' => true );                
            update_user_meta( $user_id, 'wp_capabilities', $capabilities );         
            return;
        }

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

            $capabilities = array ( 'subscriber' => true );
            update_user_meta( $user_id, 'wp_capabilities', $capabilities );
            return;

        }
share|improve this question
    
To clarify - I am NOT updating the core. The file names are included above the functions. All changes are done in the plugin directory. I also realize that using a foreach works when get_userdata name (in the plugin). However, I change that function name and the capabilities no longer write to the database. I cannot use update_user_meta because the information is written as i and not b:1... So - I'm at a loss. –  LPH Feb 19 at 5:14
    
This was updated to show how to get update_user_meta to write to the database with capabilities set to true. This approach works whereas add_cap or add_role failed to write to the database. –  LPH Feb 22 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.