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