<?php // Die if accessed directly if (!defined('ABSPATH')) { die; } $coupon = new WPSC_Coupon($_GET['coupon']); ?> <div class="wrap" id="coupon_data"> <div id="edit_coupon_box"> <h2><?php _e('Edit Coupon', 'wpsc'); ?> </h2> <script type='text/javascript'> jQuery(".pickdate").datepicker(); /* jQuery datepicker selector */ if (typeof jQuery('.pickdate').datepicker != "undefined") { jQuery('.pickdate').datepicker({ dateFormat: 'yy-mm-dd' }); } </script> <form name='edit_coupon' method="post" action="<?php echo admin_url('edit.php?post_type=wpsc-product&page=wpsc-edit-coupons'); ?> "> <table class="form-table"> <tbody> <?php do_action('wpsc_coupon_edit_top', $coupon->get('id'), $coupon->get_data());
function wpsc_display_coupons_page() { global $wpdb; /** * Update / create code that will be abstracted to its own class at some point */ if (isset($_POST) && is_array($_POST) && !empty($_POST)) { if (isset($_POST['add_coupon']) && (!isset($_POST['is_edit_coupon']) || !($_POST['is_edit_coupon'] == 'true'))) { check_admin_referer('wpsc_coupon', 'wpsc-coupon-add'); $coupon_code = $_POST['add_coupon_code']; $discount = (double) $_POST['add_discount']; $discount_type = (int) $_POST['add_discount_type']; $use_once = (int) (bool) $_POST['add_use-once']; $every_product = (int) (bool) $_POST['add_every_product']; $is_active = (int) (bool) $_POST['add_active']; $start_date = !empty($_POST['add_start']) ? date('Y-m-d', strtotime($_POST['add_start'])) . " 00:00:00" : null; $end_date = !empty($_POST['add_end']) ? date('Y-m-d', strtotime($_POST['add_end'])) . " 23:59:59" : null; $rules = $_POST['rules']; $new_rules = array(); foreach ($rules as $key => $rule) { foreach ($rule as $k => $r) { $new_rules[$k][$key] = $r; } } foreach ($new_rules as $key => $rule) { if ('' == $rule['value']) { unset($new_rules[$key]); } } $new_coupon = new WPSC_Coupon(array('coupon_code' => $coupon_code, 'value' => $discount, 'is-percentage' => $discount_type, 'use-once' => $use_once, 'is-used' => 0, 'active' => $is_active, 'every_product' => $every_product, 'start' => $start_date, 'expiry' => $end_date, 'condition' => $new_rules)); $insert = $new_coupon->save(); if ($insert) { echo '<div class="updated"><p>' . __('The coupon has been added.', 'wpsc') . '</p></div>'; } } // update an existing coupon if (isset($_POST['is_edit_coupon']) && $_POST['is_edit_coupon'] == 'true' && !isset($_POST['delete_condition']) && !isset($_POST['submit_condition'])) { check_admin_referer('wpsc_coupon', 'wpsc-coupon-edit'); $rules = isset($_POST['rules']) ? $_POST['rules'] : array(); $new_rules = array(); foreach ($rules as $key => $rule) { foreach ($rule as $k => $r) { $new_rules[$k][$key] = $r; } } foreach ($new_rules as $key => $rule) { if ('' == $rule['value']) { unset($new_rules[$key]); } } $update_coupon = new WPSC_Coupon($_POST['coupon_id']); $update_coupon->set(array('coupon_code' => $_POST['edit_coupon_code'], 'value' => $_POST['edit_coupon_amount'], 'is-percentage' => $_POST['edit_discount_type'], 'use-once' => $_POST['edit_coupon_use_once'], 'is-used' => $_POST['edit_coupon_is_used'], 'active' => $_POST['edit_coupon_active'], 'every_product' => $_POST['edit_coupon_every_product'], 'start' => !empty($_POST['edit_coupon_start']) ? get_gmt_from_date($_POST['edit_coupon_start'] . ' 00:00:00') : null, 'expiry' => !empty($_POST['edit_coupon_end']) ? get_gmt_from_date($_POST['edit_coupon_end'] . ' 23:59:59') : null, 'condition' => $new_rules)); $update = $update_coupon->save(); if ($update) { echo '<div class="updated"><p>' . __('The coupon has been updated.', 'wpsc') . '</p></div>'; } } } /** * Load the selected view */ if (isset($_GET['wpsc-action']) && $_GET['wpsc-action'] == 'add_coupon') { // load the coupon add screen include dirname(__FILE__) . '/display-coupon-add.php'; } elseif (isset($_GET['wpsc-action']) && $_GET['wpsc-action'] == 'edit_coupon') { // load the coupon add screen include dirname(__FILE__) . '/display-coupon-edit.php'; } else { require_once WPSC_FILE_PATH . '/wpsc-admin/includes/coupon-list-table-class.php'; $coupons_table = new WPSC_Coupons_List_Table(); $coupons_table->prepare_items(); ?> <div class="wrap"> <h2><?php _e('Coupons', 'wpsc'); ?> <a href="<?php echo esc_url(add_query_arg('wpsc-action', 'add_coupon')); ?> " class="add-new-h2"><?php _e('Add Coupon', 'wpsc'); ?> </a></h2> <?php do_action('wpsc_coupons_page_top'); ?> <form id="wpsc-coupons-filter" method="get" action="<?php echo admin_url('edit.php?post_type=wpsc-product&page=wpsc-edit-coupons'); ?> "> <input type="hidden" name="post_type" value="wpsc-product" /> <input type="hidden" name="page" value="wpsc-edit-coupons" /> <?php $coupons_table->views(); ?> <?php $coupons_table->display(); ?> </form> <?php do_action('wpsc_coupons_page_bottom'); ?> </div> <?php } // end view check }
/** * Delete a coupon * * @since 3.8 */ function wpsc_delete_coupon() { global $wpdb; check_admin_referer('delete-coupon'); $deleted = 0; if (isset($_GET['delete_id'])) { $coupon = new WPSC_Coupon($_GET['delete_id']); $coupon->delete(); $deleted = 1; } $sendback = wp_get_referer(); if ($deleted) { $sendback = add_query_arg('deleted', $deleted, $sendback); } $sendback = remove_query_arg(array('deleteid', 'wpsc_admin_action'), $sendback); wp_redirect(esc_url_raw($sendback)); exit; }
function _wpsc_process_transaction_coupon($purchase_log) { global $wpdb; if (!is_object($purchase_log)) { $purchase_log = new WPSC_Purchase_Log($purchase_log); } $discount_data = $purchase_log->get('discount_data'); if (!empty($discount_data)) { $coupon_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPSC_TABLE_COUPON_CODES . "` WHERE coupon_code = %s LIMIT 1", $discount_data), ARRAY_A); $coupon = new WPSC_Coupon($coupon_data['id']); $coupon->used(); } }
function test_active() { $coupon = new WPSC_Coupon(self::ACTIVE_COUPON_ID); $this->assertTrue($coupon->is_active()); $coupon->deactivate(); $this->assertFalse($coupon->is_active()); $coupon->activate(); $this->assertTrue($coupon->is_active()); }
/** * Retrieve all the data for all the discount codes * * @access private * @since 3.8.10 * @return array */ public function coupons_data() { global $wpdb; $coupons_data = array(); if (isset($_GET['paged'])) { $page = $_GET['paged']; } else { $page = 1; } $per_page = $this->per_page; $offset = ($page - 1) * $this->per_page; $status = isset($_GET['status']) ? absint($_GET['status']) : false; $where = $status !== false ? "WHERE active = {$status}" : ''; $order = isset($_GET['order']) && strtoupper($_GET['order']) === 'ASC' ? 'ASC' : 'DESC'; $limit = " LIMIT {$offset},{$per_page};"; $coupons = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_COUPON_CODES . "` {$where} ORDER BY id {$order} {$limit} ", ARRAY_A); if ($coupons) { foreach ($coupons as $data) { $coupon = new WPSC_Coupon(array('id' => $data['id'], 'coupon_code' => $data['coupon_code'], 'value' => $data['value'], 'is-percentage' => $data['is-percentage'], 'start' => $data['start'], 'expiry' => $data['expiry'], 'active' => $data['active'])); // Re-map data to array for legacy handling of this method's return data. // (would be nicer to return an object?) $coupons_data[] = array('ID' => $coupon->get('id'), 'coupon' => $coupon->get('coupon_code'), 'discount' => $coupon->get('value'), 'type' => $coupon->get('is-percentage'), 'start' => $coupon->get('start'), 'expiry' => $coupon->get('expiry'), 'status' => $coupon->get('active') == 1 ? 'active' : 'inactive'); } } return $coupons_data; }