Advertisement
designbymerovingi

Pay signup referrals on verification

Oct 17th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. /**
  2.  * Step 1: Decline Signup Referral Payouts
  3.  * First we save the transaction and then decline it since we will
  4.  * payout first when the user has activated their account
  5.  * @version 1.0
  6.  */
  7. add_filter( 'mycred_add', 'mycred_pro_decline_signup_ref', 20, 3 );
  8. function mycred_pro_decline_signup_ref( $reply, $request, $mycred ) {
  9.  
  10.     // Only applicable for signup referral and if the transaction has not already been decliend
  11.     if ( $request['ref'] != 'signup_referral' || $reply === false || $request['data'] == 'activated' ) return $reply;
  12.  
  13.     // Get the new users ID
  14.     $pending_user_id = absint( $request['ref_id'] );
  15.  
  16.     // Save this instance for when a user verifies their account
  17.     update_user_meta( $pending_user_id, 'signup_referral', array(
  18.         'reffering_user_id' => absint( $request['user_id'] ),
  19.         'amount'            => $request['amount'],
  20.         'type'              => $request['type'],
  21.         'log'               => $request['entry']
  22.     ) );
  23.  
  24.     // Decline
  25.     return false;
  26.  
  27. }
  28.  
  29. /**
  30.  * Step 2: Payout on Activation
  31.  * Once a user activates their account by logging in, we
  32.  * payout any pending referral that might exist.
  33.  * @version 1.0
  34.  */
  35. function check_first_login($user_login, $user) {   
  36.     $user_info = get_user_meta( $user->ID );
  37.     if ( $user_info["accountactivated"][0]!="accountactivated" ) {
  38.         update_user_meta( $user->ID, 'accountactivated', 'accountactivated' );
  39.         if ( function_exists( 'mycred_add' ) ) {
  40.             $pending_transaction = get_user_meta( $user->ID, 'signup_referral', true );
  41.             if ( $pending_transaction != '' ) {
  42.                 // Just to be safe
  43.                 $pending_transaction = maybe_unserialize( $pending_transaction );
  44.  
  45.                 // Payout
  46.                 mycred_add(
  47.                     'signup_referral',
  48.                     $pending_transaction['reffering_user_id'],
  49.                     $pending_transaction['amount'],
  50.                     $pending_transaction['log'],
  51.                     $user->ID,
  52.                     'activated', // added to prevent our filter from declining this as well
  53.                     $pending_transaction['type']
  54.                 );
  55.  
  56.                 // Clean up
  57.                 delete_user_meta( $user->ID, 'signup_referral' );
  58.             }
  59.         }
  60.     }
  61. }
  62. add_action('wp_login', 'check_first_login', 1, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement