Completed
Push — issues/3312 ( 6b1a83 )
by Ravinder
1313:55 queued 1307:48
created

donor-actions.php ➔ give_process_donor_deletion()   F

Complexity

Conditions 20
Paths 1536

Size

Total Lines 101

Duplication

Lines 9
Ratio 8.91 %

Importance

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