PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.0
GiveWP – Donation Plugin and Fundraising Platform v2.1.0
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / payments / actions.php
give / includes / admin / payments Last commit date
actions.php 8 years ago class-payments-table.php 8 years ago payments-history.php 8 years ago view-payment-details.php 8 years ago
actions.php
420 lines
1 <?php
2 /**
3 * Admin Payment Actions
4 *
5 * @package Give
6 * @subpackage Admin/Payments
7 * @copyright Copyright (c) 2016, WordImpress
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 *
19 * Process the payment details edit
20 *
21 * @since 1.0
22 * @access private
23 *
24 * @param array $data Donation data.
25 *
26 * @return void
27 */
28 function give_update_payment_details( $data ) {
29
30 if ( ! current_user_can( 'edit_give_payments', $data['give_payment_id'] ) ) {
31 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
32 }
33
34 check_admin_referer( 'give_update_payment_details_nonce' );
35
36 // Retrieve the payment ID.
37 $payment_id = absint( $data['give_payment_id'] );
38
39 /* @var Give_Payment $payment */
40 $payment = new Give_Payment( $payment_id );
41
42 $status = $data['give-payment-status'];
43 $date = sanitize_text_field( $data['give-payment-date'] );
44 $hour = sanitize_text_field( $data['give-payment-time-hour'] );
45
46 // Restrict to our high and low.
47 if ( $hour > 23 ) {
48 $hour = 23;
49 } elseif ( $hour < 0 ) {
50 $hour = 00;
51 }
52
53 $minute = sanitize_text_field( $data['give-payment-time-min'] );
54
55 // Restrict to our high and low.
56 if ( $minute > 59 ) {
57 $minute = 59;
58 } elseif ( $minute < 0 ) {
59 $minute = 00;
60 }
61
62 $address = give_clean( $data['give-payment-address'][0] );
63
64 $curr_total = $payment->total;
65 $new_total = give_maybe_sanitize_amount( ( ! empty( $data['give-payment-total'] ) ? $data['give-payment-total'] : 0 ) );
66 $date = date( 'Y-m-d', strtotime( $date ) ) . ' ' . $hour . ':' . $minute . ':00';
67
68 $curr_donor_id = sanitize_text_field( $data['give-current-donor'] );
69 $new_donor_id = sanitize_text_field( $data['donor-id'] );
70
71 /**
72 * Fires before updating edited donation.
73 *
74 * @since 1.0
75 * @since 1.8.9 Changes hook name give_update_edited_purchase -> give_update_edited_donation
76 *
77 * @param int $payment_id The ID of the payment.
78 */
79 do_action( 'give_update_edited_donation', $payment_id );
80
81 $payment->date = $date;
82 $updated = $payment->save();
83
84 if ( 0 === $updated ) {
85 wp_die( __( 'Error Updating Donation.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
86 }
87
88 $donor_changed = false;
89
90 if ( isset( $data['give-new-donor'] ) && $data['give-new-donor'] == '1' ) {
91
92 $email = ! empty( $data['give-new-donor-email'] ) ? sanitize_text_field( $data['give-new-donor-email'] ) : '';
93 $first_name = ! empty( $data['give-new-donor-first-name'] ) ? sanitize_text_field( $data['give-new-donor-first-name'] ) : '';
94 $last_name = ! empty( $data['give-new-donor-last-name'] ) ? sanitize_text_field( $data['give-new-donor-last-name'] ) : '';
95 $names = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
96
97 if ( empty( $email ) || empty( $first_name ) ) {
98 wp_die( __( 'New Donor requires first name and email address.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
99 }
100
101 $donor = new Give_Donor( $email );
102 if ( empty( $donor->id ) ) {
103 $donor_data = array( 'name' => $names, 'email' => $email );
104 $user_id = email_exists( $email );
105 if ( false !== $user_id ) {
106 $donor_data['user_id'] = $user_id;
107 }
108
109 if ( ! $donor->create( $donor_data ) ) {
110 // Failed to create the new donor, assume the previous donor.
111 $donor_changed = false;
112 $donor = new Give_Donor( $curr_donor_id );
113 give_set_error( 'give-payment-new-donor-fail', __( 'Error creating new donor.', 'give' ) );
114 }
115 }
116
117 // Create and Update Donor First Name and Last Name in Meta Fields.
118 $donor->update_meta( '_give_donor_first_name', $first_name );
119 $donor->update_meta( '_give_donor_last_name', $last_name );
120
121 $new_donor_id = $donor->id;
122
123 $previous_donor = new Give_Donor( $curr_donor_id );
124
125 $donor_changed = true;
126
127 } elseif ( $curr_donor_id !== $new_donor_id ) {
128
129 $donor = new Give_Donor( $new_donor_id );
130 $email = $donor->email;
131 $names = $donor->name;
132
133 $previous_donor = new Give_Donor( $curr_donor_id );
134
135 $donor_changed = true;
136
137 } else {
138 $donor = new Give_Donor( $curr_donor_id );
139 $email = $donor->email;
140 $names = $donor->name;
141 }
142
143 if ( $donor_changed ) {
144
145 // Setup first and last name from input values.
146 $first_name = $donor->get_first_name();
147 $last_name = $donor->get_last_name();
148
149 $payment->first_name = $first_name;
150 $payment->last_name = $last_name;
151
152 // Remove the stats and payment from the previous donor and attach it to the new donor.
153 $previous_donor->remove_payment( $payment_id, false );
154 $donor->attach_payment( $payment_id, false );
155
156 if ( 'publish' == $status ) {
157
158 // Reduce previous user donation count and amount.
159 $previous_donor->decrease_donation_count();
160 $previous_donor->decrease_value( $curr_total );
161
162 // If donation was completed adjust stats of new donors.
163 $donor->increase_purchase_count();
164 $donor->increase_value( $new_total );
165 }
166
167 $payment->customer_id = $donor->id;
168 } else {
169
170 if ( 'publish' === $status ) {
171 // Update user donation stat.
172 $donor->update_donation_value( $curr_total, $new_total );
173 }
174 }
175
176 // Set new meta values.
177 $payment->user_id = $donor->user_id;
178 $payment->email = $donor->email;
179 $payment->address = $address;
180 $payment->total = $new_total;
181
182 // Check for payment notes.
183 if ( ! empty( $data['give-payment-note'] ) ) {
184
185 $note = wp_kses( $data['give-payment-note'], array() );
186 give_insert_payment_note( $payment_id, $note );
187
188 }
189
190 // Set new status.
191 $payment->status = $status;
192
193 // Adjust total store earnings if the payment total has been changed.
194 if ( $new_total !== $curr_total && 'publish' == $status ) {
195
196 if ( $new_total > $curr_total ) {
197 // Increase if our new total is higher.
198 $difference = $new_total - $curr_total;
199 give_increase_total_earnings( $difference );
200
201 } elseif ( $curr_total > $new_total ) {
202 // Decrease if our new total is lower.
203 $difference = $curr_total - $new_total;
204 give_decrease_total_earnings( $difference );
205
206 }
207 }
208
209 $payment->save();
210
211 // Get new give form ID.
212 $new_form_id = absint( $data['give-payment-form-select'] );
213 $current_form_id = absint( $payment->get_meta( '_give_payment_form_id' ) );
214
215 // We are adding payment transfer code in last to remove any conflict with above functionality.
216 // For example: above code will automatically handle form stat (increase/decrease) when payment status changes.
217 // Check if user want to transfer current payment to new give form id.
218 if ( $new_form_id && $new_form_id != $current_form_id ) {
219
220 // Get new give form title.
221 $new_form_title = get_the_title( $new_form_id );
222
223 // Update payment give form meta data.
224 $payment->update_meta( '_give_payment_form_id', $new_form_id );
225 $payment->update_meta( '_give_payment_form_title', $new_form_title );
226
227 // Update price id payment metadata.
228 if ( ! give_has_variable_prices( $new_form_id ) ) {
229 $payment->update_meta( '_give_payment_price_id', '' );
230 }
231
232 // If donation was completed, adjust stats of forms.
233 if ( 'publish' == $status ) {
234
235 // Decrease sale of old give form. For other payment status.
236 $current_form = new Give_Donate_Form( $current_form_id );
237 $current_form->decrease_sales();
238 $current_form->decrease_earnings( $curr_total, $payment->ID );
239
240 // Increase sale of new give form.
241 $new_form = new Give_Donate_Form( $new_form_id );
242 $new_form->increase_sales();
243 $new_form->increase_earnings( $new_total, $payment->ID );
244 }
245
246 // Re setup payment to update new meta value in object.
247 $payment->update_payment_setup( $payment->ID );
248
249 // Update form id in payment logs.
250 Give()->async_process->data( array(
251 'data' => array( $new_form_id, $payment_id ),
252 'hook' => 'give_update_log_form_id',
253 ) )->dispatch();
254 }
255
256 // Update price id if current form is variable form.
257 /* @var Give_Donate_Form $form */
258 $form = new Give_Donate_Form( $payment->form_id );
259
260 if ( isset( $data['give-variable-price'] ) && $form->has_variable_prices() ) {
261
262 // Get payment meta data.
263 $payment_meta = $payment->get_meta();
264
265 $price_info = array();
266 $price_id = '';
267
268 // Get price info
269 if( 0 <= $data['give-variable-price'] ) {
270 foreach ( $form->prices as $variable_price ) {
271 if( $new_total === give_maybe_sanitize_amount( $variable_price['_give_amount'] ) ) {
272 $price_info = $variable_price;
273 break;
274 }
275 }
276 }
277
278 // Set price id.
279 if( ! empty( $price_info ) ) {
280 $price_id = $data['give-variable-price'];
281
282 if( $data['give-variable-price'] !== $price_info['_give_id']['level_id'] ) {
283 // Set price id to amount match.
284 $price_id = $price_info['_give_id']['level_id'];
285 }
286
287 } elseif( $form->is_custom_price_mode() ){
288 $price_id = 'custom';
289 }
290
291 // Update payment meta data.
292 $payment_meta['price_id'] = $price_id;
293
294 // Update payment give form meta data.
295 $payment->update_meta( '_give_payment_price_id', $price_id );
296 $payment->update_meta( '_give_payment_meta', $payment_meta );
297
298 // Re setup payment to update new meta value in object.
299 $payment->update_payment_setup( $payment->ID );
300 }
301
302 /**
303 * Fires after updating edited donation.
304 *
305 * @since 1.0
306 * @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation
307 *
308 * @param int $payment_id The ID of the payment.
309 */
310 do_action( 'give_updated_edited_donation', $payment_id );
311
312 wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-message=payment-updated&id=' . $payment_id ) );
313 exit;
314 }
315
316 add_action( 'give_update_payment_details', 'give_update_payment_details' );
317
318 /**
319 * Trigger a Donation Deletion.
320 *
321 * @since 1.0
322 *
323 * @param array $data Arguments passed.
324 *
325 * @return void
326 */
327 function give_trigger_donation_delete( $data ) {
328 if ( wp_verify_nonce( $data['_wpnonce'], 'give_donation_nonce' ) ) {
329
330 $payment_id = absint( $data['purchase_id'] );
331
332 if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
333 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
334 }
335
336 give_delete_donation( $payment_id );
337 wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&give-message=donation_deleted' ) );
338 give_die();
339 }
340 }
341
342 add_action( 'give_delete_payment', 'give_trigger_donation_delete' );
343
344 /**
345 * AJAX Store Donation Note
346 */
347 function give_ajax_store_payment_note() {
348
349 $payment_id = absint( $_POST['payment_id'] );
350 $note = wp_kses( $_POST['note'], array() );
351
352 if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
353 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
354 }
355
356 if ( empty( $payment_id ) ) {
357 die( '-1' );
358 }
359
360 if ( empty( $note ) ) {
361 die( '-1' );
362 }
363
364 $note_id = give_insert_payment_note( $payment_id, $note );
365 die( give_get_payment_note_html( $note_id ) );
366 }
367
368 add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' );
369
370 /**
371 * Triggers a donation note deletion without ajax
372 *
373 * @since 1.0
374 *
375 * @param array $data Arguments passed
376 *
377 * @return void
378 */
379 function give_trigger_payment_note_deletion( $data ) {
380
381 if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) {
382 return;
383 }
384
385 if ( ! current_user_can( 'edit_give_payments', $data['payment_id'] ) ) {
386 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
387 }
388
389 $edit_order_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-message=donation-note-deleted&id=' . absint( $data['payment_id'] ) );
390
391 give_delete_payment_note( $data['note_id'], $data['payment_id'] );
392
393 wp_redirect( $edit_order_url );
394 }
395
396 add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' );
397
398 /**
399 * Delete a payment note deletion with ajax
400 *
401 * @since 1.0
402 *
403 * @return void
404 */
405 function give_ajax_delete_payment_note() {
406
407 if ( ! current_user_can( 'edit_give_payments', $_POST['payment_id'] ) ) {
408 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
409 }
410
411 if ( give_delete_payment_note( $_POST['note_id'], $_POST['payment_id'] ) ) {
412 die( '1' );
413 } else {
414 die( '-1' );
415 }
416
417 }
418
419 add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' );
420