Advertisement
designbymerovingi

myCRED to myCRED Coupon

May 9th, 2016
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. /**
  2.  * Convert myCRED Points into myCRED Coupon
  3.  * Requires myCRED 1.5 or higher!
  4.  * @version 1.1
  5.  */
  6. add_shortcode( 'mycred_to_mycred_coupon', 'mycred_pro_render_points_to_mycred_coupon' );
  7. function mycred_pro_render_points_to_mycred_coupon( $atts, $content = NULL ) {
  8.  
  9.     // Users must be logged in
  10.     if ( ! is_user_logged_in() )
  11.         return 'You must be logged in to generate store coupons.';
  12.  
  13.     // myCRED must be enabled
  14.     if ( ! function_exists( 'mycred_get_unique_coupon_code' ) )
  15.         return 'myCRED must be enabled to use this shortcode';
  16.  
  17.     extract( shortcode_atts( array(
  18.         'min'          => 1,
  19.         'type'         => 'mycred_default',
  20.         'button_label' => 'Create Coupon'
  21.     ), $atts ) );
  22.  
  23.     // Load myCRED
  24.     $mycred = mycred( $type );
  25.  
  26.     // Prep
  27.     $error   = $code = false;
  28.     $output  = '';
  29.     $user_id = get_current_user_id();
  30.  
  31.     // No need to show this for excluded users
  32.     if ( $mycred->exclude_user( $user_id ) ) return;
  33.  
  34.     $balance = $mycred->get_users_balance( $user_id );
  35.  
  36.     // Minimum
  37.     if ( $mycred->amount( $min ) > 0 && $balance < $mycred->amount( $min ) )
  38.         return '<o>You are required to have at least 1 point.</p>';
  39.  
  40.     // Form submission
  41.     if ( isset( $_POST['mycred_to_mycred'] ) && wp_verify_nonce( $_POST['mycred_to_mycred']['token'], 'points-to-mycred-coupon' ) ) {
  42.  
  43.         // Make sure amounts are always positive
  44.         $amount = abs( $_POST['mycred_to_mycred']['amount'] );
  45.         $amount = $mycred->number( $amount );
  46.  
  47.         // Make sure amount is not zero
  48.         if ( $amount == $mycred->zero() )
  49.             $error = 'Amount can not be zero';
  50.  
  51.         if ( $amount < $mycred->number( $min ) )
  52.             $error = 'You must convert minimum 1 point!';
  53.  
  54.         // Make sure user has enough points
  55.         if ( $amount > $balance )
  56.             $error = 'Insufficient Funds. Please try a lower amount';
  57.  
  58.         // If no errors
  59.         if ( $error === false ) {
  60.  
  61.             // Create myCRED Coupon
  62.             $code          = mycred_get_unique_coupon_code();
  63.             $new_coupon_id = mycred_create_new_coupon( array(
  64.                 'code'        => $code,
  65.                 'value'       => $amount,
  66.                 'global_max'  => 1,
  67.                 'user_max'    => 1,
  68.                 'min_balance' => 0,
  69.                 'max_balance' => 0,
  70.                 'expires'     => ''
  71.             ) );
  72.  
  73.             // Deduct points from user
  74.             $mycred->add_creds(
  75.                 'points_to_coupon',
  76.                 $user_id,
  77.                 0-$amount,
  78.                 '%plural% conversion into store coupon: %post_title%',
  79.                 $new_coupon_id,
  80.                 array( 'ref_type' => 'post', 'code' => $code ),
  81.                 $type
  82.             );
  83.  
  84.             $balance = $balance-$amount;
  85.             $balance = $mycred->number( $balance );
  86.  
  87.             // Send coupon as an email just in case
  88.             $user = wp_get_current_user();
  89.             wp_mail( $user->user_email, 'Your Coupon Code', 'This is your new store coupon code: ' . $code );
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95.     // Show users current balance
  96.     $output .= '
  97. <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
  98.  
  99.     // Error
  100.     if ( $error !== false )
  101.         $output .= '<p style="color:red;">' . $error . '</p>';
  102.  
  103.     // Success
  104.     elseif ( $code !== false )
  105.         $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
  106.  
  107.     // The form for those who have points
  108.     if ( $balance > $mycred->zero() )
  109.         $output .= '
  110. <form action="" method="post">
  111.     <input type="hidden" name="mycred_to_mycred[token]" value="' . wp_create_nonce( 'points-to-mycred-coupon' ) . '" />
  112.     <label>Amount</label>
  113.     <input type="text" size="5" name="mycred_to_mycred[amount]" value="" />
  114.     <input type="submit" name="submit" value="' . $button_label . '" />
  115. </form>';
  116.  
  117.     // Not enough points
  118.     else
  119.         $output .= '<p>Not enough points to create coupons.</p>';
  120.  
  121.     return $output;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement