PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.3.2
GiveWP – Donation Plugin and Fundraising Platform v2.3.2
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / includes / admin / admin-actions.php

admin-actions.php in GiveWP – Donation Plugin and Fundraising Platform 2.3.2, at includes/admin/admin-actions.php

1,167 lines 31.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Actions
4 *
5 * @package Give
6 * @subpackage Admin/Actions
7 * @copyright Copyright (c) 2016, GiveWP
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 * Load wp editor by ajax.
19 *
20 * @since 1.8
21 */
22 function give_load_wp_editor() {
23 if ( ! isset( $_POST['wp_editor'] ) ) {
24 die();
25 }
26
27 $wp_editor = json_decode( base64_decode( $_POST['wp_editor'] ), true );
28 $wp_editor[2]['textarea_name'] = $_POST['textarea_name'];
29
30 wp_editor( $wp_editor[0], $_POST['wp_editor_id'], $wp_editor[2] );
31
32 die();
33 }
34
35 add_action( 'wp_ajax_give_load_wp_editor', 'give_load_wp_editor' );
36
37
38 /**
39 * Redirect admin to clean url give admin pages.
40 *
41 * @since 1.8
42 *
43 * @return bool
44 */
45 function give_redirect_to_clean_url_admin_pages() {
46 // Give admin pages.
47 $give_pages = array(
48 'give-payment-history',
49 'give-donors',
50 'give-reports',
51 'give-tools',
52 );
53
54 // Get current page.
55 $current_page = isset( $_GET['page'] ) ? esc_attr( $_GET['page'] ) : '';
56
57 // Bailout.
58 if (
59 empty( $current_page )
60 || empty( $_GET['_wp_http_referer'] )
61 || ! in_array( $current_page, $give_pages )
62 ) {
63 return false;
64 }
65
66 /**
67 * Verify current page request.
68 *
69 * @since 1.8
70 */
71 $redirect = apply_filters( "give_validate_{$current_page}", true );
72
73 if ( $redirect ) {
74 // Redirect.
75 wp_redirect(
76 remove_query_arg(
77 array( '_wp_http_referer', '_wpnonce' ),
78 wp_unslash( $_SERVER['REQUEST_URI'] )
79 )
80 );
81 exit;
82 }
83 }
84
85 add_action( 'admin_init', 'give_redirect_to_clean_url_admin_pages' );
86
87
88 /**
89 * Hide Outdated PHP Notice Shortly.
90 *
91 * This code is used with AJAX call to hide outdated PHP notice for a short period of time
92 *
93 * @since 1.8.9
94 *
95 * @return void
96 */
97 function give_hide_outdated_php_notice() {
98
99 if ( ! isset( $_POST['_give_hide_outdated_php_notices_shortly'] ) ) {
100 give_die();
101 }
102
103 // Transient key name.
104 $transient_key = '_give_hide_outdated_php_notices_shortly';
105
106 if ( Give_Cache::get( $transient_key, true ) ) {
107 return;
108 }
109
110 // Hide notice for 24 hours.
111 Give_Cache::set( $transient_key, true, DAY_IN_SECONDS, true );
112
113 give_die();
114
115 }
116
117 add_action( 'wp_ajax_give_hide_outdated_php_notice', 'give_hide_outdated_php_notice' );
118
119 /**
120 * Register admin notices.
121 *
122 * @since 1.8.9
123 */
124 function _give_register_admin_notices() {
125 // Bailout.
126 if ( ! is_admin() ) {
127 return;
128 }
129
130 // Bulk action notices.
131 if (
132 isset( $_GET['action'] ) &&
133 ! empty( $_GET['action'] )
134 ) {
135
136 // Add payment bulk notice.
137 if (
138 current_user_can( 'edit_give_payments' ) &&
139 isset( $_GET['payment'] ) &&
140 ! empty( $_GET['payment'] )
141 ) {
142 $payment_count = isset( $_GET['payment'] ) ? count( $_GET['payment'] ) : 0;
143
144 switch ( $_GET['action'] ) {
145 case 'delete':
146 Give()->notices->register_notice( array(
147 'id' => 'bulk_action_delete',
148 'type' => 'updated',
149 'description' => sprintf(
150 _n(
151 'Successfully deleted one donation.',
152 'Successfully deleted %d donations.',
153 $payment_count,
154 'give'
155 ),
156 $payment_count ),
157 'show' => true,
158 ) );
159
160 break;
161
162 case 'resend-receipt':
163 Give()->notices->register_notice( array(
164 'id' => 'bulk_action_resend_receipt',
165 'type' => 'updated',
166 'description' => sprintf(
167 _n(
168 'Successfully sent email receipt to one recipient.',
169 'Successfully sent email receipts to %d recipients.',
170 $payment_count,
171 'give'
172 ),
173 $payment_count
174 ),
175 'show' => true,
176 ) );
177 break;
178
179 case 'set-status-publish' :
180 case 'set-status-pending' :
181 case 'set-status-processing' :
182 case 'set-status-refunded' :
183 case 'set-status-revoked' :
184 case 'set-status-failed' :
185 case 'set-status-cancelled' :
186 case 'set-status-abandoned' :
187 case 'set-status-preapproval' :
188 Give()->notices->register_notice( array(
189 'id' => 'bulk_action_status_change',
190 'type' => 'updated',
191 'description' => _n(
192 'Donation status updated successfully.',
193 'Donation statuses updated successfully.',
194 $payment_count,
195 'give'
196 ),
197 'show' => true,
198 ) );
199 break;
200 }// End switch().
201 }// End if().
202 }// End if().
203
204 // Add give message notices.
205 $message_notices = give_get_admin_messages_key();
206 if ( ! empty( $message_notices ) ) {
207 foreach ( $message_notices as $message_notice ) {
208 // Donation reports errors.
209 if ( current_user_can( 'view_give_reports' ) ) {
210 switch ( $message_notice ) {
211 case 'donation-deleted' :
212 Give()->notices->register_notice( array(
213 'id' => 'give-donation-deleted',
214 'type' => 'updated',
215 'description' => __( 'The donation has been deleted.', 'give' ),
216 'show' => true,
217 ) );
218 break;
219 case 'email-sent' :
220 Give()->notices->register_notice( array(
221 'id' => 'give-email-sent',
222 'type' => 'updated',
223 'description' => __( 'The donation receipt has been resent.', 'give' ),
224 'show' => true,
225 ) );
226 break;
227 case 'refreshed-reports' :
228 Give()->notices->register_notice( array(
229 'id' => 'give-refreshed-reports',
230 'type' => 'updated',
231 'description' => __( 'The reports cache has been cleared.', 'give' ),
232 'show' => true,
233 ) );
234 break;
235 case 'donation-note-deleted' :
236 Give()->notices->register_notice( array(
237 'id' => 'give-donation-note-deleted',
238 'type' => 'updated',
239 'description' => __( 'The donation note has been deleted.', 'give' ),
240 'show' => true,
241 ) );
242 break;
243 }// End switch().
244 }// End if().
245
246 // Give settings notices and errors.
247 if ( current_user_can( 'manage_give_settings' ) ) {
248 switch ( $message_notice ) {
249 case 'settings-imported' :
250 Give()->notices->register_notice( array(
251 'id' => 'give-settings-imported',
252 'type' => 'updated',
253 'description' => __( 'The settings have been imported.', 'give' ),
254 'show' => true,
255 ) );
256 break;
257 case 'api-key-generated' :
258 Give()->notices->register_notice( array(
259 'id' => 'give-api-key-generated',
260 'type' => 'updated',
261 'description' => __( 'API keys have been generated.', 'give' ),
262 'show' => true,
263 ) );
264 break;
265 case 'api-key-exists' :
266 Give()->notices->register_notice( array(
267 'id' => 'give-api-key-exists',
268 'type' => 'updated',
269 'description' => __( 'The specified user already has API keys.', 'give' ),
270 'show' => true,
271 ) );
272 break;
273 case 'api-key-regenerated' :
274 Give()->notices->register_notice( array(
275 'id' => 'give-api-key-regenerated',
276 'type' => 'updated',
277 'description' => __( 'API keys have been regenerated.', 'give' ),
278 'show' => true,
279 ) );
280 break;
281 case 'api-key-revoked' :
282 Give()->notices->register_notice( array(
283 'id' => 'give-api-key-revoked',
284 'type' => 'updated',
285 'description' => __( 'API keys have been revoked.', 'give' ),
286 'show' => true,
287 ) );
288 break;
289 case 'sent-test-email' :
290 Give()->notices->register_notice( array(
291 'id' => 'give-sent-test-email',
292 'type' => 'updated',
293 'description' => __( 'The test email has been sent.', 'give' ),
294 'show' => true,
295 ) );
296 break;
297 case 'matched-success-failure-page':
298 Give()->notices->register_notice( array(
299 'id' => 'give-matched-success-failure-page',
300 'type' => 'updated',
301 'description' => __( 'You cannot set the success and failed pages to the same page', 'give' ),
302 'show' => true,
303 ) );
304 break;
305 }// End switch().
306 }// End if().
307
308 // Payments errors.
309 if ( current_user_can( 'edit_give_payments' ) ) {
310 switch ( $message_notice ) {
311 case 'note-added' :
312 Give()->notices->register_notice( array(
313 'id' => 'give-note-added',
314 'type' => 'updated',
315 'description' => __( 'The donation note has been added.', 'give' ),
316 'show' => true,
317 ) );
318 break;
319 case 'payment-updated' :
320 Give()->notices->register_notice( array(
321 'id' => 'give-payment-updated',
322 'type' => 'updated',
323 'description' => __( 'The donation has been updated.', 'give' ),
324 'show' => true,
325 ) );
326 break;
327 }// End switch().
328 }// End if().
329
330 // Donor Notices.
331 if ( current_user_can( 'edit_give_payments' ) ) {
332 switch ( $message_notice ) {
333 case 'donor-deleted' :
334 Give()->notices->register_notice( array(
335 'id' => 'give-donor-deleted',
336 'type' => 'updated',
337 'description' => __( 'The selected donor(s) has been deleted.', 'give' ),
338 'show' => true,
339 ) );
340 break;
341
342 case 'donor-donations-deleted' :
343 Give()->notices->register_notice( array(
344 'id' => 'give-donor-donations-deleted',
345 'type' => 'updated',
346 'description' => __( 'The selected donor(s) and the associated donation(s) has been deleted.', 'give' ),
347 'show' => true,
348 ) );
349 break;
350
351 case 'confirm-delete-donor' :
352 Give()->notices->register_notice( array(
353 'id' => 'give-confirm-delete-donor',
354 'type' => 'updated',
355 'description' => __( 'You must confirm to delete the selected donor(s).', 'give' ),
356 'show' => true,
357 ) );
358 break;
359
360 case 'invalid-donor-id' :
361 Give()->notices->register_notice( array(
362 'id' => 'give-invalid-donor-id',
363 'type' => 'updated',
364 'description' => __( 'Invalid Donor ID.', 'give' ),
365 'show' => true,
366 ) );
367 break;
368
369 case 'donor-delete-failed' :
370 Give()->notices->register_notice( array(
371 'id' => 'give-donor-delete-failed',
372 'type' => 'error',
373 'description' => __( 'Unable to delete selected donor(s).', 'give' ),
374 'show' => true,
375 ) );
376 break;
377
378 case 'email-added' :
379 Give()->notices->register_notice( array(
380 'id' => 'give-email-added',
381 'type' => 'updated',
382 'description' => __( 'Donor email added.', 'give' ),
383 'show' => true,
384 ) );
385 break;
386
387 case 'email-removed' :
388 Give()->notices->register_notice( array(
389 'id' => 'give-email-removed',
390 'type' => 'updated',
391 'description' => __( 'Donor email removed.', 'give' ),
392 'show' => true,
393 ) );
394 break;
395
396 case 'email-remove-failed' :
397 Give()->notices->register_notice( array(
398 'id' => 'give-email-remove-failed',
399 'type' => 'updated',
400 'description' => __( 'Failed to remove donor email.', 'give' ),
401 'show' => true,
402 ) );
403 break;
404
405 case 'primary-email-updated' :
406 Give()->notices->register_notice( array(
407 'id' => 'give-primary-email-updated',
408 'type' => 'updated',
409 'description' => __( 'Primary email updated for donor.', 'give' ),
410 'show' => true,
411 ) );
412 break;
413
414 case 'primary-email-failed' :
415 Give()->notices->register_notice( array(
416 'id' => 'give-primary-email-failed',
417 'type' => 'updated',
418 'description' => __( 'Failed to set primary email.', 'give' ),
419 'show' => true,
420 ) );
421 break;
422
423 case 'reconnect-user' :
424 Give()->notices->register_notice( array(
425 'id' => 'give-reconnect-user',
426 'type' => 'updated',
427 'description' => __( 'User has been successfully connected with Donor.', 'give' ),
428 'show' => true,
429 ) );
430 break;
431
432 case 'profile-updated' :
433 Give()->notices->register_notice( array(
434 'id' => 'give-profile-updated',
435 'type' => 'updated',
436 'description' => __( 'Donor information updated successfully.', 'give' ),
437 'show' => true,
438 ) );
439 break;
440 }// End switch().
441 }// End if().
442 }
443 }
444 }
445
446 add_action( 'admin_notices', '_give_register_admin_notices', - 1 );
447
448
449 /**
450 * Display admin bar when active.
451 *
452 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
453 *
454 * @return bool
455 */
456 function _give_show_test_mode_notice_in_admin_bar( $wp_admin_bar ) {
457 $is_test_mode = ! empty( $_POST['test_mode'] ) ?
458 give_is_setting_enabled( $_POST['test_mode'] ) :
459 give_is_test_mode();
460
461 if (
462 ! current_user_can( 'view_give_reports' ) ||
463 ! $is_test_mode
464 ) {
465 return false;
466 }
467
468 // Add the main site admin menu item.
469 $wp_admin_bar->add_menu( array(
470 'id' => 'give-test-notice',
471 'href' => admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways' ),
472 'parent' => 'top-secondary',
473 'title' => __( 'Give Test Mode Active', 'give' ),
474 'meta' => array(
475 'class' => 'give-test-mode-active',
476 ),
477 ) );
478
479 return true;
480 }
481
482 add_action( 'admin_bar_menu', '_give_show_test_mode_notice_in_admin_bar', 1000, 1 );
483
484 /**
485 * Outputs the Give admin bar CSS.
486 */
487 function _give_test_mode_notice_admin_bar_css() {
488 if ( ! give_is_test_mode() ) {
489 return;
490 }
491 ?>
492 <style>
493 #wpadminbar .give-test-mode-active > .ab-item {
494 color: #fff;
495 background-color: #ffba00;
496 }
497
498 #wpadminbar .give-test-mode-active:hover > .ab-item, #wpadminbar .give-test-mode-active:hover > .ab-item {
499 background-color: rgba(203, 144, 0, 1) !important;
500 color: #fff !important;
501 }
502 </style>
503 <?php
504 }
505
506 add_action( 'admin_head', '_give_test_mode_notice_admin_bar_css' );
507
508
509 /**
510 * Add Link to Import page in from donation archive and donation single page
511 *
512 * @since 1.8.13
513 */
514 function give_import_page_link_callback() {
515 ?>
516 <a href="<?php echo esc_url( give_import_page_url() ); ?>"
517 class="page-import-action page-title-action"><?php _e( 'Import Donations', 'give' ); ?></a>
518
519 <?php
520 // Check if view donation single page only.
521 if ( ! empty( $_REQUEST['view'] ) && 'view-payment-details' === (string) give_clean( $_REQUEST['view'] ) && 'give-payment-history' === give_clean( $_REQUEST['page'] ) ) {
522 ?>
523 <style type="text/css">
524 .wrap #transaction-details-heading {
525 display: inline-block;
526 }
527 </style>
528 <?php
529 }
530 }
531
532 add_action( 'give_payments_page_top', 'give_import_page_link_callback', 11 );
533
534 /**
535 * Load donation import ajax callback
536 * Fire when importing from CSV start
537 *
538 * @since 1.8.13
539 */
540 function give_donation_import_callback() {
541 // Bailout.
542 if ( ! current_user_can( 'manage_give_settings' ) ) {
543 give_die();
544 }
545
546 // Disable Give cache
547 Give_Cache::get_instance()->disable();
548
549 $import_setting = array();
550 $fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null;
551
552 parse_str( $fields, $output );
553
554 $import_setting['create_user'] = $output['create_user'];
555 $import_setting['mode'] = $output['mode'];
556 $import_setting['delimiter'] = $output['delimiter'];
557 $import_setting['csv'] = $output['csv'];
558 $import_setting['delete_csv'] = $output['delete_csv'];
559 $import_setting['dry_run'] = $output['dry_run'];
560
561 // Parent key id.
562 $main_key = maybe_unserialize( $output['main_key'] );
563
564 $current = absint( $_REQUEST['current'] );
565 $total_ajax = absint( $_REQUEST['total_ajax'] );
566 $start = absint( $_REQUEST['start'] );
567 $end = absint( $_REQUEST['end'] );
568 $next = absint( $_REQUEST['next'] );
569 $total = absint( $_REQUEST['total'] );
570 $per_page = absint( $_REQUEST['per_page'] );
571 if ( empty( $output['delimiter'] ) ) {
572 $delimiter = ',';
573 } else {
574 $delimiter = $output['delimiter'];
575 }
576
577 // Processing done here.
578 $raw_data = give_get_donation_data_from_csv( $output['csv'], $start, $end, $delimiter );
579 $raw_key = maybe_unserialize( $output['mapto'] );
580 $import_setting['raw_key'] = $raw_key;
581
582 if ( ! empty( $output['dry_run'] ) ) {
583 $import_setting['csv_raw_data'] = give_get_donation_data_from_csv( $output['csv'], 1, $end, $delimiter );
584
585 $import_setting['donors_list'] = Give()->donors->get_donors( array(
586 'number' => - 1,
587 'fields' => array( 'id', 'user_id', 'email' ),
588 ) );
589 }
590
591 // Prevent normal emails.
592 remove_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
593 remove_action( 'give_insert_user', 'give_new_user_notification', 10 );
594 remove_action( 'give_insert_payment', 'give_payment_save_page_data' );
595
596 $current_key = $start;
597 foreach ( $raw_data as $row_data ) {
598 $import_setting['donation_key'] = $current_key;
599 give_save_import_donation_to_db( $raw_key, $row_data, $main_key, $import_setting );
600 $current_key ++;
601 }
602
603 // Check if function exists or not.
604 if ( function_exists( 'give_payment_save_page_data' ) ) {
605 add_action( 'give_insert_payment', 'give_payment_save_page_data' );
606 }
607 add_action( 'give_insert_user', 'give_new_user_notification', 10, 2 );
608 add_action( 'give_complete_donation', 'give_trigger_donation_receipt', 999 );
609
610 if ( $next == false ) {
611 $json_data = array(
612 'success' => true,
613 'message' => __( 'All donation uploaded successfully!', 'give' ),
614 );
615 } else {
616 $index_start = $start;
617 $index_end = $end;
618 $last = false;
619 $next = true;
620 if ( $next ) {
621 $index_start = $index_start + $per_page;
622 $index_end = $per_page + ( $index_start - 1 );
623 }
624 if ( $index_end >= $total ) {
625 $index_end = $total;
626 $last = true;
627 }
628 $json_data = array(
629 'raw_data' => $raw_data,
630 'raw_key' => $raw_key,
631 'next' => $next,
632 'start' => $index_start,
633 'end' => $index_end,
634 'last' => $last,
635 );
636 }
637
638 $url = give_import_page_url( array(
639 'step' => '4',
640 'importer-type' => 'import_donations',
641 'csv' => $output['csv'],
642 'total' => $total,
643 'delete_csv' => $import_setting['delete_csv'],
644 'success' => ( isset( $json_data['success'] ) ? $json_data['success'] : '' ),
645 'dry_run' => $output['dry_run'],
646 ) );
647 $json_data['url'] = $url;
648
649 $current ++;
650 $json_data['current'] = $current;
651
652 $percentage = ( 100 / ( $total_ajax + 1 ) ) * $current;
653 $json_data['percentage'] = $percentage;
654
655 // Enable Give cache
656 Give_Cache::get_instance()->enable();
657
658 $json_data = apply_filters( 'give_import_ajax_responces', $json_data, $fields );
659 wp_die( json_encode( $json_data ) );
660 }
661
662 add_action( 'wp_ajax_give_donation_import', 'give_donation_import_callback' );
663
664 /**
665 * Load core settings import ajax callback
666 * Fire when importing from JSON start
667 *
668 * @since 1.8.17
669 */
670
671 function give_core_settings_import_callback() {
672 // Bailout.
673 if ( ! current_user_can( 'manage_give_settings' ) ) {
674 give_die();
675 }
676
677 $fields = isset( $_POST['fields'] ) ? $_POST['fields'] : null;
678 parse_str( $fields, $fields );
679
680 $json_data['success'] = false;
681
682 /**
683 * Filter to Modify fields that are being pass by the ajax before importing of the core setting start.
684 *
685 * @access public
686 *
687 * @since 1.8.17
688 *
689 * @param array $fields
690 *
691 * @return array $fields
692 */
693 $fields = (array) apply_filters( 'give_import_core_settings_fields', $fields );
694
695 $file_name = ( ! empty( $fields['file_name'] ) ? give_clean( $fields['file_name'] ) : false );
696
697 if ( ! empty( $file_name ) ) {
698 $type = ( ! empty( $fields['type'] ) ? give_clean( $fields['type'] ) : 'merge' );
699
700 // Get the json data from the file and then alter it in array format
701 $json_string = give_get_core_settings_json( $file_name );
702 $json_to_array = json_decode( $json_string, true );
703
704 // get the current settign from the options table.
705 $host_give_options = get_option( 'give_settings', array() );
706
707 // Save old settins for backup.
708 update_option( 'give_settings_old', $host_give_options, false );
709
710 /**
711 * Filter to Modify Core Settings that are being going to get import in options table as give settings.
712 *
713 * @access public
714 *
715 * @since 1.8.17
716 *
717 * @param array $json_to_array Setting that are being going to get imported
718 * @param array $type Type of Import
719 * @param array $host_give_options Setting old setting that used to be in the options table.
720 * @param array $fields Data that is being send from the ajax
721 *
722 * @return array $json_to_array Setting that are being going to get imported
723 */
724 $json_to_array = (array) apply_filters( 'give_import_core_settings_data', $json_to_array, $type, $host_give_options, $fields );
725
726 update_option( 'give_settings', $json_to_array, false );
727
728 $json_data['success'] = true;
729 }
730
731 $json_data['percentage'] = 100;
732
733 /**
734 * Filter to Modify core import setting page url
735 *
736 * @access public
737 *
738 * @since 1.8.17
739 *
740 * @return array $url
741 */
742 $json_data['url'] = give_import_page_url( (array) apply_filters( 'give_import_core_settings_success_url', array(
743 'step' => ( empty( $json_data['success'] ) ? '1' : '3' ),
744 'importer-type' => 'import_core_setting',
745 'success' => ( empty( $json_data['success'] ) ? '0' : '1' ),
746 ) ) );
747
748 wp_send_json( $json_data );
749 }
750
751 add_action( 'wp_ajax_give_core_settings_import', 'give_core_settings_import_callback' );
752
753 /**
754 * Initializes blank slate content if a list table is empty.
755 *
756 * @since 1.8.13
757 */
758 function give_blank_slate() {
759 $blank_slate = new Give_Blank_Slate();
760 $blank_slate->init();
761 }
762
763 add_action( 'current_screen', 'give_blank_slate' );
764
765 /**
766 * Validate Fields of User Profile
767 *
768 * @param object $errors Object of WP Errors.
769 * @param int|bool $update True or False.
770 * @param object $user WP User Data.
771 *
772 * @since 2.0
773 *
774 * @return mixed
775 */
776 function give_validate_user_profile( $errors, $update, $user ) {
777
778 if ( ! empty( $_POST['action'] ) && ( 'adduser' === $_POST['action'] || 'createuser' === $_POST['action'] ) ) {
779 return;
780 }
781
782 if ( ! empty( $user->ID ) ) {
783 $donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
784
785 if ( $donor ) {
786 // If Donor is attached with User, then validate first name.
787 if ( empty( $_POST['first_name'] ) ) {
788 $errors->add(
789 'empty_first_name',
790 sprintf(
791 '<strong>%1$s:</strong> %2$s',
792 __( 'ERROR', 'give' ),
793 __( 'Please enter your first name.', 'give' )
794 )
795 );
796 }
797 }
798 }
799
800 }
801
802 add_action( 'user_profile_update_errors', 'give_validate_user_profile', 10, 3 );
803
804 /**
805 * Show Donor Information on User Profile Page.
806 *
807 * @param object $user User Object.
808 *
809 * @since 2.0
810 */
811 function give_donor_information_profile_fields( $user ) {
812 $donor = Give()->donors->get_donor_by( 'user_id', $user->ID );
813
814 // Display Donor Information, only if donor is attached with User.
815 if ( ! empty( $donor->user_id ) ) {
816 ?>
817 <table class="form-table">
818 <tbody>
819 <tr>
820 <th scope="row"><?php _e( 'Donor', 'give' ); ?></th>
821 <td>
822 <a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor->id ); ?>">
823 <?php _e( 'View Donor Information', 'give' ); ?>
824 </a>
825 </td>
826 </tr>
827 </tbody>
828 </table>
829 <?php
830 }
831 }
832
833 add_action( 'personal_options', 'give_donor_information_profile_fields' );
834 /**
835 * Get Array of WP User Roles.
836 *
837 * @since 1.8.13
838 *
839 * @return array
840 */
841 function give_get_user_roles() {
842 $user_roles = array();
843
844 // Loop through User Roles.
845 foreach ( get_editable_roles() as $role_name => $role_info ) :
846 $user_roles[ $role_name ] = $role_info['name'];
847 endforeach;
848
849 return $user_roles;
850 }
851
852
853 /**
854 * Ajax handle for donor address.
855 *
856 * @since 2.0
857 *
858 * @return string
859 */
860 function __give_ajax_donor_manage_addresses() {
861 // Bailout.
862 if (
863 empty( $_POST['form'] ) ||
864 empty( $_POST['donorID'] )
865 ) {
866 wp_send_json_error( array(
867 'error' => 1,
868 ) );
869 }
870
871 $post = give_clean( wp_parse_args( $_POST ) );
872 $donorID = absint( $post['donorID'] );
873 $form_data = give_clean( wp_parse_args( $post['form'] ) );
874 $is_multi_address_type = ( 'billing' === $form_data['address-id'] || false !== strpos( $form_data['address-id'], '_' ) );
875 $address_type = false !== strpos( $form_data['address-id'], '_' ) ?
876 array_shift( explode( '_', $form_data['address-id'] ) ) :
877 $form_data['address-id'];
878 $address_id = false !== strpos( $form_data['address-id'], '_' ) ?
879 array_pop( explode( '_', $form_data['address-id'] ) ) :
880 null;
881 $response_data = array(
882 'action' => $form_data['address-action'],
883 'id' => $form_data['address-id'],
884 );
885
886 // Security check.
887 if ( ! wp_verify_nonce( $form_data['_wpnonce'], 'give-manage-donor-addresses' ) ) {
888 wp_send_json_error( array(
889 'error' => 1,
890 'error_msg' => wp_sprintf(
891 '<div class="notice notice-error"><p>%s</p></div>',
892 __( 'Error: Security issue.', 'give' )
893 ),
894 )
895 );
896 }
897
898 $donor = new Give_Donor( $donorID );
899
900 // Verify donor.
901 if ( ! $donor->id ) {
902 wp_send_json_error( array(
903 'error' => 3,
904 ) );
905 }
906
907 // Unset all data except address.
908 unset(
909 $form_data['_wpnonce'],
910 $form_data['address-action'],
911 $form_data['address-id']
912 );
913
914 // Process action.
915 switch ( $response_data['action'] ) {
916
917 case 'add':
918 if ( ! $donor->add_address( "{$address_type}[]", $form_data ) ) {
919 wp_send_json_error( array(
920 'error' => 1,
921 'error_msg' => wp_sprintf(
922 '<div class="notice notice-error"><p>%s</p></div>',
923 __( 'Error: Unable to save the address. Please check if address already exist.', 'give' )
924 ),
925 )
926 );
927 }
928
929 $total_addresses = count( $donor->address[ $address_type ] );
930
931 $address_index = $is_multi_address_type ?
932 $total_addresses - 1 :
933 $address_type;
934
935 $array_keys = array_keys( $donor->address[ $address_type ] );
936
937 $address_id = $is_multi_address_type ?
938 end( $array_keys ) :
939 $address_type;
940
941 $response_data['address_html'] = __give_get_format_address(
942 end( $donor->address['billing'] ),
943 array(
944 // We can add only billing address from donor screen.
945 'type' => 'billing',
946 'id' => $address_id,
947 'index' => ++ $address_index,
948 )
949 );
950 $response_data['success_msg'] = wp_sprintf(
951 '<div class="notice updated"><p>%s</p></div>',
952 __( 'Successfully added a new address to the donor.', 'give' )
953 );
954
955 if ( $is_multi_address_type ) {
956 $response_data['id'] = "{$response_data['id']}_{$address_index}";
957 }
958
959 break;
960
961 case 'remove':
962 if ( ! $donor->remove_address( $response_data['id'] ) ) {
963 wp_send_json_error( array(
964 'error' => 2,
965 'error_msg' => wp_sprintf(
966 '<div class="notice notice-error"><p>%s</p></div>',
967 __( 'Error: Unable to delete address.', 'give' )
968 ),
969 )
970 );
971 }
972
973 $response_data['success_msg'] = wp_sprintf(
974 '<div class="notice updated"><p>%s</p></div>',
975 __( 'Successfully removed a address of donor.', 'give' )
976 );
977
978 break;
979
980 case 'update':
981 if ( ! $donor->update_address( $response_data['id'], $form_data ) ) {
982 wp_send_json_error( array(
983 'error' => 3,
984 'error_msg' => wp_sprintf(
985 '<div class="notice notice-error"><p>%s</p></div>',
986 __( 'Error: Unable to update address. Please check if address already exist.', 'give' )
987 ),
988 )
989 );
990 }
991
992 $response_data['address_html'] = __give_get_format_address(
993 $is_multi_address_type ?
994 $donor->address[ $address_type ][ $address_id ] :
995 $donor->address[ $address_type ],
996 array(
997 'type' => $address_type,
998 'id' => $address_id,
999 'index' => $address_id,
1000 )
1001 );
1002 $response_data['success_msg'] = wp_sprintf(
1003 '<div class="notice updated"><p>%s</p></div>',
1004 __( 'Successfully updated a address of donor', 'give' )
1005 );
1006
1007 break;
1008 }// End switch().
1009
1010 wp_send_json_success( $response_data );
1011 }
1012
1013 add_action( 'wp_ajax_donor_manage_addresses', '__give_ajax_donor_manage_addresses' );
1014
1015 /**
1016 * Admin donor billing address label
1017 *
1018 * @since 2.0
1019 *
1020 * @param string $address_label
1021 *
1022 * @return string
1023 */
1024 function __give_donor_billing_address_label( $address_label ) {
1025 $address_label = __( 'Billing Address', 'give' );
1026
1027 return $address_label;
1028 }
1029
1030 add_action( 'give_donor_billing_address_label', '__give_donor_billing_address_label' );
1031
1032 /**
1033 * Admin donor personal address label
1034 *
1035 * @since 2.0
1036 *
1037 * @param string $address_label
1038 *
1039 * @return string
1040 */
1041 function __give_donor_personal_address_label( $address_label ) {
1042 $address_label = __( 'Personal Address', 'give' );
1043
1044 return $address_label;
1045 }
1046
1047 add_action( 'give_donor_personal_address_label', '__give_donor_personal_address_label' );
1048
1049 /**
1050 * Update Donor Information when User Profile is updated from admin.
1051 * Note: for internal use only.
1052 *
1053 * @param int $user_id
1054 *
1055 * @access public
1056 * @since 2.0
1057 *
1058 * @return bool
1059 */
1060 function give_update_donor_name_on_user_update( $user_id = 0 ) {
1061
1062 if ( current_user_can( 'edit_user', $user_id ) ) {
1063
1064 $donor = new Give_Donor( $user_id, true );
1065
1066 // Bailout, if donor doesn't exists.
1067 if ( ! $donor ) {
1068 return false;
1069 }
1070
1071 // Get User First name and Last name.
1072 $first_name = ( $_POST['first_name'] ) ? give_clean( $_POST['first_name'] ) : get_user_meta( $user_id, 'first_name', true );
1073 $last_name = ( $_POST['last_name'] ) ? give_clean( $_POST['last_name'] ) : get_user_meta( $user_id, 'last_name', true );
1074 $full_name = strip_tags( wp_unslash( trim( "{$first_name} {$last_name}" ) ) );
1075
1076 // Assign User First name and Last name to Donor.
1077 Give()->donors->update( $donor->id, array(
1078 'name' => $full_name,
1079 ) );
1080 Give()->donor_meta->update_meta( $donor->id, '_give_donor_first_name', $first_name );
1081 Give()->donor_meta->update_meta( $donor->id, '_give_donor_last_name', $last_name );
1082
1083 }
1084 }
1085
1086 add_action( 'edit_user_profile_update', 'give_update_donor_name_on_user_update', 10 );
1087 add_action( 'personal_options_update', 'give_update_donor_name_on_user_update', 10 );
1088
1089
1090 /**
1091 * Updates the email address of a donor record when the email on a user is updated
1092 * Note: for internal use only.
1093 *
1094 * @since 1.4.3
1095 * @access public
1096 *
1097 * @param int $user_id User ID.
1098 * @param WP_User|bool $old_user_data User data.
1099 *
1100 * @return bool
1101 */
1102 function give_update_donor_email_on_user_update( $user_id = 0, $old_user_data = false ) {
1103
1104 $donor = new Give_Donor( $user_id, true );
1105
1106 if ( ! $donor ) {
1107 return false;
1108 }
1109
1110 $user = get_userdata( $user_id );
1111
1112 if ( ! empty( $user ) && $user->user_email !== $donor->email ) {
1113
1114 $success = Give()->donors->update( $donor->id, array(
1115 'email' => $user->user_email,
1116 ) );
1117
1118 if ( $success ) {
1119 // Update some payment meta if we need to
1120 $payments_array = explode( ',', $donor->payment_ids );
1121
1122 if ( ! empty( $payments_array ) ) {
1123
1124 foreach ( $payments_array as $payment_id ) {
1125
1126 give_update_payment_meta( $payment_id, 'email', $user->user_email );
1127
1128 }
1129 }
1130
1131 /**
1132 * Fires after updating donor email on user update.
1133 *
1134 * @since 1.4.3
1135 *
1136 * @param WP_User $user WordPress User object.
1137 * @param Give_Donor $donor Give donor object.
1138 */
1139 do_action( 'give_update_donor_email_on_user_update', $user, $donor );
1140
1141 }
1142 }
1143
1144 }
1145
1146 add_action( 'profile_update', 'give_update_donor_email_on_user_update', 10, 2 );
1147
1148
1149 /**
1150 * Flushes Give's cache.
1151 */
1152 function give_cache_flush() {
1153 $result = Give_Cache::flush_cache();
1154
1155 if ( $result ) {
1156 wp_send_json_success( array(
1157 'message' => __( 'Cache flushed successfully.', 'give' ),
1158 ));
1159 } else {
1160 wp_send_json_error( array(
1161 'message' => __( 'An error occured while flushing the cache.', 'give' ),
1162 ));
1163 }
1164 }
1165
1166 add_action( 'wp_ajax_give_cache_flush', 'give_cache_flush', 10, 0 );
1167