Expire a user’s secondary role after X days from it being allocated

The snippet below links a user’s Easy Digital Download (EDD) download payment to allocating a secondary user role to that user.

As per the example below…

  • If a user buys a EDD download with a download ID of 340 then that
    user will be allocated a secondary user role of “buyer”… or,
  • If a user buys a EDD download with a download ID of 240 then that
    user will be allocated a secondary user role of “other_role”.

I’d like each secondary user role to be unallocated from each user X days after the successful EDD payment.

function pw_edd_run_when_purchase_complete( $payment_id, $new_status, $old_status ) {
        if ( $old_status == 'publish' || $old_status == 'complete' ) {
            return;
        } // Make sure that payments are only completed once

        // Make sure the payment completion is only processed when new status is complete
        if ( $new_status != 'publish' && $new_status != 'complete' ) {
            return;
        }

        $downloads = edd_get_payment_meta_downloads( $payment_id );
        $user_id   = edd_get_payment_user_id( $payment_id );

        if ( is_array( $downloads ) ) {
            // Increase purchase count and earnings
            foreach ( $downloads as $download ) {
                $user = new WP_User( $user_id );
                if(!$user )
                    continue;

                if( $download['id'] == 340 ) {
                     // Add custom role
                     // Change 340 to the download id of the product that a certain role corresponds
                    $user->add_role( 'buyer' );
                    //also change buyer depending on your role that you created in User Role Editor


                } elseif( $download['id'] == 240 ) {
                     // Add custom role
                      // Change 340 to the download id of the product that a certain role corresponds
                    $user->add_role( 'other_role' );
                    //also change other_role depending on your role that you created in User Role Editor
                }
            }
        }
    }

    add_action( 'edd_update_payment_status', 'pw_edd_run_when_purchase_complete', 100, 3 );

2 Answers
2

As alternative to a cron job, you may use a “just in time” removal of the role.

i.e. when an user logs in, you can check if it has the role(s) you want to expire, check current date with download date and update the user accordingly.

This assumes that you are able to get the date or timestamp of (at least) the last download of an user.

I am not familiar with EDD, but this is a prof of concept code:

add_action( 'wp_login', function($username, $user) {

   $toExpire = [ 'buyer', 'other_role' ];

   $toRemove = array_intersect($user->roles, $toExpire);

   if (count($user->roles) < 2 || empty($toRemove)) {
     // nothing to do if the user has just 1 role or none of the roles you want to expire
     return;
   }

   // the function below does NOT exists
   // I think in EDD you should be able to get the date or timestamp of an user last download
   $downloadTimestamp = get_last_download_timestamp_for_user($user->ID);

   $dayLimit = 30 * DAY_IN_SECONDS; // expire after 30 days

   if ((time() - $dayLimit) < $paymentTimestamp) {
     // do nothing if not enough time passed
     return;
   }

   if (! array_diff($toExpire, $toRemove)) {
     // if an user has ONLY roles that might expire, keep the first role
     array_shift($toRemove);
   }

   // remove expired roles
   foreach($toRemove as $role) {
     $user->remove_role($role);
   }


}, 1, 2);

Doing like so you don’t need to set a cron job, and it is easier to implement.

However, using this method, in your system you may see users with “expired” roles just because they have not logged in since the role expired. If you have (or want) statistics in this regard, you need to keep this into account.

Leave a Comment