Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'mycred_create_bulk_coupons', 'mycred_render_bulk_coupon_creation' );
- function mycred_render_bulk_coupon_creation() {
- if ( ! current_user_can( 'manage_options' ) ) return 'You do not create coupons.';
- if ( ! function_exists( 'mycred_create_new_coupon' ) ) return 'The Coupon add-on is not enabled!';
- if ( isset( $_POST['mycred_coupon_bulk'] ) && isset( $_POST['cc-token'] ) && wp_verify_nonce( $_POST['cc-token'], 'mycred-coupon-bulk-create' ) ) {
- $number = $_POST['mycred_coupon_bulk']['number'];
- $value = $_POST['mycred_coupon_bulk']['value'];
- $expire = $_POST['mycred_coupon_bulk']['expire'];
- $global_limit = $_POST['mycred_coupon_bulk']['global'];
- $user_limit = $_POST['mycred_coupon_bulk']['user'];
- $min_balance = $_POST['mycred_coupon_bulk']['min'];
- $max_balance = $_POST['mycred_coupon_bulk']['max'];
- $views = array();
- $ids = array();
- add_filter( 'mycred_get_unique_coupon_code', 'mycred_custom_coupon_codes' );
- for ( $i = 0; $i < $number; $i++ ) {
- $post_id = mycred_create_new_coupon( array(
- 'value' => $value,
- 'global_max' => $global_limit,
- 'user_max' => $user_limit,
- 'min_balance' => $min_balance,
- 'max_balance' => $max_balance,
- 'expires' => $expire
- ) );
- $ids[] = $post_id;
- if ( isset( $_POST['mycred_coupon_bulk']['view'] ) )
- $views[] = get_the_title( $post_id );
- }
- }
- ob_start();
- ?>
- <h3>Bulk Coupon Creator</h3>
- <?php if ( ! empty( $ids ) ) : ?>
- <h4><?php printf( '%d Coupons were successfully created.', count( $ids ) ); ?></h4>
- <?php if ( ! empty( $views ) ) : ?>
- <p>Your coupon codes are:</p>
- <pre><?php echo implode( ' ', $views ); ?></pre>
- <?php endif; ?>
- <?php else : ?>
- <form method="post" action="">
- <input type="hidden" name="cc-token" value="<?php echo wp_create_nonce( 'mycred-coupon-bulk-create' ); ?>" />
- <p>
- <label for="bulk-number">Teacher Prefix</label><br />
- <input type="text" name="mycred_coupon_bulk[prefix]" id="bulk-number" value="" size="8" />
- </p>
- <p>
- <label for="bulk-number">Number of Coupons to create</label><br />
- <input type="text" name="mycred_coupon_bulk[number]" id="bulk-number" value="1" size="8" />
- </p>
- <p>
- <label for="bulk-view"><input type="checkbox" name="mycred_coupon_bulk[view]" id="bulk-view" value="1" /> View Coupon codes when finished.</label>
- </p>
- <p>
- <label for="bulk-value">Coupon Value</label><br />
- <input type="text" name="mycred_coupon_bulk[value]" id="bulk-value" value="1" size="8" />
- </p>
- <p>
- <label for="bulk-expire">Expiry Date</label><br />
- <input type="text" name="mycred_coupon_bulk[expire]" id="bulk-expire" value="" size="20" placeholder="YYYY-MM-DD" />
- </p>
- <p>
- <label for="bulk-global">Global Limit</label><br />
- <input type="text" name="mycred_coupon_bulk[global]" id="bulk-global" value="1" size="8" />
- </p>
- <p>
- <label for="bulk-user">User Limit</label><br />
- <input type="text" name="mycred_coupon_bulk[user]" id="bulk-user" value="1" size="8" />
- </p>
- <p>
- <label for="bulk-min">Minimum Balance Requirement</label><br />
- <input type="text" name="mycred_coupon_bulk[min]" id="bulk-min" value="0" size="8" />
- </p>
- <p>
- <label for="bulk-max">Maximum Balance Requirement</label><br />
- <input type="text" name="mycred_coupon_bulk[max]" id="bulk-max" value="0" size="8" />
- </p>
- <p><input type="submit" class="button button-primary btn btn-primary btn-lg" value="Generate" /></p>
- </form>
- <?php endif; ?>
- <?php
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- function mycred_custom_coupon_codes( $code ) {
- global $wpdb;
- $prefix = strtoupper( $_POST['mycred_coupon_bulk']['prefix'] );
- do {
- $id = $prefix . rand( 123456, 999999 );
- $query = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s;", $id, 'mycred_coupon' ) );
- } while ( ! empty( $query ) );
- return $id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement