Test Failed
Push — issues/1944 ( 5e049b...ed954b )
by Ravinder
05:00
created

donor-actions.php ➔ give_delete_donor()   F

Complexity

Conditions 20
Paths 6240

Size

Total Lines 77
Code Lines 43

Duplication

Lines 5
Ratio 6.49 %

Importance

Changes 0
Metric Value
cc 20
eloc 43
nc 6240
nop 1
dl 5
loc 77
rs 2.2913
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
	// 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
	if ( $output['success'] ) {
114
		wp_redirect( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id={$donor_id}&give-message=profile-updated" ) );
115
	}
116
117
	exit;
118
119
}
120
121
add_action( 'give_edit-donor', 'give_edit_donor', 10, 1 );
122
123
/**
124
 * Save a donor note.
125
 *
126
 * @param array $args The $_POST array being passed.
127
 *
128
 * @since 1.0
129
 *
130
 * @return int The Note ID that was saved, or 0 if nothing was saved.
131
 */
132
function give_donor_save_note( $args ) {
133
134
	$donor_view_role = apply_filters( 'give_view_donors_role', 'view_give_reports' );
135
136 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...
137
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
138
			'response' => 403,
139
		) );
140
	}
141
142
	if ( empty( $args ) ) {
143
		return false;
144
	}
145
146
	$donor_note = trim( give_clean( $args['donor_note'] ) );
147
	$donor_id   = (int) $args['customer_id'];
148
	$nonce      = $args['add_donor_note_nonce'];
149
150
	if ( ! wp_verify_nonce( $nonce, 'add-donor-note' ) ) {
151
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
152
			'response' => 400,
153
		) );
154
	}
155
156
	if ( empty( $donor_note ) ) {
157
		give_set_error( 'empty-donor-note', __( 'A note is required.', 'give' ) );
158
	}
159
160
	if ( give_get_errors() ) {
161
		return false;
162
	}
163
164
	$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...
165
	$new_note = $donor->add_note( $donor_note );
166
167
	/**
168
	 * Fires before inserting donor note.
169
	 *
170
	 * @param int    $donor_id The ID of the donor.
171
	 * @param string $new_note Note content.
172
	 *
173
	 * @since 1.0
174
	 */
175
	do_action( 'give_pre_insert_donor_note', $donor_id, $new_note );
176
177
	if ( ! empty( $new_note ) && ! empty( $donor->id ) ) {
178
179
		ob_start();
180
		?>
181
		<div class="donor-note-wrapper dashboard-comment-wrap comment-item">
182
			<span class="note-content-wrap">
183
				<?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...
184
			</span>
185
		</div>
186
		<?php
187
		$output = ob_get_contents();
188
		ob_end_clean();
189
190
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
191
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
192
			exit;
193
		}
194
195
		return $new_note;
196
197
	}
198
199
	return false;
200
201
}
202
203
add_action( 'give_add-donor-note', 'give_donor_save_note', 10, 1 );
204
205
/**
206
 * Delete a donor.
207
 *
208
 * @param array $args The $_POST array being passed.
209
 *
210
 * @since 1.0
211
 *
212
 * @return int Whether it was a successful deletion.
213
 */
214
function give_donor_delete( $args ) {
215
216
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
217
218 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...
219
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
220
			'response' => 403,
221
		) );
222
	}
223
224
	if ( empty( $args ) ) {
225
		return false;
226
	}
227
228
	$donor_id    = (int) $args['customer_id'];
229
	$confirm     = ! empty( $args['give-donor-delete-confirm'] ) ? true : false;
230
	$remove_data = ! empty( $args['give-donor-delete-records'] ) ? true : false;
231
	$nonce       = $args['_wpnonce'];
232
233
	if ( ! wp_verify_nonce( $nonce, 'delete-donor' ) ) {
234
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
235
			'response' => 400,
236
		) );
237
	}
238
239
	if ( ! $confirm ) {
240
		give_set_error( 'donor-delete-no-confirm', __( 'Please confirm you want to delete this donor.', 'give' ) );
241
	}
242
243
	if ( give_get_errors() ) {
244
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor_id ) );
245
		exit;
246
	}
247
248
	$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...
249
250
	/**
251
	 * Fires before deleting donor.
252
	 *
253
	 * @param int  $donor_id    The ID of the donor.
254
	 * @param bool $confirm     Delete confirmation.
255
	 * @param bool $remove_data Records delete confirmation.
256
	 *
257
	 * @since 1.0
258
	 */
259
	do_action( 'give_pre_delete_donor', $donor_id, $confirm, $remove_data );
260
261
	if ( $donor->id > 0 ) {
262
263
		$payments_array = explode( ',', $donor->payment_ids );
264
		$success        = Give()->donors->delete( $donor->id );
265
266
		if ( $success ) {
267
268
			if ( $remove_data ) {
269
270
				// Remove all donations, logs, etc.
271
				foreach ( $payments_array as $payment_id ) {
272
					give_delete_donation( $payment_id );
273
				}
274
			} else {
275
276
				// Just set the donations to customer_id of 0.
277
				foreach ( $payments_array as $payment_id ) {
278
					give_update_payment_meta( $payment_id, '_give_payment_donor_id', 0 );
279
				}
280
			}
281
282
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&give-message=donor-deleted' );
283
284
		} else {
285
286
			give_set_error( 'give-donor-delete-failed', __( 'Error deleting donor.', 'give' ) );
287
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor_id );
288
289
		}
290
	} else {
291
292
		give_set_error( 'give-donor-delete-invalid-id', __( 'Invalid Donor ID.', 'give' ) );
293
		$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors' );
294
295
	}
296
297
	wp_redirect( $redirect );
298
	exit;
299
300
}
301
302
add_action( 'give_delete-donor', 'give_donor_delete', 10, 1 );
303
304
/**
305
 * Disconnect a user ID from a donor
306
 *
307
 * @param array $args Array of arguments.
308
 *
309
 * @since 1.0
310
 *
311
 * @return bool|array If the disconnect was successful.
312
 */
313
function give_disconnect_donor_user_id( $args ) {
314
315
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
316
317 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...
318
		wp_die( __( 'You do not have permission to edit this donor.', 'give' ), __( 'Error', 'give' ), array(
319
			'response' => 403,
320
		) );
321
	}
322
323
	if ( empty( $args ) ) {
324
		return false;
325
	}
326
327
	$donor_id = (int) $args['customer_id'];
328
329
	$nonce = $args['_wpnonce'];
330
331
	if ( ! wp_verify_nonce( $nonce, 'edit-donor' ) ) {
332
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
333
			'response' => 400,
334
		) );
335
	}
336
337
	$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...
338
	if ( empty( $donor->id ) ) {
339
		return false;
340
	}
341
342
	$user_id = $donor->user_id;
343
344
	/**
345
	 * Fires before disconnecting user ID from a donor.
346
	 *
347
	 * @param int $donor_id The ID of the donor.
348
	 * @param int $user_id  The ID of the user.
349
	 *
350
	 * @since 1.0
351
	 */
352
	do_action( 'give_pre_donor_disconnect_user_id', $donor_id, $user_id );
353
354
	$output     = array();
355
	$donor_args = array(
356
		'user_id' => 0,
357
	);
358
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
359
360
	$output['success'] = true;
361
	if ( ! $donor->update( $donor_args ) ) {
362
		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...
363
		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...
364
		$donor->update_meta( '_give_disconnected_user_id', $user_id );
365
366
		$output['success'] = true;
367
368
	} else {
369
		$output['success'] = false;
370
		give_set_error( 'give-disconnect-user-fail', __( 'Failed to disconnect user from donor.', 'give' ) );
371
	}
372
373
	$output['redirect'] = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' ) . $donor_id;
374
375
	/**
376
	 * Fires after disconnecting user ID from a donor.
377
	 *
378
	 * @param int $donor_id The ID of the donor.
379
	 *
380
	 * @since 1.0
381
	 */
382
	do_action( 'give_post_donor_disconnect_user_id', $donor_id );
383
384 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...
385
		header( 'Content-Type: application/json' );
386
		echo json_encode( $output );
387
		wp_die();
388
	}
389
390
	return $output;
391
392
}
393
394
add_action( 'give_disconnect-userid', 'give_disconnect_donor_user_id', 10, 1 );
395
396
/**
397
 * Add an email address to the donor from within the admin and log a donor note.
398
 *
399
 * @param array $args Array of arguments: nonce, donor id, and email address.
400
 *
401
 * @since 1.7
402
 *
403
 * @return mixed If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string).
404
 */
405
function give_add_donor_email( $args ) {
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
 *
492
 * @return bool|null
493
 */
494 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...
495
	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...
496
		return false;
497
	}
498
	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...
499
		return false;
500
	}
501
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
502
		return false;
503
	}
504
505
	$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...
506
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
507
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
508
			'response' => 403,
509
		) );
510
	}
511
512
	$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...
513
	if ( $donor->remove_email( $_GET['email'] ) ) {
514
		$url        = add_query_arg( 'give-message', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ) );
515
		$user       = wp_get_current_user();
516
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
517
		$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...
518
		$donor->add_note( $donor_note );
519
	} else {
520
		$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 ) );
521
	}
522
523
	wp_safe_redirect( $url );
524
	exit;
525
}
526
527
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
528
529
530
/**
531
 * Set an email address as the primary for a donor from within the admin and log a donor note
532
 * and redirect back to the donor interface for feedback
533
 *
534
 * @since  1.7
535
 *
536
 * @return bool|null
537
 */
538 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...
539
	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...
540
		return false;
541
	}
542
543
	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...
544
		return false;
545
	}
546
547
	if ( empty( $_GET['_wpnonce'] ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
548
		return false;
549
	}
550
551
	$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...
552
553
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
554
		wp_die( __( 'Nonce verification failed', 'give' ), __( 'Error', 'give' ), array(
555
			'response' => 403,
556
		) );
557
	}
558
559
	$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...
560
561
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
562
		$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 ) );
563
		$user       = wp_get_current_user();
564
		$user_login = ! empty( $user->user_login ) ? $user->user_login : __( 'System', 'give' );
565
		$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...
566
567
		$donor->add_note( $donor_note );
568
	} else {
569
		$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 ) );
570
	}
571
572
	wp_safe_redirect( $url );
573
	exit;
574
}
575
576
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
577
578
/**
579
 * Delete Donor using Bulk Actions.
580
 *
581
 * @param array $args An array of donor arguments.
582
 *
583
 * @since 1.8.17
584
 *
585
 * @return void
586
 */
587
function give_delete_donor( $args ) {
588
589
	$donor_edit_role = apply_filters( 'give_edit_donors_role', 'edit_give_payments' );
590
591 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...
592
		wp_die( __( 'You do not have permission to delete donors.', 'give' ), __( 'Error', 'give' ), array(
593
			'response' => 403,
594
		) );
595
	}
596
597
	$give_message     = array();
598
	$donor_ids        = ( ! empty( $_GET['donor'] ) && is_array( $_GET['donor'] ) && count( $_GET['donor'] ) > 0 ) ? $_GET['donor'] : array();
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
599
	$delete_donor     = ! empty( $_GET['give-delete-donor-confirm'] ) ? $_GET['give-delete-donor-confirm'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
600
	$delete_donations = ! empty( $_GET['give-delete-donor-records'] ) ? $_GET['give-delete-donor-records'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
601
	$nonce            = $args['_wpnonce'];
602
603
	// Verify Nonce for deleting bulk donors.
604
	if ( ! wp_verify_nonce( $nonce, 'bulk-donors' ) ) {
605
		wp_die( __( 'Cheatin&#8217; uh?', 'give' ), __( 'Error', 'give' ), array(
606
			'response' => 400,
607
		) );
608
	}
609
610
	if( count( $donor_ids ) > 0 ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
611
		foreach ( $donor_ids as $donor_id ) {
612
			$donor = new Give_Donor( $donor_id );
613
614
			if ( $donor->id > 0 ) {
615
616
				if( $delete_donor ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
617
					$donor_deleted = Give()->donors->delete( $donor->id );
618
619
					if ( $donor_deleted ) {
620
						$donation_ids  = explode( ',', $donor->payment_ids );
621
622
						if( $delete_donations ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
623
624
							// Remove all donations, logs, etc.
625
							foreach ( $donation_ids as $donation_id ) {
626
								give_delete_donation( $donation_id );
627
							}
628
629
							$give_message = 'donor-donations-deleted';
630
						} else {
631
632
							// Just set the donations to customer_id of 0.
633
							foreach ( $donation_ids as $donation_id ) {
634
								give_update_payment_meta( $donation_id, '_give_payment_customer_id', 0 );
635
							}
636
637
							$give_message = 'donor-deleted';
638
						}
639
					} else {
640
						$give_message = 'donor-delete-failed';
641
					}
642
				} else {
643
					$give_message = 'confirm-delete-donor';
644
				}
645
			} else {
646
				$give_message = 'invalid-donor-id';
647
			}
648
		}
649
650
		$args = array();
651
		if ( ! empty( $give_message ) ) {
652
			$args['give-message'] = $give_message;
653
		}
654
655
		$search = ( ! empty( $_REQUEST['s'] ) ? give_clean( $_REQUEST['s'] ) : false );
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
656
		if ( ! empty( $search ) ) {
657
			$args['s'] = $search;
658
		}
659
660
		wp_redirect( add_query_arg( $args, admin_url( 'edit.php?post_type=give_forms&page=give-donors' ) ) );
661
		give_die();
662
	}
663
}
664
665
add_action( 'give_delete_donor', 'give_delete_donor' );
666