class-donor-table.php
4 years ago
donor-actions.php
4 years ago
donor-functions.php
6 years ago
donors.php
4 years ago
donor-actions.php
682 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 | esc_url_raw( |
| 165 | add_query_arg( |
| 166 | array( |
| 167 | 'post_type' => 'give_forms', |
| 168 | 'page' => 'give-donors', |
| 169 | 'view' => 'overview', |
| 170 | 'id' => $donor_id, |
| 171 | 'give-messages[]' => 'profile-updated', |
| 172 | ), |
| 173 | admin_url( 'edit.php' ) |
| 174 | ) |
| 175 | ) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | exit; |
| 180 | |
| 181 | } |
| 182 | |
| 183 | add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 ); |
| 184 | |
| 185 | /** |
| 186 | * Save a donor note. |
| 187 | * |
| 188 | * @param array $args The $_POST array being passed. |
| 189 | * |
| 190 | * @since 1.0 |
| 191 | * |
| 192 | * @return int The Note ID that was saved, or 0 if nothing was saved. |
| 193 | */ |
| 194 | function give_donor_save_note( $args ) { |
| 195 | |
| 196 | $donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' ); |
| 197 | |
| 198 | if ( ! is_admin() || ! current_user_can( $donor_view_role ) ) { |
| 199 | wp_die( |
| 200 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 201 | __( 'Error', 'give' ), |
| 202 | array( |
| 203 | 'response' => 403, |
| 204 | ) |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | if ( empty( $args ) ) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | $donor_note = trim( give_clean( $args['donor_note'] ) ); |
| 213 | $donor_id = (int) $args['customer_id']; |
| 214 | $nonce = $args['add_donor_note_nonce']; |
| 215 | |
| 216 | if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) { |
| 217 | wp_die( |
| 218 | __( 'Cheatin’ uh?', 'give' ), |
| 219 | __( 'Error', 'give' ), |
| 220 | array( |
| 221 | 'response' => 400, |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | if ( empty( $donor_note ) ) { |
| 227 | give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) ); |
| 228 | } |
| 229 | |
| 230 | if ( give_get_errors() ) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | $donor = new Give_Donor( $donor_id ); |
| 235 | $new_note = $donor->add_note( $donor_note ); |
| 236 | |
| 237 | /** |
| 238 | * Fires before inserting donor note. |
| 239 | * |
| 240 | * @param int $donor_id The ID of the donor. |
| 241 | * @param string $new_note Note content. |
| 242 | * |
| 243 | * @since 1.0 |
| 244 | */ |
| 245 | do_action( 'give_pre_insert_donor_note', $donor_id, $new_note ); |
| 246 | |
| 247 | if ( ! empty( $new_note ) && ! empty( $donor->id ) ) { |
| 248 | |
| 249 | ob_start(); |
| 250 | ?> |
| 251 | <div class="donor-note-wrapper dashboard-comment-wrap comment-item"> |
| 252 | <span class="note-content-wrap"> |
| 253 | <?php echo stripslashes( $new_note ); ?> |
| 254 | </span> |
| 255 | </div> |
| 256 | <?php |
| 257 | $output = ob_get_contents(); |
| 258 | ob_end_clean(); |
| 259 | |
| 260 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 261 | echo $output; |
| 262 | exit; |
| 263 | } |
| 264 | |
| 265 | return $new_note; |
| 266 | |
| 267 | } |
| 268 | |
| 269 | return false; |
| 270 | |
| 271 | } |
| 272 | |
| 273 | add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 ); |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Disconnect a user ID from a donor |
| 278 | * |
| 279 | * @param array $args Array of arguments. |
| 280 | * |
| 281 | * @since 1.0 |
| 282 | * |
| 283 | * @return bool|array If the disconnect was successful. |
| 284 | */ |
| 285 | function give_disconnect_donor_user_id( $args ) { |
| 286 | |
| 287 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 288 | |
| 289 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 290 | wp_die( |
| 291 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 292 | __( 'Error', 'give' ), |
| 293 | array( |
| 294 | 'response' => 403, |
| 295 | ) |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | if ( empty( $args ) ) { |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | $donor_id = (int) $args['customer_id']; |
| 304 | |
| 305 | $nonce = $args['_wpnonce']; |
| 306 | |
| 307 | if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) { |
| 308 | wp_die( |
| 309 | __( 'Cheatin’ uh?', 'give' ), |
| 310 | __( 'Error', 'give' ), |
| 311 | array( |
| 312 | 'response' => 400, |
| 313 | ) |
| 314 | ); |
| 315 | } |
| 316 | |
| 317 | $donor = new Give_Donor( $donor_id ); |
| 318 | if ( empty( $donor->id ) ) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | $user_id = $donor->user_id; |
| 323 | |
| 324 | /** |
| 325 | * Fires before disconnecting user ID from a donor. |
| 326 | * |
| 327 | * @param int $donor_id The ID of the donor. |
| 328 | * @param int $user_id The ID of the user. |
| 329 | * |
| 330 | * @since 1.0 |
| 331 | */ |
| 332 | do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id ); |
| 333 | |
| 334 | $output = array(); |
| 335 | $donor_args = array( |
| 336 | 'user_id' => 0, |
| 337 | ); |
| 338 | |
| 339 | $redirect_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id; |
| 340 | $is_donor_updated = $donor->update( $donor_args ); |
| 341 | |
| 342 | if ( $is_donor_updated ) { |
| 343 | |
| 344 | // Set meta for disconnected donor id and user id for future reference if needed. |
| 345 | update_user_meta( $user_id, '_give_disconnected_donor_id', $donor->id ); |
| 346 | $donor->update_meta( '_give_disconnected_user_id', $user_id ); |
| 347 | |
| 348 | $redirect_url = add_query_arg( |
| 349 | 'give-messages[]', |
| 350 | 'disconnect-user', |
| 351 | $redirect_url |
| 352 | ); |
| 353 | |
| 354 | $output['success'] = true; |
| 355 | |
| 356 | } else { |
| 357 | $output['success'] = false; |
| 358 | give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) ); |
| 359 | } |
| 360 | |
| 361 | $output['redirect'] = esc_url_raw( $redirect_url ); |
| 362 | |
| 363 | /** |
| 364 | * Fires after disconnecting user ID from a donor. |
| 365 | * |
| 366 | * @param int $donor_id The ID of the donor. |
| 367 | * |
| 368 | * @since 1.0 |
| 369 | */ |
| 370 | do_action( 'give_post_donor_disconnect_user_id', $donor_id ); |
| 371 | |
| 372 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 373 | header( 'Content-Type: application/json' ); |
| 374 | echo json_encode( $output ); |
| 375 | wp_die(); |
| 376 | } |
| 377 | |
| 378 | return $output; |
| 379 | |
| 380 | } |
| 381 | |
| 382 | add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 ); |
| 383 | |
| 384 | /** |
| 385 | * Add an email address to the donor from within the admin and log a donor note. |
| 386 | * |
| 387 | * @param array $args Array of arguments: nonce, donor id, and email address. |
| 388 | * |
| 389 | * @since 1.7 |
| 390 | * |
| 391 | * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string). |
| 392 | */ |
| 393 | function give_add_donor_email( $args ) { |
| 394 | |
| 395 | $donor_id = ''; |
| 396 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 397 | |
| 398 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 399 | wp_die( |
| 400 | __( 'You do not have permission to edit this donor.', 'give' ), |
| 401 | __( 'Error', 'give' ), |
| 402 | array( |
| 403 | 'response' => 403, |
| 404 | ) |
| 405 | ); |
| 406 | } |
| 407 | |
| 408 | $output = array(); |
| 409 | if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) { |
| 410 | $output['success'] = false; |
| 411 | if ( empty( $args['email'] ) ) { |
| 412 | $output['message'] = __( 'Email address is required.', 'give' ); |
| 413 | } elseif ( empty( $args['customer_id'] ) ) { |
| 414 | $output['message'] = __( 'Donor ID is required.', 'give' ); |
| 415 | } else { |
| 416 | $output['message'] = __( 'An error has occurred. Please try again.', 'give' ); |
| 417 | } |
| 418 | } elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) { |
| 419 | $output = array( |
| 420 | 'success' => false, |
| 421 | 'message' => __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 422 | ); |
| 423 | } elseif ( ! is_email( $args['email'] ) ) { |
| 424 | $output = array( |
| 425 | 'success' => false, |
| 426 | 'message' => __( 'Invalid email.', 'give' ), |
| 427 | ); |
| 428 | } else { |
| 429 | $email = sanitize_email( $args['email'] ); |
| 430 | $donor_id = (int) $args['customer_id']; |
| 431 | $primary = 'true' === $args['primary'] ? true : false; |
| 432 | $donor = new Give_Donor( $donor_id ); |
| 433 | if ( false === $donor->add_email( $email, $primary ) ) { |
| 434 | if ( in_array( $email, $donor->emails ) ) { |
| 435 | $output = array( |
| 436 | 'success' => false, |
| 437 | 'message' => __( 'Email already associated with this donor.', 'give' ), |
| 438 | ); |
| 439 | } else { |
| 440 | $output = array( |
| 441 | 'success' => false, |
| 442 | 'message' => __( 'Email address is already associated with another donor.', 'give' ), |
| 443 | ); |
| 444 | } |
| 445 | } else { |
| 446 | $redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-messages[]=email-added' ); |
| 447 | $output = array( |
| 448 | 'success' => true, |
| 449 | 'message' => __( 'Email successfully added to donor.', 'give' ), |
| 450 | 'redirect' => $redirect, |
| 451 | ); |
| 452 | |
| 453 | $user = wp_get_current_user(); |
| 454 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 455 | $donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login ); |
| 456 | $donor->add_note( $donor_note ); |
| 457 | |
| 458 | if ( $primary ) { |
| 459 | $donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login ); |
| 460 | $donor->add_note( $donor_note ); |
| 461 | } |
| 462 | } |
| 463 | } // End if(). |
| 464 | |
| 465 | do_action( 'give_post_add_donor_email', $donor_id, $args ); |
| 466 | |
| 467 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 468 | header( 'Content-Type: application/json' ); |
| 469 | echo json_encode( $output ); |
| 470 | wp_die(); |
| 471 | } |
| 472 | |
| 473 | return $output; |
| 474 | } |
| 475 | |
| 476 | add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 ); |
| 477 | |
| 478 | |
| 479 | /** |
| 480 | * 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. |
| 481 | * |
| 482 | * @since 1.7 |
| 483 | * |
| 484 | * @return bool|null |
| 485 | */ |
| 486 | function give_remove_donor_email() { |
| 487 | if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) { |
| 488 | return false; |
| 489 | } |
| 490 | if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) { |
| 491 | return false; |
| 492 | } |
| 493 | if ( empty( $_GET['_wpnonce'] ) ) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | $nonce = $_GET['_wpnonce']; |
| 498 | if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) { |
| 499 | wp_die( |
| 500 | __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 501 | __( 'Error', 'give' ), |
| 502 | array( |
| 503 | 'response' => 403, |
| 504 | ) |
| 505 | ); |
| 506 | } |
| 507 | |
| 508 | $donor = new Give_Donor( $_GET['id'] ); |
| 509 | if ( $donor->remove_email( $_GET['email'] ) ) { |
| 510 | $url = add_query_arg( 'give-messages[]', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) ); |
| 511 | $user = wp_get_current_user(); |
| 512 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 513 | $donor_note = sprintf( __( 'Email address %1$s removed by %2$s', 'give' ), $_GET['email'], $user_login ); |
| 514 | $donor->add_note( $donor_note ); |
| 515 | } else { |
| 516 | $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 ) ); |
| 517 | } |
| 518 | |
| 519 | wp_safe_redirect( esc_url_raw( $url ) ); |
| 520 | exit; |
| 521 | } |
| 522 | |
| 523 | add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 ); |
| 524 | |
| 525 | |
| 526 | /** |
| 527 | * Set an email address as the primary for a donor from within the admin and log a donor note |
| 528 | * and redirect back to the donor interface for feedback |
| 529 | * |
| 530 | * @since 1.7 |
| 531 | * |
| 532 | * @return bool|null |
| 533 | */ |
| 534 | function give_set_donor_primary_email() { |
| 535 | if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) { |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) { |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | if ( empty( $_GET['_wpnonce'] ) ) { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | $nonce = $_GET['_wpnonce']; |
| 548 | |
| 549 | if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) { |
| 550 | wp_die( |
| 551 | __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ), |
| 552 | __( 'Error', 'give' ), |
| 553 | array( |
| 554 | 'response' => 403, |
| 555 | ) |
| 556 | ); |
| 557 | } |
| 558 | |
| 559 | $donor = new Give_Donor( $_GET['id'] ); |
| 560 | |
| 561 | if ( $donor->set_primary_email( $_GET['email'] ) ) { |
| 562 | $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 ) ); |
| 563 | $user = wp_get_current_user(); |
| 564 | $user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' ); |
| 565 | $donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $_GET['email'], $user_login ); |
| 566 | |
| 567 | $donor->add_note( $donor_note ); |
| 568 | } else { |
| 569 | $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 ) ); |
| 570 | } |
| 571 | |
| 572 | wp_safe_redirect( esc_url_raw( $url ) ); |
| 573 | exit; |
| 574 | } |
| 575 | |
| 576 | add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 ); |
| 577 | |
| 578 | |
| 579 | /** |
| 580 | * This function will process the donor deletion. |
| 581 | * |
| 582 | * @param array $args Donor Deletion Arguments. |
| 583 | * |
| 584 | * @since 2.2 |
| 585 | */ |
| 586 | function give_process_donor_deletion( $args ) { |
| 587 | // Bailout. |
| 588 | if ( ! isset( $args['give-donor-delete-confirm'] ) ) { |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | $donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' ); |
| 593 | |
| 594 | // Verify user capabilities to proceed for deleting donor. |
| 595 | if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) { |
| 596 | wp_die( |
| 597 | esc_html__( 'You do not have permission to delete donors.', 'give' ), |
| 598 | esc_html__( 'Error', 'give' ), |
| 599 | array( |
| 600 | 'response' => 403, |
| 601 | ) |
| 602 | ); |
| 603 | } |
| 604 | |
| 605 | $nonce_action = ''; |
| 606 | if ( 'delete_bulk_donor' === $args['give_action'] ) { |
| 607 | $nonce_action = 'bulk-donors'; |
| 608 | } elseif ( 'delete_donor' === $args['give_action'] ) { |
| 609 | $nonce_action = 'give-delete-donor'; |
| 610 | } |
| 611 | |
| 612 | // Verify Nonce for deleting bulk donors. |
| 613 | give_validate_nonce( $args['_wpnonce'], $nonce_action ); |
| 614 | |
| 615 | $redirect_args = array(); |
| 616 | $donor_ids = ( isset( $args['donor'] ) && is_array( $args['donor'] ) ) ? $args['donor'] : array( $args['donor_id'] ); |
| 617 | $redirect_args['order'] = ! empty( $args['order'] ) ? $args['order'] : 'DESC'; |
| 618 | $redirect_args['orderby'] = ! empty( $args['orderby'] ) ? strtolower( $args['orderby'] ) : 'id'; |
| 619 | $redirect_args['s'] = ! empty( $args['s'] ) ? $args['s'] : ''; |
| 620 | $delete_donor = ! empty( $args['give-donor-delete-confirm'] ) ? give_is_setting_enabled( $args['give-donor-delete-confirm'] ) : false; |
| 621 | $delete_donation = ! empty( $args['give-donor-delete-records'] ) ? give_is_setting_enabled( $args['give-donor-delete-records'] ) : false; |
| 622 | |
| 623 | if ( count( $donor_ids ) > 0 ) { |
| 624 | |
| 625 | // Loop through the selected donors to delete. |
| 626 | foreach ( $donor_ids as $donor_id ) { |
| 627 | |
| 628 | $donor = new Give_Donor( $donor_id ); |
| 629 | |
| 630 | // Proceed only if valid donor id is provided. |
| 631 | if ( $donor->id > 0 ) { |
| 632 | |
| 633 | /** |
| 634 | * Fires before deleting donor. |
| 635 | * |
| 636 | * @param int $donor_id The ID of the donor. |
| 637 | * @param bool $delete_donor Confirm Donor Deletion. |
| 638 | * @param bool $delete_donation Confirm Donor related donations deletion. |
| 639 | * |
| 640 | * @since 1.0 |
| 641 | */ |
| 642 | do_action( 'give_pre_delete_donor', $donor->id, $delete_donor, $delete_donation ); |
| 643 | |
| 644 | // Proceed only, if user confirmed whether they need to delete the donor. |
| 645 | if ( $delete_donor ) { |
| 646 | |
| 647 | // Delete donor and linked donations. |
| 648 | $donor_delete_status = give_delete_donor_and_related_donation( |
| 649 | $donor, |
| 650 | array( |
| 651 | 'delete_donation' => $delete_donation, |
| 652 | ) |
| 653 | ); |
| 654 | |
| 655 | if ( 1 === $donor_delete_status ) { |
| 656 | $redirect_args['give-messages[]'] = 'donor-deleted'; |
| 657 | } elseif ( 2 === $donor_delete_status ) { |
| 658 | $redirect_args['give-messages[]'] = 'donor-donations-deleted'; |
| 659 | } |
| 660 | } else { |
| 661 | $redirect_args['give-messages[]'] = 'confirm-delete-donor'; |
| 662 | } |
| 663 | } else { |
| 664 | $redirect_args['give-messages[]'] = 'invalid-donor-id'; |
| 665 | } // End if(). |
| 666 | } // End foreach(). |
| 667 | } else { |
| 668 | $redirect_args['give-messages[]'] = 'no-donor-found'; |
| 669 | } // End if(). |
| 670 | |
| 671 | $redirect_url = add_query_arg( |
| 672 | $redirect_args, |
| 673 | admin_url( 'edit.php?post_type=give_forms&page=give-donors' ) |
| 674 | ); |
| 675 | |
| 676 | wp_safe_redirect( esc_url_raw( $redirect_url ) ); |
| 677 | give_die(); |
| 678 | |
| 679 | } |
| 680 | add_action( 'give_delete_donor', 'give_process_donor_deletion' ); |
| 681 | add_action( 'give_delete_bulk_donor', 'give_process_donor_deletion' ); |
| 682 |