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