PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 3.15.0
GiveWP – Donation Plugin and Fundraising Platform v3.15.0
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 2.31.1 All 253 releases
give / includes / admin / import-functions.php
import-functions.php
1,126 lines 35.4 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 1.8.13
404 *
405 * @return array
406 */
407 return (array) apply_filters(
408 'give_import_donations_options',
409 [
410 'id' => __( 'Donation ID', 'give' ),
411 'amount' => [
412 __( 'Donation Amount', 'give' ),
413 __( 'Amount', 'give' ),
414 __( 'Donation Total', 'give' ),
415 __( 'Total', 'give' ),
416 ],
417 'currency' => [
418 __( 'Donation Currencies', 'give' ),
419 __( 'Currencies', 'give' ),
420 __( 'Currencies Code', 'give' ),
421 __( 'Currency Code', 'give' ),
422 __( 'Code', 'give' ),
423 ],
424 'post_date' => [
425 __( 'Donation Date', 'give' ),
426 __( 'Date', 'give' ),
427 ],
428 'post_time' => [
429 __( 'Donation Time', 'give' ),
430 __( 'Time', 'give' ),
431 ],
432 'title_prefix' => [
433 __( 'Title Prefix', 'give' ),
434 __( 'Prefix', 'give' ),
435 ],
436 'first_name' => [
437 __( 'Donor First Name', 'give' ),
438 __( 'First Name', 'give' ),
439 __( 'Name', 'give' ),
440 __( 'First', 'give' ),
441 ],
442 'last_name' => [
443 __( 'Donor Last Name', 'give' ),
444 __( 'Last Name', 'give' ),
445 __( 'Last', 'give' ),
446 ],
447 'company_name' => [
448 __( 'Company Name', 'give' ),
449 __( 'Donor Company Name', 'give' ),
450 __( 'Donor Company', 'give' ),
451 __( 'Company', 'give' ),
452 ],
453 'line1' => [
454 __( 'Address 1', 'give' ),
455 __( 'Address', 'give' ),
456 ],
457 'line2' => __( 'Address 2', 'give' ),
458 'city' => __( 'City', 'give' ),
459 'state' => [
460 __( 'State', 'give' ),
461 __( 'Province', 'give' ),
462 __( 'County', 'give' ),
463 __( 'Region', 'give' ),
464 ],
465 'country' => __( 'Country', 'give' ),
466 'zip' => [
467 __( 'Zip Code', 'give' ),
468 __( 'Zip', 'give' ),
469 __( 'zipcode', 'give' ),
470 __( 'Postal Code', 'give' ),
471 __( 'Postal', 'give' ),
472 ],
473 'email' => [
474 __( 'Donor Email', 'give' ),
475 __( 'Email', 'give' ),
476 __( 'Email Address', 'give' ),
477 ],
478 'post_status' => [
479 __( 'Donation Status', 'give' ),
480 __( 'Status', 'give' ),
481 ],
482 'gateway' => [
483 __( 'Payment Method', 'give' ),
484 __( 'Method', 'give' ),
485 __( 'Payment Gateway', 'give' ),
486 __( 'Gateway', 'give' ),
487 ],
488 'notes' => __( 'Notes', 'give' ),
489 'mode' => [
490 __( 'Payment Mode', 'give' ),
491 __( 'Mode', 'give' ),
492 __( 'Test Mode', 'give' ),
493 ],
494 'donor_ip' => __( 'Donor IP Address', 'give' ),
495 'post_meta' => __( 'Import as Meta', 'give' ),
496 ]
497 );
498 }
499
500 /**
501 * Return the option that are related to donations.
502 *
503 * @since 1.8.13
504 */
505 function give_import_donor_options() {
506 /**
507 * Filter to modify donors option in the import dropdown
508 *
509 * @since 1.8.13
510 *
511 * @return array
512 */
513 return (array) apply_filters(
514 'give_import_donor_options',
515 [
516 'donor_id' => __( 'Donor ID', 'give' ),
517 'user_id' => __( 'User ID', 'give' ),
518 ]
519 );
520 }
521
522 /**
523 * Return the option that are related to donations.
524 *
525 * @since 1.8.13
526 */
527 function give_import_donation_form_options() {
528 /**
529 * Filter to modify form option in the import dropdown
530 *
531 * @since 1.8.13
532 *
533 * @return array
534 */
535 return (array) apply_filters(
536 'give_import_donation_form_options',
537 [
538 'form_title' => [
539 __( 'Donation Form Title', 'give' ),
540 __( 'Donation Form', 'give' ),
541 __( 'Form Name', 'give' ),
542 __( 'Title', 'give' ),
543 __( 'Form Title', 'give' ),
544 'ignore' => [
545 __( 'Title Prefix', 'give' ),
546 __( 'Prefix', 'give' ),
547 ],
548 ],
549 'form_id' => [
550 __( 'Donation Form ID', 'give' ),
551 __( 'Form ID', 'give' ),
552 ],
553 'form_level' => [
554 __( 'Donation Level', 'give' ),
555 __( 'Level', 'give' ),
556 __( 'Level Title', 'give' ),
557 ],
558 'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ),
559 ]
560 );
561 }
562
563 /**
564 * Import CSV in DB
565 *
566 * @param int $file_id CSV id.
567 * @param int $start Start from which csv line.
568 * @param int $end End from which csv line.
569 * @param string $delimiter CSV delimeter.
570 *
571 * @return array
572 */
573 function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = 'csv' ) {
574 /**
575 * Filter to modify delimiter of Import
576 *
577 * @since 1.8.14
578 *
579 * @param string $delimiter
580 *
581 * @return string $delimiter
582 */
583 $delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
584
585 $file_dir = give_get_file_data_by_file_id( $file_id );
586
587 return give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter );
588 }
589
590 /**
591 * Get raw data from file data
592 *
593 * @since 2.1
594 *
595 * @param $file_dir
596 * @param $start
597 * @param $end
598 * @param $delimiter
599 *
600 * @return array
601 */
602 function give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter ) {
603 $raw_data = [];
604
605 $count = 0;
606 if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
607 while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) {
608 if ( $count >= $start && $count <= $end ) {
609 $raw_data[] = $row;
610 }
611 $count ++;
612 }
613 fclose( $handle );
614 }
615
616 return $raw_data;
617 }
618
619 /**
620 * Get content from the attachment id of CSV
621 *
622 * @since 2.1
623 *
624 * @param $file_id
625 *
626 * @return false|string file content
627 */
628 function give_get_file_data_by_file_id( $file_id ) {
629 return get_attached_file( $file_id );
630 }
631
632
633 /**
634 * Remove login when user register with give functions.
635 *
636 * @since 1.8.13
637 *
638 * @param $value
639 *
640 * @return bool
641 */
642 function give_log_user_in_on_register_callback( $value ) {
643 return false;
644 }
645
646 /**
647 * Add import Donation forms, donations , donor from CSV to database
648 *
649 * @since 1.8.13
650 *
651 * @param array $raw_key Setup bu user at step 2.
652 * @param array $row_data Feilds that are being imported from CSV
653 * @param array $main_key First row from the CSV
654 * @param array $import_setting Contain the global variable.
655 *
656 * @return bool
657 */
658 function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = [], $import_setting = [] ) {
659 $data = array_combine( $raw_key, $row_data );
660 $price_id = false;
661 $donor_id = 0;
662 $donor_data = [];
663 $form = [];
664 $import_setting['create_user'] = isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1;
665 $dry_run = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
666 $_dry_run_is_duplicate = false;
667 $dry_run_duplicate_form = false;
668 $dry_run_duplicate_donor = false;
669 $donation_key = empty( $import_setting['donation_key'] ) ? 1 : (int) $import_setting['donation_key'];
670 $payment_id = false;
671
672 $data = (array) apply_filters( 'give_save_import_donation_to_db', $data );
673
674 $data['amount'] = give_maybe_sanitize_amount( $data['amount'] );
675 $diff = [];
676
677 if ( ! empty( $dry_run ) && 1 !== $donation_key ) {
678 $csv_raw_data = empty( $import_setting['csv_raw_data'] ) ? [] : $import_setting['csv_raw_data'];
679 $donors_list = empty( $import_setting['donors_list'] ) ? [] : $import_setting['donors_list'];
680 $key = $donation_key - 1;
681 for ( $i = 0; $i < $key; $i ++ ) {
682 $csv_data = array_combine( $raw_key, $csv_raw_data[ $i ] );
683 $csv_data['amount'] = give_maybe_sanitize_amount( $csv_data['amount'] );
684 // check for duplicate donations
685 $diff = array_diff( $csv_data, $data );
686 if ( empty( $diff ) ) {
687 $_dry_run_is_duplicate = true;
688 $dry_run_duplicate_form = true;
689 $dry_run_duplicate_donor = true;
690 } else {
691 // check for duplicate donation form with form id
692 if ( ! empty( $csv_data['form_id'] ) && ! empty( $data['form_id'] ) && $csv_data['form_id'] === $data['form_id'] ) {
693 $form = new Give_Donate_Form( $data['form_id'] );
694 $form_id = $form->get_ID();
695 if ( ! empty( $form_id ) ) {
696 $dry_run_duplicate_form = true;
697 }
698 }
699 // check for duplicate donation form with form title
700 if ( empty( $dry_run_duplicate_form ) && ! empty( $csv_data['form_title'] ) && ! empty( $data['form_title'] ) && $csv_data['form_title'] === $data['form_title'] ) {
701 $dry_run_duplicate_form = true;
702 }
703
704 // check for duplicate donor by donor id
705 if ( ! empty( $csv_data['donor_id'] ) && ! empty( $data['donor_id'] ) && $csv_data['donor_id'] === $data['donor_id'] ) {
706 $donor = array_search( (int) $data['donor_id'], array_column( $donors_list, 'id' ) );
707 if ( ! empty( $donor ) ) {
708 $dry_run_duplicate_donor = true;
709 }
710 }
711
712 // check for duplicate donor by user id
713 if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['user_id'] ) && ! empty( $data['user_id'] ) && $csv_data['user_id'] === $data['user_id'] ) {
714 $donor = array_search( (int) $data['user_id'], array_column( $donors_list, 'user_id' ) );
715 if ( ! empty( $donor ) ) {
716 $dry_run_duplicate_donor = true;
717 } else {
718 $donor = get_user_by( 'id', $csv_data['user_id'] );
719 if ( ! empty( $donor->ID ) ) {
720 $dry_run_duplicate_donor = true;
721 }
722 }
723 }
724
725 // check for duplicate donor by donor id
726 if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['email'] ) && ! empty( $data['email'] ) && $csv_data['email'] === $data['email'] ) {
727 $dry_run_duplicate_donor = true;
728 }
729 }
730 }
731 }
732
733 if ( empty( $dry_run_duplicate_donor ) ) {
734 // Here come the login function.
735 $donor_data = give_import_get_user_from_csv( $data, $import_setting );
736 if ( empty( $dry_run ) ) {
737 if ( ! empty( $donor_data->id ) ) {
738 $donor_id = $donor_data->id;
739 } else {
740 return $payment_id;
741 }
742 }
743 } else {
744 // Get the report
745 $report = give_import_donation_report();
746 $report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
747 // update the report
748 give_import_donation_report_update( $report );
749 }
750
751 if ( empty( $dry_run_duplicate_form ) ) {
752 // get form data or register a form data.
753 $form = give_import_get_form_data_from_csv( $data, $import_setting );
754 if ( false == $form && empty( $dry_run ) ) {
755 return $payment_id;
756 } else {
757 $price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
758 }
759 } else {
760 // Get the report
761 $report = give_import_donation_report();
762 $report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
763 // update the report
764 give_import_donation_report_update( $report );
765 }
766
767 // Get the report
768 $report = give_import_donation_report();
769
770 $status = give_import_donation_get_status( $data );
771 $country = ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' );
772 $state = ( ! empty( $data['state'] ) ? ( ( $state_code = array_search( $data['state'], give_get_states( $country ) ) ) ? $state_code : $data['state'] ) : '' );
773
774 $address = [
775 'line1' => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ),
776 'line2' => ( ! empty( $data['line2'] ) ? give_clean( $data['line2'] ) : '' ),
777 'city' => ( ! empty( $data['city'] ) ? give_clean( $data['city'] ) : '' ),
778 'zip' => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ),
779 'state' => $state,
780 'country' => $country,
781 ];
782
783 $test_mode = [ 'test', 'true' ];
784 $post_date = current_time( 'mysql' );
785 if ( ! empty( $data['post_date'] ) ) {
786 if ( ! empty( $data['post_time'] ) ) {
787 $post_date = mysql2date( 'Y-m-d', $data['post_date'] );
788 $post_date = mysql2date( 'Y-m-d H:i:s', $post_date . ' ' . $data['post_time'] );
789 } else {
790 $post_date = mysql2date( 'Y-m-d H:i:s', $data['post_date'] );
791 }
792 }
793
794 // Create payment_data array
795 $payment_data = [
796 'donor_id' => $donor_id,
797 'price' => $data['amount'],
798 'status' => $status,
799 'currency' => ! empty( $data['currency'] ) && array_key_exists( $data['currency'], give_get_currencies_list() ) ? $data['currency'] : give_get_currency(),
800 'user_info' => [
801 'id' => $donor_id,
802 'email' => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ),
803 '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 ) ),
804 '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 ) ),
805 'address' => $address,
806 'title' => ! empty( $data['title_prefix'] ) ? $data['title_prefix'] : '',
807 ],
808 'gateway' => ( ! empty( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ),
809 'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : ( method_exists( $form, 'get_name' ) ? $form->get_name() : '' ) ),
810 'give_form_id' => ( ! empty( $form ) && method_exists( $form, 'get_ID' ) ) ? $form->get_ID() : '',
811 'give_price_id' => $price_id,
812 'purchase_key' => strtolower( md5( uniqid() ) ),
813 'user_email' => $data['email'],
814 'post_date' => $post_date,
815 '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' ) ) ),
816 ];
817
818 /**
819 * Filter to modify payment Data before getting imported.
820 *
821 * @since 2.1.0
822 *
823 * @param array $payment_data payment data
824 * @param array $payment_data donation data
825 * @param array $donor_data donor data
826 * @param object $donor_data form object
827 *
828 * @return array $payment_data payment data
829 */
830 $payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form );
831
832 // Get the report
833 $report = give_import_donation_report();
834
835 // Check for duplicate code.
836 $donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data );
837 if ( false !== $donation_duplicate || ! empty( $_dry_run_is_duplicate ) ) {
838 $report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate;
839 $report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 );
840 } else {
841
842 if ( empty( $dry_run ) ) {
843 add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 );
844 add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 );
845 add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 );
846 add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 );
847 add_filter( 'give_is_stop_email_notification', '__return_true' );
848
849 // If status is other than pending then first change the donation status to pending and after adding the payment meta update the donation status.
850 if ( 'pending' !== $status ) {
851 unset( $payment_data['status'] );
852 }
853
854 $payment_id = give_insert_payment( $payment_data );
855 remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 );
856 remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 );
857 remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 );
858 remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 );
859 remove_filter( 'give_is_stop_email_notification', '__return_true' );
860
861 if ( $payment_id ) {
862
863 $payment = new Give_Payment( $payment_id );
864
865 $report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
866
867 $payment->update_meta( '_give_payment_import', true );
868
869 if ( ! empty( $import_setting['csv'] ) ) {
870 $payment->update_meta( '_give_payment_import_id', $import_setting['csv'] );
871 }
872
873 // Insert Company Name.
874 if ( ! empty( $data['company_name'] ) ) {
875 $payment->update_meta( '_give_donation_company', $data['company_name'] );
876 $donor_data->update_meta( '_give_donor_company', $data['company_name'] );
877 }
878
879 // Insert Donor IP address.
880 if ( ! empty( $data['donor_ip'] ) ) {
881 $payment->update_meta( '_give_payment_donor_ip', $data['donor_ip'] );
882 }
883
884 // Insert Notes.
885 if ( ! empty( $data['notes'] ) ) {
886 $payment->add_note( $data['notes'] );
887 }
888
889 $meta_exists = array_keys( $raw_key, 'post_meta' );
890 if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) {
891 foreach ( $meta_exists as $meta_exist ) {
892 if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) {
893 $payment->update_meta( $main_key[ $meta_exist ], $row_data[ $meta_exist ] );
894 }
895 }
896 }
897
898 // update the donation status if it's other then pending
899 if ( 'pending' !== $status ) {
900 $payment->update_status( $status );
901 }
902 } else {
903 $report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 );
904 $payment_id = false;
905 }
906
907 /**
908 * Fire after payment is imported and payment meta is also being imported.
909 *
910 * @since 2.1.0
911 *
912 * @param int $payment_id payment id
913 * @param array $payment_data payment data
914 * @param array $payment_data donation data
915 * @param array $donor_data donor data
916 * @param object $donor_data form object
917 */
918 do_action( 'give_import_after_import_payment', $payment_id, $payment_data, $data, $donor_data, $form );
919 } else {
920 $report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
921 $payment_id = true;
922 }
923 }
924
925 // update the report
926 give_import_donation_report_update( $report );
927
928 return $payment_id;
929 }
930
931 /**
932 * Get Donation form status
933 *
934 * @since 2.0.2
935 *
936 * @param array $data donation data that is goingt o get imported
937 *
938 * @return string $status Donation status.
939 */
940 function give_import_donation_get_status( $data ) {
941 if ( empty( $data['post_status'] ) ) {
942 return 'publish';
943 }
944
945 $status = 'publish';
946
947 $donation_status = trim( $data['post_status'] );
948 $donation_status_key = strtolower( preg_replace( '/\s+/', '', $donation_status ) );
949
950 foreach ( give_get_payment_statuses() as $key => $value ) {
951 $match = false;
952 if ( $key === $donation_status_key ) {
953 $match = true;
954 } elseif ( stristr( $donation_status, $value ) ) {
955 $match = true;
956 }
957
958 if ( ! empty( $match ) ) {
959 $status = $key;
960 break;
961 }
962 }
963
964 return $status;
965 }
966
967 /**
968 * Alter donor information when importing donations from CSV
969 *
970 * @since 1.8.13
971 *
972 * @param $donor
973 * @param $payment_id
974 * @param $payment_data
975 *
976 * @return Give_Donor
977 */
978 function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) {
979 $old_donor = $donor;
980 if ( ! empty( $payment_data['donor_id'] ) ) {
981 $donor_id = absint( $payment_data['donor_id'] );
982 $donor = new Give_Donor( $donor_id );
983 if ( ! empty( $donor->id ) ) {
984 return $donor;
985 }
986 }
987
988 return $old_donor;
989 }
990
991 /*
992 * Give update purchase_count of give customer.
993 *
994 * @since 1.8.13
995 */
996 function give_import_donation_insert_payment( $payment_id, $payment_data ) {
997 // Update Give Customers purchase_count
998 if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) {
999 $donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true );
1000 if ( ! empty( $donor_id ) ) {
1001 $donor = new Give_Donor( $donor_id );
1002 $donor->increase_purchase_count();
1003 }
1004 }
1005 }
1006
1007 /**
1008 * Add author id in in donation post
1009 *
1010 * @since 1.8.13
1011 */
1012 function give_donation_import_give_insert_payment_args( $args, $payment_data ) {
1013 if ( ! empty( $payment_data['user_info']['id'] ) ) {
1014 $args['post_author'] = (int) $payment_data['user_info']['id'];
1015 }
1016
1017 return $args;
1018 }
1019
1020 /**
1021 * Check if Import donation is duplicate
1022 *
1023 * @since 1.8.13
1024 */
1025 function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) {
1026 $return = false;
1027 if ( ! empty( $data['post_date'] ) ) {
1028 $post_date = mysql2date( 'Y-m-d-H-i-s', $payment_data['post_date'] );
1029 $post_date = explode( '-', $post_date );
1030 $args = [
1031 'output' => 'post',
1032 'cache_results' => false,
1033 'no_found_rows' => true,
1034 'update_post_meta_cache' => false,
1035 'update_post_term_cache' => false,
1036 'fields' => 'ids',
1037 'date_query' => [
1038 [
1039 'year' => $post_date[0],
1040 'month' => $post_date[1],
1041 'day' => $post_date[2],
1042 'hour' => $post_date[3],
1043 'minute' => $post_date[4],
1044 'second' => $post_date[5],
1045 ],
1046 ],
1047 'meta_query' => [
1048 [
1049 'key' => '_give_payment_total',
1050 'value' => preg_replace( '/[\$,]/', '', $payment_data['price'] ),
1051 'compare' => 'LIKE',
1052 ],
1053 [
1054 'key' => '_give_payment_form_id',
1055 'value' => $payment_data['give_form_id'],
1056 'type' => 'numeric',
1057 'compare' => '=',
1058 ],
1059 [
1060 'key' => '_give_payment_gateway',
1061 'value' => $payment_data['gateway'],
1062 'compare' => '=',
1063 ],
1064 [
1065 'key' => '_give_payment_donor_id',
1066 'value' => isset( $donor_data->id ) ? $donor_data->id : '',
1067 'compare' => '=',
1068 ],
1069 [
1070 'key' => '_give_payment_mode',
1071 'value' => $payment_data['mode'],
1072 'compare' => '=',
1073 ],
1074 ],
1075 ];
1076
1077 $payments = new Give_Payments_Query( $args );
1078 $donations = $payments->get_payments();
1079 if ( ! empty( $donations ) ) {
1080 $return = $donations;
1081 }
1082 }
1083
1084 /**
1085 * Filter to modify donation which is getting add is duplicate or not.
1086 *
1087 * @since 1.8.18
1088 */
1089 return apply_filters( 'give_check_import_donation_duplicate', $return, $payment_data, $data, $form, $donor_data );
1090 }
1091
1092 /**
1093 * Record payment notes that is being imported from CSV.
1094 *
1095 * @since 1.8.13
1096 *
1097 * @param int $payment_id The ID number of the payment.
1098 *
1099 * @return void
1100 */
1101 function give_donation_import_insert_default_payment_note( $payment_id ) {
1102 $current_user = wp_get_current_user();
1103 give_insert_payment_note( $payment_id, wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) );
1104 }
1105
1106 /**
1107 * Return Import Page URL
1108 *
1109 * @since 1.8.13
1110 *
1111 * @param array $parameter
1112 *
1113 * @return string URL
1114 */
1115 function give_import_page_url( $parameter = [] ) {
1116 $defalut_query_arg = [
1117 'post_type' => 'give_forms',
1118 'page' => 'give-tools',
1119 'tab' => 'import',
1120 'importer-type' => 'import_donations',
1121 ];
1122 $import_query_arg = wp_parse_args( $parameter, $defalut_query_arg );
1123
1124 return esc_url_raw( add_query_arg( $import_query_arg, admin_url( 'edit.php' ) ) );
1125 }
1126