designbymerovingi

myCRED Banking: Max Balance Limit for Recurring Payouts

Jan 7th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. /**
  2.  * Adjust Eligeble Users
  3.  * Removes users that have 100 points or higher as balance from gaining a recurring payout.
  4.  * @requires myCRED 1.6 or higher
  5.  * @version 1.0
  6.  */
  7. add_filter( 'mycred_get_eligeble_users', 'mycred_pro_adjust_bank_eligeble_users', 10, 2 );
  8. function mycred_pro_adjust_bank_eligeble_users( $users, $banking ) {
  9.  
  10.     // Only applicable for "Recurring Payouts"
  11.     if ( $banking->id != 'payouts' ) return $users;
  12.  
  13.     // No use to do anything if there are no eligeble users
  14.     if ( empty( $users ) ) return $users;
  15.  
  16.     // First we need to get the correct balance key
  17.     $balance_key = $banking->mycred_type;
  18.     if ( is_multisite() && $GLOBALS['blog_id'] > 1 && ! $banking->core->use_central_logging )
  19.         $balance_key .= '_' . $GLOBALS['blog_id'];
  20.  
  21.     // For our SQL query we need to take into account if we are using decimals
  22.     $balance_format = '%d';
  23.     if ( isset( $banking->core->format['decimals'] ) && $banking->core->format['decimals'] > 0 )
  24.         $balance_format = 'CAST( %f AS DECIMAL( 10, ' . $banking->core->format['decimals'] . ' ) )';
  25.  
  26.     global $wpdb;
  27.  
  28.     // Remove all users that have 100 points or more
  29.     $max = 100;
  30.  
  31.     // Next we get all ineligible users
  32.     $ineligible_user_ids = $wpdb->get_col( $wpdb->prepare( "
  33.         SELECT DISTINCT user_id
  34.         FROM {$wpdb->usermeta}
  35.         WHERE meta_key = %s
  36.         AND meta_value >= {$balance_format};", $balance_key, $max ) );
  37.  
  38.     // If we have results, remove them from $users
  39.     if ( ! empty( $ineligible_user_ids ) ) {
  40.         $new_user_list = array();
  41.         foreach ( $users as $user_id ) {
  42.             if ( ! in_array( $user_id, $ineligible_user_ids ) )
  43.                 $new_user_list[] = $user_id;
  44.         }
  45.         $users = $new_user_list;
  46.     }
  47.  
  48.     // Return for processing
  49.     return $users;
  50.  
  51. }
Add Comment
Please, Sign In to add comment