Test Failed
Push — master ( b86487...e5e5a2 )
by Ravinder
07:05
created

donor-actions.php ➔ give_edit_donor()   F

Complexity

Conditions 33
Paths > 20000

Size

Total Lines 157
Code Lines 80

Duplication

Lines 10
Ratio 6.37 %

Importance

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