Skip to content

Commit

Permalink
Merge branch 'issue/504' into issue/176
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Walker committed May 6, 2016
2 parents 1bd4250 + 6db146c commit 9ac883d
Show file tree
Hide file tree
Showing 25 changed files with 1,367 additions and 583 deletions.
33 changes: 23 additions & 10 deletions assets/js/frontend/give-checkout-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ jQuery(function ($) {

/**
* Unformat Currency
*
* @param price
* @returns {number}
*/
Expand All @@ -187,7 +188,9 @@ jQuery(function ($) {
});

/**
* Custom Donation Amount - If user focuses on field & changes value then updates price
* Custom Donation Amount Focus In
*
* @description: If user focuses on field & changes value then updates price
*/
doc.on('focus', '.give-donation-amount .give-text-input', function (e) {

Expand All @@ -197,7 +200,10 @@ jQuery(function ($) {
$(this).removeClass('invalid-amount');

//Set data amount
$(this).data('amount', give_unformat_currency($(this).val()));
var current_total = parent_form.find('.give-final-total-amount').data('total');
$(this).data('amount', give_unformat_currency(current_total));


//This class is used for CSS purposes
$(this).parent('.give-donation-amount').addClass('give-custom-amount-focus-in');

Expand All @@ -208,39 +214,44 @@ jQuery(function ($) {
parent_form.find('.give-radio-input.give-radio-level-custom').prop('checked', true); //Radio
parent_form.find('.give-select-level').prop('selected', false); //Select
parent_form.find('.give-select-level .give-donation-level-custom').prop('selected', true); //Select

});

/**
* Custom Donation (fires on focus end aka "blur")
* Custom Donation Focus Out
*
* @description: Fires on focus end aka "blur"
*
*/
doc.on('blur', '.give-donation-amount .give-text-input', function (e) {

var parent_form = $(this).closest('form');
var pre_focus_amount = $(this).data('amount');
var this_value = $(this).val();
var minimum_amount = parent_form.find('input[name="give-form-minimum"]');
var value_min = give_unformat_currency(minimum_amount.val());
var is_level = false;
var value_now = give_unformat_currency($(this).val());
var value_now = (this_value == 0) ? value_min : give_unformat_currency(this_value);

//Set the custom amount input value format properly
var format_args = {
symbol: '',
decimal: give_global_vars.decimal_separator,
thousand: give_global_vars.thousands_separator,
precision: give_global_vars.number_decimals
};
var formatted_total = give_format_currency(value_now, format_args);

//Set the custom amount input value formatted properly
$(this).val(formatted_total);

//Flag Multi-levels for min. donation conditional
var is_level = false;
parent_form.find('*[data-price-id]').each(function () {
if (this.value !== 'custom' && give_unformat_currency(this.value) === value_now) {
is_level = true;
}
});

//Does this number have an accepted minimum value?
if (( !$.isNumeric(value_now) || value_now < value_min || value_now < 1 ) && !is_level && value_min !== 0) {
if (( value_now < value_min || value_now < 1 ) && !is_level && value_min !== 0) {

//It doesn't... Invalid Minimum
$(this).addClass('give-invalid-amount');
Expand All @@ -252,7 +263,7 @@ jQuery(function ($) {
//If no error present, create it, insert, slide down (show)
if (invalid_minimum.length === 0) {
var error = $('<div class="give_error give-invalid-minimum">' + minimum_amount + '</div>').hide();
error.insertAfter(parent_form.find('.give-total-wrap')).show();
error.insertBefore(parent_form.find('.give-total-wrap')).show();
}

} else {
Expand All @@ -265,7 +276,6 @@ jQuery(function ($) {
parent_form.find('.give-submit').prop('disabled', false);

}

//If values don't match up then proceed with updating donation total value
if (pre_focus_amount !== value_now) {

Expand All @@ -280,6 +290,7 @@ jQuery(function ($) {

});


//Multi-level Buttons: Update Amount Field based on Multi-level Donation Select
doc.on('click touchend', '.give-donation-level-btn', function (e) {
e.preventDefault(); //don't let the form submit
Expand Down Expand Up @@ -359,6 +370,7 @@ jQuery(function ($) {

//Update donation form bottom total data attr and text
parent_form.find('.give-final-total-amount').data('total', this_amount).text(formatted_total);

}

/**
Expand Down Expand Up @@ -441,4 +453,5 @@ jQuery(function ($) {
return decodeURIComponent(results[2].replace(/\+/g, " "));
}


});
4 changes: 4 additions & 0 deletions assets/scss/frontend/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Layout
clear: both;
}

span {
width: auto;
}

.give-form-title {
margin: 0 0 15px;
}
Expand Down
10 changes: 5 additions & 5 deletions give.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class Give {
/**
* Give Customers DB Object
*
* @var Give_Customer object
* @var object|Give_DB_Customers object
* @since 1.0
*/
public $customers;
Expand Down Expand Up @@ -291,7 +291,6 @@ private function includes() {
require_once GIVE_PLUGIN_DIR . 'includes/forms/widget.php';
require_once GIVE_PLUGIN_DIR . 'includes/shortcodes.php';
require_once GIVE_PLUGIN_DIR . 'includes/formatting.php';
require_once GIVE_PLUGIN_DIR . 'includes/price-functions.php';
require_once GIVE_PLUGIN_DIR . 'includes/error-tracking.php';
require_once GIVE_PLUGIN_DIR . 'includes/process-purchase.php';
require_once GIVE_PLUGIN_DIR . 'includes/login-register.php';
Expand Down Expand Up @@ -401,16 +400,17 @@ public function load_textdomain() {


/**
* The main function responsible for returning the one true Give
* Instance to functions everywhere.
* Start Give
*
* The main function responsible for returning the one true Give instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $give = Give(); ?>
*
* @since 1.0
* @return object - The one true Give Instance
* @return object|Give
*/
function Give() {
return Give::instance();
Expand Down
140 changes: 99 additions & 41 deletions includes/admin/payments/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ function give_update_payment_details( $data ) {

// Retrieve the payment ID
$payment_id = absint( $data['give_payment_id'] );
$payment = new Give_Payment( $payment_id );

// Retrieve existing payment meta
$meta = give_get_payment_meta( $payment_id );
$user_info = give_get_payment_meta_user_info( $payment_id );
$meta = $payment->get_meta();
$user_info = $payment->user_info;

$status = $data['give-payment-status'];
$date = sanitize_text_field( $data['give-payment-date'] );
$hour = sanitize_text_field( $data['give-payment-time-hour'] );

$status = $data['give-payment-status'];
$user_id = isset( $data['give-payment-user-id'] ) ? intval( $data['give-payment-user-id'] ) : '';
$date = sanitize_text_field( $data['give-payment-date'] );
$hour = sanitize_text_field( $data['give-payment-time-hour'] );
$form_id = give_get_payment_form_id($payment_id);

// Restrict to our high and low
if ( $hour > 23 ) {
Expand All @@ -62,24 +62,87 @@ function give_update_payment_details( $data ) {
$minute = 00;
}

$address = array_map( 'trim', $data['give-payment-address'][0] );
$date = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00';
$curr_total = give_sanitize_amount( give_get_payment_amount( $payment_id ) );
$new_total = give_sanitize_amount( $_POST['give-payment-total'] );
$address = array_map( 'trim', $data['give-payment-address'][0] );

$curr_total = give_sanitize_amount( $payment->total );
$new_total = give_sanitize_amount( $_POST['give-payment-total'] );
$date = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00';

$curr_customer_id = sanitize_text_field( $data['give-current-customer'] );
$new_customer_id = sanitize_text_field( $data['customer-id'] );

// Setup purchased Donations Forms and price options
$updated_donations = isset( $_POST['give-payment-details-donations'] ) ? $_POST['give-payment-details-donations'] : false;

if ( $updated_donations && ! empty( $_POST['give-payment-donations-changed'] ) ) {

foreach ( $updated_donations as $donation ) {

// If this item doesn't have a log yet, add one for each quantity count
$has_log = absint( $donation['has_log'] );
$has_log = empty( $has_log ) ? false : true;

if ( $has_log ) {
continue;
}

if ( empty( $donation['item_price'] ) ) {
$donation['item_price'] = 0.00;
}

$item_price = $donation['item_price'];
$form_id = absint( $donation['id'] );
$quantity = absint( $donation['quantity'] ) > 0 ? absint( $donation['quantity'] ) : 1;
$price_id = false;

if ( give_has_variable_prices( $form_id ) && isset( $donation['price_id'] ) ) {
$price_id = absint( $donation['price_id'] );
}

// Set some defaults
$args = array(
'quantity' => $quantity,
'item_price' => $item_price,
'price_id' => $price_id,
);

$payment->add_donation( $form_id, $args );

}

$deleted_donations = json_decode( stripcslashes( $data['give-payment-removed'] ), true );

foreach ( $deleted_donations as $deleted_donation ) {
$deleted_donation = $deleted_donation[0];

if ( empty ( $deleted_donation['id'] ) ) {
continue;
}

$price_id = empty( $deleted_donation['price_id'] ) ? 0 : (int) $deleted_donation['price_id'];

$args = array(
'quantity' => (int) $deleted_donation['quantity'],
'price_id' => (int) $price_id,
'item_price' => (float) $deleted_donation['amount'],
);

$payment->remove_donation( $deleted_donation['id'], $args );

do_action( 'give_remove_donation_from_payment', $payment_id, $deleted_donation['id'] );

}


}

do_action( 'give_update_edited_purchase', $payment_id );

// Update main payment record
$updated = wp_update_post( array(
'ID' => $payment_id,
'edit_date' => true,
'post_date' => $date
) );
$payment->date = $date;
$updated = $payment->save();

if ( 0 === $updated ) {
wp_die( esc_attr__( 'Error Updating Payment', 'give' ), esc_attr__( 'Error', 'give' ), array( 'response' => 400 ) );
wp_die( __( 'Error Updating Payment', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
}

$customer_changed = false;
Expand All @@ -90,7 +153,7 @@ function give_update_payment_details( $data ) {
$names = isset( $data['give-new-customer-name'] ) ? sanitize_text_field( $data['give-new-customer-name'] ) : '';

if ( empty( $email ) || empty( $names ) ) {
wp_die( esc_attr__( 'New Customers require a name and email address', 'give' ) );
wp_die( __( 'New Customers require a name and email address', 'give' ) );
}

$customer = new Give_Customer( $email );
Expand All @@ -102,7 +165,7 @@ function give_update_payment_details( $data ) {
}

if ( ! $customer->create( $customer_data ) ) {
// Failed to crete the new donor, assume the previous donor
// Failed to crete the new customer, assume the previous customer
$customer_changed = false;
$customer = new Give_Customer( $curr_customer_id );
give_set_error( 'give-payment-new-customer-fail', __( 'Error creating new donor', 'give' ) );
Expand Down Expand Up @@ -143,7 +206,6 @@ function give_update_payment_details( $data ) {
$last_name = implode( ' ', $names );
}


if ( $customer_changed ) {

// Remove the stats and payment from the previous customer and attach it to the new customer
Expand All @@ -160,35 +222,28 @@ function give_update_payment_details( $data ) {
$customer->increase_value( $new_total );
}

update_post_meta( $payment_id, '_give_payment_customer_id', $customer->id );

$payment->customer_id = $customer->id;
}


// Set new meta values
$user_info['id'] = $customer->user_id;
$user_info['email'] = $customer->email;
$user_info['first_name'] = $first_name;
$user_info['last_name'] = $last_name;
$user_info['address'] = $address;
$meta['user_info'] = $user_info;
$payment->user_id = $customer->user_id;
$payment->email = $customer->email;
$payment->first_name = $first_name;
$payment->last_name = $last_name;
$payment->address = $address;

$payment->total = $new_total;

// Check for payment notes
if ( ! empty( $data['give-payment-note'] ) ) {

$note = wp_kses( $data['give-payment-note'], array() );
give_insert_payment_note( $payment_id, $note );
give_insert_payment_note( $payment->ID, $note );

}

// Set new status
give_update_payment_status( $payment_id, $status );

give_update_payment_meta( $payment_id, '_give_payment_user_id', $customer->user_id );
give_update_payment_meta( $payment_id, '_give_payment_user_email', $customer->email );
give_update_payment_meta( $payment_id, '_give_payment_meta', $meta );
give_update_payment_meta( $payment_id, '_give_payment_total', $new_total );
$payment->status = $status;

// Adjust total store earnings if the payment total has been changed
if ( $new_total !== $curr_total && ( 'publish' == $status || 'revoked' == $status ) ) {
Expand All @@ -197,18 +252,18 @@ function give_update_payment_details( $data ) {
// Increase if our new total is higher
$difference = $new_total - $curr_total;
give_increase_total_earnings( $difference );
$form = new Give_Donate_Form( $form_id );
$form->increase_earnings( $difference );

} elseif ( $curr_total > $new_total ) {
// Decrease if our new total is lower
$difference = $curr_total - $new_total;
give_decrease_total_earnings( $difference );
$form = new Give_Donate_Form( $form_id );
$form->decrease_earnings( $difference );

}

}

$payment->save();

do_action( 'give_updated_edited_purchase', $payment_id );

wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-order-details&give-message=payment-updated&id=' . $payment_id ) );
Expand Down Expand Up @@ -243,6 +298,9 @@ function give_trigger_purchase_delete( $data ) {

add_action( 'give_delete_payment', 'give_trigger_purchase_delete' );

/**
* Give AJAX Store Payment Note
*/
function give_ajax_store_payment_note() {

$payment_id = absint( $_POST['payment_id'] );
Expand Down
Loading

0 comments on commit 9ac883d

Please sign in to comment.