PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
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 2.30.0 2.31.0 All 254 releases
give / includes / admin / import-functions.php

import-functions.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at includes/admin/import-functions.php

1,138 lines 35.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Import Functions
4 *
5 * @package Give
6 * @subpackage Functions
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.8.14
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Get the Import report of the donations
19 *
20 * @since 1.8.13
21 */
22 function give_import_donation_report() {
23 return get_option( 'give_import_donation_report', [] );
24 }
25
26
27 /**
28 * Update the Import report of the donations
29 *
30 * @since 1.8.13
31 */
32 function give_import_donation_report_update( $value = [] ) {
33 update_option( 'give_import_donation_report', $value, false );
34 }
35
36 /**
37 * Delete the Import report of the donations
38 *
39 * @since 1.8.13
40 */
41 function give_import_donation_report_reset() {
42 update_option( 'give_import_donation_report', [], false );
43 }
44
45 /**
46 * Give get form data from csv if not then create and form and return the form value.
47 *
48 * @since 1.8.13.
49 * @since 2.26.0 Replace deprecated get_page_by_title() with give_get_page_by_title().
50 *
51 * @param $data .
52 *
53 * @return array|bool|Give_Donate_Form|int|null|WP_Post
54 */
55 function give_import_get_form_data_from_csv( $data, $import_setting = [] ) {
56 $new_form = false;
57 $dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
58
59 // Get the import report
60 $report = give_import_donation_report();
61
62 $form = false;
63 $meta = [];
64
65 if ( ! empty( $data['form_id'] ) ) {
66 $form = new Give_Donate_Form( $data['form_id'] );
67 // Add support to older php version.
68 $form_id = $form->get_ID();
69 if ( empty( $form_id ) ) {
70 $form = false;
71 } else {
72 $report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
73 }
74 }
75
76 if ( false === $form && ! empty( $data['form_title'] ) ) {
77 $form = give_get_page_by_title($data['form_title'], OBJECT, 'give_forms');
78
79 if ( ! empty( $form->ID ) ) {
80
81 $report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
82
83 $form = new Give_Donate_Form( $form->ID );
84 } else {
85 $form = new Give_Donate_Form();
86 $args = [
87 'post_title' => $data['form_title'],
88 'post_status' => 'publish',
89 ];
90
91 if ( empty( $dry_run ) ) {
92 $form = $form->create( $args );
93 }
94
95 $report['create_form'] = ( ! empty( $report['create_form'] ) ? ( absint( $report['create_form'] ) + 1 ) : 1 );
96 $new_form = true;
97
98 }
99
100 $form = give_get_page_by_title($data['form_title'], OBJECT, 'give_forms');
101 if ( ! empty( $form->ID ) ) {
102 $form = new Give_Donate_Form( $form->ID );
103 }
104 }
105
106 if ( ! empty( $form ) && $form->get_ID() && ! empty( $data['form_level'] ) && empty( $dry_run ) ) {
107
108 $price_option = 'set';
109 $form_level = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) );
110
111 if ( 'custom' !== $form_level ) {
112 $prices = (array) $form->get_prices();
113 $price_text = [];
114 foreach ( $prices as $key => $price ) {
115 if ( isset( $price['_give_id']['level_id'] ) ) {
116 $price_text[ $price['_give_id']['level_id'] ] = ( ! empty( $price['_give_text'] ) ? strtolower( preg_replace( '/\s+/', '', $price['_give_text'] ) ) : '' );
117 }
118 }
119
120 if ( ! in_array( $form_level, $price_text ) ) {
121
122 // For generating unquiet level id.
123 $count = 1;
124 $new_level = count( $prices ) + $count;
125 while ( array_key_exists( $new_level, $price_text ) ) {
126 $count ++;
127 $new_level = count( $prices ) + $count;
128 }
129
130 $multi_level_donations = [
131 [
132 '_give_id' => [
133 'level_id' => $new_level,
134 ],
135 '_give_amount' => give_sanitize_amount_for_db( $data['amount'] ),
136 '_give_text' => $data['form_level'],
137 ],
138 ];
139
140 $price_text[ $new_level ] = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) );
141
142 if ( ! empty( $prices ) && is_array( $prices ) && ! empty( $prices[0] ) ) {
143 // Sort $prices by amount in ascending order.
144 $prices = wp_list_sort( $prices, '_give_amount', 'ASC' );
145 } else {
146 $prices = $multi_level_donations;
147 }
148
149 // Unset _give_default key from $prices.
150 foreach ( $prices as $key => $price ) {
151 if ( isset( $prices[ $key ]['_give_default'] ) ) {
152 unset( $prices[ $key ]['_give_default'] );
153 }
154 }
155
156 // Set the first $price of the $prices as default.
157 $prices[0]['_give_default'] = 'default';
158 }
159 $form->price_id = array_search( $form_level, $price_text );
160
161 $donation_levels_amounts = wp_list_pluck( $prices, '_give_amount' );
162 $min_amount = min( $donation_levels_amounts );
163 $max_amount = max( $donation_levels_amounts );
164
165 $meta = [
166 '_give_levels_minimum_amount' => $min_amount,
167 '_give_levels_maximum_amount' => $max_amount,
168 '_give_donation_levels' => array_values( $prices ),
169 ];
170
171 $price_option = 'multi';
172 } else {
173 $form->price_id = 'custom';
174 }
175
176 $defaults = [
177 '_give_set_price' => give_sanitize_amount_for_db( $data['amount'] ),
178 ];
179
180 // If new form is created.
181 if ( ! empty( $new_form ) ) {
182 $new_form = [
183 '_give_custom_amount_text' => ( ! empty( $data['form_custom_amount_text'] ) ? $data['form_custom_amount_text'] : __( 'Custom Amount', 'give' ) ),
184 '_give_logged_in_only' => 'enabled',
185 '_give_custom_amount' => 'enabled',
186 '_give_payment_import' => true,
187 '_give_display_style' => 'radios',
188 '_give_payment_display' => 'onpage',
189 'give_product_notes' => 'Donation Notes',
190 '_give_product_type' => 'default',
191 '_give_default_gateway' => 'global',
192 '_give_customize_offline_donations' => 'global',
193 '_give_show_register_form' => 'both',
194 '_give_price_option' => $price_option,
195 ];
196 $defaults = wp_parse_args( $defaults, $new_form );
197 }
198
199 $meta = wp_parse_args( $meta, $defaults );
200
201 foreach ( $meta as $key => $value ) {
202 give_update_meta( $form->get_ID(), $key, $value );
203 }
204 }
205
206 // update the report
207 give_import_donation_report_update( $report );
208
209 return $form;
210 }
211
212 /**
213 * Give get user details if not then create a user. Used in Import Donation CSV.
214 *
215 * @since 1.8.13
216 *
217 * @param $data
218 *
219 * @return bool|false|WP_User
220 */
221 function give_import_get_user_from_csv( $data, $import_setting = [] ) {
222 $report = give_import_donation_report();
223 $dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
224 $dry_run_donor_create = false;
225 $donor_data = [];
226 $donor_id = false;
227
228 // check if donor id is not empty
229 if ( ! empty( $data['donor_id'] ) ) {
230 $donor_data = new Give_Donor( (int) $data['donor_id'] );
231 if ( ! empty( $donor_data->id ) ) {
232 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
233 }
234 }
235
236 if ( empty( $donor_data->id ) && ! empty( $data['user_id'] ) ) {
237 $user_id = (int) $data['user_id'];
238 $donor_data = new Give_Donor( $user_id, true );
239
240 if ( empty( $donor_data->id ) ) {
241 $donor_data = get_user_by( 'id', $user_id );
242
243 // if no wp user is found then no donor is create with that user id
244 if ( ! empty( $donor_data->ID ) ) {
245
246 if ( empty( $dry_run ) ) {
247 $first_name = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $donor_data->user_nicename );
248 $last_name = ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ( $lastname = get_user_meta( $donor_data->ID, 'last_name', true ) ) ? $lastname : '' ) );
249 $name = $first_name . ' ' . $last_name;
250 $user_email = $donor_data->user_email;
251 $donor_args = [
252 'name' => $name,
253 'email' => $user_email,
254 'user_id' => $user_id,
255 ];
256
257 $donor_data = new Give_Donor();
258 $donor_data->create( $donor_args );
259
260 // Adding notes that donor is being imported from CSV.
261 $current_user = wp_get_current_user();
262 $donor_data->add_note( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) );
263
264 // Add is used to ensure duplicate emails are not added
265 if ( $user_email != $data['email'] && ! empty( $data['email'] ) ) {
266 $donor_data->add_meta( 'additional_email', $data['email'] );
267 }
268 } else {
269 $dry_run_donor_create = true;
270 $donor_data = [ 'id' => 1 ];
271 }
272
273 $report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
274 } elseif ( $dry_run ) {
275 $donor_data = [];
276 }
277 } else {
278 // Add is used to ensure duplicate emails are not added
279 if ( $donor_data->email != $data['email'] && empty( $dry_run ) ) {
280 $donor_data->add_meta( 'additional_email', ( ! empty( $data['email'] ) ? $data['email'] : $donor_data->email ) );
281 }
282 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
283 }
284 }
285
286 if ( empty( $donor_data->id ) && ! empty( $data['email'] ) && empty( $dry_run_donor_create ) ) {
287
288 $donor_data = new Give_Donor( $data['email'] );
289 if ( empty( $donor_data->id ) ) {
290 $donor_data = get_user_by( 'email', $data['email'] );
291
292 if ( empty( $donor_data->ID ) && isset( $import_setting['create_user'] ) && 1 === absint( $import_setting['create_user'] ) ) {
293 $data['first_name'] = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $data['email'] );
294 $data['last_name'] = ( ! empty( $data['last_name'] ) ? $data['last_name'] : '' );
295 $give_role = (array) give_get_option( 'donor_default_user_role', get_option( 'default_role', ( ( $give_donor = wp_roles()->is_role( 'give_donor' ) ) && ! empty( $give_donor ) ? 'give_donor' : 'subscriber' ) ) );
296 $donor_args = [
297 'user_login' => $data['email'],
298 'user_email' => $data['email'],
299 'user_registered' => date( 'Y-m-d H:i:s' ),
300 'user_first' => $data['first_name'],
301 'user_last' => $data['last_name'],
302 'user_pass' => wp_generate_password( 8, true ),
303 'role' => $give_role,
304 ];
305
306 /**
307 * Filter to modify user data before new user id register.
308 *
309 * @since 1.8.13
310 */
311 $donor_args = (array) apply_filters( 'give_import_insert_user_args', $donor_args, $data, $import_setting );
312
313 if ( empty( $dry_run ) ) {
314
315 // This action was added to remove the login when using the give register function.
316 add_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 );
317 $donor_id = give_register_and_login_new_user( $donor_args );
318 remove_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 );
319
320 $donor_data = new Give_Donor( $donor_id, true );
321 $donor_data->update_meta( '_give_payment_import', true );
322
323 } else {
324 $dry_run_donor_create = true;
325 $report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
326 }
327 } else {
328 $donor_id = ( ! empty( $donor_data->ID ) ? $donor_data->ID : false );
329 }
330
331 if ( empty( $dry_run_donor_create ) && ( ! empty( $donor_id ) || ( isset( $import_setting['create_user'] ) && 0 === absint( $import_setting['create_user'] ) ) ) ) {
332 $donor_data = new Give_Donor( $donor_id, true );
333
334 if ( empty( $donor_data->id ) ) {
335
336 if ( ! empty( $data['form_id'] ) ) {
337 $form = new Give_Donate_Form( $data['form_id'] );
338 }
339
340 if ( empty( $dry_run ) ) {
341 $payment_title = ( isset( $data['form_title'] ) ? $data['form_title'] : ( isset( $form ) ? $form->get_name() : __( 'New Form', 'give' ) ) );
342 $donor_args = [
343 'name' => ! is_email( $payment_title ) ? $data['first_name'] . ' ' . $data['last_name'] : '',
344 'email' => $data['email'],
345 ];
346 if ( ! empty( $donor_id ) ) {
347 $donor_args['user_id'] = $donor_id;
348 }
349 $donor_data->create( $donor_args );
350
351 // Adding notes that donor is being imported from CSV.
352 $current_user = wp_get_current_user();
353 $donor_data->add_note( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) );
354 } else {
355 $dry_run_donor_create = true;
356 }
357 $report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
358 } else {
359 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
360 }
361 }
362 } else {
363 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
364 }
365 }
366
367 // update the report
368 give_import_donation_report_update( $report );
369
370 return $donor_data;
371 }
372
373 /**
374 * Return the option that are default options.
375 *
376 * @since 1.8.13
377 */
378 function give_import_default_options() {
379 /**
380 * Filter to modify default option in the import dropdown
381 *
382 * @since 1.8.13
383 *
384 * @return array
385 */
386 return (array) apply_filters(
387 'give_import_default_options',
388 [
389 '' => __( 'Do not import', 'give' ),
390 ]
391 );
392 }
393
394 /**
395 * Return the option that are related to donations.
396 *
397 * @since 1.8.13
398 */
399 function give_import_donations_options() {
400 /**
401 * Filter to modify donations option in the import dropdown
402 *
403 * @since 4.5.0 Add gateway transaction id option
404 * @since 1.8.13
405 *
406 * @return array
407 */
408 return (array) apply_filters(
409 'give_import_donations_options',
410 [
411 'id' => __( 'Donation ID', 'give' ),
412 'amount' => [
413 __( 'Donation Amount', 'give' ),
414 __( 'Amount', 'give' ),
415 __( 'Donation Total', 'give' ),
416 __( 'Total', 'give' ),
417 ],
418 'currency' => [
419 __( 'Donation Currencies', 'give' ),
420 __( 'Currencies', 'give' ),
421 __( 'Currencies Code', 'give' ),
422 __( 'Currency Code', 'give' ),
423 __( 'Code', 'give' ),
424 ],
425 'post_date' => [
426 __( 'Donation Date', 'give' ),
427 __( 'Date', 'give' ),
428 ],
429 'post_time' => [
430 __( 'Donation Time', 'give' ),
431 __( 'Time', 'give' ),
432 ],
433 'title_prefix' => [
434 __( 'Title Prefix', 'give' ),
435 __( 'Prefix', 'give' ),
436 ],
437 'first_name' => [
438 __( 'Donor First Name', 'give' ),
439 __( 'First Name', 'give' ),
440 __( 'Name', 'give' ),
441 __( 'First', 'give' ),
442 ],
443 'last_name' => [
444 __( 'Donor Last Name', 'give' ),
445 __( 'Last Name', 'give' ),
446 __( 'Last', 'give' ),
447 ],
448 'company_name' => [
449 __( 'Company Name', 'give' ),
450 __( 'Donor Company Name', 'give' ),
451 __( 'Donor Company', 'give' ),
452 __( 'Company', 'give' ),
453 ],
454 'line1' => [
455 __( 'Address 1', 'give' ),
456 __( 'Address', 'give' ),
457 ],
458 'line2' => __( 'Address 2', 'give' ),
459 'city' => __( 'City', 'give' ),
460 'state' => [
461 __( 'State', 'give' ),
462 __( 'Province', 'give' ),
463 __( 'County', 'give' ),
464 __( 'Region', 'give' ),
465 ],
466 'country' => __( 'Country', 'give' ),
467 'zip' => [
468 __( 'Zip Code', 'give' ),
469 __( 'Zip', 'give' ),
470 __( 'zipcode', 'give' ),
471 __( 'Postal Code', 'give' ),
472 __( 'Postal', 'give' ),
473 ],
474 'email' => [
475 __( 'Donor Email', 'give' ),
476 __( 'Email', 'give' ),
477 __( 'Email Address', 'give' ),
478 ],
479 'post_status' => [
480 __( 'Donation Status', 'give' ),
481 __( 'Status', 'give' ),
482 ],
483 'gateway' => [
484 __( 'Payment Method', 'give' ),
485 __( 'Method', 'give' ),
486 __( 'Payment Gateway', 'give' ),
487 __( 'Gateway', 'give' ),
488 ],
489 'gateway_transaction_id' => [
490 __( 'Transaction ID', 'give' ),
491 __( 'Gateway Transaction ID', 'give' ),
492 __( 'Transaction Identifier', 'give' ),
493 ],
494 'notes' => __( 'Notes', 'give' ),
495 'mode' => [
496 __( 'Payment Mode', 'give' ),
497 __( 'Mode', 'give' ),
498 __( 'Test Mode', 'give' ),
499 ],
500 'donor_ip' => __( 'Donor IP Address', 'give' ),
501 'post_meta' => __( 'Import as Meta', 'give' ),
502 ]
503 );
504 }
505
506 /**
507 * Return the option that are related to donations.
508 *
509 * @since 1.8.13
510 */
511 function give_import_donor_options() {
512 /**
513 * Filter to modify donors option in the import dropdown
514 *
515 * @since 1.8.13
516 *
517 * @return array
518 */
519 return (array) apply_filters(
520 'give_import_donor_options',
521 [
522 'donor_id' => __( 'Donor ID', 'give' ),
523 'user_id' => __( 'User ID', 'give' ),
524 ]
525 );
526 }
527
528 /**
529 * Return the option that are related to donations.
530 *
531 * @since 1.8.13
532 */
533 function give_import_donation_form_options() {
534 /**
535 * Filter to modify form option in the import dropdown
536 *
537 * @since 1.8.13
538 *
539 * @return array
540 */
541 return (array) apply_filters(
542 'give_import_donation_form_options',
543 [
544 'form_title' => [
545 __( 'Donation Form Title', 'give' ),
546 __( 'Donation Form', 'give' ),
547 __( 'Form Name', 'give' ),
548 __( 'Title', 'give' ),
549 __( 'Form Title', 'give' ),
550 'ignore' => [
551 __( 'Title Prefix', 'give' ),
552 __( 'Prefix', 'give' ),
553 ],
554 ],
555 'form_id' => [
556 __( 'Donation Form ID', 'give' ),
557 __( 'Form ID', 'give' ),
558 ],
559 'form_level' => [
560 __( 'Donation Level', 'give' ),
561 __( 'Level', 'give' ),
562 __( 'Level Title', 'give' ),
563 ],
564 'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ),
565 ]
566 );
567 }
568
569 /**
570 * Import CSV in DB
571 *
572 * @param int $file_id CSV id.
573 * @param int $start Start from which csv line.
574 * @param int $end End from which csv line.
575 * @param string $delimiter CSV delimeter.
576 *
577 * @return array
578 */
579 function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = 'csv' ) {
580 /**
581 * Filter to modify delimiter of Import
582 *
583 * @since 1.8.14
584 *
585 * @param string $delimiter
586 *
587 * @return string $delimiter
588 */
589 $delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
590
591 $file_dir = give_get_file_data_by_file_id( $file_id );
592
593 return give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter );
594 }
595
596 /**
597 * Get raw data from file data
598 *
599 * @since 2.1
600 *
601 * @param $file_dir
602 * @param $start
603 * @param $end
604 * @param $delimiter
605 *
606 * @return array
607 */
608 function give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter ) {
609 $raw_data = [];
610
611 $count = 0;
612 if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
613 while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) {
614 if ( $count >= $start && $count <= $end ) {
615 $raw_data[] = $row;
616 }
617 $count ++;
618 }
619 fclose( $handle );
620 }
621
622 return $raw_data;
623 }
624
625 /**
626 * Get content from the attachment id of CSV
627 *
628 * @since 2.1
629 *
630 * @param $file_id
631 *
632 * @return false|string file content
633 */
634 function give_get_file_data_by_file_id( $file_id ) {
635 return get_attached_file( $file_id );
636 }
637
638
639 /**
640 * Remove login when user register with give functions.
641 *
642 * @since 1.8.13
643 *
644 * @param $value
645 *
646 * @return bool
647 */
648 function give_log_user_in_on_register_callback( $value ) {
649 return false;
650 }
651
652 /**
653 * Add import Donation forms, donations , donor from CSV to database
654 *
655 * @since 1.8.13
656 *
657 * @param array $raw_key Setup bu user at step 2.
658 * @param array $row_data Feilds that are being imported from CSV
659 * @param array $main_key First row from the CSV
660 * @param array $import_setting Contain the global variable.
661 *
662 * @return bool
663 */
664 function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = [], $import_setting = [] ) {
665 $data = array_combine( $raw_key, $row_data );
666 $price_id = false;
667 $donor_id = 0;
668 $donor_data = [];
669 $form = [];
670 $import_setting['create_user'] = isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1;
671 $dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
672 $_dry_run_is_duplicate = false;
673 $dry_run_duplicate_form = false;
674 $dry_run_duplicate_donor = false;
675 $donation_key = empty( $import_setting['donation_key'] ) ? 1 : (int) $import_setting['donation_key'];
676 $payment_id = false;
677
678 $data = (array) apply_filters( 'give_save_import_donation_to_db', $data );
679
680 $data['amount'] = give_maybe_sanitize_amount( $data['amount'] );
681 $diff = [];
682
683 if ( ! empty( $dry_run ) && 1 !== $donation_key ) {
684 $csv_raw_data = empty( $import_setting['csv_raw_data'] ) ? [] : $import_setting['csv_raw_data'];
685 $donors_list = empty( $import_setting['donors_list'] ) ? [] : $import_setting['donors_list'];
686 $key = $donation_key - 1;
687 for ( $i = 0; $i < $key; $i ++ ) {
688 $csv_data = array_combine( $raw_key, $csv_raw_data[ $i ] );
689 $csv_data['amount'] = give_maybe_sanitize_amount( $csv_data['amount'] );
690 // check for duplicate donations
691 $diff = array_diff( $csv_data, $data );
692 if ( empty( $diff ) ) {
693 $_dry_run_is_duplicate = true;
694 $dry_run_duplicate_form = true;
695 $dry_run_duplicate_donor = true;
696 } else {
697 // check for duplicate donation form with form id
698 if ( ! empty( $csv_data['form_id'] ) && ! empty( $data['form_id'] ) && $csv_data['form_id'] === $data['form_id'] ) {
699 $form = new Give_Donate_Form( $data['form_id'] );
700 $form_id = $form->get_ID();
701 if ( ! empty( $form_id ) ) {
702 $dry_run_duplicate_form = true;
703 }
704 }
705 // check for duplicate donation form with form title
706 if ( empty( $dry_run_duplicate_form ) && ! empty( $csv_data['form_title'] ) && ! empty( $data['form_title'] ) && $csv_data['form_title'] === $data['form_title'] ) {
707 $dry_run_duplicate_form = true;
708 }
709
710 // check for duplicate donor by donor id
711 if ( ! empty( $csv_data['donor_id'] ) && ! empty( $data['donor_id'] ) && $csv_data['donor_id'] === $data['donor_id'] ) {
712 $donor = array_search( (int) $data['donor_id'], array_column( $donors_list, 'id' ) );
713 if ( ! empty( $donor ) ) {
714 $dry_run_duplicate_donor = true;
715 }
716 }
717
718 // check for duplicate donor by user id
719 if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['user_id'] ) && ! empty( $data['user_id'] ) && $csv_data['user_id'] === $data['user_id'] ) {
720 $donor = array_search( (int) $data['user_id'], array_column( $donors_list, 'user_id' ) );
721 if ( ! empty( $donor ) ) {
722 $dry_run_duplicate_donor = true;
723 } else {
724 $donor = get_user_by( 'id', $csv_data['user_id'] );
725 if ( ! empty( $donor->ID ) ) {
726 $dry_run_duplicate_donor = true;
727 }
728 }
729 }
730
731 // check for duplicate donor by donor id
732 if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['email'] ) && ! empty( $data['email'] ) && $csv_data['email'] === $data['email'] ) {
733 $dry_run_duplicate_donor = true;
734 }
735 }
736 }
737 }
738
739 if ( empty( $dry_run_duplicate_donor ) ) {
740 // Here come the login function.
741 $donor_data = give_import_get_user_from_csv( $data, $import_setting );
742 if ( empty( $dry_run ) ) {
743 if ( ! empty( $donor_data->id ) ) {
744 $donor_id = $donor_data->id;
745 } else {
746 return $payment_id;
747 }
748 }
749 } else {
750 // Get the report
751 $report = give_import_donation_report();
752 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
753 // update the report
754 give_import_donation_report_update( $report );
755 }
756
757 if ( empty( $dry_run_duplicate_form ) ) {
758 // get form data or register a form data.
759 $form = give_import_get_form_data_from_csv( $data, $import_setting );
760 if ( false == $form && empty( $dry_run ) ) {
761 return $payment_id;
762 } else {
763 $price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
764 }
765 } else {
766 // Get the report
767 $report = give_import_donation_report();
768 $report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
769 // update the report
770 give_import_donation_report_update( $report );
771 }
772
773 // Get the report
774 $report = give_import_donation_report();
775
776 $status = give_import_donation_get_status( $data );
777 $country = ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' );
778 $state = ( ! empty( $data['state'] ) ? ( ( $state_code = array_search( $data['state'], give_get_states( $country ) ) ) ? $state_code : $data['state'] ) : '' );
779
780 $address = [
781 'line1' => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ),
782 'line2' => ( ! empty( $data['line2'] ) ? give_clean( $data['line2'] ) : '' ),
783 'city' => ( ! empty( $data['city'] ) ? give_clean( $data['city'] ) : '' ),
784 'zip' => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ),
785 'state' => $state,
786 'country' => $country,
787 ];
788
789 $test_mode = [ 'test', 'true' ];
790 $post_date = current_time( 'mysql' );
791 if ( ! empty( $data['post_date'] ) ) {
792 if ( ! empty( $data['post_time'] ) ) {
793 $post_date = mysql2date( 'Y-m-d', $data['post_date'] );
794 $post_date = mysql2date( 'Y-m-d H:i:s', $post_date . ' ' . $data['post_time'] );
795 } else {
796 $post_date = mysql2date( 'Y-m-d H:i:s', $data['post_date'] );
797 }
798 }
799
800 // Create payment_data array
801 $payment_data = [
802 'donor_id' => $donor_id,
803 'price' => $data['amount'],
804 'status' => $status,
805 'currency' => ! empty( $data['currency'] ) && array_key_exists( $data['currency'], give_get_currencies_list() ) ? $data['currency'] : give_get_currency(),
806 'user_info' => [
807 'id' => $donor_id,
808 'email' => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ),
809 'first_name' => ( ! empty( $data['first_name'] ) ? $data['first_name'] : ( ! empty( $donor_id ) && ( $first_name = get_user_meta( $donor_id, 'first_name', true ) ) ? $first_name : $donor_data->name ) ),
810 'last_name' => ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ! empty( $donor_id ) && ( $last_name = get_user_meta( $donor_id, 'last_name', true ) ) ? $last_name : $donor_data->name ) ),
811 'address' => $address,
812 'title' => ! empty( $data['title_prefix'] ) ? $data['title_prefix'] : '',
813 ],
814 'gateway' => ( ! empty( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ),
815 'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : ( method_exists( $form, 'get_name' ) ? $form->get_name() : '' ) ),
816 'give_form_id' => ( ! empty( $form ) && method_exists( $form, 'get_ID' ) ) ? $form->get_ID() : '',
817 'give_price_id' => $price_id,
818 'purchase_key' => strtolower( md5( uniqid() ) ),
819 'user_email' => $data['email'],
820 'post_date' => $post_date,
821 'mode' => ( ! empty( $data['mode'] ) ? ( in_array( strtolower( $data['mode'] ), $test_mode ) ? 'test' : 'live' ) : ( isset( $import_setting['mode'] ) ? ( true == (bool) $import_setting['mode'] ? 'test' : 'live' ) : ( give_is_test_mode() ? 'test' : 'live' ) ) ),
822 ];
823
824 /**
825 * Filter to modify payment Data before getting imported.
826 *
827 * @since 4.5.0 Add gateway transaction id to payment data
828 * @since 2.1.0
829 *
830 * @param array $payment_data payment data
831 * @param array $payment_data donation data
832 * @param array $donor_data donor data
833 * @param object $donor_data form object
834 *
835 * @return array $payment_data payment data
836 */
837 $payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form );
838
839 // Get the report
840 $report = give_import_donation_report();
841
842 // Check for duplicate code.
843 $donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data );
844 if ( false !== $donation_duplicate || ! empty( $_dry_run_is_duplicate ) ) {
845 $report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate;
846 $report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 );
847 } else {
848
849 if ( empty( $dry_run ) ) {
850 add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 );
851 add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 );
852 add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 );
853 add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 );
854 add_filter( 'give_is_stop_email_notification', '__return_true' );
855
856 // If status is other than pending then first change the donation status to pending and after adding the payment meta update the donation status.
857 if ( 'pending' !== $status ) {
858 unset( $payment_data['status'] );
859 }
860
861 $payment_id = give_insert_payment( $payment_data );
862 remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 );
863 remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 );
864 remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 );
865 remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 );
866 remove_filter( 'give_is_stop_email_notification', '__return_true' );
867
868 if ( $payment_id ) {
869
870 $payment = new Give_Payment( $payment_id );
871
872 $report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
873
874 $payment->update_meta( '_give_payment_import', true );
875
876 if ( ! empty( $import_setting['csv'] ) ) {
877 $payment->update_meta( '_give_payment_import_id', $import_setting['csv'] );
878 }
879
880 // Insert Company Name.
881 if ( ! empty( $data['company_name'] ) ) {
882 $payment->update_meta( '_give_donation_company', $data['company_name'] );
883 $donor_data->update_meta( '_give_donor_company', $data['company_name'] );
884 }
885
886 // Insert Donor IP address.
887 if ( ! empty( $data['donor_ip'] ) ) {
888 $payment->update_meta( '_give_payment_donor_ip', $data['donor_ip'] );
889 }
890
891 // Insert Transaction ID.
892 if ( ! empty( $data['gateway_transaction_id'] ) ) {
893 give_set_payment_transaction_id( $payment_id, $data['gateway_transaction_id'] );
894 }
895
896 // Insert Notes.
897 if ( ! empty( $data['notes'] ) ) {
898 $payment->add_note( $data['notes'] );
899 }
900
901 $meta_exists = array_keys( $raw_key, 'post_meta' );
902 if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) {
903 foreach ( $meta_exists as $meta_exist ) {
904 if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) {
905 $payment->update_meta( $main_key[ $meta_exist ], $row_data[ $meta_exist ] );
906 }
907 }
908 }
909
910 // update the donation status if it's other then pending
911 if ( 'pending' !== $status ) {
912 $payment->update_status( $status );
913 }
914 } else {
915 $report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 );
916 $payment_id = false;
917 }
918
919 /**
920 * Fire after payment is imported and payment meta is also being imported.
921 *
922 * @since 2.1.0
923 *
924 * @param int $payment_id payment id
925 * @param array $payment_data payment data
926 * @param array $payment_data donation data
927 * @param array $donor_data donor data
928 * @param object $donor_data form object
929 */
930 do_action( 'give_import_after_import_payment', $payment_id, $payment_data, $data, $donor_data, $form );
931 } else {
932 $report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
933 $payment_id = true;
934 }
935 }
936
937 // update the report
938 give_import_donation_report_update( $report );
939
940 return $payment_id;
941 }
942
943 /**
944 * Get Donation form status
945 *
946 * @since 2.0.2
947 *
948 * @param array $data donation data that is goingt o get imported
949 *
950 * @return string $status Donation status.
951 */
952 function give_import_donation_get_status( $data ) {
953 if ( empty( $data['post_status'] ) ) {
954 return 'publish';
955 }
956
957 $status = 'publish';
958
959 $donation_status = trim( $data['post_status'] );
960 $donation_status_key = strtolower( preg_replace( '/\s+/', '', $donation_status ) );
961
962 foreach ( give_get_payment_statuses() as $key => $value ) {
963 $match = false;
964 if ( $key === $donation_status_key ) {
965 $match = true;
966 } elseif ( stristr( $donation_status, $value ) ) {
967 $match = true;
968 }
969
970 if ( ! empty( $match ) ) {
971 $status = $key;
972 break;
973 }
974 }
975
976 return $status;
977 }
978
979 /**
980 * Alter donor information when importing donations from CSV
981 *
982 * @since 1.8.13
983 *
984 * @param $donor
985 * @param $payment_id
986 * @param $payment_data
987 *
988 * @return Give_Donor
989 */
990 function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) {
991 $old_donor = $donor;
992 if ( ! empty( $payment_data['donor_id'] ) ) {
993 $donor_id = absint( $payment_data['donor_id'] );
994 $donor = new Give_Donor( $donor_id );
995 if ( ! empty( $donor->id ) ) {
996 return $donor;
997 }
998 }
999
1000 return $old_donor;
1001 }
1002
1003 /*
1004 * Give update purchase_count of give customer.
1005 *
1006 * @since 1.8.13
1007 */
1008 function give_import_donation_insert_payment( $payment_id, $payment_data ) {
1009 // Update Give Customers purchase_count
1010 if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) {
1011 $donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true );
1012 if ( ! empty( $donor_id ) ) {
1013 $donor = new Give_Donor( $donor_id );
1014 $donor->increase_purchase_count();
1015 }
1016 }
1017 }
1018
1019 /**
1020 * Add author id in in donation post
1021 *
1022 * @since 1.8.13
1023 */
1024 function give_donation_import_give_insert_payment_args( $args, $payment_data ) {
1025 if ( ! empty( $payment_data['user_info']['id'] ) ) {
1026 $args['post_author'] = (int) $payment_data['user_info']['id'];
1027 }
1028
1029 return $args;
1030 }
1031
1032 /**
1033 * Check if Import donation is duplicate
1034 *
1035 * @since 1.8.13
1036 */
1037 function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) {
1038 $return = false;
1039 if ( ! empty( $data['post_date'] ) ) {
1040 $post_date = mysql2date( 'Y-m-d-H-i-s', $payment_data['post_date'] );
1041 $post_date = explode( '-', $post_date );
1042 $args = [
1043 'output' => 'post',
1044 'cache_results' => false,
1045 'no_found_rows' => true,
1046 'update_post_meta_cache' => false,
1047 'update_post_term_cache' => false,
1048 'fields' => 'ids',
1049 'date_query' => [
1050 [
1051 'year' => $post_date[0],
1052 'month' => $post_date[1],
1053 'day' => $post_date[2],
1054 'hour' => $post_date[3],
1055 'minute' => $post_date[4],
1056 'second' => $post_date[5],
1057 ],
1058 ],
1059 'meta_query' => [
1060 [
1061 'key' => '_give_payment_total',
1062 'value' => preg_replace( '/[\$,]/', '', $payment_data['price'] ),
1063 'compare' => 'LIKE',
1064 ],
1065 [
1066 'key' => '_give_payment_form_id',
1067 'value' => $payment_data['give_form_id'],
1068 'type' => 'numeric',
1069 'compare' => '=',
1070 ],
1071 [
1072 'key' => '_give_payment_gateway',
1073 'value' => $payment_data['gateway'],
1074 'compare' => '=',
1075 ],
1076 [
1077 'key' => '_give_payment_donor_id',
1078 'value' => isset( $donor_data->id ) ? $donor_data->id : '',
1079 'compare' => '=',
1080 ],
1081 [
1082 'key' => '_give_payment_mode',
1083 'value' => $payment_data['mode'],
1084 'compare' => '=',
1085 ],
1086 ],
1087 ];
1088
1089 $payments = new Give_Payments_Query( $args );
1090 $donations = $payments->get_payments();
1091 if ( ! empty( $donations ) ) {
1092 $return = $donations;
1093 }
1094 }
1095
1096 /**
1097 * Filter to modify donation which is getting add is duplicate or not.
1098 *
1099 * @since 1.8.18
1100 */
1101 return apply_filters( 'give_check_import_donation_duplicate', $return, $payment_data, $data, $form, $donor_data );
1102 }
1103
1104 /**
1105 * Record payment notes that is being imported from CSV.
1106 *
1107 * @since 1.8.13
1108 *
1109 * @param int $payment_id The ID number of the payment.
1110 *
1111 * @return void
1112 */
1113 function give_donation_import_insert_default_payment_note( $payment_id ) {
1114 $current_user = wp_get_current_user();
1115 give_insert_payment_note( $payment_id, wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) );
1116 }
1117
1118 /**
1119 * Return Import Page URL
1120 *
1121 * @since 1.8.13
1122 *
1123 * @param array $parameter
1124 *
1125 * @return string URL
1126 */
1127 function give_import_page_url( $parameter = [] ) {
1128 $defalut_query_arg = [
1129 'post_type' => 'give_forms',
1130 'page' => 'give-tools',
1131 'tab' => 'import',
1132 'importer-type' => 'import_donations',
1133 ];
1134 $import_query_arg = wp_parse_args( $parameter, $defalut_query_arg );
1135
1136 return esc_url_raw( add_query_arg( $import_query_arg, admin_url( 'edit.php' ) ) );
1137 }
1138