Issues (4296)

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