/**
 * Saves an edited discount
 *
 * @since 1.0
 * @param array $data Discount code data
 * @return void
 */
function edd_edit_discount($data)
{
    if (isset($data['edd-discount-nonce']) && wp_verify_nonce($data['edd-discount-nonce'], 'edd_discount_nonce')) {
        // Setup the discount code details
        $discount = array();
        foreach ($data as $key => $value) {
            if ($key != 'edd-discount-nonce' && $key != 'edd-action' && $key != 'discount-id' && $key != 'edd-redirect') {
                if (is_string($value) || is_int($value)) {
                    $discount[$key] = strip_tags(addslashes($value));
                } elseif (is_array($value)) {
                    $discount[$key] = array_map('absint', $value);
                }
            }
        }
        $old_discount = edd_get_discount_by('code', $data['code']);
        $discount['uses'] = edd_get_discount_uses($old_discount->ID);
        if (edd_store_discount($discount, $data['discount-id'])) {
            wp_redirect(add_query_arg('edd-message', 'discount_updated', $data['edd-redirect']));
            edd_die();
        } else {
            wp_redirect(add_query_arg('edd-message', 'discount_update_failed', $data['edd-redirect']));
            edd_die();
        }
    }
}
/**
 * Saves an edited discount
 *
 * @since 1.0
 * @param array $data Discount code data
 * @return void
 */
function edd_edit_discount($data)
{
    if (!isset($data['edd-discount-nonce']) || !wp_verify_nonce($data['edd-discount-nonce'], 'edd_discount_nonce')) {
        return;
    }
    if (!current_user_can('manage_shop_discounts')) {
        wp_die(__('You do not have permission to edit discount codes', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    // Setup the discount code details
    $discount = array();
    foreach ($data as $key => $value) {
        if ($key != 'edd-discount-nonce' && $key != 'edd-action' && $key != 'discount-id' && $key != 'edd-redirect') {
            if (is_string($value) || is_int($value)) {
                $discount[$key] = strip_tags(addslashes($value));
            } elseif (is_array($value)) {
                $discount[$key] = array_map('absint', $value);
            }
        }
    }
    $old_discount = edd_get_discount_by('code', $data['code']);
    $discount['uses'] = edd_get_discount_uses($old_discount->ID);
    if (edd_store_discount($discount, $data['discount-id'])) {
        wp_redirect(add_query_arg('edd-message', 'discount_updated', $data['edd-redirect']));
        edd_die();
    } else {
        wp_redirect(add_query_arg('edd-message', 'discount_update_failed', $data['edd-redirect']));
        edd_die();
    }
}
/**
 * Get Discount By Code
 *
 * Retrieves all details for a discount by its code.
 *
 * @param string $code
 *
 * @since       1.0
 * @return      int
 */
function edd_get_discount_by_code($code = '')
{
    if (empty($code) || !is_string($code)) {
        return false;
    }
    return edd_get_discount_by('code', $code);
}
/**
 * Insert Payment
 *
 * @since 1.0
 * @param array $payment_data
 * @return int|bool Payment ID if payment is inserted, false otherwise
 */
function edd_insert_payment($payment_data = array())
{
    if (empty($payment_data)) {
        return false;
    }
    // Make sure the payment is inserted with the correct timezone
    date_default_timezone_set(edd_get_timezone_id());
    // Construct the payment title
    if (isset($payment_data['user_info']['first_name']) || isset($payment_data['user_info']['last_name'])) {
        $payment_title = $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'];
    } else {
        $payment_title = $payment_data['user_email'];
    }
    // Retrieve the ID of the discount used, if any
    if ($payment_data['user_info']['discount'] != 'none') {
        $discount = edd_get_discount_by('code', $payment_data['user_info']['discount']);
    }
    // Find the next payment number, if enabled
    if (edd_get_option('enable_sequential')) {
        $number = edd_get_next_payment_number();
    }
    $args = apply_filters('edd_insert_payment_args', array('post_title' => $payment_title, 'post_status' => isset($payment_data['status']) ? $payment_data['status'] : 'pending', 'post_type' => 'edd_payment', 'post_parent' => isset($payment_data['parent']) ? $payment_data['parent'] : null, 'post_date' => isset($payment_data['post_date']) ? $payment_data['post_date'] : null, 'post_date_gmt' => isset($payment_data['post_date']) ? get_gmt_from_date($payment_data['post_date']) : null), $payment_data);
    // Create a blank payment
    $payment = wp_insert_post($args);
    if ($payment) {
        if (isset($payment_data['tax'])) {
            $cart_tax = $payment_data['tax'];
        } else {
            $taxes = $payment_data['cart_details'] ? wp_list_pluck($payment_data['cart_details'], 'tax') : array();
            $cart_tax = array_sum($taxes);
            $cart_tax += edd_get_cart_fee_tax();
        }
        $payment_meta = array('currency' => $payment_data['currency'], 'downloads' => $payment_data['downloads'], 'user_info' => $payment_data['user_info'], 'cart_details' => $payment_data['cart_details']);
        $mode = edd_is_test_mode() ? 'test' : 'live';
        $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : '';
        $gateway = empty($gateway) && isset($_POST['edd-gateway']) ? $_POST['edd-gateway'] : $gateway;
        if (!$payment_data['price']) {
            // Ensures the _edd_payment_total meta key is created for purchases with an amount of 0
            $payment_data['price'] = '0.00';
        }
        // Create or update a customer
        $customer = new EDD_Customer($payment_data['user_email']);
        $customer_data = array('name' => $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'], 'email' => $payment_data['user_email'], 'user_id' => $payment_data['user_info']['id']);
        if (empty($customer->id)) {
            $customer->create($customer_data);
        } else {
            // Only update the customer if their name or email has changed
            if ($customer_data['email'] !== $customer->email || $customer_data['name'] !== $customer->name) {
                // We shouldn't be updating the User ID here, that is an admin task
                unset($customer_data['user_id']);
                $customer->update($customer_data);
            }
        }
        $customer->attach_payment($payment, false);
        // Record the payment details
        edd_update_payment_meta($payment, '_edd_payment_meta', apply_filters('edd_payment_meta', $payment_meta, $payment_data));
        edd_update_payment_meta($payment, '_edd_payment_user_id', $payment_data['user_info']['id']);
        edd_update_payment_meta($payment, '_edd_payment_customer_id', $customer->id);
        edd_update_payment_meta($payment, '_edd_payment_user_email', $payment_data['user_email']);
        edd_update_payment_meta($payment, '_edd_payment_user_ip', edd_get_ip());
        edd_update_payment_meta($payment, '_edd_payment_purchase_key', $payment_data['purchase_key']);
        edd_update_payment_meta($payment, '_edd_payment_total', $payment_data['price']);
        edd_update_payment_meta($payment, '_edd_payment_mode', $mode);
        edd_update_payment_meta($payment, '_edd_payment_gateway', $gateway);
        edd_update_payment_meta($payment, '_edd_payment_tax', $cart_tax);
        if (!empty($discount)) {
            edd_update_payment_meta($payment, '_edd_payment_discount_id', $discount->ID);
        }
        if (edd_get_option('enable_sequential')) {
            edd_update_payment_meta($payment, '_edd_payment_number', edd_format_payment_number($number));
            update_option('edd_last_payment_number', $number);
        }
        // Clear the user's purchased cache
        delete_transient('edd_user_' . $payment_data['user_info']['id'] . '_purchases');
        do_action('edd_insert_payment', $payment, $payment_data);
        return $payment;
        // Return the ID
    }
    // Return false if no payment was inserted
    return false;
}
Ejemplo n.º 5
0
 /**
  * Create the NOD discount.
  *
  * This function generates the EDD Discount for the NOD offer using the preferences
  * that admin has defined in settings for expiry etc.
  * 
  * @params	int		$purchase_id	Required: The EDD purchase ID
  *
  * @return	obj|bool		The discount (WP_Post) object if successful, false on failure.
  */
 public function generate_discount($purchase_id)
 {
     $code = $this->generate_code();
     // Make sure the code does not already exist, otherwise create another
     if (edd_get_discount_by('code', $code)) {
         $code = $this->generate_code();
     }
     // Create the meta for the discount code
     $details = apply_filters('nod_discount_args', array('code' => $code, 'name' => __('NOD Discount for purchase ID ' . $purchase_id), 'status' => 'active', 'uses' => '', 'max' => '1', 'amount' => EDD_NOD()->settings->rate, 'start' => date('m/d/Y H:i:s'), 'expiration' => date('m/d/Y', strtotime("+" . EDD_NOD()->settings->expires)), 'type' => EDD_NOD()->settings->type, 'min_price' => '', 'product' => array(), 'product_condition' => '', 'excluded_products' => array(), 'not_global' => false, 'use_once' => '1'), $code, $purchase_id);
     // Before adding the new NOD discount code
     do_action('nod_before_nod_discount_code', $details);
     // Create the discount code. Return if unsuccessful.
     if (!edd_store_discount($details)) {
         return;
     }
     $discount = edd_get_discount_by_code($code);
     // After adding the new NOD discount code
     do_action('nod_after_nod_discount_code', $discount, $details);
     return !empty($discount) ? $discount : false;
 }