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