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