Advertisement
designbymerovingi

Custom Lottery Prize - myCRED

Feb 11th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. /**
  2. * Add Custom Lottery Prize
  3. * @since 1.0
  4. */
  5. add_filter( 'mycred_lotto_winners_get', 'mycred_pro_add_custom_lottery_prize' );
  6. function mycred_pro_add_custom_lottery_prize( $prizes ) {
  7.  
  8. $prizes['ingame'] = 'In-game item';
  9. return $prizes;
  10.  
  11. }
  12.  
  13. /**
  14. * Add Custom Lottery Prize Settings
  15. * @since 1.0
  16. */
  17. add_action( 'mycred_lotto_winnings_ingame', 'mycred_pro_add_custom_lottery_prize_settings', 10, 2 );
  18. function mycred_pro_add_custom_lottery_prize_settings( $i, $lottery ) {
  19.  
  20. $winnings = $lottery->get_winnings_prefs();
  21. if ( isset( $winnings['draw'][ $i ] ) )
  22. $value = $winnings['draw'][ $i ];
  23. else
  24. $value = '';
  25.  
  26. echo '<td><input type="text" name="mycred_lotto[winnings][draw][' . $i . ']" id="mycred-lotto-win-draw-' . $i . '" value="' . $value . '" class="long" /></td>';
  27.  
  28. }
  29.  
  30. /**
  31. * Handle Custom Lottery Prize Win
  32. * @version 1.0
  33. */
  34. add_action( 'mycred_lotto_give_ingame', 'mycred_pro_custom_lottery_prize_payout', 10, 3 );
  35. function mycred_pro_custom_lottery_prize_payout( $product_winners, $winning_user_ids, $lottery ) {
  36.  
  37. // Get the "Winnings" settings for this lottery
  38. $winnings = $lottery->get_winnings_prefs();
  39.  
  40. // Get the "Templates" settings for this ltotery
  41. $templates = $lottery->get_templates_prefs();
  42.  
  43. $admin_email = '';
  44.  
  45. // Lets loop through our winners
  46. foreach ( $winning_user_ids as $pick => $user_id ) {
  47.  
  48. // IF you do not want a drawn winner to get anything make sure the field is empty
  49. if ( $winnings['draw'][ $pick ] == '' ) continue;
  50.  
  51. // The prize should be saved in the draw field
  52. $prize = sanitize_text_field( $winnings['draw'][ $pick ] );
  53.  
  54. // Make sure the winning user still exists
  55. $winning_user = get_userdata( absint( $user_id ) );
  56. if ( isset( $winning_user->ID ) ) {
  57.  
  58. // Replace %prize% template tag with the actual prize
  59. $template = str_replace( '%prize%', $prize, $templates['win'] );
  60.  
  61. // Send Email
  62. wp_mail( $winning_user->user_email, 'Lottery Win!', $template );
  63.  
  64. $admin_email .= sprintf( 'User: %s won: %s', $winning_user->display_name, $prize ) . "\n";
  65.  
  66. }
  67.  
  68. }
  69.  
  70. // Finally, lets send an email to the admin to let him know that winners have been drawn
  71. $message = sprintf(
  72. 'The following users has been selected winners: %s.',
  73. $admin_email
  74. );
  75.  
  76. wp_mail( get_option( 'admin_email' ), 'In-Game Lottery Win!', $message );
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement