Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Convert myCRED Points into myCRED Coupon
- * Requires myCRED 1.5 or higher!
- * @version 1.1
- */
- add_shortcode( 'mycred_to_mycred_coupon', 'mycred_pro_render_points_to_mycred_coupon' );
- function mycred_pro_render_points_to_mycred_coupon( $atts, $content = NULL ) {
- // Users must be logged in
- if ( ! is_user_logged_in() )
- return 'You must be logged in to generate store coupons.';
- // myCRED must be enabled
- if ( ! function_exists( 'mycred_get_unique_coupon_code' ) )
- return 'myCRED must be enabled to use this shortcode';
- extract( shortcode_atts( array(
- 'min' => 1,
- 'type' => 'mycred_default',
- 'button_label' => 'Create Coupon'
- ), $atts ) );
- // Load myCRED
- $mycred = mycred( $type );
- // Prep
- $error = $code = false;
- $output = '';
- $user_id = get_current_user_id();
- // No need to show this for excluded users
- if ( $mycred->exclude_user( $user_id ) ) return;
- $balance = $mycred->get_users_balance( $user_id );
- // Minimum
- if ( $mycred->amount( $min ) > 0 && $balance < $mycred->amount( $min ) )
- return '<o>You are required to have at least 1 point.</p>';
- // Form submission
- if ( isset( $_POST['mycred_to_mycred'] ) && wp_verify_nonce( $_POST['mycred_to_mycred']['token'], 'points-to-mycred-coupon' ) ) {
- // Make sure amounts are always positive
- $amount = abs( $_POST['mycred_to_mycred']['amount'] );
- $amount = $mycred->number( $amount );
- // Make sure amount is not zero
- if ( $amount == $mycred->zero() )
- $error = 'Amount can not be zero';
- if ( $amount < $mycred->number( $min ) )
- $error = 'You must convert minimum 1 point!';
- // Make sure user has enough points
- if ( $amount > $balance )
- $error = 'Insufficient Funds. Please try a lower amount';
- // If no errors
- if ( $error === false ) {
- // Create myCRED Coupon
- $code = mycred_get_unique_coupon_code();
- $new_coupon_id = mycred_create_new_coupon( array(
- 'code' => $code,
- 'value' => $amount,
- 'global_max' => 1,
- 'user_max' => 1,
- 'min_balance' => 0,
- 'max_balance' => 0,
- 'expires' => ''
- ) );
- // Deduct points from user
- $mycred->add_creds(
- 'points_to_coupon',
- $user_id,
- 0-$amount,
- '%plural% conversion into store coupon: %post_title%',
- $new_coupon_id,
- array( 'ref_type' => 'post', 'code' => $code ),
- $type
- );
- $balance = $balance-$amount;
- $balance = $mycred->number( $balance );
- // Send coupon as an email just in case
- $user = wp_get_current_user();
- wp_mail( $user->user_email, 'Your Coupon Code', 'This is your new store coupon code: ' . $code );
- }
- }
- // Show users current balance
- $output .= '
- <p>Your current balance is: ' . $mycred->format_creds( $balance ) . '</p>';
- // Error
- if ( $error !== false )
- $output .= '<p style="color:red;">' . $error . '</p>';
- // Success
- elseif ( $code !== false )
- $output .= '<p>Your coupon code is: <strong>' . $code . '</strong></p>';
- // The form for those who have points
- if ( $balance > $mycred->zero() )
- $output .= '
- <form action="" method="post">
- <input type="hidden" name="mycred_to_mycred[token]" value="' . wp_create_nonce( 'points-to-mycred-coupon' ) . '" />
- <label>Amount</label>
- <input type="text" size="5" name="mycred_to_mycred[amount]" value="" />
- <input type="submit" name="submit" value="' . $button_label . '" />
- </form>';
- // Not enough points
- else
- $output .= '<p>Not enough points to create coupons.</p>';
- return $output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement