PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.0.0
GiveWP – Donation Plugin and Fundraising Platform v2.0.0
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 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / admin / tools / import / class-give-import-donations.php
give / includes / admin / tools / import Last commit date
class-give-import-core-settings.php 8 years ago class-give-import-donations.php 8 years ago
class-give-import-donations.php
908 lines
1 <?php
2 /**
3 * Donations Import Class
4 *
5 * This class handles donations import.
6 *
7 * @package Give
8 * @subpackage Classes/Give_Import_Donations
9 * @copyright Copyright (c) 2017, WordImpress
10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 * @since 1.8.14
12 */
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly
16 }
17
18 if ( ! class_exists( 'Give_Import_Donations' ) ) {
19
20 /**
21 * Give_Import_Donations.
22 *
23 * @since 1.8.14
24 */
25 final class Give_Import_Donations {
26
27 /**
28 * Importer type
29 *
30 * @since 1.8.13
31 * @var string
32 */
33 private $importer_type = 'import_donations';
34
35 /**
36 * Instance.
37 *
38 * @since
39 * @access private
40 * @var
41 */
42 static private $instance;
43
44 /**
45 * Importing donation per page.
46 *
47 * @since 1.8.14
48 *
49 * @var int
50 */
51 public static $per_page = 25;
52
53 /**
54 * Singleton pattern.
55 *
56 * @since
57 * @access private
58 */
59 private function __construct() {
60 self::$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : self::$per_page;
61 }
62
63 /**
64 * Get instance.
65 *
66 * @since
67 * @access public
68 *
69 * @return static
70 */
71 public static function get_instance() {
72 if ( null === static::$instance ) {
73 self::$instance = new static();
74 }
75
76 return self::$instance;
77 }
78
79 /**
80 * Setup
81 *
82 * @since 1.8.14
83 *
84 * @return void
85 */
86 public function setup() {
87 $this->setup_hooks();
88 }
89
90
91 /**
92 * Setup Hooks.
93 *
94 * @since 1.8.14
95 *
96 * @return void
97 */
98 private function setup_hooks() {
99 if ( ! $this->is_donations_import_page() ) {
100 return;
101 }
102
103 // Do not render main import tools page.
104 remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) );
105
106
107 // Render donation import page
108 add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) );
109
110 // Print the HTML.
111 add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 );
112
113 // Run when form submit.
114 add_action( 'give-tools_save_import', array( $this, 'save' ) );
115
116 add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 );
117
118 // Used to add submit button.
119 add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 );
120 }
121
122 /**
123 * Update notice
124 *
125 * @since 1.8.14
126 *
127 * @param $messages
128 *
129 * @return mixed
130 */
131 public function update_notices( $messages ) {
132 if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) {
133 unset( $messages['give-setting-updated'] );
134 }
135
136 return $messages;
137 }
138
139 /**
140 * Print submit and nonce button.
141 *
142 * @since 1.8.14
143 */
144 public function submit() {
145 wp_nonce_field( 'give-save-settings', '_give-save-settings' );
146 ?>
147 <input type="hidden" class="import-step" id="import-step" name="step" value="<?php echo $this->get_step(); ?>"/>
148 <input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/>
149 <?php
150 }
151
152 /**
153 * Print the HTML for importer.
154 *
155 * @since 1.8.14
156 */
157 public function html() {
158 $step = $this->get_step();
159
160 // Show progress.
161 $this->render_progress();
162 ?>
163 <section>
164 <table class="widefat export-options-table give-table <?php echo "step-{$step}"; ?>" id="<?php echo "step-{$step}"; ?>">
165 <tbody>
166 <?php
167 switch ( $this->get_step() ) {
168 case 1:
169 $this->render_media_csv();
170 break;
171
172 case 2:
173 $this->render_dropdown();
174 break;
175
176 case 3:
177 $this->start_import();
178 break;
179
180 case 4:
181 $this->import_success();
182 }
183
184 if ( false === $this->check_for_dropdown_or_import() ) {
185 ?>
186 <tr valign="top">
187 <th></th>
188 <th>
189 <input type="submit"
190 class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>"
191 id="recount-stats-submit"
192 value="<?php esc_attr_e( 'Submit', 'give' ); ?>"/>
193 </th>
194 </tr>
195 <?php
196 }
197 ?>
198 </tbody>
199 </table>
200 </section>
201 <?php
202 }
203
204 /**
205 * Show success notice
206 *
207 * @since 1.8.14
208 */
209 public function import_success() {
210
211 $delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false );
212 $csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false );
213 if ( ! empty( $delete_csv ) && ! empty( $csv ) ) {
214 wp_delete_attachment( $csv, true );
215 }
216
217 $report = give_import_donation_report();
218
219 $report_html = array(
220 'duplicate_donor' => array(
221 __( '%s duplicate %s detected', 'give' ),
222 __( 'donor', 'give' ),
223 __( 'donors', 'give' ),
224 ),
225 'create_donor' => array(
226 __( '%s %s created', 'give' ),
227 __( 'donor', 'give' ),
228 __( 'donors', 'give' ),
229 ),
230 'create_form' => array(
231 __( '%s donation %s created', 'give' ),
232 __( 'form', 'give' ),
233 __( 'forms', 'give' ),
234 ),
235 'duplicate_donation' => array(
236 __( '%s duplicate %s detected', 'give' ),
237 __( 'donation', 'give' ),
238 __( 'donations', 'give' ),
239 ),
240 'create_donation' => array(
241 __( '%s %s imported', 'give' ),
242 __( 'donation', 'give' ),
243 __( 'donations', 'give' ),
244 ),
245 );
246 $total = (int) $_GET['total'];
247 -- $total;
248 $success = (bool) $_GET['success'];
249 ?>
250 <tr valign="top" class="give-import-dropdown">
251 <th colspan="2">
252 <h2>
253 <?php
254 if ( $success ) {
255 echo sprintf(
256 __( 'Import complete! %s donations processed', 'give' ),
257 "<strong>{$total}</strong>"
258 );
259 } else {
260 echo sprintf(
261 __( 'Failed to import %s donations', 'give' ),
262 "<strong>{$total}</strong>"
263 );
264 }
265 ?>
266 </h2>
267
268 <?php
269 $text = __( 'Import Donation', 'give' );
270 $query_arg = array(
271 'post_type' => 'give_forms',
272 'page' => 'give-tools',
273 'tab' => 'import',
274 );
275 if ( $success ) {
276 $query_arg = array(
277 'post_type' => 'give_forms',
278 'page' => 'give-payment-history',
279 );
280 $text = __( 'View Donations', 'give' );
281 }
282
283 foreach ( $report as $key => $value ) {
284 if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) {
285 ?>
286 <p>
287 <?php echo esc_html( wp_sprintf( $report_html[ $key ][0], $value, _n( $report_html[ $key ][1], $report_html[ $key ][2], $value, 'give' ) ) ); ?>
288 </p>
289 <?php
290 }
291 }
292 ?>
293
294 <p>
295 <a class="button button-large button-secondary" href="<?php echo add_query_arg( $query_arg, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a>
296 </p>
297 </th>
298 </tr>
299 <?php
300 }
301
302 /**
303 * Will start Import
304 *
305 * @since 1.8.14
306 */
307 public function start_import() {
308 // Reset the donation form report.
309 give_import_donation_report_reset();
310
311 $csv = (int) $_REQUEST['csv'];
312 $delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
313 $index_start = 1;
314 $index_end = 1;
315 $next = true;
316 $total = self::get_csv_total( $csv );
317 if ( self::$per_page < $total ) {
318 $total_ajax = ceil( $total / self::$per_page );
319 $index_end = self::$per_page;
320 } else {
321 $total_ajax = 1;
322 $index_end = $total;
323 $next = false;
324 }
325 $current_percentage = 100 / ( $total_ajax + 1 );
326
327 ?>
328 <tr valign="top" class="give-import-dropdown">
329 <th colspan="2">
330 <h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ) ?></h2>
331 <p class="give-field-description"><?php esc_html_e( 'Your donations are now being imported...', 'give' ) ?></p>
332 </th>
333 </tr>
334
335 <tr valign="top" class="give-import-dropdown">
336 <th colspan="2">
337 <span class="spinner is-active"></span>
338 <div class="give-progress"
339 data-current="1"
340 data-total_ajax="<?php echo $total_ajax; ?>"
341 data-start="<?php echo $index_start; ?>"
342 data-end="<?php echo $index_end; ?>"
343 data-next="<?php echo $next; ?>"
344 data-total="<?php echo $total; ?>"
345 data-per_page="<?php echo self::$per_page; ?>">
346
347 <div style="width: <?php echo $current_percentage; ?>%"></div>
348 </div>
349 <input type="hidden" value="3" name="step">
350 <input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto"
351 class="mapto">
352 <input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv">
353 <input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode">
354 <input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user"
355 class="create_user">
356 <input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv"
357 class="delete_csv">
358 <input type="hidden" value="<?php echo $delimiter; ?>" name="delimiter">
359 <input type="hidden" value='<?php echo maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ); ?>'
360 name="main_key"
361 class="main_key">
362 </th>
363 </tr>
364
365 <script type="text/javascript">
366 jQuery(document).ready(function () {
367 give_on_donation_import_start();
368 });
369 </script>
370 <?php
371 }
372
373 /**
374 * Will return true if importing can be started or not else false.
375 *
376 * @since 1.8.14
377 */
378 public function check_for_dropdown_or_import() {
379 $return = true;
380 if ( isset( $_REQUEST['mapto'] ) ) {
381 $mapto = (array) $_REQUEST['mapto'];
382 if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) {
383 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' ) );
384 $return = false;
385 }
386
387 if ( false === in_array( 'amount', $mapto ) ) {
388 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' ) );
389 $return = false;
390 }
391
392 if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) {
393 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' ) );
394 $return = false;
395 }
396 } else {
397 $return = false;
398 }
399
400 return $return;
401 }
402
403 /**
404 * Print the Dropdown option for CSV.
405 *
406 * @since 1.8.14
407 */
408 public function render_dropdown() {
409 $csv = (int) $_GET['csv'];
410 $delimiter = ( ! empty( $_GET['delimiter'] ) ? give_clean( $_GET['delimiter'] ) : 'csv' );
411
412 // TO check if the CSV files that is being add is valid or not if not then redirect to first step again
413 if ( ! $this->is_valid_csv( $csv ) ) {
414 $url = give_import_page_url();
415 ?>
416 <script type="text/javascript">
417 window.location = "<?php echo $url; ?>"
418 </script>
419 <?php
420 } else {
421 ?>
422 <tr valign="top" class="give-import-dropdown">
423 <th colspan="2">
424 <h2 id="give-import-title"><?php esc_html_e( 'Map CSV fields to donations', 'give' ) ?></h2>
425 <p class="give-field-description"><?php esc_html_e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p>
426 </th>
427 </tr>
428
429 <tr valign="top" class="give-import-dropdown">
430 <th><b><?php esc_html_e( 'Column name', 'give' ); ?></b></th>
431 <th><b><?php esc_html_e( 'Map to field', 'give' ); ?></b></th>
432 </tr>
433
434 <?php
435 $raw_key = $this->get_importer( $csv, 0, $delimiter );
436 $mapto = (array) ( isset( $_REQUEST['mapto'] ) ? $_REQUEST['mapto'] : array() );
437
438 foreach ( $raw_key as $index => $value ) {
439 ?>
440 <tr valign="top" class="give-import-option">
441 <th><?php echo $value; ?></th>
442 <th>
443 <?php
444 $this->get_columns( $index, $value, $mapto );
445 ?>
446 </th>
447 </tr>
448 <?php
449 }
450 }
451 }
452
453 /**
454 * @param $option_value
455 * @param $value
456 *
457 * @return string
458 */
459 public function selected( $option_value, $value ) {
460 $option_value = strtolower( $option_value );
461 $value = strtolower( $value );
462
463 $selected = '';
464 if ( stristr( $value, $option_value ) ) {
465 $selected = 'selected';
466 } elseif ( strrpos( $value, '_' ) && stristr( $option_value, __( 'Import as Meta', 'give' ) ) ) {
467 $selected = 'selected';
468 }
469
470 return $selected;
471 }
472
473 /**
474 * Print the columns from the CSV.
475 *
476 * @since 1.8.14
477 * @access private
478 *
479 * @param string $index
480 * @param bool $value
481 * @param array $mapto
482 *
483 * @return void
484 */
485 private function get_columns( $index, $value = false, $mapto = array() ) {
486 $default = give_import_default_options();
487 $current_mapto = (string) ( ! empty( $mapto[ $index ] ) ? $mapto[ $index ] : '' );
488 ?>
489 <select name="mapto[<?php echo $index; ?>]">
490 <?php $this->get_dropdown_option_html( $default, $current_mapto, $value ); ?>
491
492 <optgroup label="<?php _e( 'Donations', 'give' ); ?>">
493 <?php
494 $this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value );
495 ?>
496 </optgroup>
497
498 <optgroup label="<?php _e( 'Donors', 'give' ); ?>">
499 <?php
500 $this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value );
501 ?>
502 </optgroup>
503
504 <optgroup label="<?php _e( 'Forms', 'give' ); ?>">
505 <?php
506 $this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value );
507 ?>
508 </optgroup>
509
510 <?php
511 /**
512 * Fire the action
513 * You can use this filter to add new options.
514 *
515 * @since 1.8.15
516 */
517 do_action( 'give_import_dropdown_option', $index, $value, $mapto, $current_mapto );
518 ?>
519 </select>
520 <?php
521 }
522
523 /**
524 * Print the option html for select in importer
525 *
526 * @since 1.8.15
527 * @access public
528 *
529 * @param array $options
530 * @param string $current_mapto
531 * @param bool $value
532 *
533 * @return void
534 */
535 public function get_dropdown_option_html( $options, $current_mapto, $value = false ) {
536 foreach ( $options as $option => $option_value ) {
537 $option_value_texts = (array) $option_value;
538 $option_text = $option_value_texts[0];
539
540 $checked = ( ( $current_mapto === $option ) ? 'selected' : false );
541 if ( empty( $checked ) ) {
542 foreach ( $option_value_texts as $option_value_text ) {
543 $checked = $this->selected( $option_value_text, $value );
544 if ( $checked ) {
545 break;
546 }
547 }
548 }
549
550 echo sprintf(
551 '<option value="%1$s" %2$s >%3$s</option>',
552 $option,
553 $checked,
554 $option_text
555 );
556 }
557 }
558
559 /**
560 * Get column count of csv file.
561 *
562 * @since 1.8.14
563 *
564 * @param $file_id
565 *
566 * @return bool|int
567 */
568 public function get_csv_total( $file_id ) {
569 $total = false;
570 if ( $file_id ) {
571 $file_dir = get_attached_file( $file_id );
572 if ( $file_dir ) {
573 $file = new SplFileObject( $file_dir, 'r' );
574 $file->seek( PHP_INT_MAX );
575 $total = $file->key() + 1;
576 }
577 }
578
579 return $total;
580 }
581
582 /**
583 * Get the CSV fields title from the CSV.
584 *
585 * @since 1.8.14
586 *
587 * @param (int) $file_id
588 * @param int $index
589 * @param string $delimiter
590 *
591 * @return array|bool $raw_data title of the CSV file fields
592 */
593 public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) {
594 /**
595 * Filter to modify delimiter of Import.
596 *
597 * @since 1.8.14
598 *
599 * Return string $delimiter.
600 */
601 $delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
602
603 $raw_data = false;
604 $file_dir = get_attached_file( $file_id );
605 if ( $file_dir ) {
606 if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
607 $raw_data = fgetcsv( $handle, $index, $delimiter );
608 // Remove BOM signature from the first item.
609 if ( isset( $raw_data[0] ) ) {
610 $raw_data[0] = $this->remove_utf8_bom( $raw_data[0] );
611 }
612 }
613 }
614
615 return $raw_data;
616 }
617
618 /**
619 * Remove UTF-8 BOM signature.
620 *
621 * @since 1.8.14
622 *
623 * @param string $string String to handle.
624 *
625 * @return string
626 */
627 public function remove_utf8_bom( $string ) {
628 if ( 'efbbbf' === substr( bin2hex( $string ), 0, 6 ) ) {
629 $string = substr( $string, 3 );
630 }
631
632 return $string;
633 }
634
635
636 /**
637 * Is used to show the process when user upload the donor form.
638 *
639 * @since 1.8.14
640 */
641 public function render_progress() {
642 $step = $this->get_step();
643 ?>
644 <ol class="give-progress-steps">
645 <li class="<?php echo( 1 === $step ? 'active' : '' ); ?>">
646 <?php esc_html_e( 'Upload CSV file', 'give' ); ?>
647 </li>
648 <li class="<?php echo( 2 === $step ? 'active' : '' ); ?>">
649 <?php esc_html_e( 'Column mapping', 'give' ); ?>
650 </li>
651 <li class="<?php echo( 3 === $step ? 'active' : '' ); ?>">
652 <?php esc_html_e( 'Import', 'give' ); ?>
653 </li>
654 <li class="<?php echo( 4 === $step ? 'active' : '' ); ?>">
655 <?php esc_html_e( 'Done!', 'give' ); ?>
656 </li>
657 </ol>
658 <?php
659 }
660
661 /**
662 * Will return the import step.
663 *
664 * @since 1.8.14
665 *
666 * @return int $step on which step doest the import is on.
667 */
668 public function get_step() {
669 $step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 );
670 $on_step = 1;
671
672 if ( empty( $step ) || 1 === $step ) {
673 $on_step = 1;
674 } elseif ( $this->check_for_dropdown_or_import() ) {
675 $on_step = 3;
676 } elseif ( 2 === $step ) {
677 $on_step = 2;
678 } elseif ( 4 === $step ) {
679 $on_step = 4;
680 }
681
682 return $on_step;
683 }
684
685 /**
686 * Render donations import page
687 *
688 * @since 1.8.14
689 */
690 public function render_page() {
691 include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-donations.php';
692 }
693
694 /**
695 * Add CSV upload HTMl
696 *
697 * Print the html of the file upload from which CSV will be uploaded.
698 *
699 * @since 1.8.14
700 * @return void
701 */
702 public function render_media_csv() {
703 ?>
704 <tr valign="top">
705 <th colspan="2">
706 <h2 id="give-import-title"><?php esc_html_e( 'Import donations from a CSV file', 'give' ) ?></h2>
707 <p class="give-field-description"><?php esc_html_e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ) ?></p>
708 </th>
709 </tr>
710 <?php
711 $csv = ( isset( $_POST['csv'] ) ? give_clean( $_POST['csv'] ) : '' );
712 $csv_id = ( isset( $_POST['csv_id'] ) ? give_clean( $_POST['csv_id'] ) : '' );
713 $delimiter = ( isset( $_POST['delimiter'] ) ? give_clean( $_POST['delimiter'] ) : 'csv' );
714 $mode = empty( $_POST['mode'] ) ?
715 'disabled' :
716 ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? 'enabled' : 'disabled' );
717 $create_user = empty( $_POST['create_user'] ) ?
718 'enabled' :
719 ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? 'enabled' : 'disabled' );
720 $delete_csv = empty( $_POST['delete_csv'] ) ?
721 'enabled' :
722 ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? 'enabled' : 'disabled' );
723
724 // Reset csv and csv_id if csv
725 if ( empty( $csv_id ) || ! $this->is_valid_csv( $csv_id, $csv ) ) {
726 $csv_id = $csv = '';
727 }
728 $per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page;
729
730 $settings = array(
731 array(
732 'id' => 'csv',
733 'name' => __( 'Choose a CSV file:', 'give' ),
734 'type' => 'file',
735 'attributes' => array( 'editing' => 'false', 'library' => 'text' ),
736 'description' => __( 'The file must be a Comma Seperated Version (CSV) file type only.', 'give' ),
737 'fvalue' => 'url',
738 'default' => $csv,
739 ),
740 array(
741 'id' => 'csv_id',
742 'type' => 'hidden',
743 'value' => $csv_id,
744 ),
745 array(
746 'id' => 'delimiter',
747 'name' => __( 'CSV Delimiter:', 'give' ),
748 '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' ),
749 'default' => $delimiter,
750 'type' => 'select',
751 'options' => array(
752 'csv' => esc_html__( 'Comma', 'give' ),
753 'tab-separated-values' => esc_html__( 'Tab', 'give' ),
754 ),
755 ),
756 array(
757 'id' => 'mode',
758 'name' => __( 'Test Mode:', 'give' ),
759 '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' ),
760 'default' => $mode,
761 'type' => 'radio_inline',
762 'options' => array(
763 'enabled' => __( 'Enabled', 'give' ),
764 'disabled' => __( 'Disabled', 'give' ),
765 ),
766 ),
767 array(
768 'id' => 'create_user',
769 'name' => __( 'Create WP users for new donors:', 'give' ),
770 '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' ),
771 'default' => $create_user,
772 'type' => 'radio_inline',
773 'options' => array(
774 'enabled' => __( 'Enabled', 'give' ),
775 'disabled' => __( 'Disabled', 'give' ),
776 ),
777 ),
778 array(
779 'id' => 'delete_csv',
780 'name' => __( 'Delete CSV after import:', 'give' ),
781 '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' ),
782 'default' => $delete_csv,
783 'type' => 'radio_inline',
784 'options' => array(
785 'enabled' => __( 'Enabled', 'give' ),
786 'disabled' => __( 'Disabled', 'give' ),
787 ),
788 ),
789 array(
790 'id' => 'per_page',
791 'name' => __( 'Process Rows Per Batch:', 'give' ),
792 'type' => 'number',
793 'description' => __( 'Determine how many rows you would like to import per cycle.', 'give' ),
794 'default' => $per_page,
795 'class' => 'give-text-small',
796 ),
797 );
798
799 $settings = apply_filters( 'give_import_file_upload_html', $settings );
800
801 Give_Admin_Settings::output_fields( $settings, 'give_settings' );
802 }
803
804 /**
805 * Run when user click on the submit button.
806 *
807 * @since 1.8.14
808 */
809 public function save() {
810 // Get the current step.
811 $step = $this->get_step();
812
813 // Validation for first step.
814 if ( 1 === $step ) {
815 $csv_id = absint( $_POST['csv_id'] );
816
817 if ( $this->is_valid_csv( $csv_id, esc_url( $_POST['csv'] ) ) ) {
818
819 $url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array(
820 'step' => '2',
821 'importer-type' => $this->importer_type,
822 'csv' => $csv_id,
823 'delimiter' => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv',
824 'mode' => empty( $_POST['mode'] ) ?
825 '0' :
826 ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ),
827 'create_user' => empty( $_POST['create_user'] ) ?
828 '0' :
829 ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ),
830 'delete_csv' => empty( $_POST['delete_csv'] ) ?
831 '1' :
832 ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ),
833 'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page,
834 ) ) );
835 ?>
836 <script type="text/javascript">
837 window.location = "<?php echo $url; ?>"
838 </script>
839 <?php
840 }
841 }
842 }
843
844 /**
845 * Check if user uploaded csv is valid or not.
846 *
847 * @since 1.8.14
848 * @access public
849 *
850 * @param int|bool $csv ID of the CSV files.
851 * @param string $match_url ID of the CSV files.
852 *
853 * @return bool $has_error CSV is valid or not.
854 */
855 private function is_valid_csv( $csv = false, $match_url = '' ) {
856 $is_valid_csv = true;
857
858 if ( $csv ) {
859 $csv_url = wp_get_attachment_url( $csv );
860
861 $delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
862
863 if (
864 ! $csv_url ||
865 ( ! empty( $match_url ) && ( $csv_url !== $match_url ) ) ||
866 ( ( $mime_type = get_post_mime_type( $csv ) ) && ! strpos( $mime_type, $delimiter ) )
867 ) {
868 $is_valid_csv = false;
869 Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
870 }
871 } else {
872 $is_valid_csv = false;
873 Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
874 }
875
876 return $is_valid_csv;
877 }
878
879
880 /**
881 * Render report import field
882 *
883 * @since 1.8.14
884 * @access public
885 *
886 * @param $field
887 * @param $option_value
888 */
889 public function render_import_field( $field, $option_value ) {
890 include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-imports.php';
891 }
892
893 /**
894 * Get if current page import donations page or not
895 *
896 * @since 1.8.14
897 * @return bool
898 */
899 private function is_donations_import_page() {
900 return 'import' === give_get_current_setting_tab() &&
901 isset( $_GET['importer-type'] ) &&
902 $this->importer_type === give_clean( $_GET['importer-type'] );
903 }
904 }
905
906 Give_Import_Donations::get_instance()->setup();
907 }
908