Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

customer-actions.php ➔ give_remove_donor_email()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 9
nop 0
dl 0
loc 30
rs 4.909
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Customer (Donors)
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Customers
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 customer edit
19
 *
20
 * @since  1.0
21
 *
22
 * @param  array $args The $_POST array being passed
23
 *
24
 * @return array $output Response messages
25
 */
26
function give_edit_customer( $args ) {
27
	
28
	$customer_edit_role = apply_filters( 'give_edit_customers_role', 'edit_give_payments' );
29
30
	if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) {
31
		wp_die( esc_html__( 'You do not have permission to edit this donor.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
32
	}
33
34
	if ( empty( $args ) ) {
35
		return;
36
	}
37
38
	$customer_info = $args['customerinfo'];
39
	$customer_id   = (int) $args['customerinfo']['id'];
40
	$nonce         = $args['_wpnonce'];
41
42
	if ( ! wp_verify_nonce( $nonce, 'edit-customer' ) ) {
43
		wp_die( esc_html__( 'Cheatin&#8217; uh?', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
44
	}
45
46
	$customer = new Give_Customer( $customer_id );
47
	if ( empty( $customer->id ) ) {
48
		return false;
49
	}
50
51
	$defaults = array(
52
		'name'    => '',
53
		'user_id' => 0
54
	);
55
56
	$customer_info = wp_parse_args( $customer_info, $defaults );
57
58
	if ( (int) $customer_info['user_id'] != (int) $customer->user_id ) {
59
60
		// Make sure we don't already have this user attached to a customer
61
		if ( ! empty( $customer_info['user_id'] ) && false !== Give()->customers->get_customer_by( 'user_id', $customer_info['user_id'] ) ) {
62
			give_set_error( 'give-invalid-customer-user_id', sprintf( esc_html__( 'The User ID %d is already associated with a different donor.', 'give' ), $customer_info['user_id'] ) );
63
		}
64
65
		// Make sure it's actually a user
66
		$user = get_user_by( 'id', $customer_info['user_id'] );
67
		if ( ! empty( $customer_info['user_id'] ) && false === $user ) {
68
			give_set_error( 'give-invalid-user_id', sprintf( esc_html__( 'The User ID %d does not exist. Please assign an existing user.', 'give' ), $customer_info['user_id'] ) );
69
		}
70
71
	}
72
73
	// Record this for later
74
	$previous_user_id = $customer->user_id;
75
76
	if ( give_get_errors() ) {
77
		return;
78
	}
79
80
	// Setup the customer address, if present
81
	$address = array();
82
	if ( intval( $customer_info['user_id'] ) > 0 ) {
83
84
		$current_address = get_user_meta( $customer_info['user_id'], '_give_user_address', true );
85
86
		if ( false === $current_address ) {
87
			$address['line1']   = isset( $customer_info['line1'] ) ? $customer_info['line1'] : '';
88
			$address['line2']   = isset( $customer_info['line2'] ) ? $customer_info['line2'] : '';
89
			$address['city']    = isset( $customer_info['city'] ) ? $customer_info['city'] : '';
90
			$address['country'] = isset( $customer_info['country'] ) ? $customer_info['country'] : '';
91
			$address['zip']     = isset( $customer_info['zip'] ) ? $customer_info['zip'] : '';
92
			$address['state']   = isset( $customer_info['state'] ) ? $customer_info['state'] : '';
93
		} else {
94
			$current_address    = wp_parse_args( $current_address, array(
95
				'line1',
96
				'line2',
97
				'city',
98
				'zip',
99
				'state',
100
				'country'
101
			) );
102
			$address['line1']   = isset( $customer_info['line1'] ) ? $customer_info['line1'] : $current_address['line1'];
103
			$address['line2']   = isset( $customer_info['line2'] ) ? $customer_info['line2'] : $current_address['line2'];
104
			$address['city']    = isset( $customer_info['city'] ) ? $customer_info['city'] : $current_address['city'];
105
			$address['country'] = isset( $customer_info['country'] ) ? $customer_info['country'] : $current_address['country'];
106
			$address['zip']     = isset( $customer_info['zip'] ) ? $customer_info['zip'] : $current_address['zip'];
107
			$address['state']   = isset( $customer_info['state'] ) ? $customer_info['state'] : $current_address['state'];
108
		}
109
110
	}
111
112
	// Sanitize the inputs
113
	$customer_data            = array();
114
	$customer_data['name']    = strip_tags( stripslashes( $customer_info['name'] ) );
115
	$customer_data['user_id'] = $customer_info['user_id'];
116
117
	$customer_data = apply_filters( 'give_edit_customer_info', $customer_data, $customer_id );
118
	$address       = apply_filters( 'give_edit_customer_address', $address, $customer_id );
119
120
	$customer_data = array_map( 'sanitize_text_field', $customer_data );
121
	$address       = array_map( 'sanitize_text_field', $address );
122
123
124
	/**
125
	 * Fires before editing customer.
126
	 *
127
	 * @since 1.0
128
	 *
129
	 * @param int   $customer_id   The ID of the customer.
130
	 * @param array $customer_data The customer data.
131
	 * @param array $address       The customer address.
132
	 */
133
	do_action( 'give_pre_edit_customer', $customer_id, $customer_data, $address );
134
135
	$output         = array();
136
137
	if ( $customer->update( $customer_data ) ) {
138
139
		if ( ! empty( $customer->user_id ) && $customer->user_id > 0 ) {
140
			update_user_meta( $customer->user_id, '_give_user_address', $address );
141
		}
142
143
		// Update some donation meta if we need to
144
		$payments_array = explode( ',', $customer->payment_ids );
145
146
		if ( $customer->user_id != $previous_user_id ) {
147
			foreach ( $payments_array as $payment_id ) {
148
				give_update_payment_meta( $payment_id, '_give_payment_user_id', $customer->user_id );
149
			}
150
		}
151
152
		$output['success']       = true;
153
		$customer_data           = array_merge( $customer_data, $address );
154
		$output['customer_info'] = $customer_data;
155
156
	} else {
157
158
		$output['success'] = false;
159
160
	}
161
162
	/**
163
	 * Fires after editing customer.
164
	 *
165
	 * @since 1.0
166
	 *
167
	 * @param int   $customer_id   The ID of the customer.
168
	 * @param array $customer_data The customer data.
169
	 */
170
	do_action( 'give_post_edit_customer', $customer_id, $customer_data );
171
172
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
173
		header( 'Content-Type: application/json' );
174
		echo json_encode( $output );
175
		wp_die();
176
	}
177
178
	return $output;
179
180
}
181
182
add_action( 'give_edit-customer', 'give_edit_customer', 10, 1 );
183
184
/**
185
 * Save a customer note being added
186
 *
187
 * @since  1.0
188
 *
189
 * @param  array $args The $_POST array being passeed
190
 *
191
 * @return int         The Note ID that was saved, or 0 if nothing was saved
192
 */
193
function give_customer_save_note( $args ) {
194
195
	$customer_view_role = apply_filters( 'give_view_customers_role', 'view_give_reports' );
196
197
	if ( ! is_admin() || ! current_user_can( $customer_view_role ) ) {
198
		wp_die( esc_html__( 'You do not have permission to edit this donor.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
199
	}
200
201
	if ( empty( $args ) ) {
202
		return;
203
	}
204
205
	$customer_note = trim( sanitize_text_field( $args['customer_note'] ) );
206
	$customer_id   = (int) $args['customer_id'];
207
	$nonce         = $args['add_customer_note_nonce'];
208
209
	if ( ! wp_verify_nonce( $nonce, 'add-customer-note' ) ) {
210
		wp_die( esc_html__( 'Cheatin&#8217; uh?', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
211
	}
212
213
	if ( empty( $customer_note ) ) {
214
		give_set_error( 'empty-customer-note', esc_html__( 'A note is required.', 'give' ) );
215
	}
216
217
	if ( give_get_errors() ) {
218
		return;
219
	}
220
221
	$customer = new Give_Customer( $customer_id );
222
	$new_note = $customer->add_note( $customer_note );
223
224
	/**
225
	 * Fires before inserting customer note.
226
	 *
227
	 * @since 1.0
228
	 *
229
	 * @param int    $customer_id The ID of the customer.
230
	 * @param string $new_note    Note content.
231
	 */
232
	do_action( 'give_pre_insert_customer_note', $customer_id, $new_note );
233
234
	if ( ! empty( $new_note ) && ! empty( $customer->id ) ) {
235
236
		ob_start();
237
		?>
238
		<div class="customer-note-wrapper dashboard-comment-wrap comment-item">
239
			<span class="note-content-wrap">
240
				<?php echo stripslashes( $new_note ); ?>
241
			</span>
242
		</div>
243
		<?php
244
		$output = ob_get_contents();
245
		ob_end_clean();
246
247
		if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
248
			echo $output;
249
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_customer_save_note() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
250
		}
251
252
		return $new_note;
253
254
	}
255
256
	return false;
257
258
}
259
260
add_action( 'give_add-customer-note', 'give_customer_save_note', 10, 1 );
261
262
/**
263
 * Delete a customer
264
 *
265
 * @since  1.0
266
 *
267
 * @param  array $args The $_POST array being passed
268
 *
269
 * @return int Whether it was a successful deletion
270
 */
271
function give_customer_delete( $args ) {
272
273
	$customer_edit_role = apply_filters( 'give_edit_customers_role', 'edit_give_payments' );
274
275
	if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) {
276
		wp_die( esc_html__( 'You do not have permission to delete donors.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
277
	}
278
279
	if ( empty( $args ) ) {
280
		return;
281
	}
282
283
	$customer_id = (int) $args['customer_id'];
284
	$confirm     = ! empty( $args['give-customer-delete-confirm'] ) ? true : false;
285
	$remove_data = ! empty( $args['give-customer-delete-records'] ) ? true : false;
286
	$nonce       = $args['_wpnonce'];
287
288
	if ( ! wp_verify_nonce( $nonce, 'delete-customer' ) ) {
289
		wp_die( esc_html__( 'Cheatin&#8217; uh?', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
290
	}
291
292
	if ( ! $confirm ) {
293
		give_set_error( 'customer-delete-no-confirm', esc_html__( 'Please confirm you want to delete this donor.', 'give' ) );
294
	}
295
296
	if ( give_get_errors() ) {
297
		wp_redirect( admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $customer_id ) );
298
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_customer_delete() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
299
	}
300
301
	$customer = new Give_Customer( $customer_id );
302
303
	/**
304
	 * Fires before deleting customer.
305
	 *
306
	 * @since 1.0
307
	 *
308
	 * @param int  $customer_id The ID of the customer.
309
	 * @param bool $confirm     Delete confirmation.
310
	 * @param bool $remove_data Records delete confirmation.
311
	 */
312
	do_action( 'give_pre_delete_customer', $customer_id, $confirm, $remove_data );
313
	
314
	if ( $customer->id > 0 ) {
315
316
		$payments_array = explode( ',', $customer->payment_ids );
317
		$success        = Give()->customers->delete( $customer->id );
318
319
		if ( $success ) {
320
321
			if ( $remove_data ) {
322
323
				// Remove all donations, logs, etc
324
				foreach ( $payments_array as $payment_id ) {
325
					give_delete_purchase( $payment_id );
326
				}
327
328
			} else {
329
330
				// Just set the donations to customer_id of 0
331
				foreach ( $payments_array as $payment_id ) {
332
					give_update_payment_meta( $payment_id, '_give_payment_customer_id', 0 );
333
				}
334
335
			}
336
337
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&give-message=customer-deleted' );
338
339
		} else {
340
341
			give_set_error( 'give-donor-delete-failed', esc_html__( 'Error deleting donor.', 'give' ) );
342
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $customer_id );
343
344
		}
345
346
	} else {
347
348
		give_set_error( 'give-customer-delete-invalid-id', esc_html__( 'Invalid Donor ID.', 'give' ) );
349
		$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors' );
350
351
	}
352
353
	wp_redirect( $redirect );
354
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_customer_delete() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
355
356
}
357
358
add_action( 'give_delete-customer', 'give_customer_delete', 10, 1 );
359
360
/**
361
 * Disconnect a user ID from a donor
362
 *
363
 * @since  1.0
364
 *
365
 * @param  array $args Array of arguments
366
 *
367
 * @return bool        If the disconnect was successful
368
 */
369
function give_disconnect_customer_user_id( $args ) {
370
371
	$customer_edit_role = apply_filters( 'give_edit_customers_role', 'edit_give_payments' );
372
373
	if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) {
374
		wp_die( esc_html__( 'You do not have permission to edit this donor.', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
375
	}
376
377
	if ( empty( $args ) ) {
378
		return;
379
	}
380
381
	$customer_id = (int) $args['customer_id'];
382
	$nonce       = $args['_wpnonce'];
383
384
	if ( ! wp_verify_nonce( $nonce, 'edit-customer' ) ) {
385
		wp_die( esc_html__( 'Cheatin&#8217; uh?', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 400 ) );
386
	}
387
388
	$customer = new Give_Customer( $customer_id );
389
	if ( empty( $customer->id ) ) {
390
		return false;
391
	}
392
393
	$user_id = $customer->user_id;
394
395
	/**
396
	 * Fires before disconnecting user ID from a donor.
397
	 *
398
	 * @since 1.0
399
	 *
400
	 * @param int $customer_id The ID of the customer.
401
	 * @param int $user_id     The ID of the user.
402
	 */
403
	do_action( 'give_pre_customer_disconnect_user_id', $customer_id, $user_id );
404
405
	$output = array();
406
	$customer_args = array( 'user_id' => 0 );
407
408
	if ( $customer->update( $customer_args ) ) {
409
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
410
411
		if ( ! empty( $customer->payment_ids ) ) {
412
			$wpdb->query( "UPDATE $wpdb->postmeta SET meta_value = 0 WHERE meta_key = '_give_payment_user_id' AND post_id IN ( $customer->payment_ids )" );
413
		}
414
415
		$output['success'] = true;
416
417
	} else {
418
419
		$output['success'] = false;
420
		give_set_error( 'give-disconnect-user-fail', esc_html__( 'Failed to disconnect user from donor.', 'give' ) );
421
	}
422
423
	/**
424
	 * Fires after disconnecting user ID from a donor.
425
	 *
426
	 * @since 1.0
427
	 *
428
	 * @param int $customer_id The ID of the customer.
429
	 */
430
	do_action( 'give_post_customer_disconnect_user_id', $customer_id );
431
432
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
433
		header( 'Content-Type: application/json' );
434
		echo json_encode( $output );
435
		wp_die();
436
	}
437
438
	return $output;
439
440
}
441
442
add_action( 'give_disconnect-userid', 'give_disconnect_customer_user_id', 10, 1 );
443
444
/**
445
 * Add an email address to the donor from within the admin and log a donor note
446
 *
447
 * @since  1.7
448
 * @param  array $args  Array of arguments: nonce, customer id, and email address
449
 * @return mixed        If DOING_AJAX echos out JSON, otherwise returns array of success (bool) and message (string)
450
 */
451
function give_add_donor_email( $args ) {
452
	$customer_edit_role = apply_filters( 'give_edit_customers_role', 'edit_give_payments' );
453
454
	if ( ! is_admin() || ! current_user_can( $customer_edit_role ) ) {
455
		wp_die( esc_html__( 'You do not have permission to edit this donor.', 'edit' ) );
456
	}
457
458
	$output = array();
459
	if ( empty( $args ) || empty( $args['email'] ) || empty( $args['customer_id'] ) ) {
460
		$output['success'] = false;
461
		if ( empty( $args['email'] ) ) {
462
			$output['message'] = esc_html__( 'Email address is required.', 'give' );
463
		} else if ( empty( $args['customer_id'] ) ) {
464
			$output['message'] = esc_html__( 'Customer ID is required.', 'give' );
465
		} else {
466
			$output['message'] = esc_html__( 'An error has occurred. Please try again.', 'give' );
467
		}
468
	} else if ( ! wp_verify_nonce( $args['_wpnonce'], 'give_add_donor_email' ) ) {
469
		$output = array(
470
			'success' => false,
471
			'message' => esc_html__( 'Nonce verification failed.', 'give' ),
472
		);
473
	} else if ( ! is_email( $args['email'] ) ) {
474
		$output = array(
475
			'success' => false,
476
			'message' => esc_html__( 'Invalid email address.', 'give' ),
477
		);
478
	} else {
479
		$email       = sanitize_email($args['email'] );
480
		$customer_id = (int) $args['customer_id'];
481
		$primary     = 'true' === $args['primary'] ? true : false;
482
		$customer    = new Give_Customer( $customer_id );
483
		if ( false === $customer->add_email( $email, $primary ) ) {
484
			if ( in_array( $email, $customer->emails ) ) {
485
				$output = array(
486
					'success'  => false,
487
					'message'  => esc_html__( 'Email already associated with this donor.', 'give' ),
488
				);
489
			} else {
490
				$output = array(
491
					'success' => false,
492
					'message' => esc_html__( 'Email address is already associated with another donor.', 'give' ),
493
				);
494
			}
495
		} else {
496
			$redirect = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $customer_id . '&give-message=email-added' );
497
			$output = array(
498
				'success'  => true,
499
				'message'  => esc_html__( 'Email successfully added to donor.', 'give' ),
500
				'redirect' => $redirect,
501
			);
502
503
			$user          = wp_get_current_user();
504
			$user_login    = ! empty( $user->user_login ) ? $user->user_login : esc_html__( 'System', 'give' );
505
			$customer_note = sprintf( __( 'Email address %s added by %s', 'give' ), $email, $user_login );
506
			$customer->add_note( $customer_note );
507
508
			if ( $primary ) {
509
				$customer_note = sprintf( __( 'Email address %s set as primary by %s', 'give' ), $email, $user_login );
510
				$customer->add_note( $customer_note );
511
			}
512
		}
513
	}
514
515
	do_action( 'give_post_add_customer_email', $customer_id, $args );
0 ignored issues
show
Bug introduced by
The variable $customer_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
516
517
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
518
		header( 'Content-Type: application/json' );
519
		echo json_encode( $output );
520
		wp_die();
521
	}
522
523
	return $output;
524
}
525
add_action( 'give_add_donor_email', 'give_add_donor_email', 10, 1 );
526
527
528
/**
529
 * Remove an email address to the donor from within the admin and log a donor note
530
 * and redirect back to the donor interface for feedback
531
 *
532
 * @since  1.7
533
 * @return void|bool
534
 */
535
function give_remove_donor_email() {
536
	if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
537
		return false;
538
	}
539
	if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) {
540
		return false;
541
	}
542
	if ( empty( $_GET['_wpnonce'] ) ) {
543
		return false;
544
	}
545
546
	$nonce = $_GET['_wpnonce'];
547
	if ( ! wp_verify_nonce( $nonce, 'give-remove-donor-email' ) ) {
548
		wp_die( esc_html__( 'Nonce verification failed', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
549
	}
550
551
	$customer = new Give_Customer( $_GET['id'] );
552
	if ( $customer->remove_email( $_GET['email'] ) ) {
553
		$url = add_query_arg( 'give-message', 'email-removed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $customer->id ) );
554
		$user          = wp_get_current_user();
555
		$user_login    = ! empty( $user->user_login ) ? $user->user_login : esc_html__( 'System', 'give' );
556
		$customer_note = sprintf( __( 'Email address %s removed by %s', 'give' ), $_GET['email'], $user_login );
557
		$customer->add_note( $customer_note );
558
	} else {
559
		$url = add_query_arg( 'give-message', 'email-remove-failed', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $customer->id ) );
560
	}
561
562
	wp_safe_redirect( $url );
563
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_remove_donor_email() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
564
}
565
add_action( 'give_remove_donor_email', 'give_remove_donor_email', 10 );
566
567
568
/**
569
 * Set an email address as the primary for a donor from within the admin and log a donor note
570
 * and redirect back to the donor interface for feedback
571
 *
572
 * @since  1.7
573
 * @return void|bool
574
 */
575
function give_set_donor_primary_email() {
576
	if ( empty( $_GET['id'] ) || ! is_numeric( $_GET['id'] ) ) {
577
		return false;
578
	}
579
580
	if ( empty( $_GET['email'] ) || ! is_email( $_GET['email'] ) ) {
581
		return false;
582
	}
583
584
	if ( empty( $_GET['_wpnonce'] ) ) {
585
		return false;
586
	}
587
588
	$nonce = $_GET['_wpnonce'];
589
590
	if ( ! wp_verify_nonce( $nonce, 'give-set-donor-primary-email' ) ) {
591
		wp_die( esc_html__( 'Nonce verification failed', 'give' ), esc_html__( 'Error', 'give' ), array( 'response' => 403 ) );
592
	}
593
594
	$donor = new Give_Customer( $_GET['id'] );
595
596
	if ( $donor->set_primary_email( $_GET['email'] ) ) {
597
		$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 ) );
598
		$user          = wp_get_current_user();
599
		$user_login    = ! empty( $user->user_login ) ? $user->user_login : esc_html__( 'System', 'give' );
600
		$donor_note    = sprintf( __( 'Email address %s set as primary by %s', 'give' ), $_GET['email'], $user_login );
601
602
		$donor->add_note( $donor_note );
603
	} else {
604
		$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 ) );
605
	}
606
607
	wp_safe_redirect( $url );
608
	exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The function give_set_donor_primary_email() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
609
}
610
add_action( 'give_set_donor_primary_email', 'give_set_donor_primary_email', 10 );
611