Test Failed
Push — issues/2114 ( 447fcd )
by Ravinder
06:06
created

import-functions.php ➔ give_import_donations_options()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
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 );
34
}
35
36
37
/**
38
 * Delete the Import report of the donations
39
 *
40
 * @since 1.8.13
41
 */
42
function give_import_donation_report_reset() {
43
	update_option( 'give_import_donation_report', array() );
44
}
45
46
/**
47
 * Give get form data from csv if not then create and form and return the form value.
48
 *
49
 * @since 1.8.13.
50
 *
51
 * @param $data .
52
 *
53
 * @return array|bool|Give_Donate_Form|int|null|WP_Post
54
 */
55
function give_import_get_form_data_from_csv( $data, $import_setting = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $import_setting 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...
56
	$new_form = 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
Duplication introduced by
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
			$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
70
			$form                     = false;
71
		}
72
	}
73
74
	if ( false === $form && ! empty( $data['form_title'] ) ) {
75
		$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' );
76
77
		if ( ! empty( $form->ID ) ) {
78
79
			$report['duplicate_form'] = ( ! empty( $report['duplicate_form'] ) ? ( absint( $report['duplicate_form'] ) + 1 ) : 1 );
80
81
			$form = new Give_Donate_Form( $form->ID );
0 ignored issues
show
Unused Code introduced by
$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...
82
		} else {
83
			$form                  = new Give_Donate_Form();
84
			$args                  = array(
85
				'post_title'  => $data['form_title'],
86
				'post_status' => 'publish',
87
			);
88
			$form                  = $form->create( $args );
0 ignored issues
show
Unused Code introduced by
$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...
89
			$report['create_form'] = ( ! empty( $report['create_form'] ) ? ( absint( $report['create_form'] ) + 1 ) : 1 );
90
			$new_form              = true;
91
92
		}
93
94
		$form = get_page_by_title( $data['form_title'], OBJECT, 'give_forms' );
95
		$form = new Give_Donate_Form( $form->ID );
96
	}
97
98
	if ( ! empty( $form ) && $form->get_ID() ) {
99
		if ( ! empty( $data['form_level'] ) && 'custom' != (string) strtolower( $data['form_level'] ) ) {
100
			$prices     = (array) $form->get_prices();
101
			$price_text = array();
102 View Code Duplication
			foreach ( $prices as $key => $price ) {
0 ignored issues
show
Duplication introduced by
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...
103
				if ( isset( $price['_give_id']['level_id'] ) ) {
104
					$price_text[ $price['_give_id']['level_id'] ] = ( ! empty( $price['_give_text'] ) ? $price['_give_text'] : '' );
105
				}
106
			}
107
108
			if ( ! in_array( $data['form_level'], $price_text ) ) {
109
110
				// For generating unquiet level id.
111
				$count     = 1;
112
				$new_level = count( $prices ) + $count;
113
				while ( array_key_exists( $new_level, $price_text ) ) {
114
					$count ++;
115
					$new_level = count( $prices ) + $count;
116
				}
117
118
				$multi_level_donations = array(
119
					array(
120
						'_give_id'     => array(
121
							'level_id' => $new_level,
122
						),
123
						'_give_amount' => give_sanitize_amount_for_db( $data['amount'] ),
124
						'_give_text'   => $data['form_level'],
125
					),
126
				);
127
128
				$price_text[ $new_level ] = $data['form_level'];
129
130
				if ( ! empty( $prices ) && is_array( $prices ) && ! empty( $prices[0] ) ) {
131
					$prices = wp_parse_args( $multi_level_donations, $prices );
132
				} else {
133
					$multi_level_donations[0]['_give_default'] = 'default';
134
					$prices                                    = $multi_level_donations;
135
				}
136
			}
137
			$form->price_id = array_search( $data['form_level'], $price_text );
138
139
			$donation_levels_amounts = wp_list_pluck( $prices, '_give_amount' );
140
			$min_amount              = min( $donation_levels_amounts );
141
			$max_amount              = max( $donation_levels_amounts );
142
143
			$meta = array(
144
				'_give_levels_minimum_amount' => $min_amount,
145
				'_give_levels_maximum_amount' => $max_amount,
146
				'_give_price_option'          => 'multi',
147
				'_give_donation_levels'       => array_values( $prices ),
148
			);
149
		} else {
150
			$form->price_id = 'custom';
151
		}
152
153
		$defaults = array(
154
			'_give_set_price'    => give_sanitize_amount_for_db( $data['amount'] ),
155
			'_give_price_option' => 'set',
156
		);
157
158
		// If new form is created.
159
		if ( ! empty( $new_form ) ) {
160
			$new_form = array(
161
				'_give_custom_amount_text' => ( ! empty( $data['form_custom_amount_text'] ) ? $data['form_custom_amount_text'] : 'Custom' ),
162
				'_give_logged_in_only'     => 'enabled',
163
				'_give_custom_amount'      => 'enabled',
164
				'_give_payment_import'     => true,
165
				'_give_display_style'      => 'radios',
166
				'_give_payment_display'    => 'onpage',
167
				'give_product_notes'       => 'Donation Notes',
168
				'_give_product_type'       => 'default',
169
				'_give_default_gateway'    => 'global',
170
				'_give_show_register_form' => 'both',
171
			);
172
			$defaults = wp_parse_args( $defaults, $new_form );
173
		}
174
175
		$meta = wp_parse_args( $meta, $defaults );
176
177
		foreach ( $meta as $key => $value ) {
178
			give_update_meta( $form->get_ID(), $key, $value );
179
		}
180
	}
181
182
	// update the report
183
	give_import_donation_report_update( $report );
184
185
	return $form;
186
}
187
188
/**
189
 * Give get user details if not then create a user. Used in Import Donation CSV.
190
 *
191
 * @since 1.8.13
192
 *
193
 * @param $data
194
 *
195
 * @return bool|false|WP_User
196
 */
197
function give_import_get_user_from_csv( $data, $import_setting = array() ) {
198
	$report      = give_import_donation_report();
199
	$donor_data  = false;
200
	$customer_id = false;
0 ignored issues
show
Unused Code introduced by
$customer_id 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...
201
202
	// check if donor id is not empty
203
	if ( ! empty( $data['donor_id'] ) ) {
204
		$donor_data = new Give_Donor( (int) $data['donor_id'] );
0 ignored issues
show
Documentation introduced by
(int) $data['donor_id'] is of type integer, but the function expects a boolean.

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...
205 View Code Duplication
		if ( ! empty( $donor_data->id ) ) {
0 ignored issues
show
Duplication introduced by
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...
206
			$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
207
		}
208
	}
209
210
	if ( empty( $donor_data->id ) && ! empty( $data['user_id'] ) ) {
211
		$user_id    = (int) $data['user_id'];
212
		$donor_data = new Give_Donor( $user_id, true );
0 ignored issues
show
Documentation introduced by
$user_id is of type integer, but the function expects a boolean.

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...
213
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
214
215
		if ( empty( $donor_data->id ) ) {
216
			$donor_data = get_user_by( 'id', $user_id );
217
			if ( ! empty( $donor_data->ID ) ) {
218
				$first_name = ( ! empty( $data['first_name'] ) ? $data['first_name'] : $donor_data->user_nicename );
219
				$last_name  = ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ( $lastname = get_user_meta( $donor_data->ID, 'last_name', true ) ) ? $lastname : '' ) );
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
220
				$name       = $first_name . ' ' . $last_name;
221
				$user_email = $donor_data->user_email;
222
				$donor_args = array(
223
					'name'    => $name,
224
					'email'   => $user_email,
225
					'user_id' => $user_id,
226
				);
227
228
				$donor_data = new Give_Donor();
229
				$donor_data->create( $donor_args );
230
231
				// Adding notes that donor is being imported from CSV.
232
				$current_user = wp_get_current_user();
233
				$donor_data->add_note( esc_html( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ) );
234
235
				// Add is used to ensure duplicate emails are not added
236 View Code Duplication
				if ( $user_email != $data['email'] && ! empty( $data['email'] ) ) {
0 ignored issues
show
Duplication introduced by
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...
237
					$donor_data->add_meta( 'additional_email', $data['email'] );
238
				}
239
240
				$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
241
			} else {
242
			}
243
		} else {
244
			// Add is used to ensure duplicate emails are not added
245 View Code Duplication
			if ( $donor_data->email != $data['email'] ) {
0 ignored issues
show
Duplication introduced by
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...
246
				$donor_data->add_meta( 'additional_email', ( ! empty( $data['email'] ) ? $data['email'] : $donor_data->email ) );
247
			}
248
			$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
249
		}
250
	}
251
252
	if ( empty( $donor_data->id ) && ! empty( $data['email'] ) ) {
253
254
		$donor_data = new Give_Donor( $data['email'] );
255
		if ( empty( $donor_data->id ) ) {
256
			$donor_data = get_user_by( 'email', $data['email'] );
257
258
			if ( empty( $donor_data->ID ) && ! empty( $data['first_name'] ) && ! empty( $data['last_name'] ) && isset( $import_setting['create_user'] ) && 1 === absint( $import_setting['create_user'] ) ) {
259
				$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' ) ) );
260
				$donor_args = array(
261
					'user_login'      => $data['email'],
262
					'user_email'      => $data['email'],
263
					'user_registered' => date( 'Y-m-d H:i:s' ),
264
					'user_first'      => $data['first_name'],
265
					'user_last'       => $data['last_name'],
266
					'user_pass'       => wp_generate_password( 8, true ),
267
					'role'            => $give_role,
268
				);
269
270
				/**
271
				 * Filter to modify user data before new user id register.
272
				 *
273
				 * @since 1.8.13
274
				 */
275
				$donor_args = (array) apply_filters( 'give_import_insert_user_args', $donor_args, $data, $import_setting );
276
277
				// This action was added to remove the login when using the give register function.
278
				add_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 );
279
				$customer_id = give_register_and_login_new_user( $donor_args );
280
				remove_filter( 'give_log_user_in_on_register', 'give_log_user_in_on_register_callback', 11 );
281
282
				update_user_meta( $customer_id, '_give_payment_import', true );
0 ignored issues
show
introduced by
update_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
283
				$donor_data = new Give_Donor( $customer_id, true );
0 ignored issues
show
Documentation introduced by
$customer_id is of type integer, but the function expects a boolean.

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...
284
			} else {
285
				$customer_id = ( ! empty( $donor_data->ID ) ? $donor_data->ID : false );
286
			}
287
288
			if ( ! empty( $customer_id ) || ( isset( $import_setting['create_user'] ) && 0 === absint( $import_setting['create_user'] ) ) ) {
289
				$donor_data = new Give_Donor( $customer_id, true );
290
291
				if ( empty( $donor_data->id ) ) {
292
293
					if ( ! empty( $data['form_id'] ) ) {
294
						$form = new Give_Donate_Form( $data['form_id'] );
295
					}
296
297
					$payment_title = ( isset( $data['form_title'] ) ? $data['form_title'] : ( isset( $form ) ? $form->get_name() : esc_html__( 'New Form', 'give' ) ) );
298
					$donor_args    = array(
299
						'name'  => ! is_email( $payment_title ) ? $data['first_name'] . ' ' . $data['last_name'] : '',
300
						'email' => $data['email'],
301
					);
302
303
					if ( ! empty( $customer_id ) ) {
304
						$donor_args['user_id'] = $customer_id;
305
					}
306
307
					$donor_data->create( $donor_args );
308
309
					// Adding notes that donor is being imported from CSV.
310
					$current_user = wp_get_current_user();
311
					$donor_data->add_note( esc_html( wp_sprintf( __( 'This donor was imported by %s', 'give' ), $current_user->user_email ) ) );
312
313
					$report['create_donor'] = ( ! empty( $report['create_donor'] ) ? ( absint( $report['create_donor'] ) + 1 ) : 1 );
314 View Code Duplication
				} else {
0 ignored issues
show
Duplication introduced by
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...
315
					$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
316
				}
317
			}
318 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
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...
319
			$report['duplicate_donor'] = ( ! empty( $report['duplicate_donor'] ) ? ( absint( $report['duplicate_donor'] ) + 1 ) : 1 );
320
		}
321
	}
322
323
// update the report
324
	give_import_donation_report_update( $report );
325
326
	return $donor_data;
327
}
328
329
/**
330
 * Return the option that are default options.
331
 *
332
 * @since 1.8.13
333
 */
334
function give_import_default_options() {
335
	/**
336
	 * Filter to modify defalut option in the import dropdown
337
	 *
338
	 * @since 1.8.13
339
	 *
340
	 * @return array
341
	 */
342
	return (array) apply_filters( 'give_import_default_options', array(
343
		'' => __( 'Do not import', 'give' ),
344
	) );
345
}
346
347
/**
348
 * Return the option that are related to donations.
349
 *
350
 * @since 1.8.13
351
 */
352
function give_import_donations_options() {
353
	/**
354
	 * Filter to modify donations option in the import dropdown
355
	 *
356
	 * @since 1.8.13
357
	 *
358
	 * @return array
359
	 */
360
	return (array) apply_filters( 'give_import_donations_options', array(
361
		''            => __( 'Do not import', 'give' ),
362
		'id'          => __( 'Donation ID', 'give' ),
363
		'amount'      => __( 'Donation Amount', 'give' ),
364
		'post_date'   => __( 'Donation Date', 'give' ),
365
		'first_name'  => __( 'Donor First Name', 'give' ),
366
		'last_name'   => __( 'Donor Last Name', 'give' ),
367
		'line1'       => __( 'Address 1', 'give' ),
368
		'line2'       => __( 'Address 2', 'give' ),
369
		'city'        => __( 'City', 'give' ),
370
		'state'       => __( 'State', 'give' ),
371
		'country'     => __( 'Country', 'give' ),
372
		'zip'         => __( 'Zip', 'give' ),
373
		'email'       => __( 'Donor Email', 'give' ),
374
		'post_status' => __( 'Donation Status', 'give' ),
375
		'gateway'     => __( 'Payment Method', 'give' ),
376
		'notes'       => __( 'Notes', 'give' ),
377
		'mode'        => __( 'Test Mode', 'give' ),
378
		'post_meta'   => __( 'Import as Meta', 'give' ),
379
	) );
380
}
381
382
/**
383
 * Return the option that are related to donations.
384
 *
385
 * @since 1.8.13
386
 */
387
function give_import_donor_options() {
388
	/**
389
	 * Filter to modify donors option in the import dropdown
390
	 *
391
	 * @since 1.8.13
392
	 *
393
	 * @return array
394
	 */
395
	return (array) apply_filters( 'give_import_donor_options', array(
396
		'donor_id' => __( 'Donor ID', 'give' ),
397
		'user_id'  => __( 'User ID', 'give' ),
398
	) );
399
}
400
401
/**
402
 * Return the option that are related to donations.
403
 *
404
 * @since 1.8.13
405
 */
406
function give_import_donation_form_options() {
407
	/**
408
	 * Filter to modify form option in the import dropdown
409
	 *
410
	 * @since 1.8.13
411
	 *
412
	 * @return array
413
	 */
414
	return (array) apply_filters( 'give_import_donation_form_options', array(
415
		'form_id'                 => __( 'Donation Form ID', 'give' ),
416
		'form_title'              => __( 'Donation Form', 'give' ),
417
		'form_level'              => __( 'Donation Level', 'give' ),
418
		'form_custom_amount_text' => __( 'Custom Amount Text', 'give' ),
419
	) );
420
}
421
422
/**
423
 * Import CSV in DB
424
 *
425
 * @param int $file_id CSV id
426
 * @param array $mapto Map csv to meta key.
0 ignored issues
show
Bug introduced by
There is no parameter named $mapto. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
427
 * @param int $start Start from which csv line.
428
 * @param int $end End from which csv line.
429
 */
430
function give_get_donation_data_from_csv( $file_id, $start, $end, $delimiter = ',' ) {
431
	$raw_data = array();
432
	$file_dir = get_attached_file( $file_id );
433
	$count    = 0;
434
	if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
435
		while ( false !== ( $row = fgetcsv( $handle, 0, $delimiter ) ) ) {
436
			if ( $count >= $start && $count <= $end ) {
437
				$raw_data[] = $row;
438
			}
439
			$count ++;
440
		}
441
		fclose( $handle );
442
	}
443
444
	return $raw_data;
445
}
446
447
448
/**
449
 * Remove login when user register with give functions.
450
 *
451
 * @since 1.8.13
452
 *
453
 * @param $value
454
 *
455
 * @return bool
456
 */
457
function give_log_user_in_on_register_callback( $value ) {
0 ignored issues
show
Unused Code introduced by
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...
458
	return false;
459
}
460
461
/**
462
 * Add import Donation forms, donations , donor from CSV to database
463
 *
464
 * @since 1.8.13
465
 *
466
 * @param array $raw_key Setup bu user at step 2.
467
 * @param array $row_data Feilds that are being imported from CSV
468
 * @param array $main_key First row from the CSV
469
 * @param array $import_setting Contain the global variable.
470
 */
471
function give_save_import_donation_to_db( $raw_key, $row_data, $main_key = array(), $import_setting = array() ) {
472
	$data                          = array_combine( $raw_key, $row_data );
473
	$price_id                      = false;
0 ignored issues
show
Unused Code introduced by
$price_id 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...
474
	$customer_id                   = 0;
475
	$import_setting['create_user'] = ( isset( $import_setting['create_user'] ) ? $import_setting['create_user'] : 1 );
476
477
	$data = (array) apply_filters( 'give_save_import_donation_to_db', $data );
478
479
	if ( ! strpos( $data['amount'], '.' ) ) {
480
		$data['amount'] = $data['amount'] . '.00';
481
	}
482
483
	// Here come the login function.
484
	$donor_data = give_import_get_user_from_csv( $data, $import_setting );
485
	if ( ! empty( $donor_data->id ) ) {
486
		if ( ! empty( $donor_data->user_id ) ) {
487
			$customer_id = $donor_data->user_id;
488
		} elseif ( ! empty( $data['user_id'] ) ) {
489
			$customer_id = $data['user_id'];
490
		}
491
	} else {
492
		return false;
493
	}
494
495
	// get form data or register a form data.
496
	$form = give_import_get_form_data_from_csv( $data, $import_setting );
497
	if ( false == $form ) {
498
		return false;
499
	} else {
500
		$price_id = ( ! empty( $form->price_id ) ) ? $form->price_id : false;
501
	}
502
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
503
504
	$address = array(
505
		'line1'   => ( ! empty( $data['line1'] ) ? give_clean( $data['line1'] ) : '' ),
506
		'line2'   => ( ! empty( $data['line1'] ) ? give_clean( $data['line2'] ) : '' ),
507
		'city'    => ( ! empty( $data['line1'] ) ? give_clean( $data['city'] ) : '' ),
508
		'zip'     => ( ! empty( $data['zip'] ) ? give_clean( $data['zip'] ) : '' ),
509
		'state'   => ( ! empty( $data['state'] ) ? give_clean( $data['state'] ) : '' ),
510
		'country' => ( ! empty( $data['country'] ) ? ( ( $country_code = array_search( $data['country'], give_get_country_list() ) ) ? $country_code : $data['country'] ) : '' ),
511
	);
512
513
	//Create payment_data array
514
	$payment_data = array(
515
		'donor_id'        => $donor_data->id,
516
		'price'           => $data['amount'],
517
		'status'          => ( ! empty( $data['post_status'] ) ? $data['post_status'] : 'publish' ),
518
		'currency'        => give_get_currency(),
519
		'user_info'       => array(
520
			'id'         => $customer_id,
521
			'email'      => ( ! empty( $data['email'] ) ? $data['email'] : ( isset( $donor_data->email ) ? $donor_data->email : false ) ),
522
			'first_name' => ( ! empty( $data['first_name'] ) ? $data['first_name'] : ( ! empty( $customer_id ) && ( $first_name = get_user_meta( $customer_id, 'first_name', true ) ) ? $first_name : $donor_data->name ) ),
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
523
			'last_name'  => ( ! empty( $data['last_name'] ) ? $data['last_name'] : ( ! empty( $customer_id ) && ( $last_name = get_user_meta( $customer_id, 'last_name', true ) ) ? $last_name : $donor_data->name ) ),
0 ignored issues
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
524
			'address'    => $address,
525
		),
526
		'gateway'         => ( ! empty( $data['gateway'] ) && 'offline' != strtolower( $data['gateway'] ) ? strtolower( $data['gateway'] ) : 'manual' ),
527
		'give_form_title' => ( ! empty( $data['form_title'] ) ? $data['form_title'] : $form->get_name() ),
528
		'give_form_id'    => (string) $form->get_ID(),
529
		'give_price_id'   => $price_id,
530
		'purchase_key'    => strtolower( md5( uniqid() ) ),
531
		'user_email'      => $data['email'],
532
		'post_date'       => ( ! empty( $data['post_date'] ) ? mysql2date( 'Y-m-d H:i:s', $data['post_date'] ) : current_time( 'mysql' ) ),
533
		'mode'            => ( ! empty( $data['mode'] ) ? ( 'true' == (string) $data['mode'] || 'TRUE' == (string) $data['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...
534
	);
535
536
	$payment_data = apply_filters( 'give_import_before_import_payment', $payment_data, $data, $donor_data, $form );
537
538
	// Get the report
539
	$report = give_import_donation_report();
540
541
	// Check for duplicate code.
542
	if ( true === give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) ) {
543
		$report['duplicate_donation'] = ( ! empty( $report['duplicate_donation'] ) ? ( absint( $report['duplicate_donation'] ) + 1 ) : 1 );
544
	} else {
545
		add_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1, 1 );
546
		add_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11, 2 );
547
		add_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11, 3 );
548
		add_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11, 2 );
549
		$payment = give_insert_payment( $payment_data );
550
		remove_action( 'give_update_payment_status', 'give_donation_import_insert_default_payment_note', 1 );
551
		remove_filter( 'give_insert_payment_args', 'give_donation_import_give_insert_payment_args', 11 );
552
		remove_filter( 'give_update_donor_information', 'give_donation_import_update_donor_information', 11 );
553
		remove_action( 'give_insert_payment', 'give_import_donation_insert_payment', 11 );
554
555
		if ( $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment 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...
556
557
			$report['create_donation'] = ( ! empty( $report['create_donation'] ) ? ( absint( $report['create_donation'] ) + 1 ) : 1 );
558
559
			update_post_meta( $payment, '_give_payment_import', true );
560
561
			if ( ! empty( $import_setting['csv'] ) ) {
562
				update_post_meta( $payment, '_give_payment_import_id', $import_setting['csv'] );
563
			}
564
565
			// Insert Notes.
566
			if ( ! empty( $data['notes'] ) ) {
567
				give_insert_payment_note( $payment, $data['notes'] );
568
			}
569
570
			$meta_exists = array_keys( $raw_key, 'post_meta' );
571
			if ( ! empty( $main_key ) && ! empty( $meta_exists ) ) {
572
				foreach ( $meta_exists as $meta_exist ) {
573
					if ( ! empty( $main_key[ $meta_exist ] ) && ! empty( $row_data[ $meta_exist ] ) ) {
574
						update_post_meta( $payment, $main_key[ $meta_exist ], $row_data[ $meta_exist ] );
575
					}
576
				}
577
			}
578
		} else {
579
			$report['failed_donation'] = ( ! empty( $report['failed_donation'] ) ? ( absint( $report['failed_donation'] ) + 1 ) : 1 );
580
		}
581
	}
582
583
	// update the report
584
	give_import_donation_report_update( $report );
585
}
586
587
/**
588
 * Alter donor information when importing donations from CSV
589
 *
590
 * @since 1.8.13
591
 *
592
 * @param $donor
593
 * @param $payment_id
594
 * @param $payment_data
595
 *
596
 * @return Give_Donor
597
 */
598
function give_donation_import_update_donor_information( $donor, $payment_id, $payment_data ) {
0 ignored issues
show
Unused Code introduced by
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...
599
	$old_donor = $donor;
600
	if ( ! empty( $payment_data['donor_id'] ) ) {
601
		$donor_id = absint( $payment_data['donor_id'] );
602
		$donor    = new Give_Donor( $donor_id );
603
		if ( ! empty( $donor->id ) ) {
604
			return $donor;
605
		}
606
	}
607
608
	return $old_donor;
609
}
610
611
/*
612
 * Give update purchase_count of give customer.
613
 *
614
 * @since 1.8.13
615
 */
616
function give_import_donation_insert_payment( $payment_id, $payment_data ) {
617
	// Update Give Customers purchase_count
618
	if ( ! empty( $payment_data['status'] ) && ( 'complete' === (string) $payment_data['status'] || 'publish' === (string) $payment_data['status'] ) ) {
619
		$donor_id = (int) get_post_meta( $payment_id, '_give_payment_customer_id', true );
620
		if ( ! empty( $donor_id ) ) {
621
			$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

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...
622
			$donor->increase_purchase_count();
623
		}
624
	}
625
}
626
627
/**
628
 * Add author id in in donation post
629
 *
630
 * @since 1.8.13
631
 */
632
function give_donation_import_give_insert_payment_args( $args, $payment_data ) {
633
	if ( ! empty( $payment_data['user_info']['id'] ) ) {
634
		$args['post_author'] = (int) $payment_data['user_info']['id'];
635
	}
636
637
	return $args;
638
}
639
640
/**
641
 * Check if Import donation is duplicate
642
 *
643
 * @since 1.8.13
644
 */
645
function give_check_import_donation_duplicate( $payment_data, $data, $form, $donor_data ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form 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...
Unused Code introduced by
The parameter $donor_data 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...
646
	$return = false;
647
	if ( ! empty( $data['post_date'] ) ) {
648
		$post_date = mysql2date( 'Y-m-d-H-i-s', $data['post_date'] );
649
		$post_date = explode( '-', $post_date );
650
		$args      = array(
651
			'post_type'              => 'give_payment',
652
			'cache_results'          => false,
653
			'no_found_rows'          => true,
654
			'update_post_meta_cache' => false,
655
			'update_post_term_cache' => false,
656
			'fields'                 => 'ids',
657
			'date_query'             => array(
658
				array(
659
					'year'   => $post_date[0],
660
					'month'  => $post_date[1],
661
					'day'    => $post_date[2],
662
					'hour'   => $post_date[3],
663
					'minute' => $post_date[4],
664
					'second' => $post_date[5],
665
				),
666
			),
667
			'meta_query'             => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
668
				array(
669
					'key'     => '_give_payment_total',
670
					'value'   => preg_replace( '/[\$,]/', '', $payment_data['price'] ),
671
					'compare' => 'LIKE',
672
				),
673
				array(
674
					'key'     => '_give_payment_form_id',
675
					'value'   => $payment_data['give_form_id'],
676
					'type'    => 'numeric',
677
					'compare' => '=',
678
				),
679
				array(
680
					'key'     => '_give_payment_gateway',
681
					'value'   => $payment_data['gateway'],
682
					'compare' => '=',
683
				),
684
			),
685
		);
686
687
		$payments  = new Give_Payments_Query( $args );
688
		$donations = $payments->get_payments();
689
		if ( ! empty( $donations ) ) {
690
			return true;
691
		}
692
	}
693
694
	return $return;
695
}
696
697
/**
698
 * Record payment notes that is being imported from CSV.
699
 *
700
 * @since  1.8.13
701
 *
702
 * @param  int $payment_id The ID number of the payment.
703
 *
704
 * @return void
705
 */
706
function give_donation_import_insert_default_payment_note( $payment_id ) {
707
	$current_user = wp_get_current_user();
708
	give_insert_payment_note( $payment_id, esc_html( wp_sprintf( __( 'This donation was imported by %s', 'give' ), $current_user->user_email ) ) );
709
}
710
711
/**
712
 * Return Import Page URL
713
 *
714
 * @since 1.8.13
715
 *
716
 * @param array $parameter
717
 *
718
 * @return string URL
719
 */
720
function give_import_page_url( $parameter = array() ) {
721
	$defalut_query_arg = array(
722
		'post_type' => 'give_forms',
723
		'page'      => 'give-tools',
724
		'tab'       => 'import',
725
	);
726
	$import_query_arg  = wp_parse_args( $parameter, $defalut_query_arg );
727
728
	return add_query_arg( $import_query_arg, admin_url( 'edit.php' ) );
729
}