Test Failed
Push — master ( 3995a3...673dde )
by Devin
05:40 queued 01:04
created

donor-actions.php ➔ give_donor_delete()   F

Complexity

Conditions 15
Paths 290

Size

Total Lines 95
Code Lines 44

Duplication

Lines 5
Ratio 5.26 %

Importance

Changes 0
Metric Value
cc 15
eloc 44
nc 290
nop 1
dl 5
loc 95
rs 3.7313
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
	$defaults = array(
247
		'redirect' => true,
248
	);
249
	wp_parse_args( $args, $defaults );
250
251
	$donor_id    = (int) $args['customer_id'];
252
	$confirm     = ! empty( $args['give-donor-delete-confirm'] ) ? true : false;
253
	$remove_data = ! empty( $args['give-donor-delete-records'] ) ? true : false;
254
	$nonce       = $args['_wpnonce'];
255
256
	if ( ! wp_verify_nonce( $nonce, 'delete-donor' ) ) {
257
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
258
			'response' => 400,
259
		) );
260
	}
261
262
	if ( ! $confirm ) {
263
		give_set_error( 'donor-delete-no-confirm', __( 'Please confirm you want to delete this donor.', 'give' ) );
264
	}
265
266
	if ( give_get_errors() ) {
267
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id ) );
268
		exit;
269
	}
270
271
	$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...
272
273
	/**
274
	 * Fires before deleting donor.
275
	 *
276
	 * @param int  $donor_id    The ID of the donor.
277
	 * @param bool $confirm     Delete confirmation.
278
	 * @param bool $remove_data Records delete confirmation.
279
	 *
280
	 * @since 1.0
281
	 */
282
	do_action( 'give_pre_delete_donor', $donor_id, $confirm, $remove_data );
283
284
	if ( $donor->id > 0 ) {
285
286
		$payments_array = explode( ',', $donor->payment_ids );
287
		$success        = Give()->donors->delete( $donor->id );
288
289
		if ( $success ) {
290
291
			if ( $remove_data ) {
292
293
				// Remove all donations, logs, etc.
294
				foreach ( $payments_array as $payment_id ) {
295
					give_delete_donation( $payment_id );
296
				}
297
			} else {
298
299
				// Just set the donations to customer_id of 0.
300
				foreach ( $payments_array as $payment_id ) {
301
					give_update_payment_meta( $payment_id, '_give_payment_customer_id', 0 );
302
				}
303
			}
304
305
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&give-message=donor-deleted' );
306
307
		} else {
308
309
			give_set_error( 'give-donor-delete-failed', __( 'Error deleting donor.', 'give' ) );
310
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor_id );
311
312
		}
313
	} else {
314
315
		give_set_error( 'give-donor-delete-invalid-id', __( 'Invalid Donor ID.', 'give' ) );
316
		$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors' );
317
318
	}
319
320
	// Proceed Redirect, only if redirect argument is set to true.
321
	if( $args['redirect'] ) {
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...
322
		wp_redirect( $redirect );
323
		exit;
324
	}
325
326
}
327
328
add_action( 'give_delete-donor', 'give_donor_delete', 10, 1 );
329
330
/**
331
 * Disconnect a user ID from a donor
332
 *
333
 * @param array $args Array of arguments.
334
 *
335
 * @since 1.0
336
 *
337
 * @return bool|array If the disconnect was successful.
338
 */
339
function give_disconnect_donor_user_id( $args ) {
340
341
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
342
343 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...
344
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
345
			'response' => 403,
346
		) );
347
	}
348
349
	if ( empty( $args ) ) {
350
		return false;
351
	}
352
353
	$donor_id = (int) $args['customer_id'];
354
355
	$nonce = $args['_wpnonce'];
356
357
	if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) {
358
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
359
			'response' => 400,
360
		) );
361
	}
362
363
	$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...
364
	if ( empty( $donor->id ) ) {
365
		return false;
366
	}
367
368
	$user_id = $donor->user_id;
369
370
	/**
371
	 * Fires before disconnecting user ID from a donor.
372
	 *
373
	 * @param int $donor_id The ID of the donor.
374
	 * @param int $user_id  The ID of the user.
375
	 *
376
	 * @since 1.0
377
	 */
378
	do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id );
379
380
	$output     = array();
381
	$donor_args = array(
382
		'user_id' => 0,
383
	);
384
385
	if ( $donor->update( $donor_args ) ) {
386
		global $wpdb;
387
388
		if ( ! empty( $donor->payment_ids ) ) {
389
			$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...
390
		}
391
392
		// Set Donor Disconnection status true, if user and donor are disconnected with each other.
393
		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...
394
		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...
395
		$donor->update_meta( '_give_disconnected_user_id', $user_id );
396
397
		$output['success'] = true;
398
399
	} else {
400
401
		$output['success'] = false;
402
		give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
403
	}
404
405
	$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
406
407
	/**
408
	 * Fires after disconnecting user ID from a donor.
409
	 *
410
	 * @param int $donor_id The ID of the donor.
411
	 *
412
	 * @since 1.0
413
	 */
414
	do_action( 'give_post_donor_disconnect_user_id', $donor_id );
415
416 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...
417
		header( 'Content-Type: application/json' );
418
		echo json_encode( $output );
419
		wp_die();
420
	}
421
422
	return $output;
423
424
}
425
426
add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 );
427
428
/**
429
 * Add an email address to the donor from within the admin and log a donor note.
430
 *
431
 * @param array $args Array of arguments: nonce, donor id, and email address.
432
 *
433
 * @since 1.7
434
 *
435
 * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string).
436
 */
437
function give_add_donor_email( $args ) {
438
439
	$donor_id = '';
440
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
441
442 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...
443
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
444
			'response' => 403,
445
		) );
446
	}
447
448
	$output = array();
449
	if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) {
450
		$output['success'] = false;
451
		if ( empty( $args['email'] ) ) {
452
			$output['message'] = __( 'Email address is required.', 'give' );
453
		} elseif ( empty( $args['customer_id'] ) ) {
454
			$output['message'] = __( 'Donor ID is required.', 'give' );
455
		} else {
456
			$output['message'] = __( 'An error has occurred. Please try again.', 'give' );
457
		}
458
	} elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) {
459
		$output = array(
460
			'success' => false,
461
			'message' => __( 'Nonce verification failed.', 'give' ),
462
		);
463
	} elseif ( ! is_email( $args['email'] ) ) {
464
		$output = array(
465
			'success' => false,
466
			'message' => __( 'Invalid email.', 'give' ),
467
		);
468
	} else {
469
		$email    = sanitize_email( $args['email'] );
470
		$donor_id = (int) $args['customer_id'];
471
		$primary  = 'true' === $args['primary'] ? true : false;
472
		$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...
473
		if ( false === $donor->add_email( $email, $primary ) ) {
474
			if ( in_array( $email, $donor->emails ) ) {
475
				$output = array(
476
					'success' => false,
477
					'message' => __( 'Email already associated with this donor.', 'give' ),
478
				);
479
			} else {
480
				$output = array(
481
					'success' => false,
482
					'message' => __( 'Email address is already associated with another donor.', 'give' ),
483
				);
484
			}
485
		} else {
486
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-message=email-added' );
487
			$output   = array(
488
				'success'  => true,
489
				'message'  => __( 'Email successfully added to donor.', 'give' ),
490
				'redirect' => $redirect,
491
			);
492
493
			$user       = wp_get_current_user();
494
			$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
495
			$donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login );
496
			$donor->add_note( $donor_note );
497
498
			if ( $primary ) {
499
				$donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login );
500
				$donor->add_note( $donor_note );
501
			}
502
		}
503
	} // End if().
504
505
	do_action( 'give_post_add_donor_email', $donor_id, $args );
506
507 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...
508
		header( 'Content-Type: application/json' );
509
		echo json_encode( $output );
510
		wp_die();
511
	}
512
513
	return $output;
514
}
515
516
add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 );
517
518
519
/**
520
 * 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.
521
 *
522
 * @since  1.7
523
 *
524
 * @return bool|null
525
 */
526 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...
527
	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...
528
		return false;
529
	}
530
	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...
531
		return false;
532
	}
533
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
534
		return false;
535
	}
536
537
	$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...
538
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
539
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
540
			'response' => 403,
541
		) );
542
	}
543
544
	$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...
545
	if ( $donor->remove_email( $_GET['email'] ) ) {
546
		$url        = add_query_arg( 'give-message', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) );
547
		$user       = wp_get_current_user();
548
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
549
		$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...
550
		$donor->add_note( $donor_note );
551
	} else {
552
		$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 ) );
553
	}
554
555
	wp_safe_redirect( $url );
556
	exit;
557
}
558
559
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
560
561
562
/**
563
 * Set an email address as the primary for a donor from within the admin and log a donor note
564
 * and redirect back to the donor interface for feedback
565
 *
566
 * @since  1.7
567
 *
568
 * @return bool|null
569
 */
570 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...
571
	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...
572
		return false;
573
	}
574
575
	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...
576
		return false;
577
	}
578
579
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
580
		return false;
581
	}
582
583
	$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...
584
585
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
586
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
587
			'response' => 403,
588
		) );
589
	}
590
591
	$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...
592
593
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
594
		$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 ) );
595
		$user       = wp_get_current_user();
596
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
597
		$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...
598
599
		$donor->add_note( $donor_note );
600
	} else {
601
		$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 ) );
602
	}
603
604
	wp_safe_redirect( $url );
605
	exit;
606
}
607
608
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
609