Advertisement
designbymerovingi

Change role on myCRED content purchase

May 9th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. /**
  2.  * Promote Based on Content Purchase
  3.  * Changes a users role based on their myCRED content purchases.
  4.  * @version 1.0
  5.  */
  6. add_filter( 'mycred_add_finished', 'mycred_me_rolechange_buy_content', 99, 3 );
  7. function mycred_me_rolechange_buy_content( $reply, $request, $mycred ) {
  8.  
  9.     // Only applicable if the user gained points for purchasing content
  10.     if ( $reply === false || $request['ref'] != 'buy_content' ) return $reply;
  11.  
  12.     // Exclude admins
  13.     if ( user_can( $request['user_id'], 'manage_options' ) ) return $reply;
  14.  
  15.     extract( $request );
  16.  
  17.     $new_role    = 'vip_role';
  18.     $requirement = 9000;
  19.     $user        = get_userdata( $user_id );
  20.  
  21.     global $wpdb;
  22.  
  23.     // How many points have the user spent on buying points?
  24.     $total = $wpdb->get_var( $wpdb->prepare( "SELECT SUM( creds ) FROM {$mycred->log_table} WHERE ref = 'buy_content' AND user_id = %d AND ctype = %s;", $user_id, $type ) );
  25.  
  26.     // If no results are found, use the amount paid now
  27.     if ( $total === NULL )
  28.         $total = $amount;
  29.  
  30.     // Change users role if they have spent the required amount and do not yet have
  31.     // the new role.
  32.     if ( abs( $total ) >= $requirement && ! in_array( $new_role, $user->roles ) )
  33.         wp_update_user( array(
  34.             'ID'   => $user_id,
  35.             'role' => $new_role
  36.         ) );
  37.  
  38.     return $reply;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement