Test Failed
Push — issues/370 ( 51cc5e...d56982 )
by Ravinder
05:20
created

donor-actions.php ➔ give_edit_donor()   D

Complexity

Conditions 14
Paths 66

Size

Total Lines 90
Code Lines 47

Duplication

Lines 10
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 66
nop 1
dl 10
loc 90
rs 4.9516
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
		'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
	// Sanitize the inputs.
88
	$donor_data            = array();
89
	$donor_data['name']    = strip_tags( stripslashes( $donor_info['name'] ) );
90
	$donor_data['user_id'] = $donor_info['user_id'];
91
92
	$donor_data             = apply_filters( 'give_edit_donor_info', $donor_data, $donor_id );
93
94
	/**
95
	 * Filter the address
96
	 * @todo unnecessary filter because we are not storing donor address to user.
97
	 *
98
	 * @since 1.0
99
	 */
100
	$address                = apply_filters( 'give_edit_donor_address', array(), $donor_id );
101
102
	$donor_data             = give_clean( $donor_data );
103
	$address                = give_clean( $address );
104
105
	$output = give_connect_user_donor_profile( $donor, $donor_data, $address );
0 ignored issues
show
Bug introduced by
It seems like $donor_data defined by give_clean($donor_data) on line 102 can also be of type string; however, give_connect_user_donor_profile() does only seem to accept array, maybe add an additional type check?

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

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

    return array();
}

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

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

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

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

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

    return array();
}

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

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

Loading history...
106
107 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...
108
		header( 'Content-Type: application/json' );
109
		echo json_encode( $output );
110
		wp_die();
111
	}
112
113
	return $output;
114
115
}
116
117
add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 );
118
119
/**
120
 * Save a donor note.
121
 *
122
 * @since  1.0
123
 *
124
 * @param  array $args The $_POST array being passed.
125
 *
126
 * @return int         The Note ID that was saved, or 0 if nothing was saved.
127
 */
128
function give_donor_save_note( $args ) {
129
130
	$donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' );
131
132 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...
133
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
134
			'response' => 403,
135
		) );
136
	}
137
138
	if ( empty( $args ) ) {
139
		return false;
140
	}
141
142
	$donor_note = trim( give_clean( $args['donor_note'] ) );
143
	$donor_id   = (int) $args['customer_id'];
144
	$nonce      = $args['add_donor_note_nonce'];
145
146
	if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) {
147
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
148
			'response' => 400,
149
		) );
150
	}
151
152
	if ( empty( $donor_note ) ) {
153
		give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) );
154
	}
155
156
	if ( give_get_errors() ) {
157
		return false;
158
	}
159
160
	$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...
161
	$new_note = $donor->add_note( $donor_note );
162
163
	/**
164
	 * Fires before inserting donor note.
165
	 *
166
	 * @since 1.0
167
	 *
168
	 * @param int    $donor_id The ID of the donor.
169
	 * @param string $new_note Note content.
170
	 */
171
	do_action( 'give_pre_insert_donor_note', $donor_id, $new_note );
172
173
	if ( ! empty( $new_note ) && ! empty( $donor->id ) ) {
174
175
		ob_start();
176
		?>
177
		<div class="donor-note-wrapper dashboard-comment-wrap comment-item">
178
			<span class="note-content-wrap">
179
				<?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...
180
			</span>
181
		</div>
182
		<?php
183
		$output = ob_get_contents();
184
		ob_end_clean();
185
186
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
187
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
188
			exit;
189
		}
190
191
		return $new_note;
192
193
	}
194
195
	return false;
196
197
}
198
199
add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 );
200
201
/**
202
 * Delete a donor.
203
 *
204
 * @since  1.0
205
 *
206
 * @param  array $args The $_POST array being passed.
207
 *
208
 * @return int Whether it was a successful deletion.
209
 */
210
function give_donor_delete( $args ) {
211
212
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
213
214 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...
215
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
216
			'response' => 403,
217
		) );
218
	}
219
220
	if ( empty( $args ) ) {
221
		return false;
222
	}
223
224
	$donor_id    = (int) $args['customer_id'];
225
	$confirm     = ! empty( $args['give-donor-delete-confirm'] ) ? true : false;
226
	$remove_data = ! empty( $args['give-donor-delete-records'] ) ? true : false;
227
	$nonce       = $args['_wpnonce'];
228
229
	if ( ! wp_verify_nonce( $nonce, 'delete-donor' ) ) {
230
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
231
			'response' => 400,
232
		) );
233
	}
234
235
	if ( ! $confirm ) {
236
		give_set_error( 'donor-delete-no-confirm', __( 'Please confirm you want to delete this donor.', 'give' ) );
237
	}
238
239
	if ( give_get_errors() ) {
240
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id ) );
241
		exit;
242
	}
243
244
	$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...
245
246
	/**
247
	 * Fires before deleting donor.
248
	 *
249
	 * @since 1.0
250
	 *
251
	 * @param int  $donor_id    The ID of the donor.
252
	 * @param bool $confirm     Delete confirmation.
253
	 * @param bool $remove_data Records delete confirmation.
254
	 */
255
	do_action( 'give_pre_delete_donor', $donor_id, $confirm, $remove_data );
256
257
	if ( $donor->id > 0 ) {
258
259
		$payments_array = explode( ',', $donor->payment_ids );
260
		$success        = Give()->donors->delete( $donor->id );
261
262
		if ( $success ) {
263
264
			if ( $remove_data ) {
265
266
				// Remove all donations, logs, etc.
267
				foreach ( $payments_array as $payment_id ) {
268
					give_delete_donation( $payment_id );
269
				}
270
			} else {
271
272
				// Just set the donations to customer_id of 0.
273
				foreach ( $payments_array as $payment_id ) {
274
					give_update_payment_meta( $payment_id, '_give_payment_donor_id', 0 );
275
				}
276
			}
277
278
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&give-message=donor-deleted' );
279
280
		} else {
281
282
			give_set_error( 'give-donor-delete-failed', __( 'Error deleting donor.', 'give' ) );
283
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor_id );
284
285
		}
286
	} else {
287
288
		give_set_error( 'give-donor-delete-invalid-id', __( 'Invalid Donor ID.', 'give' ) );
289
		$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors' );
290
291
	}
292
293
	wp_redirect( $redirect );
294
	exit;
295
296
}
297
298
add_action( 'give_delete-donor', 'give_donor_delete', 10, 1 );
299
300
/**
301
 * Disconnect a user ID from a donor
302
 *
303
 * @since  1.0
304
 *
305
 * @param  array $args Array of arguments.
306
 *
307
 * @return bool|array        If the disconnect was successful.
308
 */
309
function give_disconnect_donor_user_id( $args ) {
310
311
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
312
313 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...
314
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
315
			'response' => 403,
316
		) );
317
	}
318
319
	if ( empty( $args ) ) {
320
		return false;
321
	}
322
323
	$donor_id = (int) $args['customer_id'];
324
325
	$nonce = $args['_wpnonce'];
326
327
	if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) {
328
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
329
			'response' => 400,
330
		) );
331
	}
332
333
	$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...
334
	if ( empty( $donor->id ) ) {
335
		return false;
336
	}
337
338
	$user_id = $donor->user_id;
339
340
	/**
341
	 * Fires before disconnecting user ID from a donor.
342
	 *
343
	 * @since 1.0
344
	 *
345
	 * @param int $donor_id The ID of the donor.
346
	 * @param int $user_id  The ID of the user.
347
	 */
348
	do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id );
349
350
	$output     = array();
351
	$donor_args = array(
352
		'user_id' => 0,
353
	);
354
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
355
356
	$output['success'] = true;
357
	if ( ! $donor->update( $donor_args ) ) {
358
		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...
359
		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...
360
		$donor->update_meta( '_give_disconnected_user_id', $user_id );
361
362
		$output['success'] = true;
363
364
	} else {
365
		$output['success'] = false;
366
		give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
367
	}
368
369
	$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
370
371
	/**
372
	 * Fires after disconnecting user ID from a donor.
373
	 *
374
	 * @since 1.0
375
	 *
376
	 * @param int $donor_id The ID of the donor.
377
	 */
378
	do_action( 'give_post_donor_disconnect_user_id', $donor_id );
379
380 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...
381
		header( 'Content-Type: application/json' );
382
		echo json_encode( $output );
383
		wp_die();
384
	}
385
386
	return $output;
387
388
}
389
390
add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 );
391
392
/**
393
 * Add an email address to the donor from within the admin and log a donor note.
394
 *
395
 * @since  1.7
396
 *
397
 * @param  array $args Array of arguments: nonce, donor id, and email address.
398
 *
399
 * @return mixed        If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string).
400
 */
401
function give_add_donor_email( $args ) {
402
	/**
403
	 * Define variable
404
	 *
405
	 * @since 1.8.14
406
	 */
407
	$donor_id = '';
408
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
409
410 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...
411
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
412
			'response' => 403,
413
		) );
414
	}
415
416
	$output = array();
417
	if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) {
418
		$output['success'] = false;
419
		if ( empty( $args['email'] ) ) {
420
			$output['message'] = __( 'Email address is required.', 'give' );
421
		} elseif ( empty( $args['customer_id'] ) ) {
422
			$output['message'] = __( 'Donor ID is required.', 'give' );
423
		} else {
424
			$output['message'] = __( 'An error has occurred. Please try again.', 'give' );
425
		}
426
	} elseif ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) {
427
		$output = array(
428
			'success' => false,
429
			'message' => __( 'Nonce verification failed.', 'give' ),
430
		);
431
	} elseif ( ! is_email( $args['email'] ) ) {
432
		$output = array(
433
			'success' => false,
434
			'message' => __( 'Invalid email.', 'give' ),
435
		);
436
	} else {
437
		$email    = sanitize_email( $args['email'] );
438
		$donor_id = (int) $args['customer_id'];
439
		$primary  = 'true' === $args['primary'] ? true : false;
440
		$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...
441
		if ( false === $donor->add_email( $email, $primary ) ) {
442
			if ( in_array( $email, $donor->emails ) ) {
443
				$output = array(
444
					'success' => false,
445
					'message' => __( 'Email already associated with this donor.', 'give' ),
446
				);
447
			} else {
448
				$output = array(
449
					'success' => false,
450
					'message' => __( 'Email address is already associated with another donor.', 'give' ),
451
				);
452
			}
453
		} else {
454
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id . '&give-message=email-added' );
455
			$output   = array(
456
				'success'  => true,
457
				'message'  => __( 'Email successfully added to donor.', 'give' ),
458
				'redirect' => $redirect,
459
			);
460
461
			$user       = wp_get_current_user();
462
			$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
463
			$donor_note = sprintf( __( 'Email address %1$s added by %2$s', 'give' ), $email, $user_login );
464
			$donor->add_note( $donor_note );
465
466
			if ( $primary ) {
467
				$donor_note = sprintf( __( 'Email address %1$s set as primary by %2$s', 'give' ), $email, $user_login );
468
				$donor->add_note( $donor_note );
469
			}
470
		}
471
	}// End if().
472
473
	do_action( 'give_post_add_donor_email', $donor_id, $args );
474
475 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...
476
		header( 'Content-Type: application/json' );
477
		echo json_encode( $output );
478
		wp_die();
479
	}
480
481
	return $output;
482
}
483
484
add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 );
485
486
487
/**
488
 * 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.
489
 *
490
 * @since  1.7
491
 * @return bool|null
492
 */
493 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...
494
	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...
495
		return false;
496
	}
497
	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...
498
		return false;
499
	}
500
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
501
		return false;
502
	}
503
504
	$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...
505
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
506
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
507
			'response' => 403,
508
		) );
509
	}
510
511
	$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...
512
	if ( $donor->remove_email( $_GET['email'] ) ) {
513
		$url        = add_query_arg( 'give-message', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) );
514
		$user       = wp_get_current_user();
515
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
516
		$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...
517
		$donor->add_note( $donor_note );
518
	} else {
519
		$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 ) );
520
	}
521
522
	wp_safe_redirect( $url );
523
	exit;
524
}
525
526
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
527
528
529
/**
530
 * Set an email address as the primary for a donor from within the admin and log a donor note
531
 * and redirect back to the donor interface for feedback
532
 *
533
 * @since  1.7
534
 * @return bool|null
535
 */
536 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...
537
	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...
538
		return false;
539
	}
540
541
	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...
542
		return false;
543
	}
544
545
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
546
		return false;
547
	}
548
549
	$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...
550
551
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
552
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
553
			'response' => 403,
554
		) );
555
	}
556
557
	$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...
558
559
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
560
		$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 ) );
561
		$user       = wp_get_current_user();
562
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
563
		$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...
564
565
		$donor->add_note( $donor_note );
566
	} else {
567
		$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 ) );
568
	}
569
570
	wp_safe_redirect( $url );
571
	exit;
572
}
573
574
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
575