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 |