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