class-donor-table.php
6 years ago
donor-actions.php
6 years ago
donor-functions.php
6 years ago
donors.php
6 years ago
donor-actions.php
680 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Donors |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Donors |
| 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 | * Processes a donor edit. |
| 19 | * |
| 20 | * @param array $args The $_POST array being passed. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | * |
| 24 | * @return array|bool $output Response messages |
| 25 | */ |
| 26 | function give_edit_donor( $args ) { |
| 27 | |
| 28 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 29 | |
| 30 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 31 | wp_die( |
| 32 | esc_html__( 'You do not have permission to edit this donor.', 'give' ), |
| 33 | esc_html__( 'Error', 'give' ), |
| 34 | array( |
| 35 | 'response' => 403, |
| 36 | ) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | if ( empty( $args ) ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | // Sanitize Data. |
| 45 | $args = give_clean( $args ); |
| 46 | |
| 47 | $args = wp_parse_args( |
| 48 | $args, |
| 49 | array( |
| 50 | 'give_anonymous_donor' => 0, |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | // Verify Nonce. |
| 55 | if ( ! wp_verify_nonce( $args['_wpnonce'], 'edit-donor' ) ) { |
| 56 | wp_die( |
| 57 | esc_html__( 'Cheatin’ uh?', 'give' ), |
| 58 | esc_html__( 'Error', 'give' ), |
| 59 | array( |
| 60 | 'response' => 400, |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | $donor_info = $args['donor_info']; |
| 66 | $donor_id = intval( $donor_info['id'] ); |
| 67 | |
| 68 | $donor = new Give_Donor( $donor_id ); |
| 69 | |
| 70 | // Bailout, if donor id doesn't exists. |
| 71 | if ( empty( $donor->id ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | $defaults = array( |
| 76 | 'title' => '', |
| 77 | 'name' => '', |
| 78 | 'user_id' => 0, |
| 79 | 'line1' => '', |
| 80 | 'line2' => '', |
| 81 | 'city' => '', |
| 82 | 'zip' => '', |
| 83 | 'state' => '', |
| 84 | 'country' => '', |
| 85 | ); |
| 86 | |
| 87 | $donor_info = wp_parse_args( $donor_info, $defaults ); |
| 88 | |
| 89 | if ( (int) $donor_info['user_id'] !== (int) $donor->user_id ) { |
| 90 | |
| 91 | // Make sure we don't already have this user attached to a donor. |
| 92 | if ( ! empty( $donor_info['user_id'] ) && false !== Give()->donors->get_donor_by( 'user_id', $donor_info['user_id'] ) ) { |
| 93 | give_set_error( |
| 94 | 'give-invalid-donor-user_id', |
| 95 | sprintf( |
| 96 | /* translators: %d User ID */ |
| 97 | __( 'The User ID #%d is already associated with a different donor.', 'give' ), |
| 98 | $donor_info['user_id'] |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // Make sure it's actually a user. |
| 104 | $user = get_user_by( 'id', $donor_info['user_id'] ); |
| 105 | if ( ! empty( $donor_info['user_id'] ) && false === $user ) { |
| 106 | give_set_error( |
| 107 | 'give-invalid-user_id', |
| 108 | sprintf( |
| 109 | /* translators: %d User ID */ |
| 110 | __( 'The User ID #%d does not exist. Please assign an existing user.', 'give' ), |
| 111 | $donor_info['user_id'] |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Bailout, if errors are present. |
| 118 | if ( give_get_errors() ) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | $donor->update_meta( '_give_anonymous_donor', absint( $args['give_anonymous_donor'] ) ); |
| 123 | |
| 124 | // Save company name in when admin update donor company name from dashboard. |
| 125 | $donor->update_meta( '_give_donor_company', sanitize_text_field( $args['give_donor_company'] ) ); |
| 126 | |
| 127 | // If First name of donor is empty, then fetch the current first name of donor. |
| 128 | if ( empty( $donor_info['first_name'] ) ) { |
| 129 | $donor_info['first_name'] = $donor->get_first_name(); |
| 130 | } |
| 131 | |
| 132 | // Sanitize the inputs. |
| 133 | $donor_data = array(); |
| 134 | $donor_data['name'] = trim( "{$donor_info['first_name']} {$donor_info['last_name']}" ); |
| 135 | $donor_data['first_name'] = $donor_info['first_name']; |
| 136 | $donor_data['last_name'] = $donor_info['last_name']; |
| 137 | $donor_data['title'] = $donor_info['title']; |
| 138 | $donor_data['user_id'] = $donor_info['user_id']; |
| 139 | |
| 140 | $donor_data = apply_filters( 'give_edit_donor_info', $donor_data, $donor_id ); |
| 141 | |
| 142 | /** |
| 143 | * Filter the address |
| 144 | * |
| 145 | * @todo unnecessary filter because we are not storing donor address to user. |
| 146 | * |
| 147 | * @since 1.0 |
| 148 | */ |
| 149 | $address = apply_filters( 'give_edit_donor_address', array(), $donor_id ); |
| 150 | |
| 151 | $donor_data = give_clean( $donor_data ); |
| 152 | $address = give_clean( $address ); |
| 153 | |
| 154 | $output = give_connect_user_donor_profile( $donor, $donor_data, $address ); |
| 155 | |
| 156 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 157 | header( 'Content-Type: application/json' ); |
| 158 | echo wp_json_encode( $output ); |
| 159 | wp_die(); |
| 160 | } |
| 161 | |
| 162 | if ( $output['success'] ) { |
| 163 | wp_safe_redirect( |
| 164 | add_query_arg( |
| 165 | array( |
| 166 | 'post_type' => 'give_forms', |
| 167 | 'page' => 'give-donors', |
| 168 | 'view' => 'overview', |
| 169 | 'id' => $donor_id, |
| 170 | 'give-messages[]' => 'profile-updated', |
| 171 | ), |
| 172 | esc_url( admin_url( 'edit.php' ) ) |
| 173 | ) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | exit; |
| 178 | |
| 179 | } |
| 180 | |
| 181 | add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 ); |
| 182 | |
| 183 | /** |
| 184 | * Save a donor note. |
| 185 | * |
| 186 | * @param array $args The $_POST array being passed. |
| 187 | * |
| 188 | * @since 1.0 |
| 189 | * |
| 190 | * @return int The Note ID that was saved, or 0 if nothing was saved. |
| 191 | */ |
| 192 | function give_donor_save_note( $args ) { |
| 193 | |
| 194 | $donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' ); |
| 195 | |
| 196 | if ( ! is_admin() || ! current_user_can( $donor_view_role ) ) { |
| 197 | wp_die( |
| 198 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 199 | __( 'Error', 'give' ), |
| 200 | array( |
| 201 | 'response' => 403, |
| 202 | ) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | if ( empty( $args ) ) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | $donor_note = trim( give_clean( $args['donor_note'] ) ); |
| 211 | $donor_id = (int) $args['customer_id']; |
| 212 | $nonce = $args['add_donor_note_nonce']; |
| 213 | |
| 214 | if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) { |
| 215 | wp_die( |
| 216 | __( 'Cheatin’ uh?', 'give' ), |
| 217 | __( 'Error', 'give' ), |
| 218 | array( |
| 219 | 'response' => 400, |
| 220 | ) |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | if ( empty( $donor_note ) ) { |
| 225 | give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) ); |
| 226 | } |
| 227 | |
| 228 | if ( give_get_errors() ) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | $donor = new Give_Donor( $donor_id ); |
| 233 | $new_note = $donor->add_note( $donor_note ); |
| 234 | |
| 235 | /** |
| 236 | * Fires before inserting donor note. |
| 237 | * |
| 238 | * @param int $donor_id The ID of the donor. |
| 239 | * @param string $new_note Note content. |
| 240 | * |
| 241 | * @since 1.0 |
| 242 | */ |
| 243 | do_action( 'give_pre_insert_donor_note', $donor_id, $new_note ); |
| 244 | |
| 245 | if ( ! empty( $new_note ) && ! empty( $donor->id ) ) { |
| 246 | |
| 247 | ob_start(); |
| 248 | ?> |
| 249 | <div class="donor-note-wrapper dashboard-comment-wrap comment-item"> |
| 250 | <span class="note-content-wrap"> |
| 251 | <?php echo stripslashes( $new_note ); ?> |
| 252 | </span> |
| 253 | </div> |
| 254 | <?php |
| 255 | $output = ob_get_contents(); |
| 256 | ob_end_clean(); |
| 257 | |
| 258 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 259 | echo $output; |
| 260 | exit; |
| 261 | } |
| 262 | |
| 263 | return $new_note; |
| 264 | |
| 265 | } |
| 266 | |
| 267 | return false; |
| 268 | |
| 269 | } |
| 270 | |
| 271 | add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 ); |
| 272 | |
| 273 | |
| 274 | /** |
| 275 | * Disconnect a user ID from a donor |
| 276 | * |
| 277 | * @param array $args Array of arguments. |
| 278 | * |
| 279 | * @since 1.0 |
| 280 | * |
| 281 | * @return bool|array If the disconnect was successful. |
| 282 | */ |
| 283 | function give_disconnect_donor_user_id( $args ) { |
| 284 | |
| 285 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 286 | |
| 287 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 288 | wp_die( |
| 289 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 290 | __( 'Error', 'give' ), |
| 291 | array( |
| 292 | 'response' => 403, |
| 293 | ) |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | if ( empty( $args ) ) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | $donor_id = (int) $args['customer_id']; |
| 302 | |
| 303 | $nonce = $args['_wpnonce']; |
| 304 | |
| 305 | if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) { |
| 306 | wp_die( |
| 307 | __( 'Cheatin’ uh?', 'give' ), |
| 308 | __( 'Error', 'give' ), |
| 309 | array( |
| 310 | 'response' => 400, |
| 311 | ) |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | $donor = new Give_Donor( $donor_id ); |
| 316 | if ( empty( $donor->id ) ) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | $user_id = $donor->user_id; |
| 321 | |
| 322 | /** |
| 323 | * Fires before disconnecting user ID from a donor. |
| 324 | * |
| 325 | * @param int $donor_id The ID of the donor. |
| 326 | * @param int $user_id The ID of the user. |
| 327 | * |
| 328 | * @since 1.0 |
| 329 | */ |
| 330 | do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id ); |
| 331 | |
| 332 | $output = array(); |
| 333 | $donor_args = array( |
| 334 | 'user_id' => 0, |
| 335 | ); |
| 336 | |
| 337 | $redirect_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id; |
| 338 | $is_donor_updated = $donor->update( $donor_args ); |
| 339 | |
| 340 | if ( $is_donor_updated ) { |
| 341 | |
| 342 | // Set meta for disconnected donor id and user id for future reference if needed. |
| 343 | update_user_meta( $user_id, '_give_disconnected_donor_id', $donor->id ); |
| 344 | $donor->update_meta( '_give_disconnected_user_id', $user_id ); |
| 345 | |
| 346 | $redirect_url = add_query_arg( |
| 347 | 'give-messages[]', |
| 348 | 'disconnect-user', |
| 349 | $redirect_url |
| 350 | ); |
| 351 | |
| 352 | $output['success'] = true; |
| 353 | |
| 354 | } else { |
| 355 | $output['success'] = false; |
| 356 | give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) ); |
| 357 | } |
| 358 | |
| 359 | $output['redirect'] = $redirect_url; |
| 360 | |
| 361 | /** |
| 362 | * Fires after disconnecting user ID from a donor. |
| 363 | * |
| 364 | * @param int $donor_id The ID of the donor. |
| 365 | * |
| 366 | * @since 1.0 |
| 367 | */ |
| 368 | do_action( 'give_post_donor_disconnect_user_id', $donor_id ); |
| 369 | |
| 370 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 371 | header( 'Content-Type: application/json' ); |
| 372 | echo json_encode( $output ); |
| 373 | wp_die(); |
| 374 | } |
| 375 | |
| 376 | return $output; |
| 377 | |
| 378 | } |
| 379 | |
| 380 | add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 ); |
| 381 | |
| 382 | /** |
| 383 | * Add an email address to the donor from within the admin and log a donor note. |
| 384 | * |
| 385 | * @param array $args Array of arguments: nonce, donor id, and email address. |
| 386 | * |
| 387 | * @since 1.7 |
| 388 | * |
| 389 | * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string). |
| 390 | */ |
| 391 | function give_add_donor_email( $args ) { |
| 392 | |
| 393 | $donor_id = ''; |
| 394 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 395 | |
| 396 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 397 | wp_die( |
| 398 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 399 | __( 'Error', 'give' ), |
| 400 | array( |
| 401 | 'response' => 403, |
| 402 | ) |
| 403 | ); |
| 404 | } |
| 405 | |
| 406 | $output = array(); |
| 407 | if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) { |
| 408 | $output['success'] = false; |
| 409 | if ( empty( $args['email'] ) ) { |
| 410 | $output['message'] = __( 'Email address is required.', 'give' ); |
| 411 | } elseif ( empty( $args['customer_id'] ) ) { |
| 412 | $output['message'] = __( 'Donor ID is required.', 'give' ); |
| 413 | } else { |
| 414 | $output['message'] = __( 'An error has occurred. Please try again.', 'give' ); |
| 415 | } |
| 416 | } elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) { |
| 417 | $output = array( |
| 418 | 'success' => false, |
| 419 | 'message' => __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 420 | ); |
| 421 | } elseif ( ! is_email( $args['email'] ) ) { |
| 422 | $output = array( |
| 423 | 'success' => false, |
| 424 | 'message' => __( 'Invalid email.', 'give' ), |
| 425 | ); |
| 426 | } else { |
| 427 | $email = sanitize_email( $args['email'] ); |
| 428 | $donor_id = (int) $args['customer_id']; |
| 429 | $primary = 'true' === $args['primary'] ? true : false; |
| 430 | $donor = new Give_Donor( $donor_id ); |
| 431 | if ( false === $donor->add_email( $email, $primary ) ) { |
| 432 | if ( in_array( $email, $donor->emails ) ) { |
| 433 | $output = array( |
| 434 | 'success' => false, |
| 435 | 'message' => __( 'Email already associated with this donor.', 'give' ), |
| 436 | ); |
| 437 | } else { |
| 438 | $output = array( |
| 439 | 'success' => false, |
| 440 | 'message' => __( 'Email address is already associated with another donor.', 'give' ), |
| 441 | ); |
| 442 | } |
| 443 | } else { |
| 444 | $redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-messages[]=email-added' ); |
| 445 | $output = array( |
| 446 | 'success' => true, |
| 447 | 'message' => __( 'Email successfully added to donor.', 'give' ), |
| 448 | 'redirect' => $redirect, |
| 449 | ); |
| 450 | |
| 451 | $user = wp_get_current_user(); |
| 452 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 453 | $donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login ); |
| 454 | $donor->add_note( $donor_note ); |
| 455 | |
| 456 | if ( $primary ) { |
| 457 | $donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login ); |
| 458 | $donor->add_note( $donor_note ); |
| 459 | } |
| 460 | } |
| 461 | } // End if(). |
| 462 | |
| 463 | do_action( 'give_post_add_donor_email', $donor_id, $args ); |
| 464 | |
| 465 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 466 | header( 'Content-Type: application/json' ); |
| 467 | echo json_encode( $output ); |
| 468 | wp_die(); |
| 469 | } |
| 470 | |
| 471 | return $output; |
| 472 | } |
| 473 | |
| 474 | add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 ); |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Remove an email address to the donor from within the admin and log a donor note and redirect back to the donor interface for feedback. |
| 479 | * |
| 480 | * @since 1.7 |
| 481 | * |
| 482 | * @return bool|null |
| 483 | */ |
| 484 | function give_remove_donor_email() { |
| 485 | if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) { |
| 486 | return false; |
| 487 | } |
| 488 | if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) { |
| 489 | return false; |
| 490 | } |
| 491 | if ( empty( $_GET['_wpnonce'] ) ) { |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | $nonce = $_GET['_wpnonce']; |
| 496 | if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) { |
| 497 | wp_die( |
| 498 | __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 499 | __( 'Error', 'give' ), |
| 500 | array( |
| 501 | 'response' => 403, |
| 502 | ) |
| 503 | ); |
| 504 | } |
| 505 | |
| 506 | $donor = new Give_Donor( $_GET['id'] ); |
| 507 | if ( $donor->remove_email( $_GET['email'] ) ) { |
| 508 | $url = add_query_arg( 'give-messages[]', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) ); |
| 509 | $user = wp_get_current_user(); |
| 510 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 511 | $donor_note = sprintf( __( 'Email address %1$s removed by %2$s', 'give' ), $_GET['email'], $user_login ); |
| 512 | $donor->add_note( $donor_note ); |
| 513 | } else { |
| 514 | $url = add_query_arg( 'give-messages[]', 'email-remove-failed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) ); |
| 515 | } |
| 516 | |
| 517 | wp_safe_redirect( $url ); |
| 518 | exit; |
| 519 | } |
| 520 | |
| 521 | add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 ); |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * Set an email address as the primary for a donor from within the admin and log a donor note |
| 526 | * and redirect back to the donor interface for feedback |
| 527 | * |
| 528 | * @since 1.7 |
| 529 | * |
| 530 | * @return bool|null |
| 531 | */ |
| 532 | function give_set_donor_primary_email() { |
| 533 | if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) { |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) { |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | if ( empty( $_GET['_wpnonce'] ) ) { |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | $nonce = $_GET['_wpnonce']; |
| 546 | |
| 547 | if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) { |
| 548 | wp_die( |
| 549 | __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 550 | __( 'Error', 'give' ), |
| 551 | array( |
| 552 | 'response' => 403, |
| 553 | ) |
| 554 | ); |
| 555 | } |
| 556 | |
| 557 | $donor = new Give_Donor( $_GET['id'] ); |
| 558 | |
| 559 | if ( $donor->set_primary_email( $_GET['email'] ) ) { |
| 560 | $url = add_query_arg( 'give-messages[]', 'primary-email-updated', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) ); |
| 561 | $user = wp_get_current_user(); |
| 562 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 563 | $donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $_GET['email'], $user_login ); |
| 564 | |
| 565 | $donor->add_note( $donor_note ); |
| 566 | } else { |
| 567 | $url = add_query_arg( 'give-messages[]', 'primary-email-failed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) ); |
| 568 | } |
| 569 | |
| 570 | wp_safe_redirect( $url ); |
| 571 | exit; |
| 572 | } |
| 573 | |
| 574 | add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 ); |
| 575 | |
| 576 | |
| 577 | /** |
| 578 | * This function will process the donor deletion. |
| 579 | * |
| 580 | * @param array $args Donor Deletion Arguments. |
| 581 | * |
| 582 | * @since 2.2 |
| 583 | */ |
| 584 | function give_process_donor_deletion( $args ) { |
| 585 | // Bailout. |
| 586 | if ( ! isset( $args['give-donor-delete-confirm'] ) ) { |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 591 | |
| 592 | // Verify user capabilities to proceed for deleting donor. |
| 593 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 594 | wp_die( |
| 595 | esc_html__( 'You do not have permission to delete donors.', 'give' ), |
| 596 | esc_html__( 'Error', 'give' ), |
| 597 | array( |
| 598 | 'response' => 403, |
| 599 | ) |
| 600 | ); |
| 601 | } |
| 602 | |
| 603 | $nonce_action = ''; |
| 604 | if ( 'delete_bulk_donor' === $args['give_action'] ) { |
| 605 | $nonce_action = 'bulk-donors'; |
| 606 | } elseif ( 'delete_donor' === $args['give_action'] ) { |
| 607 | $nonce_action = 'give-delete-donor'; |
| 608 | } |
| 609 | |
| 610 | // Verify Nonce for deleting bulk donors. |
| 611 | give_validate_nonce( $args['_wpnonce'], $nonce_action ); |
| 612 | |
| 613 | $redirect_args = array(); |
| 614 | $donor_ids = ( isset( $args['donor'] ) && is_array( $args['donor'] ) ) ? $args['donor'] : array( $args['donor_id'] ); |
| 615 | $redirect_args['order'] = ! empty( $args['order'] ) ? $args['order'] : 'DESC'; |
| 616 | $redirect_args['orderby'] = ! empty( $args['orderby'] ) ? strtolower( $args['orderby'] ) : 'id'; |
| 617 | $redirect_args['s'] = ! empty( $args['s'] ) ? $args['s'] : ''; |
| 618 | $delete_donor = ! empty( $args['give-donor-delete-confirm'] ) ? give_is_setting_enabled( $args['give-donor-delete-confirm'] ) : false; |
| 619 | $delete_donation = ! empty( $args['give-donor-delete-records'] ) ? give_is_setting_enabled( $args['give-donor-delete-records'] ) : false; |
| 620 | |
| 621 | if ( count( $donor_ids ) > 0 ) { |
| 622 | |
| 623 | // Loop through the selected donors to delete. |
| 624 | foreach ( $donor_ids as $donor_id ) { |
| 625 | |
| 626 | $donor = new Give_Donor( $donor_id ); |
| 627 | |
| 628 | // Proceed only if valid donor id is provided. |
| 629 | if ( $donor->id > 0 ) { |
| 630 | |
| 631 | /** |
| 632 | * Fires before deleting donor. |
| 633 | * |
| 634 | * @param int $donor_id The ID of the donor. |
| 635 | * @param bool $delete_donor Confirm Donor Deletion. |
| 636 | * @param bool $delete_donation Confirm Donor related donations deletion. |
| 637 | * |
| 638 | * @since 1.0 |
| 639 | */ |
| 640 | do_action( 'give_pre_delete_donor', $donor->id, $delete_donor, $delete_donation ); |
| 641 | |
| 642 | // Proceed only, if user confirmed whether they need to delete the donor. |
| 643 | if ( $delete_donor ) { |
| 644 | |
| 645 | // Delete donor and linked donations. |
| 646 | $donor_delete_status = give_delete_donor_and_related_donation( |
| 647 | $donor, |
| 648 | array( |
| 649 | 'delete_donation' => $delete_donation, |
| 650 | ) |
| 651 | ); |
| 652 | |
| 653 | if ( 1 === $donor_delete_status ) { |
| 654 | $redirect_args['give-messages[]'] = 'donor-deleted'; |
| 655 | } elseif ( 2 === $donor_delete_status ) { |
| 656 | $redirect_args['give-messages[]'] = 'donor-donations-deleted'; |
| 657 | } |
| 658 | } else { |
| 659 | $redirect_args['give-messages[]'] = 'confirm-delete-donor'; |
| 660 | } |
| 661 | } else { |
| 662 | $redirect_args['give-messages[]'] = 'invalid-donor-id'; |
| 663 | } // End if(). |
| 664 | } // End foreach(). |
| 665 | } else { |
| 666 | $redirect_args['give-messages[]'] = 'no-donor-found'; |
| 667 | } // End if(). |
| 668 | |
| 669 | $redirect_url = add_query_arg( |
| 670 | $redirect_args, |
| 671 | admin_url( 'edit.php?post_type=give_forms&page=give-donors' ) |
| 672 | ); |
| 673 | |
| 674 | wp_safe_redirect( $redirect_url ); |
| 675 | give_die(); |
| 676 | |
| 677 | } |
| 678 | add_action( 'give_delete_donor', 'give_process_donor_deletion' ); |
| 679 | add_action( 'give_delete_bulk_donor', 'give_process_donor_deletion' ); |
| 680 |