• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

The check_role: Indentation and Responsibilities

This entry is part of a series of entries "December 2016 - XenWord Development Log"
User Roles.png

Even though I'm still going through the widgets, I started looking at this method in the class XenWord_XF_Users, which strangely sits in the file name class-xenword-add-wpusers.php. This needs to be fixed.

I quickly decided the method breaks way too many of my new coding guidelines that were adopted for this project (See Object Calisthenics ). In particular, the deep indentations are a clue that there are too many things happening in this function; the single responsibility for the function should be to check the role -- nothing else.

Sketched out ideas

First, the $wp_role array should be called here and not done in the check_role. I'll refactor this out to it's own function.

Second, I don't like the conditional for $user_role[0]. If there are multiple roles then can they reside in different locations in the array? This will need to be tested. I'm going to check into using array_key_exists.

Third, the check for no XenForo secondary usergroup does not belong in check_role. It should also have it's own function.

This is a start. I have to think about how to refactor the function.

public function check_role() {
        $wp_role = array();
        if ( $this->options['use_adv_mapping'] === '1' ) {
            // This is a string array separated by commas
            $userGroupIds = $this->visitor['secondary_group_ids'];
            $userGroups = explode(',', $userGroupIds);
            foreach ( $userGroups as $userGroupId ) {
                $xf_role = 'xf_role_' . $userGroupId;
                $wp_role[] = $this->options[$xf_role];
        } else {
            $user_id = XenWord::getVisitor()->getUserId();
            $user = new WP_User( $user_id );
            $user_role = array();
            if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
                foreach ( $user->roles as $role ) {
                    $user_role[] = $role;
                // Check if user is an editor, author, or contributor
                if ( $user_role[0] == 'editor' || $user_role[0] = 'author' || $user_role[0] == 'contributor' ) {
                } else {
                    $wp_role[] = $this->options['wp_default_role'];
            } else {
                $wp_role[] = $this->options['wp_default_role'];
        // Check if no secondary user group
        if ( null == $wp_role[0]  && $this->visitor['is_banned'] !== 1 ) {
            $wp_role[] = $this->options['wp_default_role'];
        /** Check if the user is already in the database */
        $this->check_user( $wp_role );

Current Status

Refactoring will take some research on the use of the array. I'll build some test files to see how to best pull the information from the array.
Next entry in series Google Integration
Previous entry in series Widgets

Blog entry information

Last update

More entries in Technology

More entries from LPH