Jump to content

Need this code to demote user role if points fall below minimum


Recommended Posts

The following code works as is to promote a user role when a points balance minimum is met, but I also need the role to change back to prior role if the points balance falls below the minimum. For example if points balance drops to 900 then the new role will be set to 'contributor'.

 /**
     * Promote Based on Balance
     * Changes a users role based on their myCRED balance.
     * @version 1.0.4
     */
    add_filter( 'mycred_add_finished', 'check_for_role_change', 99, 3 );
    function check_for_role_change( $reply, $request, $mycred ) {
        // Make sure that if any other filter has declined this we also decline
        if ( $reply === false ) return $reply;
    
        // Exclude admins
        if ( user_can( $request['user_id'], 'manage_options' ) ) return $reply;
    
        extract( $request );
    
        // Minimum balance requirement for each role
        $thresholds = array(
            'contributor'   => 100,
            'author'        => 1000,
            'editor'        => 10000,
            'administrator' => 100000
        );
    
            // Get users current balance
        $current_balance = $mycred->get_users_balance( $user_id, $type );
        $current_balance = $current_balance + $amount;
    
        // Check if the users current balance awards a new role
        $new_role = false;
        foreach ( $thresholds as $role => $min ) {
            if ( $current_balance > $min )
                $new_role = $role;
        }
    
        // Change users role if we have one
        if ( $new_role !== false )
            wp_update_user( array(
                'ID'   => $user_id,
                'role' => $new_role
            ) );
    
        return $reply;
    }

 

I may be missing something but it appears that your function will do what you want already.  You are testing the new balance against the min values for each category.  Whatever min value is identified as what s/b a new role you are assigning at the end already.

 

Edited by ginerjm

I am using myCRED to distribute points and have been testing it. It only works to promote a user from Subscriber to Author after I add points to that user. But when I deduct points back to below the minimum the user role remains at Author.

Add some echos to diagnose what is happening .  What does that extract line do for you here?

Change new role to be a blank (or assign 'contributor') to start.  Then assign the new role when your test finds one that fits.  If new role is not blank at the end of the loop then post it.

Edited by ginerjm
new suggestion

With a couple of db tables like this

Table: user                                    Table: role
+---------+----------+--------+                +---------+---------------+-----------+------------+
| user_id | username | points |                | role_id | role_name     | point_min | points_max |
+---------+----------+--------+                +---------+---------------+-----------+------------+
|       1 | John     |     66 |                |       5 | -             |         0 |        100 |
|       2 | Paul     |    101 |                |       6 | Contributor   |       101 |       1000 |
|       3 | George   |   3000 |                |       7 | Author        |      1001 |      10000 |
|       4 | Ringo    | 200000 |                |       8 | Editor        |     10001 |     100000 |
+---------+----------+--------+                |       9 | Administrator |    100001 |  999999999 |
                                               +---------+---------------+-----------+------------+

Then a simple query

SELECT username
     , rolename
FROM user u
     JOIN
     role r ON u.points BETWEEN r.points_min AND r.points_max;
     

does the job for you

+----------+---------------+
| username | rolename      |
+----------+---------------+
| John     | -             |
| Paul     | Contributor   |
| George   | Author        |
| Ringo    | Administrator |
+----------+---------------+

 

  • Like 3

PS If you really want to do it in PHP then you can apply the same method to an array

$thresholds = array(
            '-'             => [0, 100],
            'contributor'   => [101, 1000],
            'author'        => [1001, 10000],
            'editor'        => [10001, 100000],
            'administrator' => [100001, 999999999]
        );
        
echo getUserRole(50000, $thresholds);       //--> editor


function getUserRole($current_balance, $thresholds)
{
    foreach ( $thresholds as $role => $range ) {
    
        if (  $range[0] <= $current_balance && $current_balance <= $range[1] ) {
             return $role;
        }    
    }
    return false;   
}

 

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.