Issues (4335)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

includes/admin/import-functions.php (27 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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', array() );
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 = array() ) {
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', array(), 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
 *
50
 * @param $data .
51
 *
52
 * @return array|bool|Give_Donate_Form|int|null|WP_Post
53
 */
54
function give_import_get_form_data_from_csv( $data, $import_setting = array() ) {
55
	$new_form = false;
56
	$dry_run  = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
57
58
	// Get the import report
59
	$report = give_import_donation_report();
60
61
	$form = false;
62
	$meta = array();
63
64
	if ( ! empty( $data['form_id'] ) ) {
65
		$form = new Give_Donate_Form( $data['form_id'] );
66
		// Add support to older php version.
67
		$form_id = $form->get_ID();
68 View Code Duplication
		if ( empty( $form_id ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
			$form = false;
70
		} else {
71
			$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
72
		}
73
	}
74
75
	if ( false === $form && ! empty( $data['form_title'] ) ) {
76
		$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' );
77
78
		if ( ! empty( $form->ID ) ) {
79
80
			$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
81
82
			$form = new Give_Donate_Form( $form->ID );
0 ignored issues
show
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
		} else {
84
			$form = new Give_Donate_Form();
85
			$args = array(
86
				'post_title'  => $data['form_title'],
87
				'post_status' => 'publish',
88
			);
89
90
			if ( empty( $dry_run ) ) {
91
				$form = $form->create( $args );
0 ignored issues
show
$form is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
			}
93
94
			$report['create_form'] = ( ! empty( $report['create_form'] ) ? ( absint( $report['create_form'] ) + 1 ) : 1 );
95
			$new_form              = true;
96
97
		}
98
99
		$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' );
100
		if ( ! empty( $form->ID ) ) {
101
			$form = new Give_Donate_Form( $form->ID );
102
		}
103
	}
104
105
	if ( ! empty( $form ) && $form->get_ID() && ! empty( $data['form_level'] ) && empty( $dry_run ) ) {
106
107
		$price_option = 'set';
108
		$form_level   = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) );
109
110
		if ( 'custom' !== $form_level ) {
111
			$prices     = (array) $form->get_prices();
112
			$price_text = array();
113
			foreach ( $prices as $key => $price ) {
114
				if ( isset( $price['_give_id']['level_id'] ) ) {
115
					$price_text[ $price['_give_id']['level_id'] ] = ( ! empty( $price['_give_text'] ) ? strtolower( preg_replace( '/\s+/', '', $price['_give_text'] ) ) : '' );
116
				}
117
			}
118
119
			if ( ! in_array( $form_level, $price_text ) ) {
120
121
				// For generating unquiet level id.
122
				$count     = 1;
123
				$new_level = count( $prices ) + $count;
124
				while ( array_key_exists( $new_level, $price_text ) ) {
125
					$count ++;
126
					$new_level = count( $prices ) + $count;
127
				}
128
129
				$multi_level_donations = array(
130
					array(
131
						'_give_id'     => array(
132
							'level_id' => $new_level,
133
						),
134
						'_give_amount' => give_sanitize_amount_for_db( $data['amount'] ),
135
						'_give_text'   => $data['form_level'],
136
					),
137
				);
138
139
				$price_text[ $new_level ] = strtolower( preg_replace( '/\s+/', '', $data['form_level'] ) );
140
141
				if ( ! empty( $prices ) && is_array( $prices ) && ! empty( $prices[0] ) ) {
142
					$prices = wp_parse_args( $multi_level_donations, $prices );
143
144
					// Sort $prices by amount in ascending order.
145
					$prices = wp_list_sort( $prices, '_give_amount', 'ASC' );
146
				} else {
147
					$prices = $multi_level_donations;
148
				}
149
150
				// Unset _give_default key from $prices.
151
				foreach ( $prices as $key => $price ) {
152
					if ( isset( $prices[ $key ]['_give_default'] ) ) {
153
						unset( $prices[ $key ]['_give_default'] );
154
					}
155
				}
156
157
				// Set the first $price of the $prices as default.
158
				$prices[0]['_give_default'] = 'default';
159
			}
160
			$form->price_id = array_search( $form_level, $price_text );
161
162
			$donation_levels_amounts = wp_list_pluck( $prices, '_give_amount' );
163
			$min_amount              = min( $donation_levels_amounts );
164
			$max_amount              = max( $donation_levels_amounts );
165
166
			$meta = array(
167
				'_give_levels_minimum_amount' => $min_amount,
168
				'_give_levels_maximum_amount' => $max_amount,
169
				'_give_donation_levels'       => array_values( $prices ),
170
			);
171
172
			$price_option = 'multi';
173
		} else {
174
			$form->price_id = 'custom';
175
		}
176
177
		$defaults = array(
178
			'_give_set_price' => give_sanitize_amount_for_db( $data['amount'] ),
179
		);
180
181
		// If new form is created.
182
		if ( ! empty( $new_form ) ) {
183
			$new_form = array(
184
				'_give_custom_amount_text'          => ( ! empty( $data['form_custom_amount_text'] ) ? $data['form_custom_amount_text'] : 'custom' ),
185
				'_give_logged_in_only'              => 'enabled',
186
				'_give_custom_amount'               => 'enabled',
187
				'_give_payment_import'              => true,
188
				'_give_display_style'               => 'radios',
189
				'_give_payment_display'             => 'onpage',
190
				'give_product_notes'                => 'Donation Notes',
191
				'_give_product_type'                => 'default',
192
				'_give_default_gateway'             => 'global',
193
				'_give_customize_offline_donations' => 'global',
194
				'_give_show_register_form'          => 'both',
195
				'_give_price_option'                => $price_option,
196
			);
197
			$defaults = wp_parse_args( $defaults, $new_form );
198
		}
199
200
		$meta = wp_parse_args( $meta, $defaults );
201
202
		foreach ( $meta as $key => $value ) {
203
			give_update_meta( $form->get_ID(), $key, $value );
204
		}
205
	}
206
207
	// update the report
208
	give_import_donation_report_update( $report );
209
210
	return $form;
211
}
212
213
/**
214
 * Give get user details if not then create a user. Used in Import Donation CSV.
215
 *
216
 * @since 1.8.13
217
 *
218
 * @param $data
219
 *
220
 * @return bool|false|WP_User
221
 */
222
function give_import_get_user_from_csv( $data, $import_setting = array() ) {
223
	$report               = give_import_donation_report();
224
	$dry_run              = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
225
	$dry_run_donor_create = false;
226
	$donor_data           = array();
227
	$donor_id             = false;
228
229
	// check if donor id is not empty
230
	if ( ! empty( $data['donor_id'] ) ) {
231
		$donor_data = new Give_Donor( (int) $data['donor_id'] );
232 View Code Duplication
		if ( ! empty( $donor_data->id ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
			$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
234
		}
235
	}
236
237
	if ( empty( $donor_data->id ) && ! empty( $data['user_id'] ) ) {
238
		$user_id    = (int) $data['user_id'];
239
		$donor_data = new Give_Donor( $user_id, true );
240
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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 : '' ) );
0 ignored issues
show
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
251
					$name       = $first_name . ' ' . $last_name;
252
					$user_email = $donor_data->user_email;
253
					$donor_args = array(
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           = array( '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 = array();
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         = array(
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 View Code Duplication
				} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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    = array(
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;
0 ignored issues
show
$dry_run_donor_create is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
358
					}
359
					$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
360 View Code Duplication
				} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
					$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
362
				}
363
			}
364 View Code Duplication
		} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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( 'give_import_default_options', array(
389
		'' => __( 'Do not import', 'give' ),
390
	) );
391
}
392
393
/**
394
 * Return the option that are related to donations.
395
 *
396
 * @since 1.8.13
397
 */
398
function give_import_donations_options() {
399
	/**
400
	 * Filter to modify donations option in the import dropdown
401
	 *
402
	 * @since 1.8.13
403
	 *
404
	 * @return array
405
	 */
406
	return (array) apply_filters( 'give_import_donations_options', array(
407
		'id'           => __( 'Donation ID', 'give' ),
408
		'amount'       => array(
409
			__( 'Donation Amount', 'give' ),
410
			__( 'Amount', 'give' ),
411
			__( 'Donation Total', 'give' ),
412
			__( 'Total', 'give' ),
413
		),
414
		'currency'     => array(
415
			__( 'Donation Currencies', 'give' ),
416
			__( 'Currencies', 'give' ),
417
			__( 'Currencies Code', 'give' ),
418
			__( 'Currency Code', 'give' ),
419
			__( 'Code', 'give' ),
420
		),
421
		'post_date'    => array(
422
			__( 'Donation Date', 'give' ),
423
			__( 'Date', 'give' ),
424
		),
425
		'post_time'    => array(
426
			__( 'Donation Time', 'give' ),
427
			__( 'Time', 'give' ),
428
		),
429
		'title_prefix' => array(
430
			__( 'Title Prefix', 'give' ),
431
			__( 'Prefix', 'give' ),
432
		),
433
		'first_name'   => array(
434
			__( 'Donor First Name', 'give' ),
435
			__( 'First Name', 'give' ),
436
			__( 'Name', 'give' ),
437
			__( 'First', 'give' ),
438
		),
439
		'last_name'    => array(
440
			__( 'Donor Last Name', 'give' ),
441
			__( 'Last Name', 'give' ),
442
			__( 'Last', 'give' ),
443
		),
444
		'company_name' => array(
445
			__( 'Company Name', 'give' ),
446
			__( 'Donor Company Name', 'give' ),
447
			__( 'Donor Company', 'give' ),
448
			__( 'Company', 'give' ),
449
		),
450
		'line1'        => array(
451
			__( 'Address 1', 'give' ),
452
			__( 'Address', 'give' ),
453
		),
454
		'line2'        => __( 'Address 2', 'give' ),
455
		'city'         => __( 'City', 'give' ),
456
		'state'        => array(
457
			__( 'State', 'give' ),
458
			__( 'Province', 'give' ),
459
			__( 'County', 'give' ),
460
			__( 'Region', 'give' ),
461
		),
462
		'country'      => __( 'Country', 'give' ),
463
		'zip'          => array(
464
			__( 'Zip Code', 'give' ),
465
			__( 'Zip', 'give' ),
466
			__( 'zipcode', 'give' ),
467
			__( 'Postal Code', 'give' ),
468
			__( 'Postal', 'give' ),
469
		),
470
		'email'        => array(
471
			__( 'Donor Email', 'give' ),
472
			__( 'Email', 'give' ),
473
			__( 'Email Address', 'give' ),
474
		),
475
		'post_status'  => array(
476
			__( 'Donation Status', 'give' ),
477
			__( 'Status', 'give' ),
478
		),
479
		'gateway'      => array(
480
			__( 'Payment Method', 'give' ),
481
			__( 'Method', 'give' ),
482
			__( 'Payment Gateway', 'give' ),
483
			__( 'Gateway', 'give' ),
484
		),
485
		'notes'        => __( 'Notes', 'give' ),
486
		'mode'         => array(
487
			__( 'Payment Mode', 'give' ),
488
			__( 'Mode', 'give' ),
489
			__( 'Test Mode', 'give' ),
490
		),
491
		'donor_ip'     => __( 'Donor IP Address', 'give' ),
492
		'post_meta'    => __( 'Import as Meta', 'give' ),
493
	) );
494
}
495
496
/**
497
 * Return the option that are related to donations.
498
 *
499
 * @since 1.8.13
500
 */
501
function give_import_donor_options() {
502
	/**
503
	 * Filter to modify donors option in the import dropdown
504
	 *
505
	 * @since 1.8.13
506
	 *
507
	 * @return array
508
	 */
509
	return (array) apply_filters( 'give_import_donor_options', array(
510
		'donor_id' => __( 'Donor ID', 'give' ),
511
		'user_id'  => __( 'User ID', 'give' ),
512
	) );
513
}
514
515
/**
516
 * Return the option that are related to donations.
517
 *
518
 * @since 1.8.13
519
 */
520
function give_import_donation_form_options() {
521
	/**
522
	 * Filter to modify form option in the import dropdown
523
	 *
524
	 * @since 1.8.13
525
	 *
526
	 * @return array
527
	 */
528
	return (array) apply_filters( 'give_import_donation_form_options', array(
529
		'form_title'              => array(
530
			__( 'Donation Form Title', 'give' ),
531
			__( 'Donation Form', 'give' ),
532
			__( 'Form Name', 'give' ),
533
			__( 'Title', 'give' ),
534
			__( 'Form Title', 'give' ),
535
			'ignore' => array(
536
				__( 'Title Prefix', 'give' ),
537
				__( 'Prefix', 'give' ),
538
			),
539
		),
540
		'form_id'                 => array(
541
			__( 'Donation Form ID', 'give' ),
542
			__( 'Form ID', 'give' ),
543
		),
544
		'form_level'              => array(
545
			__( 'Donation Level', 'give' ),
546
			__( 'Level', 'give' ),
547
			__( 'Level Title', 'give' ),
548
		),
549
		'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ),
550
	) );
551
}
552
553
/**
554
 * Import CSV in DB
555
 *
556
 * @param int    $file_id   CSV id.
557
 * @param int    $start     Start from which csv line.
558
 * @param int    $end       End from which csv line.
559
 * @param string $delimiter CSV delimeter.
560
 *
561
 * @return array
562
 */
563
function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = 'csv' ) {
564
	/**
565
	 * Filter to modify delimiter of Import
566
	 *
567
	 * @since 1.8.14
568
	 *
569
	 * @param string $delimiter
570
	 *
571
	 * @return string $delimiter
572
	 */
573
	$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
574
575
	$file_dir = give_get_file_data_by_file_id( $file_id );
576
577
	return give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter );
578
}
579
580
/**
581
 * Get raw data from file data
582
 *
583
 * @since 2.1
584
 *
585
 * @param $file_dir
586
 * @param $start
587
 * @param $end
588
 * @param $delimiter
589
 *
590
 * @return array
591
 */
592
function give_get_raw_data_from_file( $file_dir, $start, $end, $delimiter ) {
593
	$raw_data = array();
594
595
	$count    = 0;
596
	if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
597
		while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) {
598
			if ( $count >= $start && $count <= $end ) {
599
				$raw_data[] = $row;
600
			}
601
			$count ++;
602
		}
603
		fclose( $handle );
604
	}
605
606
	return $raw_data;
607
}
608
609
/**
610
 * Get content from the attachment id of CSV
611
 *
612
 * @since 2.1
613
 *
614
 * @param $file_id
615
 *
616
 * @return false|string file content
617
 */
618
function give_get_file_data_by_file_id( $file_id ) {
619
	return get_attached_file( $file_id );
620
}
621
622
623
/**
624
 * Remove login when user register with give functions.
625
 *
626
 * @since 1.8.13
627
 *
628
 * @param $value
629
 *
630
 * @return bool
631
 */
632
function give_log_user_in_on_register_callback( $value ) {
0 ignored issues
show
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
633
	return false;
634
}
635
636
/**
637
 * Add import Donation forms, donations , donor from CSV to database
638
 *
639
 * @since 1.8.13
640
 *
641
 * @param array $raw_key Setup bu user at step 2.
642
 * @param array $row_data Feilds that are being imported from CSV
643
 * @param array $main_key First row from the CSV
644
 * @param array $import_setting Contain the global variable.
645
 *
646
 * @return bool
647
 */
648
function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = array(), $import_setting = array() ) {
649
	$data                          = array_combine( $raw_key, $row_data );
650
	$price_id                      = false;
651
	$donor_id                      = 0;
652
	$donor_data                    = array();
653
	$form                          = array();
654
	$import_setting['create_user'] = isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1;
655
	$dry_run                       = isset( $import_setting['dry_run'] ) ? $import_setting['dry_run'] : false;
656
	$_dry_run_is_duplicate         = false;
657
	$dry_run_duplicate_form        = false;
658
	$dry_run_duplicate_donor       = false;
659
	$donation_key                  = empty( $import_setting['donation_key'] ) ? 1 : (int) $import_setting['donation_key'];
660
	$payment_id                    = false;
661
662
	$data = (array) apply_filters( 'give_save_import_donation_to_db', $data );
663
664
	$data['amount'] = give_maybe_sanitize_amount( $data['amount'] );
665
	$diff           = array();
0 ignored issues
show
$diff is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
666
667
	if ( ! empty( $dry_run ) && 1 !== $donation_key ) {
668
		$csv_raw_data = empty( $import_setting['csv_raw_data'] ) ? array() : $import_setting['csv_raw_data'];
669
		$donors_list  = empty( $import_setting['donors_list'] ) ? array() : $import_setting['donors_list'];
670
		$key          = $donation_key - 1;
671
		for ( $i = 0; $i < $key; $i ++ ) {
672
			$csv_data           = array_combine( $raw_key, $csv_raw_data[ $i ] );
673
			$csv_data['amount'] = give_maybe_sanitize_amount( $csv_data['amount'] );
674
			// check for duplicate donations
675
			$diff = array_diff( $csv_data, $data );
676
			if ( empty( $diff ) ) {
677
				$_dry_run_is_duplicate   = true;
678
				$dry_run_duplicate_form  = true;
679
				$dry_run_duplicate_donor = true;
680
			} else {
681
				// check for duplicate donation form with form id
682
				if ( ! empty( $csv_data['form_id'] ) && ! empty( $data['form_id'] ) && $csv_data['form_id'] === $data['form_id'] ) {
683
					$form    = new Give_Donate_Form( $data['form_id'] );
684
					$form_id = $form->get_ID();
685
					if ( ! empty( $form_id ) ) {
686
						$dry_run_duplicate_form = true;
687
					}
688
				}
689
				// check for duplicate donation form with form title
690
				if ( empty( $dry_run_duplicate_form ) && ! empty( $csv_data['form_title'] ) && ! empty( $data['form_title'] ) && $csv_data['form_title'] === $data['form_title'] ) {
691
					$dry_run_duplicate_form = true;
692
				}
693
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
694
695
				// check for duplicate donor by donor id
696
				if ( ! empty( $csv_data['donor_id'] ) && ! empty( $data['donor_id'] ) && $csv_data['donor_id'] === $data['donor_id'] ) {
697
					$donor = array_search( (int) $data['donor_id'], array_column( 'id', $donors_list ) );
698
					if ( ! empty( $donor ) ) {
699
						$dry_run_duplicate_donor = true;
700
					}
701
				}
702
703
				// check for duplicate donor by user id
704
				if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['user_id'] ) && ! empty( $data['user_id'] ) && $csv_data['user_id'] === $data['user_id'] ) {
705
					$donor = array_search( (int) $data['user_id'], array_column( 'user_id', $donors_list ) );
706
					if ( ! empty( $donor ) ) {
707
						$dry_run_duplicate_donor = true;
708
					} else {
709
						$donor = get_user_by( 'id', $csv_data['user_id'] );
710
						if ( ! empty( $donor->ID ) ) {
711
							$dry_run_duplicate_donor = true;
712
						}
713
					}
714
				}
715
716
				// check for duplicate donor by donor id
717
				if ( empty( $dry_run_duplicate_donor ) && ! empty( $csv_data['email'] ) && ! empty( $data['email'] ) && $csv_data['email'] === $data['email'] ) {
718
					$dry_run_duplicate_donor = true;
719
				}
720
			}
721
		}
722
	}
723
724
	if ( empty( $dry_run_duplicate_donor ) ) {
725
		// Here come the login function.
726
		$donor_data = give_import_get_user_from_csv( $data, $import_setting );
727
		if ( empty( $dry_run ) ) {
728
			if ( ! empty( $donor_data->id ) ) {
729
				$donor_id = $donor_data->id;
730
			} else {
731
				return $payment_id;
732
			}
733
		}
734 View Code Duplication
	} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
735
		// Get the report
736
		$report                    = give_import_donation_report();
737
		$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
738
		// update the report
739
		give_import_donation_report_update( $report );
740
	}
741
742
	if ( empty( $dry_run_duplicate_form ) ) {
743
		// get form data or register a form data.
744
		$form = give_import_get_form_data_from_csv( $data, $import_setting );
745
		if ( false == $form && empty( $dry_run ) ) {
746
			return $payment_id;
747
		} else {
748
			$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
749
		}
750 View Code Duplication
	} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
751
		// Get the report
752
		$report                   = give_import_donation_report();
753
		$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
754
		// update the report
755
		give_import_donation_report_update( $report );
756
	}
757
758
	// Get the report
759
	$report = give_import_donation_report();
0 ignored issues
show
$report is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
760
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
761
762
	$status  = give_import_donation_get_status( $data );
763
	$country = ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' );
764
	$state   = ( ! empty( $data['state'] ) ? ( ( $state_code = array_search( $data['state'], give_get_states( $country ) ) ) ? $state_code : $data['state'] ) : '' );
765
766
	$address = array(
767
		'line1'   => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ),
768
		'line2'   => ( ! empty( $data['line2'] ) ? give_clean( $data['line2'] ) : '' ),
769
		'city'    => ( ! empty( $data['city'] ) ? give_clean( $data['city'] ) : '' ),
770
		'zip'     => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ),
771
		'state'   => $state,
772
		'country' => $country,
773
	);
774
775
	$test_mode = array( 'test', 'true', );
776
	$post_date = current_time( 'mysql' );
777
	if ( ! empty( $data['post_date'] ) ) {
778
		if ( ! empty( $data['post_time'] ) ) {
779
			$post_date = mysql2date( 'Y-m-d', $data['post_date'] );
780
			$post_date = mysql2date( 'Y-m-d H:i:s', $post_date . ' ' . $data['post_time'] );
781
		} else {
782
			$post_date = mysql2date( 'Y-m-d H:i:s', $data['post_date'] );
783
		}
784
	}
785
786
	//Create payment_data array
787
	$payment_data = array(
788
		'donor_id'        => $donor_id,
789
		'price'           => $data['amount'],
790
		'status'          => $status,
791
		'currency'        => ! empty( $data['currency'] ) && array_key_exists( $data['currency'], give_get_currencies_list() ) ? $data['currency'] : give_get_currency(),
792
		'user_info'       => array(
793
			'id'         => $donor_id,
794
			'email'      => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ),
795
			'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 ) ),
0 ignored issues
show
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
796
			'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 ) ),
0 ignored issues
show
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
797
			'address'    => $address,
798
			'title'      => ! empty( $data['title_prefix'] ) ? $data['title_prefix'] : '',
799
		),
800
		'gateway'         => ( ! empty( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ),
801
		'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : ( method_exists( $form, 'get_name' ) ? $form->get_name() : '' ) ),
802
		'give_form_id'    => method_exists( $form, 'get_ID' ) ? $form->get_ID() : '',
803
		'give_price_id'   => $price_id,
804
		'purchase_key'    => strtolower( md5( uniqid() ) ),
805
		'user_email'      => $data['email'],
806
		'post_date'       => $post_date,
807
		'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' ) ) ),
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
808
	);
809
810
	/**
811
	 * Filter to modify payment Data before getting imported.
812
	 *
813
	 * @since 2.1.0
814
	 *
815
	 * @param array $payment_data payment data
816
	 * @param array $payment_data donation data
817
	 * @param array $donor_data donor data
818
	 * @param object $donor_data form object
819
	 *
820
	 * @return array $payment_data payment data
821
	 */
822
	$payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form );
823
824
	// Get the report
825
	$report = give_import_donation_report();
826
827
	// Check for duplicate code.
828
	$donation_duplicate = give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data );
829
	if ( false !== $donation_duplicate || ! empty( $_dry_run_is_duplicate ) ) {
830
		$report['donation_details'][ $import_setting['donation_key'] ]['duplicate'] = $donation_duplicate;
831
		$report['duplicate_donation']                                               = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 );
832
	} else {
833
834
		if ( empty( $dry_run ) ) {
835
			add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 );
836
			add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 );
837
			add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 );
838
			add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 );
839
			add_filter( 'give_is_stop_email_notification', '__return_true' );
840
841
			// if it status is other then pending then first change the donation status to pending and after adding the payment meta update the donation status.
842
			if ( 'pending' !== $status ) {
843
				unset( $payment_data['status'] );
844
			}
845
846
			$payment_id = give_insert_payment( $payment_data );
847
			remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 );
848
			remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 );
849
			remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 );
850
			remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 );
851
			remove_filter( 'give_is_stop_email_notification', '__return_true' );
852
853
			if ( $payment_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment_id of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
854
855
				$payment = new Give_Payment( $payment_id );
856
857
				$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
858
859
				$payment->update_meta( '_give_payment_import', true );
0 ignored issues
show
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
860
861
				if ( ! empty( $import_setting['csv'] ) ) {
862
					$payment->update_meta( '_give_payment_import_id', $import_setting['csv'] );
863
				}
864
865
				// Insert Company Name.
866
				if ( ! empty( $data['company_name'] ) ) {
867
					$payment->update_meta( '_give_donation_company', $data['company_name'] );
868
					$donor_data->update_meta( '_give_donor_company', $data['company_name'] );
869
				}
870
871
				// Insert Donor IP address.
872
				if ( ! empty( $data['donor_ip'] ) ) {
873
					$payment->update_meta( '_give_payment_donor_ip', $data['donor_ip'] );
874
				}
875
876
				// Insert Notes.
877
				if ( ! empty( $data['notes'] ) ) {
878
					$payment->add_note( $data['notes'] );
879
				}
880
881
				$meta_exists = array_keys( $raw_key, 'post_meta' );
882
				if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) {
883
					foreach ( $meta_exists as $meta_exist ) {
884
						if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) {
885
							$payment->update_meta( $main_key[ $meta_exist ], $row_data[ $meta_exist ] );
886
						}
887
					}
888
				}
889
890
				// update the donation status if it's other then pending
891
				if ( 'pending' !== $status ) {
892
					$payment->update_status( $status );
893
				}
0 ignored issues
show
Blank line found after control structure
Loading history...
894
895 View Code Duplication
			} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
896
				$report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 );
897
				$payment_id                = false;
898
			}
899
900
			/**
901
			 * Fire after payment is imported and payment meta is also being imported.
902
			 *
903
			 * @since 2.1.0
904
			 *
905
			 * @param int $payment payment id
906
			 * @param array $payment_data payment data
907
			 * @param array $payment_data donation data
908
			 * @param array $donor_data donor data
909
			 * @param object $donor_data form object
910
			 */
911
			do_action( 'give_import_after_import_payment', $payment, $payment_data, $data, $donor_data, $form );
912 View Code Duplication
		} else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
913
			$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
914
			$payment_id                = true;
915
		}
916
	}
917
918
	// update the report
919
	give_import_donation_report_update( $report );
920
921
	return $payment_id;
922
}
923
924
/**
925
 * Get Donation form status
926
 *
927
 * @since 2.0.2
928
 *
929
 * @param array $data donation data that is goingt o get imported
930
 *
931
 * @return string $status Donation status.
932
 */
933
function give_import_donation_get_status( $data ) {
934
	if ( empty( $data['post_status'] ) ) {
935
		return 'publish';
936
	}
937
938
	$status = 'publish';
939
940
	$donation_status = trim( $data['post_status'] );
941
	$donation_status_key      = strtolower( preg_replace( '/\s+/', '', $donation_status ) );
942
943
	foreach ( give_get_payment_statuses() as $key => $value ) {
944
		$match = false;
945
		if ( $key === $donation_status_key ) {
946
			$match = true;
947
		} else if ( stristr( $donation_status, $value ) ) {
948
			$match = true;
949
		}
950
951
		if ( ! empty( $match ) ) {
952
			$status = $key;
953
			break;
954
		}
955
	}
956
957
	return $status;
958
}
959
960
/**
961
 * Alter donor information when importing donations from CSV
962
 *
963
 * @since 1.8.13
964
 *
965
 * @param $donor
966
 * @param $payment_id
967
 * @param $payment_data
968
 *
969
 * @return Give_Donor
970
 */
971
function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) {
0 ignored issues
show
The parameter $payment_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
972
	$old_donor = $donor;
973
	if ( ! empty( $payment_data['donor_id'] ) ) {
974
		$donor_id = absint( $payment_data['donor_id'] );
975
		$donor    = new Give_Donor( $donor_id );
976
		if ( ! empty( $donor->id ) ) {
977
			return $donor;
978
		}
979
	}
980
981
	return $old_donor;
982
}
983
984
/*
985
 * Give update purchase_count of give customer.
986
 *
987
 * @since 1.8.13
988
 */
989
function give_import_donation_insert_payment( $payment_id, $payment_data ) {
990
	// Update Give Customers purchase_count
991
	if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) {
992
		$donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true );
993
		if ( ! empty( $donor_id ) ) {
994
			$donor = new Give_Donor( $donor_id );
995
			$donor->increase_purchase_count();
996
		}
997
	}
998
}
999
1000
/**
1001
 * Add author id in in donation post
1002
 *
1003
 * @since 1.8.13
1004
 */
1005
function give_donation_import_give_insert_payment_args( $args, $payment_data ) {
1006
	if ( ! empty( $payment_data['user_info']['id'] ) ) {
1007
		$args['post_author'] = (int) $payment_data['user_info']['id'];
1008
	}
1009
1010
	return $args;
1011
}
1012
1013
/**
1014
 * Check if Import donation is duplicate
1015
 *
1016
 * @since 1.8.13
1017
 */
1018
function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) {
1019
	$return = false;
1020
	if ( ! empty( $data['post_date'] ) ) {
1021
		$post_date = mysql2date( 'Y-m-d-H-i-s', $payment_data['post_date'] );
1022
		$post_date = explode( '-', $post_date );
1023
		$args      = array(
1024
			'output'                 => 'post',
1025
			'cache_results'          => false,
1026
			'no_found_rows'          => true,
1027
			'update_post_meta_cache' => false,
1028
			'update_post_term_cache' => false,
1029
			'fields'                 => 'ids',
1030
			'date_query'             => array(
1031
				array(
1032
					'year'   => $post_date[0],
1033
					'month'  => $post_date[1],
1034
					'day'    => $post_date[2],
1035
					'hour'   => $post_date[3],
1036
					'minute' => $post_date[4],
1037
					'second' => $post_date[5],
1038
				),
1039
			),
1040
			'meta_query'             => array(
0 ignored issues
show
Detected usage of meta_query, possible slow query.
Loading history...
1041
				array(
1042
					'key'     => '_give_payment_total',
1043
					'value'   => preg_replace( '/[\$,]/', '', $payment_data['price'] ),
1044
					'compare' => 'LIKE',
1045
				),
1046
				array(
1047
					'key'     => '_give_payment_form_id',
1048
					'value'   => $payment_data['give_form_id'],
1049
					'type'    => 'numeric',
1050
					'compare' => '=',
1051
				),
1052
				array(
1053
					'key'     => '_give_payment_gateway',
1054
					'value'   => $payment_data['gateway'],
1055
					'compare' => '=',
1056
				),
1057
				array(
1058
					'key'     => '_give_payment_donor_id',
1059
					'value'   => isset( $donor_data->id ) ? $donor_data->id : '',
1060
					'compare' => '=',
1061
				),
1062
				array(
1063
					'key'     => '_give_payment_mode',
1064
					'value'   => $payment_data['mode'],
1065
					'compare' => '=',
1066
				),
1067
			),
1068
		);
1069
1070
		$payments  = new Give_Payments_Query( $args );
1071
		$donations = $payments->get_payments();
1072
		if ( ! empty( $donations ) ) {
1073
			$return = $donations;
1074
		}
1075
	}
1076
1077
	/**
1078
	 * Filter to modify donation which is getting add is duplicate or not.
1079
	 *
1080
	 * @since 1.8.18
1081
	 */
1082
	return apply_filters( 'give_check_import_donation_duplicate', $return, $payment_data, $data, $form, $donor_data );
1083
}
1084
1085
/**
1086
 * Record payment notes that is being imported from CSV.
1087
 *
1088
 * @since  1.8.13
1089
 *
1090
 * @param  int $payment_id The ID number of the payment.
1091
 *
1092
 * @return void
1093
 */
1094
function give_donation_import_insert_default_payment_note( $payment_id ) {
1095
	$current_user = wp_get_current_user();
1096
	give_insert_payment_note( $payment_id, wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) );
1097
}
1098
1099
/**
1100
 * Return Import Page URL
1101
 *
1102
 * @since 1.8.13
1103
 *
1104
 * @param array $parameter
1105
 *
1106
 * @return string URL
1107
 */
1108
function give_import_page_url( $parameter = array() ) {
1109
	$defalut_query_arg = array(
1110
		'post_type'     => 'give_forms',
1111
		'page'          => 'give-tools',
1112
		'tab'           => 'import',
1113
		'importer-type' => 'import_donations',
1114
	);
1115
	$import_query_arg  = wp_parse_args( $parameter, $defalut_query_arg );
1116
1117
	return add_query_arg( $import_query_arg, admin_url( 'edit.php' ) );
1118
}
1119