Test Failed
Pull Request — master (#2482)
by Devin
05:37
created

donor-actions.php ➔ give_delete_donor()   F

Complexity

Conditions 21
Paths 13056

Size

Total Lines 75
Code Lines 42

Duplication

Lines 5
Ratio 6.67 %

Importance

Changes 0
Metric Value
cc 21
eloc 42
nc 13056
nop 1
dl 5
loc 75
rs 2.3844
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 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...
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 = $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&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
46
			'response' => 400,
47
		) );
48
	}
49
50
	$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
	// Setup the donor address, if present.
88
	$address = array();
89
	if ( intval( $donor_info['user_id'] ) > 0 ) {
90
91
		$current_address = (array) get_user_meta( $donor_info['user_id'], '_give_user_address', true );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
92
93
		if ( is_array( $current_address ) && 0 < count( $current_address ) ) {
94
			$current_address    = wp_parse_args( $current_address, $defaults );
95
			$address['line1']   = ! empty( $donor_info['line1'] ) ? $donor_info['line1'] : $current_address['line1'];
96
			$address['line2']   = ! empty( $donor_info['line2'] ) ? $donor_info['line2'] : $current_address['line2'];
97
			$address['city']    = ! empty( $donor_info['city'] ) ? $donor_info['city'] : $current_address['city'];
98
			$address['country'] = ! empty( $donor_info['country'] ) ? $donor_info['country'] : $current_address['country'];
99
			$address['zip']     = ! empty( $donor_info['zip'] ) ? $donor_info['zip'] : $current_address['zip'];
100
			$address['state']   = ! empty( $donor_info['state'] ) ? $donor_info['state'] : $current_address['state'];
101
		} else {
102
			$address['line1']   = ! empty( $donor_info['line1'] ) ? $donor_info['line1'] : '';
103
			$address['line2']   = ! empty( $donor_info['line2'] ) ? $donor_info['line2'] : '';
104
			$address['city']    = ! empty( $donor_info['city'] ) ? $donor_info['city'] : '';
105
			$address['country'] = ! empty( $donor_info['country'] ) ? $donor_info['country'] : '';
106
			$address['zip']     = ! empty( $donor_info['zip'] ) ? $donor_info['zip'] : '';
107
			$address['state']   = ! empty( $donor_info['state'] ) ? $donor_info['state'] : '';
108
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
109
110
	}
111
112
	// Sanitize the inputs.
113
	$donor_data            = array();
114
	$donor_data['name']    = strip_tags( stripslashes( $donor_info['name'] ) );
115
	$donor_data['user_id'] = $donor_info['user_id'];
116
117
	$donor_data = apply_filters( 'give_edit_donor_info', $donor_data, $donor_id );
118
	$address    = apply_filters( 'give_edit_donor_address', $address, $donor_id );
119
120
	$donor_data = array_map( 'sanitize_text_field', $donor_data );
121
	$address    = array_map( 'sanitize_text_field', $address );
122
123
	$output = give_connect_user_donor_profile( $donor, $donor_data, $address );
124
125 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...
126
		header( 'Content-Type: application/json' );
127
		echo json_encode( $output );
128
		wp_die();
129
	}
130
131
	if ( $output['success'] ) {
132
		wp_redirect( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id={$donor_id}&give-message=profile-updated" ) );
133
	}
134
135
	exit;
136
137
}
138
139
add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 );
140
141
/**
142
 * Save a donor note.
143
 *
144
 * @param array $args The $_POST array being passed.
145
 *
146
 * @since 1.0
147
 *
148
 * @return int The Note ID that was saved, or 0 if nothing was saved.
149
 */
150
function give_donor_save_note( $args ) {
151
152
	$donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' );
153
154 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...
155
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
156
			'response' => 403,
157
		) );
158
	}
159
160
	if ( empty( $args ) ) {
161
		return false;
162
	}
163
164
	$donor_note = trim( sanitize_text_field( $args['donor_note'] ) );
165
	$donor_id   = (int) $args['customer_id'];
166
	$nonce      = $args['add_donor_note_nonce'];
167
168
	if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) {
169
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
170
			'response' => 400,
171
		) );
172
	}
173
174
	if ( empty( $donor_note ) ) {
175
		give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) );
176
	}
177
178
	if ( give_get_errors() ) {
179
		return false;
180
	}
181
182
	$donor    = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
	$new_note = $donor->add_note( $donor_note );
184
185
	/**
186
	 * Fires before inserting donor note.
187
	 *
188
	 * @param int    $donor_id The ID of the donor.
189
	 * @param string $new_note Note content.
190
	 *
191
	 * @since 1.0
192
	 */
193
	do_action( 'give_pre_insert_donor_note', $donor_id, $new_note );
194
195
	if ( ! empty( $new_note ) && ! empty( $donor->id ) ) {
196
197
		ob_start();
198
		?>
199
		<div class="donor-note-wrapper dashboard-comment-wrap comment-item">
200
			<span class="note-content-wrap">
201
				<?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...
202
			</span>
203
		</div>
204
		<?php
205
		$output = ob_get_contents();
206
		ob_end_clean();
207
208
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
209
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
210
			exit;
211
		}
212
213
		return $new_note;
214
215
	}
216
217
	return false;
218
219
}
220
221
add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 );
222
223
/**
224
 * Delete a donor.
225
 *
226
 * @param array $args The $_POST array being passed.
227
 *
228
 * @since 1.0
229
 *
230
 * @return int Whether it was a successful deletion.
231
 */
232
function give_donor_delete( $args ) {
233
234
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
235
236 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...
237
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
238
			'response' => 403,
239
		) );
240
	}
241
242
	if ( empty( $args ) ) {
243
		return false;
244
	}
245
246
	$donor_id    = (int) $args['customer_id'];
247
	$confirm     = ! empty( $args['give-donor-delete-confirm'] ) ? true : false;
248
	$remove_data = ! empty( $args['give-donor-delete-records'] ) ? true : false;
249
	$nonce       = $args['_wpnonce'];
250
251
	if ( ! wp_verify_nonce( $nonce, 'delete-donor' ) ) {
252
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
253
			'response' => 400,
254
		) );
255
	}
256
257
	if ( ! $confirm ) {
258
		give_set_error( 'donor-delete-no-confirm', __( 'Please confirm you want to delete this donor.', 'give' ) );
259
	}
260
261
	if ( give_get_errors() ) {
262
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id ) );
263
		exit;
264
	}
265
266
	$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
267
268
	/**
269
	 * Fires before deleting donor.
270
	 *
271
	 * @param int  $donor_id    The ID of the donor.
272
	 * @param bool $confirm     Delete confirmation.
273
	 * @param bool $remove_data Records delete confirmation.
274
	 *
275
	 * @since 1.0
276
	 */
277
	do_action( 'give_pre_delete_donor', $donor_id, $confirm, $remove_data );
278
279
	if ( $donor->id > 0 ) {
280
281
		$payments_array = explode( ',', $donor->payment_ids );
282
		$success        = Give()->donors->delete( $donor->id );
283
284
		if ( $success ) {
285
286
			if ( $remove_data ) {
287
288
				// Remove all donations, logs, etc.
289
				foreach ( $payments_array as $payment_id ) {
290
					give_delete_donation( $payment_id );
291
				}
292
			} else {
293
294
				// Just set the donations to customer_id of 0.
295
				foreach ( $payments_array as $payment_id ) {
296
					give_update_payment_meta( $payment_id, '_give_payment_customer_id', 0 );
297
				}
298
			}
299
300
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&give-message=donor-deleted' );
301
302
		} else {
303
304
			give_set_error( 'give-donor-delete-failed', __( 'Error deleting donor.', 'give' ) );
305
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor_id );
306
307
		}
308
	} else {
309
310
		give_set_error( 'give-donor-delete-invalid-id', __( 'Invalid Donor ID.', 'give' ) );
311
		$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors' );
312
313
	}
314
315
	wp_redirect( $redirect );
316
	exit;
317
318
}
319
320
add_action( 'give_delete-donor', 'give_donor_delete', 10, 1 );
321
322
/**
323
 * Disconnect a user ID from a donor
324
 *
325
 * @param array $args Array of arguments.
326
 *
327
 * @since 1.0
328
 *
329
 * @return bool|array If the disconnect was successful.
330
 */
331
function give_disconnect_donor_user_id( $args ) {
332
333
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
334
335 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...
336
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
337
			'response' => 403,
338
		) );
339
	}
340
341
	if ( empty( $args ) ) {
342
		return false;
343
	}
344
345
	$donor_id = (int) $args['customer_id'];
346
347
	$nonce = $args['_wpnonce'];
348
349
	if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) {
350
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
351
			'response' => 400,
352
		) );
353
	}
354
355
	$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
356
	if ( empty( $donor->id ) ) {
357
		return false;
358
	}
359
360
	$user_id = $donor->user_id;
361
362
	/**
363
	 * Fires before disconnecting user ID from a donor.
364
	 *
365
	 * @param int $donor_id The ID of the donor.
366
	 * @param int $user_id  The ID of the user.
367
	 *
368
	 * @since 1.0
369
	 */
370
	do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id );
371
372
	$output     = array();
373
	$donor_args = array(
374
		'user_id' => 0,
375
	);
376
377
	if ( $donor->update( $donor_args ) ) {
378
		global $wpdb;
379
380
		if ( ! empty( $donor->payment_ids ) ) {
381
			$wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = '_give_payment_user_id' AND post_id IN ( $donor->payment_ids )" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
382
		}
383
384
		// Set Donor Disconnection status true, if user and donor are disconnected with each other.
385
		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...
386
		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...
387
		$donor->update_meta( '_give_disconnected_user_id', $user_id );
388
389
		$output['success'] = true;
390
391
	} else {
392
393
		$output['success'] = false;
394
		give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
395
	}
396
397
	$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
398
399
	/**
400
	 * Fires after disconnecting user ID from a donor.
401
	 *
402
	 * @param int $donor_id The ID of the donor.
403
	 *
404
	 * @since 1.0
405
	 */
406
	do_action( 'give_post_donor_disconnect_user_id', $donor_id );
407
408 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...
409
		header( 'Content-Type: application/json' );
410
		echo json_encode( $output );
411
		wp_die();
412
	}
413
414
	return $output;
415
416
}
417
418
add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 );
419
420
/**
421
 * Add an email address to the donor from within the admin and log a donor note.
422
 *
423
 * @param array $args Array of arguments: nonce, donor id, and email address.
424
 *
425
 * @since 1.7
426
 *
427
 * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string).
428
 */
429
function give_add_donor_email( $args ) {
430
431
	$donor_id = '';
432
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
433
434 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...
435
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
436
			'response' => 403,
437
		) );
438
	}
439
440
	$output = array();
441
	if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) {
442
		$output['success'] = false;
443
		if ( empty( $args['email'] ) ) {
444
			$output['message'] = __( 'Email address is required.', 'give' );
445
		} elseif ( empty( $args['customer_id'] ) ) {
446
			$output['message'] = __( 'Donor ID is required.', 'give' );
447
		} else {
448
			$output['message'] = __( 'An error has occurred. Please try again.', 'give' );
449
		}
450
	} elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) {
451
		$output = array(
452
			'success' => false,
453
			'message' => __( 'Nonce verification failed.', 'give' ),
454
		);
455
	} elseif ( ! is_email( $args['email'] ) ) {
456
		$output = array(
457
			'success' => false,
458
			'message' => __( 'Invalid email.', 'give' ),
459
		);
460
	} else {
461
		$email    = sanitize_email( $args['email'] );
462
		$donor_id = (int) $args['customer_id'];
463
		$primary  = 'true' === $args['primary'] ? true : false;
464
		$donor    = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
465
		if ( false === $donor->add_email( $email, $primary ) ) {
466
			if ( in_array( $email, $donor->emails ) ) {
467
				$output = array(
468
					'success' => false,
469
					'message' => __( 'Email already associated with this donor.', 'give' ),
470
				);
471
			} else {
472
				$output = array(
473
					'success' => false,
474
					'message' => __( 'Email address is already associated with another donor.', 'give' ),
475
				);
476
			}
477
		} else {
478
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-message=email-added' );
479
			$output   = array(
480
				'success'  => true,
481
				'message'  => __( 'Email successfully added to donor.', 'give' ),
482
				'redirect' => $redirect,
483
			);
484
485
			$user       = wp_get_current_user();
486
			$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
487
			$donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login );
488
			$donor->add_note( $donor_note );
489
490
			if ( $primary ) {
491
				$donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login );
492
				$donor->add_note( $donor_note );
493
			}
494
		}
495
	} // End if().
496
497
	do_action( 'give_post_add_donor_email', $donor_id, $args );
498
499 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...
500
		header( 'Content-Type: application/json' );
501
		echo json_encode( $output );
502
		wp_die();
503
	}
504
505
	return $output;
506
}
507
508
add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 );
509
510
511
/**
512
 * 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.
513
 *
514
 * @since  1.7
515
 *
516
 * @return bool|null
517
 */
518 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...
519
	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...
520
		return false;
521
	}
522
	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...
523
		return false;
524
	}
525
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
526
		return false;
527
	}
528
529
	$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...
530
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
531
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
532
			'response' => 403,
533
		) );
534
	}
535
536
	$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...
537
	if ( $donor->remove_email( $_GET['email'] ) ) {
538
		$url        = add_query_arg( 'give-message', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) );
539
		$user       = wp_get_current_user();
540
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
541
		$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...
542
		$donor->add_note( $donor_note );
543
	} else {
544
		$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 ) );
545
	}
546
547
	wp_safe_redirect( $url );
548
	exit;
549
}
550
551
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
552
553
554
/**
555
 * Set an email address as the primary for a donor from within the admin and log a donor note
556
 * and redirect back to the donor interface for feedback
557
 *
558
 * @since  1.7
559
 *
560
 * @return bool|null
561
 */
562 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...
563
	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...
564
		return false;
565
	}
566
567
	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...
568
		return false;
569
	}
570
571
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
572
		return false;
573
	}
574
575
	$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...
576
577
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
578
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
579
			'response' => 403,
580
		) );
581
	}
582
583
	$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...
584
585
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
586
		$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 ) );
587
		$user       = wp_get_current_user();
588
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
589
		$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...
590
591
		$donor->add_note( $donor_note );
592
	} else {
593
		$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 ) );
594
	}
595
596
	wp_safe_redirect( $url );
597
	exit;
598
}
599
600
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
601
602
/**
603
 * Delete Donor using Bulk Actions.
604
 *
605
 * @param array $args An array of donor arguments.
606
 *
607
 * @since 1.8.17
608
 *
609
 * @return void
610
 */
611
function give_delete_donor( $args ) {
612
613
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
614
615 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...
616
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
617
			'response' => 403,
618
		) );
619
	}
620
621
	$give_args            = array();
622
	$donor_ids            = ( ! empty( $_GET['donor'] ) && is_array( $_GET['donor'] ) && count( $_GET['donor'] ) > 0 ) ? $_GET['donor'] : array();
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...
623
	$delete_donor         = ! empty( $_GET['give-delete-donor-confirm'] ) ? $_GET['give-delete-donor-confirm'] : '';
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...
624
	$delete_donations     = ! empty( $_GET['give-delete-donor-records'] ) ? $_GET['give-delete-donor-records'] : '';
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...
625
	$search_keyword       = ! empty( $_GET['s'] ) ? $_GET['s'] : '';
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...
626
	$give_args['orderby'] = ! empty( $_GET['orderby'] ) ? $_GET['orderby'] : '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...
627
	$give_args['order']   = ! empty( $_GET['order'] ) ? $_GET['order'] : 'desc';
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...
628
	$nonce                = $args['_wpnonce'];
629
630
	// Verify Nonce for deleting bulk donors.
631
	if ( ! wp_verify_nonce( $nonce, 'bulk-donors' ) ) {
632
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
633
			'response' => 400,
634
		) );
635
	}
636
637
	if( count( $donor_ids ) > 0 ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
638
		foreach ( $donor_ids as $donor_id ) {
639
			$donor = new Give_Donor( $donor_id );
640
641
			if ( $donor->id > 0 ) {
642
643
				if( $delete_donor ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
644
					$donor_deleted = Give()->donors->delete( $donor->id );
645
646
					if ( $donor_deleted ) {
647
						$donation_ids  = explode( ',', $donor->payment_ids );
648
649
						if( $delete_donations ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
650
651
							// Remove all donations, logs, etc.
652
							foreach ( $donation_ids as $donation_id ) {
653
								give_delete_donation( $donation_id );
654
							}
655
656
							$give_args['give-message'] = 'donor-donations-deleted';
657
						} else {
658
659
							// Just set the donations to customer_id of 0.
660
							foreach ( $donation_ids as $donation_id ) {
661
								give_update_payment_meta( $donation_id, '_give_payment_customer_id', 0 );
662
							}
663
664
							$give_args['give-message'] = 'donor-deleted';
665
						}
666
					} else {
667
						$give_args['give-message'] = 'donor-delete-failed';
668
					}
669
				} else {
670
					$give_args['give-message'] = 'confirm-delete-donor';
671
				}
672
			} else {
673
				$give_args['give-message'] = 'invalid-donor-id';
674
			}
675
		}
676
677
		// Add Search Keyword on redirection, if it exists.
678
		if ( ! empty( $search_keyword ) ) {
679
			$give_args['s'] = $search_keyword;
680
		}
681
682
		wp_redirect( add_query_arg( $give_args, admin_url( 'edit.php?post_type=give_forms&page=give-donors' ) ) );
683
		give_die();
684
	}
685
}
686
687
add_action( 'give_delete_donor', 'give_delete_donor' );