forked from impress-org/givewp
/
class-give-import-donations.php
1059 lines (953 loc) · 30.1 KB
/
class-give-import-donations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Donations Import Class
*
* This class handles donations import.
*
* @package Give
* @subpackage Classes/Give_Import_Donations
* @copyright Copyright (c) 2017, WordImpress
* @license https://opensource.org/licenses/gpl-license GNU Public License
* @since 1.8.14
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'Give_Import_Donations' ) ) {
/**
* Give_Import_Donations.
*
* @since 1.8.14
*/
final class Give_Import_Donations {
/**
* Importer type
*
* @since 1.8.13
* @var string
*/
private $importer_type = 'import_donations';
/**
* Instance.
*
* @since
* @access private
* @var
*/
static private $instance;
/**
* Importing donation per page.
*
* @since 1.8.14
*
* @var int
*/
public static $per_page = 25;
/**
* Importing donation per page.
*
* @since 2.1
*
* @var int
*/
public $is_csv_valid = false;
/**
* Singleton pattern.
*
* @since
* @access private
*/
private function __construct() {
self::$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : self::$per_page;
}
/**
* Get instance.
*
* @since
* @access public
*
* @return static
*/
public static function get_instance() {
if ( null === static::$instance ) {
self::$instance = new static();
}
return self::$instance;
}
/**
* Setup
*
* @since 1.8.14
*
* @return void
*/
public function setup() {
$this->setup_hooks();
}
/**
* Setup Hooks.
*
* @since 1.8.14
*
* @return void
*/
private function setup_hooks() {
if ( ! $this->is_donations_import_page() ) {
return;
}
// Do not render main import tools page.
remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) );
// Render donation import page
add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) );
// Print the HTML.
add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 );
// Run when form submit.
add_action( 'give-tools_save_import', array( $this, 'save' ) );
add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 );
// Used to add submit button.
add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 );
}
/**
* Update notice
*
* @since 1.8.14
*
* @param $messages
*
* @return mixed
*/
public function update_notices( $messages ) {
if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) {
unset( $messages['give-setting-updated'] );
}
return $messages;
}
/**
* Print submit and nonce button.
*
* @since 1.8.14
*/
public function submit() {
wp_nonce_field( 'give-save-settings', '_give-save-settings' );
?>
<input type="hidden" class="import-step" id="import-step" name="step"
value="<?php echo $this->get_step(); ?>"/>
<input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/>
<?php
}
/**
* Print the HTML for importer.
*
* @since 1.8.14
*/
public function html() {
$step = $this->get_step();
// Show progress.
$this->render_progress();
?>
<section>
<table
class="widefat export-options-table give-table <?php echo "step-{$step}"; ?> <?php echo( 1 === $step && ! empty( $this->is_csv_valid ) ? 'give-hidden' : '' ); ?> "
id="<?php echo "step-{$step}"; ?>">
<tbody>
<?php
switch ( $step ) {
case 1:
$this->render_media_csv();
break;
case 2:
$this->render_dropdown();
break;
case 3:
$this->start_import();
break;
case 4:
$this->import_success();
}
if ( false === $this->check_for_dropdown_or_import() ) {
?>
<tr valign="top">
<th>
<input type="submit"
class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>"
id="recount-stats-submit"
value="
<?php
/**
* Filter to modify donation importer submit button text.
*
* @since 2.1
*/
echo apply_filters( 'give_import_donation_submit_button_text', __( 'Submit', 'give' ) );
?>
"/>
</th>
<th>
<?php
/**
* Action to add submit button description.
*
* @since 2.1
*/
do_action( 'give_import_donation_submit_button' );
?>
</th>
</tr>
<?php
}
?>
</tbody>
</table>
</section>
<?php
}
/**
* Show success notice
*
* @since 1.8.14
*/
public function import_success() {
$delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false );
$csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false );
$dry_run = isset( $_GET['dry_run'] ) ? absint( $_GET['dry_run'] ) : 1;
if ( ! empty( $delete_csv ) && ! empty( $csv ) && empty( $dry_run ) ) {
wp_delete_attachment( $csv, true );
}
$report = give_import_donation_report();
$report_html = array(
'duplicate_donor' => array(
__( '%s duplicate %s detected', 'give' ),
__( '%s duplicate %s will be detected', 'give' ),
__( 'donor', 'give' ),
__( 'donors', 'give' ),
),
'create_donor' => array(
__( '%s %s created', 'give' ),
__( '%s %s will be going to get created', 'give' ),
__( 'donor', 'give' ),
__( 'donors', 'give' ),
),
'create_form' => array(
__( '%s donation %s created', 'give' ),
__( '%s donation %s will be going to get created', 'give' ),
__( 'form', 'give' ),
__( 'forms', 'give' ),
),
'duplicate_donation' => array(
__( '%s duplicate %s detected', 'give' ),
__( '%s duplicate %s will be detected', 'give' ),
__( 'donation', 'give' ),
__( 'donations', 'give' ),
),
'create_donation' => array(
__( '%s %s imported', 'give' ),
__( '%s %s will going to get imported', 'give' ),
__( 'donation', 'give' ),
__( 'donations', 'give' ),
),
);
$total = (int) $_GET['total'];
-- $total;
$success = (bool) $_GET['success'];
?>
<tr valign="top" class="give-import-dropdown">
<th colspan="2">
<h2>
<?php
if ( $success ) {
if ( $dry_run ) {
printf(
_n( 'Dry run import complete! %s donation processed', 'Dry run import complete! %s donations processed', $total, 'give' ),
"<strong>{$total}</strong>"
);
} else {
printf(
_n( 'Import complete! %s donation processed', 'Import complete! %s donations processed', $total, 'give' ),
"<strong>{$total}</strong>"
);
}
} else {
printf(
_n( 'Failed to import %s donation', 'Failed to import %s donations', $total, 'give' ),
"<strong>{$total}</strong>"
);
}
?>
</h2>
<?php
$text = __( 'Import Donation', 'give' );
$query_arg = array(
'post_type' => 'give_forms',
'page' => 'give-tools',
'tab' => 'import',
);
if ( $success ) {
if ( $dry_run ) {
$text = __( 'Begin Actual Import', 'give' );
$query_arg['step'] = 2;
$query_arg['dry_run'] = 0;
} else {
$text = __( 'View Donations', 'give' );
$query_arg = array(
'post_type' => 'give_forms',
'page' => 'give-payment-history',
);
}
}
$url = add_query_arg( $query_arg );
foreach ( $report as $key => $value ) {
if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) {
?>
<p>
<?php printf( $report_html[ $key ][$dry_run], $value, _n( $report_html[ $key ][2], $report_html[ $key ][3], $value, 'give' ) ); ?>
</p>
<?php
}
}
?>
<p>
<a class="button button-large button-secondary"
href="<?php echo $url; ?>"><?php echo $text; ?></a>
</p>
</th>
</tr>
<?php
}
/**
* Will start Import
*
* @since 1.8.14
*/
public function start_import() {
// Reset the donation form report.
give_import_donation_report_reset();
$csv = (int) $_REQUEST['csv'];
$delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
$index_start = 1;
$index_end = 1;
$next = true;
$total = self::get_csv_total( $csv );
if ( self::$per_page < $total ) {
$total_ajax = ceil( $total / self::$per_page );
$index_end = self::$per_page;
} else {
$total_ajax = 1;
$index_end = $total;
$next = false;
}
$current_percentage = 100 / ( $total_ajax + 1 );
?>
<tr valign="top" class="give-import-dropdown">
<th colspan="2">
<h2 id="give-import-title"><?php _e( 'Importing', 'give' ) ?></h2>
<p class="give-field-description"><?php _e( 'Your donations are now being imported...', 'give' ) ?></p>
</th>
</tr>
<tr valign="top" class="give-import-dropdown">
<th colspan="2">
<span class="spinner is-active"></span>
<div class="give-progress"
data-current="1"
data-total_ajax="<?php echo $total_ajax; ?>"
data-start="<?php echo $index_start; ?>"
data-end="<?php echo $index_end; ?>"
data-next="<?php echo $next; ?>"
data-total="<?php echo $total; ?>"
data-per_page="<?php echo self::$per_page; ?>">
<div style="width: <?php echo $current_percentage; ?>%"></div>
</div>
<input type="hidden" value="3" name="step">
<input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto"
class="mapto">
<input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv">
<input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode">
<input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user"
class="create_user">
<input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv"
class="delete_csv">
<input type="hidden" value="<?php echo $delimiter; ?>" name="delimiter">
<input type="hidden" value="<?php echo absint( $_REQUEST['dry_run'] ); ?>" name="dry_run">
<input type="hidden"
value='<?php echo maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ); ?>'
name="main_key"
class="main_key">
</th>
</tr>
<?php
}
/**
* Will return true if importing can be started or not else false.
*
* @since 1.8.14
*/
public function check_for_dropdown_or_import() {
$return = true;
if ( isset( $_REQUEST['mapto'] ) ) {
$mapto = (array) $_REQUEST['mapto'];
if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) {
Give_Admin_Settings::add_error( 'give-import-csv-form', __( 'In order to import donations, a column must be mapped to either the "Donation Form Title" or "Donation Form ID" field. Please map a column to one of those fields.', 'give' ) );
$return = false;
}
if ( false === in_array( 'amount', $mapto ) ) {
Give_Admin_Settings::add_error( 'give-import-csv-amount', __( 'In order to import donations, a column must be mapped to the "Amount" field. Please map a column to that field.', 'give' ) );
$return = false;
}
if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) {
Give_Admin_Settings::add_error( 'give-import-csv-donor', __( 'In order to import donations, a column must be mapped to either the "Donor Email" or "Donor ID" field. Please map a column to that field.', 'give' ) );
$return = false;
}
} else {
$return = false;
}
return $return;
}
/**
* Print the Dropdown option for CSV.
*
* @since 1.8.14
*/
public function render_dropdown() {
$csv = (int) $_GET['csv'];
$delimiter = ( ! empty( $_GET['delimiter'] ) ? give_clean( $_GET['delimiter'] ) : 'csv' );
// TO check if the CSV files that is being add is valid or not if not then redirect to first step again
if ( ! $this->is_valid_csv( $csv ) ) {
$url = give_import_page_url();
?>
<input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo $url; ?>"/>
<?php
} else {
?>
<tr valign="top" class="give-import-dropdown">
<th colspan="2">
<h2 id="give-import-title"><?php _e( 'Map CSV fields to donations', 'give' ) ?></h2>
<p class="give-import-donation-required-fields-title"><?php _e( 'Required Fields' ); ?></p>
<p class="give-field-description"><?php _e( 'These fields are required for the import to submitted' ); ?></p>
<ul class="give-import-donation-required-fields">
<li class="give-import-donation-required-email"
title="Please configure all required fields to start the import process.">
<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
<span class="give-import-donation-required-text">
<?php
_e( 'Email Address', 'give' );
?>
</span>
</li>
<li class="give-import-donation-required-first"
title="Please configure all required fields to start the import process.">
<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
<span class="give-import-donation-required-text">
<?php
_e( 'First Name', 'give' );
?>
</span>
</li>
<li class="give-import-donation-required-amount"
title="Please configure all required fields to start the import process.">
<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
<span class="give-import-donation-required-text">
<?php
_e( 'Donation Amount', 'give' );
?>
</span>
</li>
<li class="give-import-donation-required-form"
title="Please configure all required fields to start the import process.">
<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
<span class="give-import-donation-required-text">
<?php
_e( 'Form Title or ID', 'give' );
?>
</span>
</li>
</ul>
<p class="give-field-description"><?php _e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p>
</th>
</tr>
<tr valign="top" class="give-import-dropdown">
<th><b><?php _e( 'Column name', 'give' ); ?></b></th>
<th><b><?php _e( 'Map to field', 'give' ); ?></b></th>
</tr>
<?php
$raw_key = $this->get_importer( $csv, 0, $delimiter );
$mapto = (array) ( isset( $_REQUEST['mapto'] ) ? $_REQUEST['mapto'] : array() );
foreach ( $raw_key as $index => $value ) {
?>
<tr valign="top" class="give-import-option">
<th><?php echo $value; ?></th>
<th>
<?php
$this->get_columns( $index, $value, $mapto );
?>
</th>
</tr>
<?php
}
}
}
/**
* @param $option_value
* @param $value
*
* @return string
*/
public function selected( $option_value, $value ) {
$option_value = strtolower( $option_value );
$value = strtolower( $value );
$selected = '';
if ( stristr( $value, $option_value ) ) {
$selected = 'selected';
} elseif ( strrpos( $value, 'give_' ) && stristr( $option_value, __( 'Import as Meta', 'give' ) ) ) {
$selected = 'selected';
}
return $selected;
}
/**
* Print the columns from the CSV.
*
* @since 1.8.14
* @access private
*
* @param string $index
* @param bool $value
* @param array $mapto
*
* @return void
*/
private function get_columns( $index, $value = false, $mapto = array() ) {
$default = give_import_default_options();
$current_mapto = (string) ( ! empty( $mapto[ $index ] ) ? $mapto[ $index ] : '' );
?>
<select name="mapto[<?php echo $index; ?>]">
<?php $this->get_dropdown_option_html( $default, $current_mapto, $value ); ?>
<optgroup label="<?php _e( 'Donations', 'give' ); ?>">
<?php
$this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value );
?>
</optgroup>
<optgroup label="<?php _e( 'Donors', 'give' ); ?>">
<?php
$this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value );
?>
</optgroup>
<optgroup label="<?php _e( 'Forms', 'give' ); ?>">
<?php
$this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value );
?>
</optgroup>
<?php
/**
* Fire the action
* You can use this filter to add new options.
*
* @since 1.8.15
*/
do_action( 'give_import_dropdown_option', $index, $value, $mapto, $current_mapto );
?>
</select>
<?php
}
/**
* Print the option html for select in importer
*
* @since 1.8.15
* @access public
*
* @param array $options
* @param string $current_mapto
* @param bool $value
*
* @return void
*/
public function get_dropdown_option_html( $options, $current_mapto, $value = false ) {
foreach ( $options as $option => $option_value ) {
$option_value_texts = (array) $option_value;
$option_text = $option_value_texts[0];
$checked = ( ( $current_mapto === $option ) ? 'selected' : false );
if ( empty( $checked ) ) {
foreach ( $option_value_texts as $option_value_text ) {
$checked = $this->selected( $option_value_text, $value );
if ( $checked ) {
break;
}
}
}
echo sprintf(
'<option value="%1$s" %2$s >%3$s</option>',
$option,
$checked,
$option_text
);
}
}
/**
* Get column count of csv file.
*
* @since 1.8.14
*
* @param $file_id
*
* @return bool|int
*/
public function get_csv_total( $file_id ) {
$total = false;
if ( $file_id ) {
$file_dir = get_attached_file( $file_id );
if ( $file_dir ) {
$file = new SplFileObject( $file_dir, 'r' );
$file->seek( PHP_INT_MAX );
$total = $file->key() + 1;
}
}
return $total;
}
/**
* Get the CSV fields title from the CSV.
*
* @since 1.8.14
*
* @param (int) $file_id
* @param int $index
* @param string $delimiter
*
* @return array|bool $raw_data title of the CSV file fields
*/
public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) {
/**
* Filter to modify delimiter of Import.
*
* @since 1.8.14
*
* Return string $delimiter.
*/
$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
$raw_data = false;
$file_dir = get_attached_file( $file_id );
if ( $file_dir ) {
if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
$raw_data = fgetcsv( $handle, $index, $delimiter );
// Remove BOM signature from the first item.
if ( isset( $raw_data[0] ) ) {
$raw_data[0] = $this->remove_utf8_bom( $raw_data[0] );
}
}
}
return $raw_data;
}
/**
* Remove UTF-8 BOM signature.
*
* @since 1.8.14
*
* @param string $string String to handle.
*
* @return string
*/
public function remove_utf8_bom( $string ) {
if ( 'efbbbf' === substr( bin2hex( $string ), 0, 6 ) ) {
$string = substr( $string, 3 );
}
return $string;
}
/**
* Is used to show the process when user upload the donor form.
*
* @since 1.8.14
*/
public function render_progress() {
$step = $this->get_step();
?>
<ol class="give-progress-steps">
<li class="<?php echo( 1 === $step ? 'active' : '' ); ?>">
<?php _e( 'Upload CSV file', 'give' ); ?>
</li>
<li class="<?php echo( 2 === $step ? 'active' : '' ); ?>">
<?php _e( 'Column mapping', 'give' ); ?>
</li>
<li class="<?php echo( 3 === $step ? 'active' : '' ); ?>">
<?php _e( 'Import', 'give' ); ?>
</li>
<li class="<?php echo( 4 === $step ? 'active' : '' ); ?>">
<?php _e( 'Done!', 'give' ); ?>
</li>
</ol>
<?php
}
/**
* Will return the import step.
*
* @since 1.8.14
*
* @return int $step on which step doest the import is on.
*/
public function get_step() {
$step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 );
$on_step = 1;
if ( empty( $step ) || 1 === $step ) {
$on_step = 1;
} elseif ( $this->check_for_dropdown_or_import() ) {
$on_step = 3;
} elseif ( 2 === $step ) {
$on_step = 2;
} elseif ( 4 === $step ) {
$on_step = 4;
}
return $on_step;
}
/**
* Render donations import page
*
* @since 1.8.14
*/
public function render_page() {
include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-donations.php';
}
/**
* Print Dry Run HTML on donation import page
*
* @since 2.1
*/
public function give_import_donation_submit_button_render_media_csv() {
$dry_run = isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 1;
?>
<div>
<label for="dry_run">
<input type="hidden" name="dry_run" value="0"/>
<input type="checkbox" name="dry_run" id="dry_run" class="dry_run"
value="1" <?php checked( 1, $dry_run ); ?> >
<strong><?php _e( 'Dry Run', 'give' ); ?></strong>
</label>
<p class="give-field-description">
<?php
_e( 'Preview what the import would look like without making any defalut changes to your site or your database.', 'give' );
?>
</p>
</div>
<?php
}
/**
* Change submit button text on first step of importing donation.
*
* @since 2.1
*
* @param $text
*
* @return string
*/
function give_import_donation_submit_text_render_media_csv( $text ) {
return __( 'Being Import', 'give' );
}
/**
* Add CSV upload HTMl
*
* Print the html of the file upload from which CSV will be uploaded.
*
* @since 1.8.14
* @return void
*/
public function render_media_csv() {
add_filter( 'give_import_donation_submit_button_text', array(
$this,
'give_import_donation_submit_text_render_media_csv'
) );
add_action( 'give_import_donation_submit_button', array(
$this,
'give_import_donation_submit_button_render_media_csv'
) );
?>
<tr valign="top">
<th colspan="2">
<h2 id="give-import-title"><?php _e( 'Import donations from a CSV file', 'give' ) ?></h2>
<p class="give-field-description"><?php _e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ) ?></p>
</th>
</tr>
<?php
$csv = ( isset( $_POST['csv'] ) ? give_clean( $_POST['csv'] ) : '' );
$csv_id = ( isset( $_POST['csv_id'] ) ? give_clean( $_POST['csv_id'] ) : '' );
$delimiter = ( isset( $_POST['delimiter'] ) ? give_clean( $_POST['delimiter'] ) : 'csv' );
$mode = empty( $_POST['mode'] ) ?
'disabled' :
( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? 'enabled' : 'disabled' );
$create_user = empty( $_POST['create_user'] ) ?
'enabled' :
( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? 'enabled' : 'disabled' );
$delete_csv = empty( $_POST['delete_csv'] ) ?
'enabled' :
( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? 'enabled' : 'disabled' );
// Reset csv and csv_id if csv
if ( empty( $csv_id ) || ! $this->is_valid_csv( $csv_id, $csv ) ) {
$csv_id = $csv = '';
}
$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page;
$sample_file_text = sprintf( 'Download the sample file <a href="%s">here</a>.', esc_url( GIVE_PLUGIN_URL . 'sample-data/sample-data.csv' ) );
$csv_description = sprintf(
'%1$s %2$s',
__( 'The file must be a Comma Seperated Version (CSV) file type only.', 'give' ),
$sample_file_text
);
$settings = array(
array(
'id' => 'csv',
'name' => __( 'Choose a CSV file:', 'give' ),
'type' => 'file',
'attributes' => array( 'editing' => 'false', 'library' => 'text' ),
'description' => $csv_description,
'fvalue' => 'url',
'default' => $csv,
),
array(
'id' => 'csv_id',
'type' => 'hidden',
'value' => $csv_id,
),
array(
'id' => 'delimiter',
'name' => __( 'CSV Delimiter:', 'give' ),
'description' => __( 'In case your CSV file supports a different type of separator (or delimiter) -- like a tab or space -- you can set that here.', 'give' ),
'default' => $delimiter,
'type' => 'select',
'options' => array(
'csv' => __( 'Comma', 'give' ),
'tab-separated-values' => __( 'Tab', 'give' ),
),
),
array(
'id' => 'mode',
'name' => __( 'Test Mode:', 'give' ),
'description' => __( 'Test mode allows you to preview what this import would look like without making any actual changes to your site or your database.', 'give' ),
'default' => $mode,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'create_user',
'name' => __( 'Create WP users for new donors:', 'give' ),
'description' => __( 'The importer can create WordPress user accounts based on the names and email addresses of the donations in your CSV file. Enable this option if you\'d like the importer to do that.', 'give' ),
'default' => $create_user,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'delete_csv',
'name' => __( 'Delete CSV after import:', 'give' ),
'description' => __( 'Your CSV file will be uploaded via the WordPress Media Library. It\'s a good idea to delete it after the import is finished so that your sensitive data is not accessible on the web. Disable this only if you plan to delete the file manually later.', 'give' ),
'default' => $delete_csv,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'per_page',
'name' => __( 'Process Rows Per Batch:', 'give' ),
'type' => 'number',
'description' => __( 'Determine how many rows you would like to import per cycle.', 'give' ),
'default' => $per_page,
'class' => 'give-text-small',
),
);
$settings = apply_filters( 'give_import_file_upload_html', $settings );
if ( empty( $this->is_csv_valid ) ) {
Give_Admin_Settings::output_fields( $settings, 'give_settings' );
} else {
?>
<input type="hidden" name="is_csv_valid" class="is_csv_valid"
value="<?php echo $this->is_csv_valid; ?>">
<?php
}
}
/**
* Run when user click on the submit button.
*
* @since 1.8.14
*/
public function save() {
// Get the current step.
$step = $this->get_step();
// Validation for first step.
if ( 1 === $step ) {
$csv_id = absint( $_POST['csv_id'] );
if ( $this->is_valid_csv( $csv_id, esc_url( $_POST['csv'] ) ) ) {
$url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array(
'step' => '2',
'importer-type' => $this->importer_type,
'csv' => $csv_id,
'delimiter' => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv',
'mode' => empty( $_POST['mode'] ) ?
'0' :
( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ),
'create_user' => empty( $_POST['create_user'] ) ?
'0' :
( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ),
'delete_csv' => empty( $_POST['delete_csv'] ) ?
'1' :
( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ),
'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page,
'dry_run' => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 1,
) ) );
$this->is_csv_valid = $url;
}
}
}
/**
* Check if user uploaded csv is valid or not.
*
* @since 1.8.14
* @access public