PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.2.0
GiveWP – Donation Plugin and Fundraising Platform v2.2.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 7 years ago class-payments-table.php 7 years ago payments-history.php 8 years ago view-payment-details.php 7 years ago
actions.php
481 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 $payment->anonymous = isset( $data['give_anonymous_donation'] ) ? absint( $data['give_anonymous_donation'] ) : 0;
83
84
85 $updated = $payment->save();
86
87 if ( 0 === $updated ) {
88 wp_die( __( 'Error Updating Donation.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
89 }
90
91 $donor_changed = false;
92
93 if ( isset( $data['give-new-donor'] ) && $data['give-new-donor'] == '1' ) {
94
95 $email = ! empty( $data['give-new-donor-email'] ) ? sanitize_text_field( $data['give-new-donor-email'] ) : '';
96 $first_name = ! empty( $data['give-new-donor-first-name'] ) ? sanitize_text_field( $data['give-new-donor-first-name'] ) : '';
97 $last_name = ! empty( $data['give-new-donor-last-name'] ) ? sanitize_text_field( $data['give-new-donor-last-name'] ) : '';
98 $names = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
99
100 if ( empty( $email ) || empty( $first_name ) ) {
101 wp_die( __( 'New Donor requires first name and email address.', 'give' ), __( 'Error', 'give' ), array( 'response' => 400 ) );
102 }
103
104 $donor = new Give_Donor( $email );
105 if ( empty( $donor->id ) ) {
106 $donor_data = array( 'name' => $names, 'email' => $email );
107 $user_id = email_exists( $email );
108 if ( false !== $user_id ) {
109 $donor_data['user_id'] = $user_id;
110 }
111
112 if ( ! $donor->create( $donor_data ) ) {
113 // Failed to create the new donor, assume the previous donor.
114 $donor_changed = false;
115 $donor = new Give_Donor( $curr_donor_id );
116 give_set_error( 'give-payment-new-donor-fail', __( 'Error creating new donor.', 'give' ) );
117 }
118 }
119
120 // Create and Update Donor First Name and Last Name in Meta Fields.
121 $donor->update_meta( '_give_donor_first_name', $first_name );
122 $donor->update_meta( '_give_donor_last_name', $last_name );
123
124 $new_donor_id = $donor->id;
125
126 $previous_donor = new Give_Donor( $curr_donor_id );
127
128 $donor_changed = true;
129
130 } elseif ( $curr_donor_id !== $new_donor_id ) {
131
132 $donor = new Give_Donor( $new_donor_id );
133 $email = $donor->email;
134 $names = $donor->name;
135
136 $previous_donor = new Give_Donor( $curr_donor_id );
137
138 $donor_changed = true;
139
140 } else {
141 $donor = new Give_Donor( $curr_donor_id );
142 $email = $donor->email;
143 $names = $donor->name;
144 }
145
146 if ( $donor_changed ) {
147
148 // Setup first and last name from input values.
149 $first_name = $donor->get_first_name();
150 $last_name = $donor->get_last_name();
151
152 $payment->first_name = $first_name;
153 $payment->last_name = $last_name;
154
155 // Remove the stats and payment from the previous donor and attach it to the new donor.
156 $previous_donor->remove_payment( $payment_id, false );
157 $donor->attach_payment( $payment_id, false );
158
159 if ( 'publish' == $status ) {
160
161 // Reduce previous user donation count and amount.
162 $previous_donor->decrease_donation_count();
163 $previous_donor->decrease_value( $curr_total );
164
165 // If donation was completed adjust stats of new donors.
166 $donor->increase_purchase_count();
167 $donor->increase_value( $new_total );
168 }
169
170 $payment->customer_id = $donor->id;
171 } else {
172
173 if ( 'publish' === $status ) {
174 // Update user donation stat.
175 $donor->update_donation_value( $curr_total, $new_total );
176 }
177 }
178
179 // Set new meta values.
180 $payment->user_id = $donor->user_id;
181 $payment->email = $donor->email;
182 $payment->address = $address;
183 $payment->total = $new_total;
184
185 // Check for payment notes.
186 if ( ! empty( $data['give-payment-note'] ) ) {
187
188 $note = wp_kses( $data['give-payment-note'], array() );
189 give_insert_payment_note( $payment_id, $note );
190
191 }
192
193 // Set new status.
194 $payment->status = $status;
195
196 // Adjust total store earnings if the payment total has been changed.
197 if ( $new_total !== $curr_total && 'publish' == $status ) {
198
199 if ( $new_total > $curr_total ) {
200 // Increase if our new total is higher.
201 $difference = $new_total - $curr_total;
202 give_increase_total_earnings( $difference );
203
204 // Increase form earnings.
205 give_increase_earnings( $payment->form_id, $difference, $payment->ID );
206 } elseif ( $curr_total > $new_total ) {
207 // Decrease if our new total is lower.
208 $difference = $curr_total - $new_total;
209 give_decrease_total_earnings( $difference );
210
211 // Decrease form earnings.
212 give_decrease_form_earnings( $payment->form_id, $difference, $payment->ID );
213 }
214 }
215
216 $payment->save();
217
218 // Get new give form ID.
219 $new_form_id = absint( $data['give-payment-form-select'] );
220 $current_form_id = absint( $payment->get_meta( '_give_payment_form_id' ) );
221
222 // We are adding payment transfer code in last to remove any conflict with above functionality.
223 // For example: above code will automatically handle form stat (increase/decrease) when payment status changes.
224 // Check if user want to transfer current payment to new give form id.
225 if ( $new_form_id && $new_form_id != $current_form_id ) {
226
227 // Get new give form title.
228 $new_form_title = get_the_title( $new_form_id );
229
230 // Update payment give form meta data.
231 $payment->update_meta( '_give_payment_form_id', $new_form_id );
232 $payment->update_meta( '_give_payment_form_title', $new_form_title );
233
234 // Update price id payment metadata.
235 if ( ! give_has_variable_prices( $new_form_id ) ) {
236 $payment->update_meta( '_give_payment_price_id', '' );
237 }
238
239 // If donation was completed, adjust stats of forms.
240 if ( 'publish' == $status ) {
241
242 // Decrease sale of old give form. For other payment status.
243 $current_form = new Give_Donate_Form( $current_form_id );
244 $current_form->decrease_sales();
245 $current_form->decrease_earnings( $curr_total, $payment->ID );
246
247 // Increase sale of new give form.
248 $new_form = new Give_Donate_Form( $new_form_id );
249 $new_form->increase_sales();
250 $new_form->increase_earnings( $new_total, $payment->ID );
251 }
252
253 // Re setup payment to update new meta value in object.
254 $payment->update_payment_setup( $payment->ID );
255
256 // Update form id in payment logs.
257 Give()->async_process->data( array(
258 'data' => array( $new_form_id, $payment_id ),
259 'hook' => 'give_update_log_form_id',
260 ) )->dispatch();
261 }
262
263 // Update price id if current form is variable form.
264 /* @var Give_Donate_Form $form */
265 $form = new Give_Donate_Form( $payment->form_id );
266
267 if ( isset( $data['give-variable-price'] ) && $form->has_variable_prices() ) {
268
269 // Get payment meta data.
270 $payment_meta = $payment->get_meta();
271
272 $price_info = array();
273 $price_id = '';
274
275 // Get price info
276 if( 0 <= $data['give-variable-price'] ) {
277 foreach ( $form->prices as $variable_price ) {
278 if( $new_total === give_maybe_sanitize_amount( $variable_price['_give_amount'] ) ) {
279 $price_info = $variable_price;
280 break;
281 }
282 }
283 }
284
285 // Set price id.
286 if( ! empty( $price_info ) ) {
287 $price_id = $data['give-variable-price'];
288
289 if( $data['give-variable-price'] !== $price_info['_give_id']['level_id'] ) {
290 // Set price id to amount match.
291 $price_id = $price_info['_give_id']['level_id'];
292 }
293
294 } elseif( $form->is_custom_price_mode() ){
295 $price_id = 'custom';
296 }
297
298 // Update payment meta data.
299 $payment_meta['price_id'] = $price_id;
300
301 // Update payment give form meta data.
302 $payment->update_meta( '_give_payment_price_id', $price_id );
303 $payment->update_meta( '_give_payment_meta', $payment_meta );
304
305 // Re setup payment to update new meta value in object.
306 $payment->update_payment_setup( $payment->ID );
307 }
308
309 $comment_id = isset( $data['give_comment_id'] ) ? absint( $data['give_comment_id'] ) : 0;
310 $is_anonymous_donation = give_is_anonymous_donation_field_enabled( $payment->form_id );
311
312 if ( $is_anonymous_donation ) {
313 give_update_meta( $payment->ID, '_give_anonymous_donation', $payment->anonymous );
314 Give()->donor_meta->update_meta( $payment->donor_id, '_give_anonymous_donor', $payment->anonymous );
315
316 // Update comment meta if admin is not updating comment.
317 if( $comment_id ) {
318 update_comment_meta( $comment_id, '_give_anonymous_donation', $payment->anonymous );
319 }
320 }
321
322 // Update comment.
323 if ( give_is_donor_comment_field_enabled( $payment->form_id ) ) {
324 // We are access comment directly from $_POST because comment formatting remove because of give_clean in give_post_actions.
325 $data['give_comment'] = trim( $_POST['give_comment'] );
326
327 if ( empty( $data['give_comment'] ) ) {
328 // Delete comment if empty
329 Give_Comment::delete( $comment_id, $payment_id, 'payment' );
330
331 } else {
332
333 // Update/Insert comment.
334 $is_update_comment_meta = ! $comment_id;
335
336 $comment_args = array(
337 'comment_author_email' => $payment->email
338 );
339
340 if ( $comment_id ) {
341 $comment_args['comment_ID'] = $comment_id;
342 }
343
344 $comment_id = give_insert_donor_donation_comment(
345 $payment->ID,
346 $payment->donor_id,
347 $data['give_comment'],
348 $comment_args
349 );
350
351 if ( $is_update_comment_meta ) {
352 update_comment_meta( $comment_id, '_give_anonymous_donation', $is_anonymous_donation );
353 }
354 }
355
356 $donor_has_comment = empty( $data['give_comment'] )
357 ? ( $latest_comment = give_get_donor_latest_comment( $payment->donor_id ) && empty( $latest_comment ) ? '0' : '1' )
358 : '1';
359
360 Give()->donor_meta->update_meta( $payment->donor_id, '_give_has_comment', $donor_has_comment );
361 }
362
363 /**
364 * Fires after updating edited donation.
365 *
366 * @since 1.0
367 * @since 1.8.9 Changes hook name give_updated_edited_purchase -> give_updated_edited_donation
368 *
369 * @param int $payment_id The ID of the payment.
370 */
371 do_action( 'give_updated_edited_donation', $payment_id );
372
373 wp_safe_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=payment-updated&id=' . $payment_id ) );
374 exit;
375 }
376
377 add_action( 'give_update_payment_details', 'give_update_payment_details' );
378
379 /**
380 * Trigger a Donation Deletion.
381 *
382 * @since 1.0
383 *
384 * @param array $data Arguments passed.
385 *
386 * @return void
387 */
388 function give_trigger_donation_delete( $data ) {
389 if ( wp_verify_nonce( $data['_wpnonce'], 'give_donation_nonce' ) ) {
390
391 $payment_id = absint( $data['purchase_id'] );
392
393 if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
394 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
395 }
396
397 give_delete_donation( $payment_id );
398 wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&give-messages[]=donation-deleted' ) );
399 give_die();
400 }
401 }
402
403 add_action( 'give_delete_payment', 'give_trigger_donation_delete' );
404
405 /**
406 * AJAX Store Donation Note
407 */
408 function give_ajax_store_payment_note() {
409
410 $payment_id = absint( $_POST['payment_id'] );
411 $note = wp_kses( $_POST['note'], array() );
412
413 if ( ! current_user_can( 'edit_give_payments', $payment_id ) ) {
414 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
415 }
416
417 if ( empty( $payment_id ) ) {
418 die( '-1' );
419 }
420
421 if ( empty( $note ) ) {
422 die( '-1' );
423 }
424
425 $note_id = give_insert_payment_note( $payment_id, $note );
426 die( give_get_payment_note_html( $note_id ) );
427 }
428
429 add_action( 'wp_ajax_give_insert_payment_note', 'give_ajax_store_payment_note' );
430
431 /**
432 * Triggers a donation note deletion without ajax
433 *
434 * @since 1.0
435 *
436 * @param array $data Arguments passed
437 *
438 * @return void
439 */
440 function give_trigger_payment_note_deletion( $data ) {
441
442 if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_delete_payment_note_' . $data['note_id'] ) ) {
443 return;
444 }
445
446 if ( ! current_user_can( 'edit_give_payments', $data['payment_id'] ) ) {
447 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
448 }
449
450 $edit_order_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details&give-messages[]=donation-note-deleted&id=' . absint( $data['payment_id'] ) );
451
452 give_delete_payment_note( $data['note_id'], $data['payment_id'] );
453
454 wp_redirect( $edit_order_url );
455 }
456
457 add_action( 'give_delete_payment_note', 'give_trigger_payment_note_deletion' );
458
459 /**
460 * Delete a payment note deletion with ajax
461 *
462 * @since 1.0
463 *
464 * @return void
465 */
466 function give_ajax_delete_payment_note() {
467
468 if ( ! current_user_can( 'edit_give_payments', $_POST['payment_id'] ) ) {
469 wp_die( __( 'You do not have permission to edit payments.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
470 }
471
472 if ( give_delete_payment_note( $_POST['note_id'], $_POST['payment_id'] ) ) {
473 die( '1' );
474 } else {
475 die( '-1' );
476 }
477
478 }
479
480 add_action( 'wp_ajax_give_delete_payment_note', 'give_ajax_delete_payment_note' );
481