PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / includes / admin / import-functions.php

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

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