/** * Checks to see if a Give form has variable prices enabled. * * @since 1.0 * * @param int $form_id ID number of the form to check * * @return bool true if has variable prices, false otherwise */ function give_has_variable_prices($form_id = 0) { if (empty($form_id)) { return false; } $form = new Give_Donate_Form($form_id); return $form->has_variable_prices(); }
/** * Test Set Goal * * @covers Give_Donate_Form::get_goal */ public function test_get_goal() { $simple_form = new Give_Donate_Form($this->_simple_form->ID); $this->assertEquals(0, $simple_form->get_goal()); update_post_meta($simple_form->ID, '_give_set_goal', 5000); $simple_form = new Give_Donate_Form($this->_simple_form->ID); $this->assertEquals(5000, $simple_form->get_goal()); }
<?php /** * This template is used to display the goal with [give_goal] */ $goal_option = get_post_meta($form_id, '_give_goal_option', true); $form = new Give_Donate_Form($form_id); $goal = $form->goal; $goal_format = get_post_meta($form_id, '_give_goal_format', true); $income = $form->get_earnings(); $color = get_post_meta($form_id, '_give_goal_color', true); $show_text = (bool) isset($args['show_text']) ? filter_var($args['show_text'], FILTER_VALIDATE_BOOLEAN) : true; $show_bar = (bool) isset($args['show_bar']) ? filter_var($args['show_bar'], FILTER_VALIDATE_BOOLEAN) : true; //Sanity check - respect shortcode args if (isset($args['show_goal']) && $args['show_goal'] === false) { return false; } //Sanity check - ensure form has goal set to output if (empty($form->ID) || is_singular('give_forms') && $goal_option !== 'yes' || $goal_option !== 'yes' || $goal == 0) { //not this form, bail return false; } $progress = round($income / $goal * 100, 2); if ($income >= $goal) { $progress = 100; } ?> <div class="give-goal-progress"> <?php if (!empty($show_text)) { ?>
/** * Decreases the total earnings of a form. Primarily for when a purchase is refunded. * * @since 1.0 * * @param int $give_form_id Give Form ID * @param int $amount Earnings * * @return bool|int */ function give_decrease_earnings($give_form_id = 0, $amount) { $form = new Give_Donate_Form($give_form_id); return $form->decrease_earnings($amount); }
/** * Show Give Goals * @since 1.0 * * @param int $form_id * * @return bool */ function give_show_goal_progress($form_id) { $goal_option = get_post_meta($form_id, '_give_goal_option', true); $form = new Give_Donate_Form($form_id); $goal = $form->goal; $income = $form->get_earnings(); $color = get_post_meta($form_id, '_give_goal_color', true); if (empty($form->ID) || $goal_option !== 'yes' || $goal == 0) { return false; } $progress = round($income / $goal * 100, 2); if ($income > $goal) { $progress = 100; } $output = '<div class="goal-progress">'; $output .= '<div class="raised">'; $output .= sprintf(_x('%s of %s raised', 'give', 'This text displays the amount of income raised compared to the goal.'), '<span class="income">' . give_currency_filter(give_format_amount($income)) . '</span>', '<span class="goal-text">' . give_currency_filter(give_format_amount($goal))) . '</span>'; $output .= '</div>'; $output .= '<div class="progress-bar">'; $output .= '<span style="width: ' . esc_attr($progress) . '%;'; if (!empty($color)) { $output .= 'background-color:' . $color; } $output .= '"></span>'; $output .= '</div></div><!-- /.goal-progress -->'; echo apply_filters('give_goal_output', $output); }
/** * Auto set correct donation level id on basis of amount. * * Note: If amount does not match to donation level amount then level id will be auto select to first match level id on basis of amount. * * @param array $valid_data * @param array $data * * @return bool */ function give_validate_multi_donation_form_level($valid_data, $data) { /* @var Give_Donate_Form $form*/ $form = new Give_Donate_Form($data['give-form-id']); $donation_level_matched = false; if ($form->is_multi_type_donation_form()) { // Bailout. if (!($variable_prices = $form->get_prices())) { return false; } // Sanitize donation amount. $data['give-amount'] = give_sanitize_amount($data['give-amount']); // Get number of decimals. $default_decimals = give_get_price_decimals(); if ($data['give-amount'] === give_sanitize_amount(give_get_price_option_amount($data['give-form-id'], $data['give-price-id']), $default_decimals)) { return true; } // Find correct donation level from all donation levels. foreach ($variable_prices as $variable_price) { // Sanitize level amount. $variable_price['_give_amount'] = give_sanitize_amount($variable_price['_give_amount'], $default_decimals); // Set first match donation level ID. if ($data['give-amount'] === $variable_price['_give_amount']) { $_POST['give-price-id'] = $variable_price['_give_id']['level_id']; $donation_level_matched = true; break; } } // If donation amount is not find in donation levels then check if form has custom donation feature enable or not. // If yes then set price id to custom if amount is greater then custom minimum amount (if any). if (!$donation_level_matched && 'yes' === get_post_meta($data['give-form-id'], '_give_custom_amount', true)) { // Sanitize custom minimum amount. $custom_minimum_amount = give_sanitize_amount(get_post_meta($data['give-form-id'], '_give_custom_amount_minimum', true), $default_decimals); if ($data['give-amount'] >= $custom_minimum_amount) { $_POST['give-price-id'] = 'custom'; $donation_level_matched = true; } } } return $donation_level_matched ? true : false; }
/** * Show Give Goals * @since 1.0 * * @param int $form_id * @param array $args * * @return mixed */ function give_show_goal_progress($form_id, $args) { $goal_option = get_post_meta($form_id, '_give_goal_option', true); $form = new Give_Donate_Form($form_id); $goal = $form->goal; $income = $form->get_earnings(); $color = get_post_meta($form_id, '_give_goal_color', true); $show_text = (bool) isset($args['show_text']) ? filter_var($args['show_text'], FILTER_VALIDATE_BOOLEAN) : true; $show_bar = (bool) isset($args['show_bar']) ? filter_var($args['show_bar'], FILTER_VALIDATE_BOOLEAN) : true; //Sanity check - respect shortcode args if (isset($args['show_goal']) && $args['show_goal'] === false) { return false; } //Sanity check - ensure form has goal set to output if (empty($form->ID) || is_singular('give_forms') && $goal_option !== 'yes' || $goal_option !== 'yes' || $goal == 0) { //not this form, bail return false; } $progress = round($income / $goal * 100, 2); if ($income > $goal) { $progress = 100; } $output = '<div class="goal-progress">'; //Goal Progress Text if (!empty($show_text)) { $output .= '<div class="raised">'; $output .= sprintf(_x('%s of %s raised', 'This text displays the amount of income raised compared to the goal.', 'give'), '<span class="income">' . give_currency_filter(give_format_amount($income)) . '</span>', '<span class="goal-text">' . give_currency_filter(give_format_amount($goal))) . '</span>'; $output .= '</div>'; } //Goal Progress Bar if (!empty($show_bar)) { $output .= '<div class="progress-bar">'; $output .= '<span style="width: ' . esc_attr($progress) . '%;'; if (!empty($color)) { $output .= 'background-color:' . $color; } $output .= '"></span>'; $output .= '</div><!-- /.progress-bar -->'; } $output .= '</div><!-- /.goal-progress -->'; echo apply_filters('give_goal_output', $output); return false; }
/** * Returns the minimum price amount of a form, only enforced for the custom amount input. * * @since 1.3.6 * * @param int $form_id ID number of the form to retrieve the minimum price for * * @return mixed string|int Minimum price of the form */ function give_get_form_minimum_price($form_id = 0) { if (empty($form_id)) { return false; } $form = new Give_Donate_Form($form_id); return $form->__get('minimum_price'); }
/** * Save * * Once items have been set, an update is needed to save them to the database. * * @access public * * @return bool True of the save occurred, false if it failed or wasn't needed */ public function save() { $saved = false; //Must have an ID if (empty($this->ID)) { $payment_id = $this->insert_payment(); if (false === $payment_id) { $saved = false; } else { $this->ID = $payment_id; } } //Set ID if not matching if ($this->ID !== $this->_ID) { $this->ID = $this->_ID; } // If we have something pending, let's save it if (!empty($this->pending)) { $total_increase = 0; $total_decrease = 0; foreach ($this->pending as $key => $value) { switch ($key) { case 'donations': // Update totals for pending donations foreach ($this->pending[$key] as $item) { $quantity = isset($item['quantity']) ? $item['quantity'] : 1; $price_id = isset($item['price_id']) ? $item['price_id'] : 0; switch ($item['action']) { case 'add': $price = $item['price']; if ('publish' === $this->status || 'complete' === $this->status) { // Add sales logs $log_date = date_i18n('Y-m-d G:i:s', current_time('timestamp')); $y = 0; while ($y < $quantity) { give_record_sale_in_log($item['id'], $this->ID, $price_id, $log_date); $y++; } $form = new Give_Donate_Form($item['id']); $form->increase_sales($quantity); $form->increase_earnings($price); $total_increase += $price; } break; case 'remove': $log_args = array('post_type' => 'give_log', 'post_parent' => $item['id'], 'numberposts' => $quantity, 'meta_query' => array(array('key' => '_give_log_payment_id', 'value' => $this->ID, 'compare' => '='), array('key' => '_give_log_price_id', 'value' => $price_id, 'compare' => '='))); $found_logs = get_posts($log_args); foreach ($found_logs as $log) { wp_delete_post($log->ID, true); } if ('publish' === $this->status || 'complete' === $this->status) { $form = new Give_Donate_Form($item['id']); $form->decrease_sales($quantity); $form->decrease_earnings($item['amount']); $total_decrease += $item['amount']; } break; } } break; case 'fees': if ('publish' !== $this->status && 'complete' !== $this->status) { break; } if (empty($this->pending[$key])) { break; } foreach ($this->pending[$key] as $fee) { switch ($fee['action']) { case 'add': $total_increase += $fee['amount']; break; case 'remove': $total_decrease += $fee['amount']; break; } } break; case 'status': $this->update_status($this->status); break; case 'gateway': $this->update_meta('_give_payment_gateway', $this->gateway); break; case 'mode': $this->update_meta('_give_payment_mode', $this->mode); break; case 'transaction_id': $this->update_meta('_give_payment_transaction_id', $this->transaction_id); break; case 'ip': $this->update_meta('_give_payment_user_ip', $this->ip); break; case 'customer_id': $this->update_meta('_give_payment_customer_id', $this->customer_id); break; case 'user_id': $this->update_meta('_give_payment_user_id', $this->user_id); break; case 'form_title': $this->update_meta('_give_payment_form_title', $this->form_title); break; case 'form_id': $this->update_meta('_give_payment_form_id', $this->form_id); break; case 'price_id': $this->update_meta('_give_payment_price_id', $this->price_id); break; case 'first_name': $this->user_info['first_name'] = $this->first_name; break; case 'last_name': $this->user_info['last_name'] = $this->last_name; break; case 'address': $this->user_info['address'] = $this->address; break; case 'email': $this->update_meta('_give_payment_user_email', $this->email); break; case 'key': $this->update_meta('_give_payment_purchase_key', $this->key); break; case 'number': $this->update_meta('_give_payment_number', $this->number); break; case 'date': $args = array('ID' => $this->ID, 'post_date' => $this->date, 'edit_date' => true); wp_update_post($args); break; case 'completed_date': $this->update_meta('_give_completed_date', $this->completed_date); break; case 'parent_payment': $args = array('ID' => $this->ID, 'post_parent' => $this->parent_payment); wp_update_post($args); break; default: do_action('give_payment_save', $this, $key); break; } } if ('pending' !== $this->status) { $customer = new Give_Customer($this->customer_id); $total_change = $total_increase - $total_decrease; if ($total_change < 0) { $total_change = -$total_change; // Decrease the customer's purchase stats $customer->decrease_value($total_change); give_decrease_total_earnings($total_change); } else { if ($total_change > 0) { // Increase the customer's purchase stats $customer->increase_value($total_change); give_increase_total_earnings($total_change); } } } $this->update_meta('_give_payment_total', $this->total); $new_meta = array('form_title' => $this->form_title, 'form_id' => $this->form_id, 'price_id' => $this->price_id, 'fees' => $this->fees, 'currency' => $this->currency, 'user_info' => $this->user_info); $meta = $this->get_meta(); $merged_meta = array_merge($meta, $new_meta); // Only save the payment meta if it's changed if (md5(serialize($meta)) !== md5(serialize($merged_meta))) { $updated = $this->update_meta('_give_payment_meta', $merged_meta); if (false !== $updated) { $saved = true; } } $this->pending = array(); $saved = true; } if (true === $saved) { $this->setup_payment($this->ID); } return $saved; }
/** * Get Donation Form * * @since 1.0 * * @param array $args Arguments for display * * @return string $purchase_form */ function give_get_donation_form($args = array()) { global $post; $form_id = is_object($post) ? $post->ID : 0; if (isset($args['id'])) { $form_id = $args['id']; } $defaults = apply_filters('give_form_args_defaults', array('form_id' => $form_id)); $args = wp_parse_args($args, $defaults); $form = new Give_Donate_Form($args['form_id']); //bail if no form ID if (empty($form->ID)) { return false; } $payment_mode = give_get_chosen_gateway($form->ID); $form_action = add_query_arg(apply_filters('give_form_action_args', array('payment-mode' => $payment_mode)), give_get_current_page_url()); //Sanity Check: Donation form not published or user doesn't have permission to view drafts if ('publish' !== $form->post_status && !current_user_can('edit_give_forms', $form->ID)) { return false; } //Get the form wrap CSS classes. $form_wrap_classes = $form->get_form_wrap_classes($args); //Get the <form> tag wrap CSS classes. $form_classes = $form->get_form_classes($args); ob_start(); /** * Fires before the post form outputs. * * @since 1.0 * * @param int $form ->ID The current form ID * @param array $args An array of form args */ do_action('give_pre_form_output', $form->ID, $args); ?> <div id="give-form-<?php echo $form->ID; ?> -wrap" class="<?php echo $form_wrap_classes; ?> "> <?php if ($form->is_close_donation_form()) { //Get Goal thank you message. $display_thankyou_message = get_post_meta($form->ID, '_give_form_goal_achieved_message', true); $display_thankyou_message = !empty($display_thankyou_message) ? $display_thankyou_message : esc_html__('Thank you to all our donors, we have met our fundraising goal.', 'give'); //Print thank you message. apply_filters('give_goal_closed_output', give_output_error($display_thankyou_message, true, 'success')); } else { if (isset($args['show_title']) && $args['show_title'] == true) { echo apply_filters('give_form_title', '<h2 class="give-form-title">' . get_the_title($form_id) . '</h2>'); } do_action('give_pre_form', $form->ID, $args); ?> <form id="give-form-<?php echo $form_id; ?> " class="<?php echo $form_classes; ?> " action="<?php echo esc_url_raw($form_action); ?> " method="post"> <input type="hidden" name="give-form-id" value="<?php echo $form->ID; ?> "/> <input type="hidden" name="give-form-title" value="<?php echo htmlentities($form->post_title); ?> "/> <input type="hidden" name="give-current-url" value="<?php echo htmlspecialchars(give_get_current_page_url()); ?> "/> <input type="hidden" name="give-form-url" value="<?php echo htmlspecialchars(give_get_current_page_url()); ?> "/> <input type="hidden" name="give-form-minimum" value="<?php echo give_format_amount(give_get_form_minimum_price($form->ID)); ?> "/> <!-- The following field is for robots only, invisible to humans: --> <span class="give-hidden" style="display: none !important;"> <label for="give-form-honeypot-<?php echo $form_id; ?> "></label> <input id="give-form-honeypot-<?php echo $form_id; ?> " type="text" name="give-honeypot" class="give-honeypot give-hidden"/> </span> <?php //Price ID hidden field for variable (mult-level) donation forms if (give_has_variable_prices($form_id)) { //get default selected price ID $prices = apply_filters('give_form_variable_prices', give_get_variable_prices($form_id), $form_id); $price_id = 0; //loop through prices foreach ($prices as $price) { if (isset($price['_give_default']) && $price['_give_default'] === 'default') { $price_id = $price['_give_id']['level_id']; } } ?> <input type="hidden" name="give-price-id" value="<?php echo $price_id; ?> "/> <?php } do_action('give_checkout_form_top', $form->ID, $args); do_action('give_payment_mode_select', $form->ID, $args); do_action('give_checkout_form_bottom', $form->ID, $args); ?> </form> <?php do_action('give_post_form', $form->ID, $args); ?> <?php } ?> <!--end #give-form-<?php echo absint($form->ID); ?> --></div> <?php /** * Fires after the post form outputs. * * @since 1.0 * * @param int $form ->ID The current form ID * @param array $args An array of form args */ do_action('give_post_form_output', $form->ID, $args); $final_output = ob_get_clean(); echo apply_filters('give_donate_form', $final_output, $args); }
/** * * Process the payment details edit * * @access private * * @param $data * * @since 1.0 * @return void * */ function give_update_payment_details($data) { if (!current_user_can('edit_give_payments', $data['give_payment_id'])) { wp_die(__('You do not have permission to edit this payment record', 'give'), __('Error', 'give'), array('response' => 403)); } check_admin_referer('give_update_payment_details_nonce'); // Retrieve the payment ID $payment_id = absint($data['give_payment_id']); // Retrieve existing payment meta $meta = give_get_payment_meta($payment_id); $user_info = give_get_payment_meta_user_info($payment_id); $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) { $hour = 23; } elseif ($hour < 0) { $hour = 00; } $minute = sanitize_text_field($data['give-payment-time-min']); // Restrict to our high and low if ($minute > 59) { $minute = 59; } elseif ($minute < 0) { $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']); $curr_customer_id = sanitize_text_field($data['give-current-customer']); $new_customer_id = sanitize_text_field($data['customer-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)); if (0 === $updated) { wp_die(esc_attr__('Error Updating Payment', 'give'), esc_attr__('Error', 'give'), array('response' => 400)); } $customer_changed = false; if (isset($data['give-new-customer']) && $data['give-new-customer'] == '1') { $email = isset($data['give-new-customer-email']) ? sanitize_text_field($data['give-new-customer-email']) : ''; $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')); } $customer = new Give_Customer($email); if (empty($customer->id)) { $customer_data = array('name' => $names, 'email' => $email); $user_id = email_exists($email); if (false !== $user_id) { $customer_data['user_id'] = $user_id; } if (!$customer->create($customer_data)) { // 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 customer', 'give')); } } $new_customer_id = $customer->id; $previous_customer = new Give_Customer($curr_customer_id); $customer_changed = true; } elseif ($curr_customer_id !== $new_customer_id) { $customer = new Give_Customer($new_customer_id); $email = $customer->email; $names = $customer->name; $previous_customer = new Give_Customer($curr_customer_id); $customer_changed = true; } else { $customer = new Give_Customer($curr_customer_id); $email = $customer->email; $names = $customer->name; } // Setup first and last name from input values $names = explode(' ', $names); $first_name = !empty($names[0]) ? $names[0] : ''; $last_name = ''; if (!empty($names[1])) { unset($names[0]); $last_name = implode(' ', $names); } if ($customer_changed) { // Remove the stats and payment from the previous customer and attach it to the new customer $previous_customer->remove_payment($payment_id, false); $customer->attach_payment($payment_id, false); // If purchase was completed and not ever refunded, adjust stats of customers if ('revoked' == $status || 'publish' == $status) { $previous_customer->decrease_purchase_count(); $previous_customer->decrease_value($new_total); $customer->increase_purchase_count(); $customer->increase_value($new_total); } update_post_meta($payment_id, '_give_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; // 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); } // 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); // Adjust total store earnings if the payment total has been changed if ($new_total !== $curr_total && ('publish' == $status || 'revoked' == $status)) { if ($new_total > $curr_total) { // 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); } } 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)); exit; }
/** * Determine if custom price mode is enabled or disabled * This function is wrapper function to Give_Donate_Form::is_custom_price_mode() * * @since 1.6 * * @param int $form_id Form ID. * * @use Give_Donate_Form::is_custom_price_mode() * * @return bool */ function give_is_custom_price_mode($form_id = 0) { if (empty($form_id)) { return false; } $form = new Give_Donate_Form($form_id); return $form->is_custom_price_mode(); }
/** * * Process the payment details edit * * @access private * * @param array $data * * @since 1.0 * @return void * */ function give_update_payment_details($data) { if (!current_user_can('edit_give_payments', $data['give_payment_id'])) { wp_die(esc_html__('You do not have permission to edit payment records.', 'give'), esc_html__('Error', 'give'), array('response' => 403)); } check_admin_referer('give_update_payment_details_nonce'); // Retrieve the payment ID $payment_id = absint($data['give_payment_id']); /* @var Give_Payment $payment */ $payment = new Give_Payment($payment_id); // Retrieve existing payment meta $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']); // Restrict to our high and low if ($hour > 23) { $hour = 23; } elseif ($hour < 0) { $hour = 00; } $minute = sanitize_text_field($data['give-payment-time-min']); // Restrict to our high and low if ($minute > 59) { $minute = 59; } elseif ($minute < 0) { $minute = 00; } $address = array_map('trim', $data['give-payment-address'][0]); $curr_total = give_sanitize_amount($payment->total); $new_total = give_sanitize_amount($data['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']); /** * Fires before updating edited purchase. * * @since 1.0 * * @param int $payment_id The ID of the payment. */ do_action('give_update_edited_purchase', $payment_id); $payment->date = $date; $updated = $payment->save(); if (0 === $updated) { wp_die(esc_html__('Error Updating Payment.', 'give'), esc_html__('Error', 'give'), array('response' => 400)); } $customer_changed = false; if (isset($data['give-new-customer']) && $data['give-new-customer'] == '1') { $email = isset($data['give-new-customer-email']) ? sanitize_text_field($data['give-new-customer-email']) : ''; $names = isset($data['give-new-customer-name']) ? sanitize_text_field($data['give-new-customer-name']) : ''; if (empty($email) || empty($names)) { wp_die(esc_html__('New Customers require a name and email address.', 'give'), esc_html__('Error', 'give'), array('response' => 400)); } $customer = new Give_Customer($email); if (empty($customer->id)) { $customer_data = array('name' => $names, 'email' => $email); $user_id = email_exists($email); if (false !== $user_id) { $customer_data['user_id'] = $user_id; } if (!$customer->create($customer_data)) { // Failed to crete the new donor, assume the previous donor $customer_changed = false; $customer = new Give_Customer($curr_customer_id); give_set_error('give-payment-new-customer-fail', esc_html__('Error creating new donor.', 'give')); } } $new_customer_id = $customer->id; $previous_customer = new Give_Customer($curr_customer_id); $customer_changed = true; } elseif ($curr_customer_id !== $new_customer_id) { $customer = new Give_Customer($new_customer_id); $email = $customer->email; $names = $customer->name; $previous_customer = new Give_Customer($curr_customer_id); $customer_changed = true; } else { $customer = new Give_Customer($curr_customer_id); $email = $customer->email; $names = $customer->name; } // Setup first and last name from input values $names = explode(' ', $names); $first_name = !empty($names[0]) ? $names[0] : ''; $last_name = ''; if (!empty($names[1])) { unset($names[0]); $last_name = implode(' ', $names); } if ($customer_changed) { // Remove the stats and payment from the previous customer and attach it to the new customer $previous_customer->remove_payment($payment_id, false); $customer->attach_payment($payment_id, false); if ('publish' == $status) { // Reduce previous user donation count and amount. $previous_customer->decrease_purchase_count(); $previous_customer->decrease_value($curr_total); // If purchase was completed adjust stats of new customers. $customer->increase_purchase_count(); $customer->increase_value($new_total); } $payment->customer_id = $customer->id; } else { if ('publish' === $status) { // Update user donation stat. $customer->update_donation_value($curr_total, $new_total); } } // Set new meta values $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); } // Set new status $payment->status = $status; // Adjust total store earnings if the payment total has been changed if ($new_total !== $curr_total && 'publish' == $status) { if ($new_total > $curr_total) { // Increase if our new total is higher $difference = $new_total - $curr_total; give_increase_total_earnings($difference); } elseif ($curr_total > $new_total) { // Decrease if our new total is lower $difference = $curr_total - $new_total; give_decrease_total_earnings($difference); } } $payment->save(); // Get new give form ID. $new_form_id = absint($data['forms']); $current_form_id = absint($payment->get_meta('_give_payment_form_id')); // We are adding payment transfer code in last to remove any conflict with above functionality. // For example: above code will automatically handle form stat (increase/decrease) when payment status changes. /* Check if user want to transfer current payment to new give form id. */ if ($new_form_id != $current_form_id) { // Get new give form title. $new_form_title = get_the_title($new_form_id); // Update new give form data in payment data. $payment_meta = $payment->get_meta(); $payment_meta['form_title'] = $new_form_title; $payment_meta['form_id'] = $new_form_id; // Update price id post meta data for set donation form. if (!give_has_variable_prices($new_form_id)) { $payment_meta['price_id'] = ''; } // Update payment give form meta data. $payment->update_meta('_give_payment_form_id', $new_form_id); $payment->update_meta('_give_payment_form_title', $new_form_title); $payment->update_meta('_give_payment_meta', $payment_meta); // Update price id payment metadata. if (!give_has_variable_prices($new_form_id)) { $payment->update_meta('_give_payment_price_id', ''); } // If purchase was completed, adjust stats of forms if ('publish' == $status) { // Decrease sale of old give form. For other payment status $current_form = new Give_Donate_Form($current_form_id); $current_form->decrease_sales(); $current_form->decrease_earnings($curr_total); // Increase sale of new give form. $new_form = new Give_Donate_Form($new_form_id); $new_form->increase_sales(); $new_form->increase_earnings($new_total); } // Re setup payment to update new meta value in object. $payment->update_payment_setup($payment->ID); } // Update price id if current form is variable form. if (!empty($data['give-variable-price']) && give_has_variable_prices($payment->form_id)) { // Get payment meta data. $payment_meta = $payment->get_meta(); // Set payment id to empty string if variable price id is negative ( i.e. custom amount feature enabled ). $data['give-variable-price'] = 'custom' === $data['give-variable-price'] ? 'custom' : 0 < $data['give-variable-price'] ? $data['give-variable-price'] : ''; // Update payment meta data. $payment_meta['price_id'] = $data['give-variable-price']; // Update payment give form meta data. $payment->update_meta('_give_payment_price_id', $data['give-variable-price']); $payment->update_meta('_give_payment_meta', $payment_meta); // Re setup payment to update new meta value in object. $payment->update_payment_setup($payment->ID); } /** * Fires after updating edited purchase. * * @since 1.0 * * @param int $payment_id The ID of the payment. */ 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)); exit; }