Completed
Pull Request — master (#1055)
by Rami
19:01
created

customer-actions.php ➔ give_set_donor_primary_email()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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