/** * Return a singleton instance * * @return self */ public static function init() { if (!self::$_instance) { self::$_instance = new WPUF_Coupons(); } return self::$_instance; }
function coupon_discount($coupon_code, $amount, $pack_id) { if (empty($coupon_code)) { return $amount; } $coupon = get_page_by_title($coupon_code, 'OBJECT', 'wpuf_coupon'); $coupon_meta = WPUF_Coupons::init()->get_coupon_meta($coupon->ID); $coupon_usage = get_post_meta($coupon->ID, 'coupon_usage', true); $coupon_usage = count($coupon_usage); $start_date = date('Y-d-m', strtotime($coupon_meta['start_date'])); $end_date = date('Y-d-m', strtotime($coupon_meta['end_date'])); $today = date('Y-d-m', time()); $current_use_email = wp_get_current_user()->user_email; if (empty($coupon_meta['amount']) || $coupon_meta['amount'] == 0) { return $amount; } if ($coupon_meta['package'] != 'all' && $coupon_meta['package'] != $pack_id) { return $amount; } if ($coupon_meta['usage_limit'] < $coupon_usage) { return $amount; } if ($start_date > $today && $today > $end_date) { return $amount; } if (count($coupon_meta['access']) && !in_array($current_use_email, $coupon_meta['access'])) { return $amount; } if ($coupon_meta['type'] == 'amount') { $new_amount = $amount - $coupon_meta['amount']; } else { $new_amount = $amount * $coupon_meta['amount'] / 100; } if ($new_amount >= 0) { return $new_amount; } return $amount; }
/** * Print the main settings form * * @return void */ function settings_form() { global $post; $coupon = WPUF_Coupons::init()->get_coupon_meta($post->ID); $start_date = !empty($coupon['start_date']) ? date_i18n('M j, Y', strtotime($coupon['start_date'])) : ''; $end_date = !empty($coupon['end_date']) ? date_i18n('M j, Y', strtotime($coupon['end_date'])) : ''; $access = !empty($coupon['access']) ? $coupon['access'] : array(); $access = implode("\n", $access); ?> <style> .chosen-container-multi .chosen-choices { height: 30px !important; } </style> <table class="form-table" style="width: 100%"> <tbody> <input type="hidden" name="wpuf_coupon" id="wpuf_coupon_editor" value="<?php echo wp_create_nonce('wpuf_coupon_editor'); ?> " /> <?php do_action('wpuf_admin_coupon_form_top', $post->ID, $coupon); ?> <tr valign="top"> <td scope="row" class="label" for="wpuf-type"><span><?php _e('Type', 'wpuf'); ?> </span></td> <td> <select id="wpuf-type" name="type"> <option value="amount" <?php selected($coupon['type'], 'amount'); ?> ><?php _e('Fixed Price', 'wpuf'); ?> </option> <option value="percent" <?php selected($coupon['type'], 'percent'); ?> ><?php _e('Percentage', 'wpuf'); ?> </option> </select> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-amount"><?php _e('Amount', 'wpuf'); ?> </label></td> <td> <input type="text" size="25" id="wpuf-amount" value="<?php echo esc_attr($coupon['amount']); ?> " name="amount" /> <p class="description"><?php _e('Amount without <code>%</code> or currency symbol', 'wpuf'); ?> </p> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-content"><?php _e('Description', 'wpuf'); ?> </label></td> <td> <textarea cols="45" rows="3" id="wpuf-content" name="post_content"><?php echo esc_textarea($post->post_content); ?> </textarea> <p class="description"><?php _e('Give a description of this coupon', 'wpuf'); ?> </p> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-package"><?php _e('Package', 'wpuf'); ?> </label></td> <td> <select id="wpuf-package" multiple name="package[]" style="height: 100px !important;"><?php echo $this->get_pack_dropdown($coupon['package']); ?> </select> <p class="description"><?php _e('Select one or more packages to apply coupon', 'wpuf'); ?> </p> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-usage-limit"><?php _e('Usage Limit', 'wpuf'); ?> </label></td> <td> <input type="text" size="25" id="wpuf-usage-limit" value="<?php echo esc_attr($coupon['usage_limit']); ?> " name="usage_limit" /> <p class="description"><?php _e('How many times the coupon can be used? Give a numeric value.', 'wpuf'); ?> </p> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-validity"><?php _e('Validity', 'wpuf'); ?> </label></td> <td> <input type="text" class="wpuf-date-picker" placeholder="<?php _e('Start date', 'wpuf'); ?> " size="25" id="" value="<?php echo esc_attr($start_date); ?> " name="start_date" /> <input type="text" class="wpuf-date-picker" placeholder="<?php _e('End date', 'wpuf'); ?> " size="25" id="" value="<?php echo esc_attr($end_date); ?> " name="end_date" /> <span class="description"></span> </td> </tr> <tr valign="top"> <td scope="row" class="label"><label for="wpuf-trial-priod"><?php _e('Email Restriction', 'wpuf'); ?> </label></td> <td> <textarea type="text" size="25" id="wpuf-trial-priod" name="access" /><?php echo esc_attr($access); ?> </textarea> <p class="description"><?php _e('Only users with these email addresses will be able to use this coupon. Enter Email addresses. One per each line.', 'wpuf'); ?> </p> </td> </tr> <?php do_action('wpuf_admin_coupon_form_bottom', $post->ID, $coupon); ?> </tbody> </table> <script type="text/javascript"> jQuery( function($) { $('.wpuf-date-picker').datepicker(); $('#wpuf-package').chosen({'width':'250px'}); }); </script> <?php }
/** * Prepare the payment form and send to paypal * * @since 0.8 * @param array $data payment info */ function prepare_to_send($data) { $user_id = $data['user_info']['id']; $listener_url = add_query_arg('action', 'wpuf_paypal_success', home_url('/')); $redirect_page_id = wpuf_get_option('payment_success', 'wpuf_payment'); if ($redirect_page_id) { $return_url = add_query_arg('action', 'wpuf_paypal_success', get_permalink($redirect_page_id)); } else { $return_url = add_query_arg('action', 'wpuf_paypal_success', get_permalink(wpuf_get_option('subscription_page', 'wpuf_payment'))); } $billing_amount = empty($data['price']) ? 0 : number_format($data['price'], 2); if (isset($_POST['coupon_id']) && !empty($_POST['coupon_id'])) { $billing_amount = WPUF_Coupons::init()->discount($billing_amount, $_POST['coupon_id'], $data['item_number']); $coupon_id = $_POST['coupon_id']; } else { $coupon_id = ''; } if ($billing_amount == 0) { WPUF_Subscription::init()->new_subscription($user_id, $data['item_number'], $profile_id = null, false, 'free'); wp_redirect($return_url); exit; } if ($data['type'] == 'pack' && $data['custom']['recurring_pay'] == 'yes') { $trial_cost = !empty($data['custom']['trial_cost']) ? number_format($data['custom']['trial_cost']) : '0'; if ($data['custom']['cycle_period'] == "day") { $period = "D"; } elseif ($data['custom']['cycle_period'] == "week") { $period = "W"; } elseif ($data['custom']['cycle_period'] == "month") { $period = "M"; } elseif ($data['custom']['cycle_period'] == "year") { $period = "Y"; } if ($data['custom']['trial_duration_type'] == "day") { $trial_period = "D"; } elseif ($data['custom']['trial_duration_type'] == "week") { $trial_period = "W"; } elseif ($data['custom']['trial_duration_type'] == "month") { $trial_period = "M"; } elseif ($data['custom']['trial_duration_type'] == "year") { $trial_period = "Y"; } $paypal_args = array('cmd' => '_xclick-subscriptions', 'business' => wpuf_get_option('paypal_email', 'wpuf_payment'), 'a3' => $billing_amount, 'p3' => !empty($data['custom']['billing_cycle_number']) ? $data['custom']['billing_cycle_number'] : '0', 't3' => $period, 'item_name' => $data['custom']['post_title'], 'custom' => json_encode(array('billing_amount' => $billing_amount, 'trial_cost' => $trial_cost, 'type' => $data['type'], 'user_id' => $user_id, 'coupon_id' => $coupon_id)), 'no_shipping' => '1', 'shipping' => '0', 'no_note' => '1', 'currency_code' => $data['currency'], 'item_number' => $data['item_number'], 'charset' => 'UTF-8', 'rm' => '2', 'return' => $return_url, 'notify_url' => $listener_url, 'src' => '1', 'sra' => '1', 'srt' => intval($data['custom']['billing_limit'])); if ($data['custom']['trial_status'] == 'yes') { $paypal_args['a1'] = $trial_cost; $paypal_args['p1'] = $data['custom']['trial_duration']; $paypal_args['t1'] = $trial_period; } } else { $paypal_args = array('cmd' => '_xclick', 'business' => wpuf_get_option('paypal_email', 'wpuf_payment'), 'amount' => $billing_amount, 'item_name' => isset($data['custom']['post_title']) ? $data['custom']['post_title'] : $data['item_name'], 'no_shipping' => '1', 'shipping' => '0', 'no_note' => '1', 'currency_code' => $data['currency'], 'item_number' => $data['item_number'], 'charset' => 'UTF-8', 'rm' => '2', 'custom' => json_encode(array('type' => $data['type'], 'user_id' => $user_id, 'coupon_id' => $coupon_id)), 'return' => $return_url, 'notify_url' => $listener_url); } $this->set_mode(); $paypal_url = $this->gateway_url . http_build_query($paypal_args); wp_redirect($paypal_url); exit; }
public function instantiate() { WPUF_Login::init(); new WPUF_Frontend_Form_Profile(); if (is_admin()) { new WPUF_Updates(); new WPUF_Admin_Posting_Profile(); new WPUF_Admin_Coupon(); WPUF_Coupons::init(); } }
/** * Instantiate the classes * * @return void */ function instantiate() { // var_dump( get_option( 'wpuf_payment' ) ); new WPUF_Upload(); new WPUF_Frontend_Form_Profile(); new WPUF_Payment(); WPUF_Frontend_Form_Post::init(); // requires for form preview WPUF_Login::init(); WPUF_Subscription::init(); WPUF_Coupons::init(); if (is_admin()) { WPUF_Admin_Settings::init(); new WPUF_Admin_Form(); new WPUF_Admin_Posting(); new WPUF_Admin_Posting_Profile(); new WPUF_Admin_Coupon(); new WPUF_Updates(); new WPUF_Admin_Subscription(); new WPUF_Admin_Installer(); } else { new WPUF_Frontend_Dashboard(); } }