donor-actions.php ➔ give_process_donor_deletion()   F
last analyzed

Complexity

Conditions 18
Paths 1536

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 1536
nop 1
dl 0
loc 87
rs 0.7
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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( esc_html__( 'You do not have permission to edit this donor.', 'give' ), esc_html__( 'Error', 'give' ), array(
32
			'response' => 403,
33
		) );
34
	}
35
36
	if ( empty( $args ) ) {
37
		return false;
38
	}
39
40
	// Sanitize Data.
41
	$args = give_clean( $args );
42
43
	// Verify Nonce.
44 View Code Duplication
	if ( ! wp_verify_nonce( $args['_wpnonce'], 'edit-donor' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
		wp_die( esc_html__( 'Cheatin&#8217; uh?', 'give' ), esc_html__( 'Error', 'give' ), array(
46
			'response' => 400,
47
		) );
48
	}
49
50
	$donor_info = $args['donor_info'];
51
	$donor_id   = intval( $donor_info['id'] );
52
53
	$donor = new Give_Donor( $donor_id );
54
55
	// Bailout, if donor id doesn't exists.
56
	if ( empty( $donor->id ) ) {
57
		return false;
58
	}
59
60
	$defaults = array(
61
		'title'   => '',
62
		'name'    => '',
63
		'user_id' => 0,
64
		'line1'   => '',
65
		'line2'   => '',
66
		'city'    => '',
67
		'zip'     => '',
68
		'state'   => '',
69
		'country' => '',
70
	);
71
72
	$donor_info = wp_parse_args( $donor_info, $defaults );
73
74
	if ( (int) $donor_info['user_id'] !== (int) $donor->user_id ) {
75
76
		// Make sure we don't already have this user attached to a donor.
77
		if ( ! empty( $donor_info['user_id'] ) && false !== Give()->donors->get_donor_by( 'user_id', $donor_info['user_id'] ) ) {
78
			give_set_error(
79
				'give-invalid-donor-user_id',
80
				sprintf(
81
					/* translators: %d User ID */
82
					__( 'The User ID #%d is already associated with a different donor.', 'give' ),
83
					$donor_info['user_id']
84
				)
85
			);
86
		}
87
88
		// Make sure it's actually a user.
89
		$user = get_user_by( 'id', $donor_info['user_id'] );
90
		if ( ! empty( $donor_info['user_id'] ) && false === $user ) {
91
			give_set_error(
92
				'give-invalid-user_id',
93
				sprintf(
94
					/* translators: %d User ID */
95
					__( 'The User ID #%d does not exist. Please assign an existing user.', 'give' ),
96
					$donor_info['user_id']
97
				)
98
			);
99
		}
100
	}
101
102
	// Bailout, if errors are present.
103
	if ( give_get_errors() ) {
104
		return false;
105
	}
106
107
	$donor->update_meta( '_give_anonymous_donor', absint( $args['give_anonymous_donor'] ) );
108
109
	// Save company name in when admin update donor company name from dashboard.
110
	$donor->update_meta( '_give_donor_company', sanitize_text_field( $args['give_donor_company'] ) );
111
112
	// If First name of donor is empty, then fetch the current first name of donor.
113
	if ( empty( $donor_info['first_name'] ) ) {
114
		$donor_info['first_name'] = $donor->get_first_name();
115
	}
116
117
	// Sanitize the inputs.
118
	$donor_data               = array();
119
	$donor_data['name']       = trim( "{$donor_info['first_name']} {$donor_info['last_name']}" );
120
	$donor_data['first_name'] = $donor_info['first_name'];
121
	$donor_data['last_name']  = $donor_info['last_name'];
122
	$donor_data['title']      = $donor_info['title'];
123
	$donor_data['user_id']    = $donor_info['user_id'];
124
125
	$donor_data = apply_filters( 'give_edit_donor_info', $donor_data, $donor_id );
126
127
	/**
128
	 * Filter the address
129
	 *
130
	 * @todo unnecessary filter because we are not storing donor address to user.
131
	 *
132
	 * @since 1.0
133
	 */
134
	$address = apply_filters( 'give_edit_donor_address', array(), $donor_id );
135
136
	$donor_data = give_clean( $donor_data );
137
	$address    = give_clean( $address );
138
139
	$output = give_connect_user_donor_profile( $donor, $donor_data, $address );
0 ignored issues
show
Bug introduced by
It seems like $donor_data defined by give_clean($donor_data) on line 136 can also be of type string; however, give_connect_user_donor_profile() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $address defined by give_clean($address) on line 137 can also be of type string; however, give_connect_user_donor_profile() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
140
141 View Code Duplication
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
		header( 'Content-Type: application/json' );
143
		echo wp_json_encode( $output );
144
		wp_die();
145
	}
146
147
	if ( $output['success'] ) {
148
		wp_safe_redirect( add_query_arg(
149
			array(
150
				'post_type'       => 'give_forms',
151
				'page'            => 'give-donors',
152
				'view'            => 'overview',
153
				'id'              => $donor_id,
154
				'give-messages[]' => 'profile-updated'
155
			),
156
			esc_url( admin_url( 'edit.php' ) )
157
		) );
158
	}
159
160
	exit;
161
162
}
163
164
add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 );
165
166
/**
167
 * Save a donor note.
168
 *
169
 * @param array $args The $_POST array being passed.
170
 *
171
 * @since 1.0
172
 *
173
 * @return int The Note ID that was saved, or 0 if nothing was saved.
174
 */
175
function give_donor_save_note( $args ) {
176
177
	$donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' );
178
179 View Code Duplication
	if ( ! is_admin() || ! current_user_can( $donor_view_role ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
181
			'response' => 403,
182
		) );
183
	}
184
185
	if ( empty( $args ) ) {
186
		return false;
187
	}
188
189
	$donor_note = trim( give_clean( $args['donor_note'] ) );
190
	$donor_id   = (int) $args['customer_id'];
191
	$nonce      = $args['add_donor_note_nonce'];
192
193
	if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) {
194
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
195
			'response' => 400,
196
		) );
197
	}
198
199
	if ( empty( $donor_note ) ) {
200
		give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) );
201
	}
202
203
	if ( give_get_errors() ) {
204
		return false;
205
	}
206
207
	$donor    = new Give_Donor( $donor_id );
208
	$new_note = $donor->add_note( $donor_note );
209
210
	/**
211
	 * Fires before inserting donor note.
212
	 *
213
	 * @param int    $donor_id The ID of the donor.
214
	 * @param string $new_note Note content.
215
	 *
216
	 * @since 1.0
217
	 */
218
	do_action( 'give_pre_insert_donor_note', $donor_id, $new_note );
219
220
	if ( ! empty( $new_note ) && ! empty( $donor->id ) ) {
221
222
		ob_start();
223
		?>
224
		<div class="donor-note-wrapper dashboard-comment-wrap comment-item">
225
			<span class="note-content-wrap">
226
				<?php echo stripslashes( $new_note ); ?>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'stripslashes'
Loading history...
227
			</span>
228
		</div>
229
		<?php
230
		$output = ob_get_contents();
231
		ob_end_clean();
232
233
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
234
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
235
			exit;
236
		}
237
238
		return $new_note;
239
240
	}
241
242
	return false;
243
244
}
245
246
add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 );
247
248
249
/**
250
 * Disconnect a user ID from a donor
251
 *
252
 * @param array $args Array of arguments.
253
 *
254
 * @since 1.0
255
 *
256
 * @return bool|array If the disconnect was successful.
257
 */
258
function give_disconnect_donor_user_id( $args ) {
259
260
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
261
262 View Code Duplication
	if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
264
			'response' => 403,
265
		) );
266
	}
267
268
	if ( empty( $args ) ) {
269
		return false;
270
	}
271
272
	$donor_id = (int) $args['customer_id'];
273
274
	$nonce = $args['_wpnonce'];
275
276
	if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) {
277
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
278
			'response' => 400,
279
		) );
280
	}
281
282
	$donor = new Give_Donor( $donor_id );
283
	if ( empty( $donor->id ) ) {
284
		return false;
285
	}
286
287
	$user_id = $donor->user_id;
288
289
	/**
290
	 * Fires before disconnecting user ID from a donor.
291
	 *
292
	 * @param int $donor_id The ID of the donor.
293
	 * @param int $user_id  The ID of the user.
294
	 *
295
	 * @since 1.0
296
	 */
297
	do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id );
298
299
	$output     = array();
300
	$donor_args = array(
301
		'user_id' => 0,
302
	);
303
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
304
305
	$output['success'] = true;
306
	if ( ! $donor->update( $donor_args ) ) {
307
		update_user_meta( $user_id, '_give_is_donor_disconnected', true );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
308
		update_user_meta( $user_id, '_give_disconnected_donor_id', $donor->id );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
309
		$donor->update_meta( '_give_disconnected_user_id', $user_id );
310
311
		$output['success'] = true;
312
313
	} else {
314
		$output['success'] = false;
315
		give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
316
	}
317
318
	$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
319
320
	/**
321
	 * Fires after disconnecting user ID from a donor.
322
	 *
323
	 * @param int $donor_id The ID of the donor.
324
	 *
325
	 * @since 1.0
326
	 */
327
	do_action( 'give_post_donor_disconnect_user_id', $donor_id );
328
329 View Code Duplication
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
330
		header( 'Content-Type: application/json' );
331
		echo json_encode( $output );
332
		wp_die();
333
	}
334
335
	return $output;
336
337
}
338
339
add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 );
340
341
/**
342
 * Add an email address to the donor from within the admin and log a donor note.
343
 *
344
 * @param array $args Array of arguments: nonce, donor id, and email address.
345
 *
346
 * @since 1.7
347
 *
348
 * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string).
349
 */
350
function give_add_donor_email( $args ) {
351
352
	$donor_id = '';
353
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
354
355 View Code Duplication
	if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
356
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
357
			'response' => 403,
358
		) );
359
	}
360
361
	$output = array();
362
	if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) {
363
		$output['success'] = false;
364
		if ( empty( $args['email'] ) ) {
365
			$output['message'] = __( 'Email address is required.', 'give' );
366
		} elseif ( empty( $args['customer_id'] ) ) {
367
			$output['message'] = __( 'Donor ID is required.', 'give' );
368
		} else {
369
			$output['message'] = __( 'An error has occurred. Please try again.', 'give' );
370
		}
371
	} elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) {
372
		$output = array(
373
			'success' => false,
374
			'message' => __( 'Nonce verification failed.', 'give' ),
375
		);
376
	} elseif ( ! is_email( $args['email'] ) ) {
377
		$output = array(
378
			'success' => false,
379
			'message' => __( 'Invalid email.', 'give' ),
380
		);
381
	} else {
382
		$email    = sanitize_email( $args['email'] );
383
		$donor_id = (int) $args['customer_id'];
384
		$primary  = 'true' === $args['primary'] ? true : false;
385
		$donor    = new Give_Donor( $donor_id );
386
		if ( false === $donor->add_email( $email, $primary ) ) {
387
			if ( in_array( $email, $donor->emails ) ) {
388
				$output = array(
389
					'success' => false,
390
					'message' => __( 'Email already associated with this donor.', 'give' ),
391
				);
392
			} else {
393
				$output = array(
394
					'success' => false,
395
					'message' => __( 'Email address is already associated with another donor.', 'give' ),
396
				);
397
			}
398
		} else {
399
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-messages[]=email-added' );
400
			$output   = array(
401
				'success'  => true,
402
				'message'  => __( 'Email successfully added to donor.', 'give' ),
403
				'redirect' => $redirect,
404
			);
405
406
			$user       = wp_get_current_user();
407
			$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
408
			$donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login );
409
			$donor->add_note( $donor_note );
410
411
			if ( $primary ) {
412
				$donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login );
413
				$donor->add_note( $donor_note );
414
			}
415
		}
416
	} // End if().
417
418
	do_action( 'give_post_add_donor_email', $donor_id, $args );
419
420 View Code Duplication
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
421
		header( 'Content-Type: application/json' );
422
		echo json_encode( $output );
423
		wp_die();
424
	}
425
426
	return $output;
427
}
428
429
add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 );
430
431
432
/**
433
 * 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.
434
 *
435
 * @since  1.7
436
 *
437
 * @return bool|null
438
 */
439 View Code Duplication
function give_remove_donor_email() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
440
	if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
441
		return false;
442
	}
443
	if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
444
		return false;
445
	}
446
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
447
		return false;
448
	}
449
450
	$nonce = $_GET['_wpnonce'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
451
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
452
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
453
			'response' => 403,
454
		) );
455
	}
456
457
	$donor = new Give_Donor( $_GET['id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
458
	if ( $donor->remove_email( $_GET['email'] ) ) {
459
		$url        = add_query_arg( 'give-messages[]', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) );
460
		$user       = wp_get_current_user();
461
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
462
		$donor_note = sprintf( __( 'Email address %1$s removed by %2$s', 'give' ), $_GET['email'], $user_login );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
463
		$donor->add_note( $donor_note );
464
	} else {
465
		$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 ) );
466
	}
467
468
	wp_safe_redirect( $url );
469
	exit;
470
}
471
472
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
473
474
475
/**
476
 * Set an email address as the primary for a donor from within the admin and log a donor note
477
 * and redirect back to the donor interface for feedback
478
 *
479
 * @since  1.7
480
 *
481
 * @return bool|null
482
 */
483 View Code Duplication
function give_set_donor_primary_email() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
484
	if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
485
		return false;
486
	}
487
488
	if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
489
		return false;
490
	}
491
492
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
493
		return false;
494
	}
495
496
	$nonce = $_GET['_wpnonce'];
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
497
498
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
499
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
500
			'response' => 403,
501
		) );
502
	}
503
504
	$donor = new Give_Donor( $_GET['id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
505
506
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
507
		$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 ) );
508
		$user       = wp_get_current_user();
509
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
510
		$donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $_GET['email'], $user_login );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
511
512
		$donor->add_note( $donor_note );
513
	} else {
514
		$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 ) );
515
	}
516
517
	wp_safe_redirect( $url );
518
	exit;
519
}
520
521
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
522
523
524
/**
525
 * This function will process the donor deletion.
526
 *
527
 * @param array $args Donor Deletion Arguments.
528
 *
529
 * @since 2.2
530
 */
531
function give_process_donor_deletion( $args ) {
532
533
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
534
535
	// Verify user capabilities to proceed for deleting donor.
536
	if ( ! is_admin() || ! current_user_can( $donor_edit_role ) ) {
537
		wp_die(
538
			esc_html__( 'You do not have permission to delete donors.', 'give' ),
539
			esc_html__( 'Error', 'give' ),
540
			array(
541
				'response' => 403,
542
			)
543
		);
544
	}
545
546
	$nonce_action = '';
547
	if ( 'delete_bulk_donor' === $args['give_action'] ) {
548
		$nonce_action = 'bulk-donors';
549
	} elseif ( 'delete_donor' === $args['give_action'] ) {
550
		$nonce_action = 'give-delete-donor';
551
	}
552
553
	// Verify Nonce for deleting bulk donors.
554
	give_validate_nonce( $args['_wpnonce'], $nonce_action );
555
556
	$redirect_args            = array();
557
	$donor_ids                = ( isset( $args['donor'] ) && is_array( $args['donor'] ) ) ? $args['donor'] : array( $args['donor_id'] );
558
	$redirect_args['order']   = ! empty( $args['order'] ) ? $args['order'] : 'DESC';
559
	$redirect_args['orderby'] = ! empty( $args['orderby'] ) ? $args['orderby'] : 'ID';
560
	$redirect_args['s']       = ! empty( $args['s'] ) ? $args['s'] : '';
561
	$delete_donor             = ! empty( $args['give-donor-delete-confirm'] ) ? give_is_setting_enabled( $args['give-donor-delete-confirm'] ) : false;
562
	$delete_donation          = ! empty( $args['give-donor-delete-records'] ) ? give_is_setting_enabled( $args['give-donor-delete-records'] ) : false;
563
564
	if ( count( $donor_ids ) > 0 ) {
565
566
		// Loop through the selected donors to delete.
567
		foreach ( $donor_ids as $donor_id ) {
568
569
			$donor = new Give_Donor( $donor_id );
570
571
			// Proceed only if valid donor id is provided.
572
			if ( $donor->id > 0 ) {
573
574
				/**
575
				 * Fires before deleting donor.
576
				 *
577
				 * @param int  $donor_id     The ID of the donor.
578
				 * @param bool $delete_donor Confirm Donor Deletion.
579
				 * @param bool $delete_donation  Confirm Donor related donations deletion.
580
				 *
581
				 * @since 1.0
582
				 */
583
				do_action( 'give_pre_delete_donor', $donor->id, $delete_donor, $delete_donation );
584
585
				// Proceed only, if user confirmed whether they need to delete the donor.
586
				if ( $delete_donor ) {
587
588
					// Delete donor and linked donations.
589
					$donor_delete_status = give_delete_donor_and_related_donation( $donor, array(
590
						'delete_donation' => $delete_donation,
591
					) );
592
593
					if ( 1 === $donor_delete_status ) {
594
						$redirect_args['give-messages[]'] = 'donor-deleted';
595
					} elseif ( 2 === $donor_delete_status ) {
596
						$redirect_args['give-messages[]'] = 'donor-donations-deleted';
597
					}
598
				} else {
599
					$redirect_args['give-messages[]'] = 'confirm-delete-donor';
600
				}
601
			} else {
602
				$redirect_args['give-messages[]'] = 'invalid-donor-id';
603
			} // End if().
604
		} // End foreach().
605
	} else {
606
		$redirect_args['give-messages[]'] = 'no-donor-found';
607
	} // End if().
608
609
	$redirect_url = add_query_arg(
610
		$redirect_args,
611
		admin_url( 'edit.php?post_type=give_forms&page=give-donors' )
612
	);
613
614
	wp_safe_redirect( $redirect_url );
615
	give_die();
616
617
}
618
add_action( 'give_delete_donor', 'give_process_donor_deletion' );
619
add_action( 'give_delete_bulk_donor', 'give_process_donor_deletion' );
620