Give_Import_Donations::save()   C
last analyzed

Complexity

Conditions 12
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 3
nop 0
dl 0
loc 32
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Donations Import Class
4
 *
5
 * This class handles donations import.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Give_Import_Donations
9
 * @copyright   Copyright (c) 2017, GiveWP
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       1.8.14
12
 */
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit; // Exit if accessed directly
16
}
17
18
if ( ! class_exists( 'Give_Import_Donations' ) ) {
19
20
	/**
21
	 * Give_Import_Donations.
22
	 *
23
	 * @since 1.8.14
24
	 */
25
	final class Give_Import_Donations {
26
27
		/**
28
		 * Importer type
29
		 *
30
		 * @since 1.8.13
31
		 * @var string
32
		 */
33
		private $importer_type = 'import_donations';
34
35
		/**
36
		 * Instance.
37
		 *
38
		 * @since
39
		 * @access private
40
		 * @var
41
		 */
42
		static private $instance;
43
44
		/**
45
		 * Importing donation per page.
46
		 *
47
		 * @since 1.8.14
48
		 *
49
		 * @var   int
50
		 */
51
		public static $per_page = 25;
52
53
		/**
54
		 * Importing donation per page.
55
		 *
56
		 * @since 2.1
57
		 *
58
		 * @var   int
59
		 */
60
		public $is_csv_valid = false;
61
62
		/**
63
		 * Singleton pattern.
64
		 *
65
		 * @since
66
		 * @access private
67
		 */
68
		private function __construct() {
69
			self::$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : self::$per_page;
70
		}
71
72
		/**
73
		 * Get instance.
74
		 *
75
		 * @since
76
		 * @access public
77
		 *
78
		 * @return static
79
		 */
80
		public static function get_instance() {
81
			if ( null === static::$instance ) {
82
				self::$instance = new static();
83
			}
84
85
			return self::$instance;
86
		}
87
88
		/**
89
		 * Setup
90
		 *
91
		 * @since 1.8.14
92
		 *
93
		 * @return void
94
		 */
95
		public function setup() {
96
			$this->setup_hooks();
97
		}
98
99
100
		/**
101
		 * Setup Hooks.
102
		 *
103
		 * @since 1.8.14
104
		 *
105
		 * @return void
106
		 */
107 View Code Duplication
		private function setup_hooks() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
			if ( ! $this->is_donations_import_page() ) {
109
				return;
110
			}
111
112
			// Do not render main import tools page.
113
			remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) );
114
115
116
			// Render donation import page
117
			add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) );
118
119
			// Print the HTML.
120
			add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 );
121
122
			// Run when form submit.
123
			add_action( 'give-tools_save_import', array( $this, 'save' ) );
124
125
			add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 );
126
127
			// Used to add submit button.
128
			add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 );
129
		}
130
131
		/**
132
		 * Update notice
133
		 *
134
		 * @since 1.8.14
135
		 *
136
		 * @param $messages
137
		 *
138
		 * @return mixed
139
		 */
140 View Code Duplication
		public function update_notices( $messages ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
141
			if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) {
142
				unset( $messages['give-setting-updated'] );
143
			}
144
145
			return $messages;
146
		}
147
148
		/**
149
		 * Print submit and nonce button.
150
		 *
151
		 * @since 1.8.14
152
		 */
153
		public function submit() {
154
			wp_nonce_field( 'give-save-settings', '_give-save-settings' );
155
			?>
156
			<input type="hidden" class="import-step" id="import-step" name="step"
157
				   value="<?php echo $this->get_step(); ?>"/>
158
			<input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/>
159
			<?php
160
		}
161
162
		/**
163
		 * Print the HTML for importer.
164
		 *
165
		 * @since 1.8.14
166
		 */
167
		public function html() {
168
			$step = $this->get_step();
169
170
			// Show progress.
171
			$this->render_progress();
172
			?>
173
			<section>
174
				<table
175
						class="widefat export-options-table give-table <?php echo "step-{$step}"; ?> <?php echo( 1 === $step && ! empty( $this->is_csv_valid ) ? 'give-hidden' : '' ); ?>  "
176
						id="<?php echo "step-{$step}"; ?>">
177
					<tbody>
178
					<?php
179
					switch ( $step ) {
180
						case 1:
181
							$this->render_media_csv();
182
							break;
183
184
						case 2:
185
							$this->render_dropdown();
186
							break;
187
188
						case 3:
189
							$this->start_import();
190
							break;
191
192
						case 4:
193
							$this->import_success();
194
					}
195
					if ( false === $this->check_for_dropdown_or_import() ) {
196
						?>
197
						<tr valign="top">
198
							<th>
199
								<input type="submit"
200
									   class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>"
201
									   id="recount-stats-submit"
202
									   value="
203
									       <?php
204
								       /**
205
								        * Filter to modify donation importer submit button text.
206
								        *
207
								        * @since 2.1
208
								        */
209
								       echo apply_filters( 'give_import_donation_submit_button_text', __( 'Submit', 'give' ) );
210
								       ?>
211
											"/>
212
							</th>
213
							<th>
214
								<?php
215
								/**
216
								 * Action to add submit button description.
217
								 *
218
								 * @since 2.1
219
								 */
220
								do_action( 'give_import_donation_submit_button' );
221
								?>
222
							</th>
223
						</tr>
224
						<?php
225
					}
226
					?>
227
					</tbody>
228
				</table>
229
			</section>
230
			<?php
231
		}
232
233
		/**
234
		 * Show success notice
235
		 *
236
		 * @since 1.8.14
237
		 */
238
		public function import_success() {
239
240
			$delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false );
241
			$csv        = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false );
242
			if ( ! empty( $delete_csv ) && ! empty( $csv ) ) {
243
				wp_delete_attachment( $csv, true );
244
			}
245
246
			$report = give_import_donation_report();
247
248
			$report_html = array(
249
				'duplicate_donor'    => array(
250
					__( '%s duplicate %s detected', 'give' ),
251
					__( '%s duplicate %s detected', 'give' ),
252
					__( 'donor', 'give' ),
253
					__( 'donors', 'give' ),
254
				),
255
				'create_donor'       => array(
256
					__( '%s %s created', 'give' ),
257
					__( '%s %s will be created', 'give' ),
258
					__( 'donor', 'give' ),
259
					__( 'donors', 'give' ),
260
				),
261
				'create_form'        => array(
262
					__( '%s donation %s created', 'give' ),
263
					__( '%s donation %s will be created', 'give' ),
264
					__( 'form', 'give' ),
265
					__( 'forms', 'give' ),
266
				),
267
				'duplicate_donation' => array(
268
					__( '%s duplicate %s detected', 'give' ),
269
					__( '%s duplicate %s detected', 'give' ),
270
					__( 'donation', 'give' ),
271
					__( 'donations', 'give' ),
272
				),
273
				'create_donation'    => array(
274
					__( '%s %s imported', 'give' ),
275
					__( '%s %s will be imported', 'give' ),
276
					__( 'donation', 'give' ),
277
					__( 'donations', 'give' ),
278
				),
279
			);
280
			$total       = (int) $_GET['total'];
281
			-- $total;
282
			$success = (bool) $_GET['success'];
283
			$dry_run = empty( $_GET['dry_run'] ) ? 0 : absint( $_GET['dry_run'] );
284
			?>
285
			<tr valign="top" class="give-import-dropdown">
286
				<th colspan="2">
287
					<h2>
288
						<?php
289
						if ( $success ) {
290
							if ( $dry_run ) {
291
								printf(
292
									_n( 'Dry run import complete! %s donation processed', 'Dry run import complete! %s donations processed', $total, 'give' ),
293
									"<strong>{$total}</strong>"
294
								);
295
							} else {
296
								printf(
297
									_n( 'Import complete! %s donation processed', 'Import complete! %s donations processed', $total, 'give' ),
298
									"<strong>{$total}</strong>"
299
								);
300
							}
301
						} else {
302
							printf(
303
								_n( 'Failed to import %s donation', 'Failed to import %s donations', $total, 'give' ),
304
								"<strong>{$total}</strong>"
305
							);
306
						}
307
						?>
308
					</h2>
309
310
					<?php
311
					$text      = __( 'Import Donation', 'give' );
312
					$query_arg = array(
313
						'post_type' => 'give_forms',
314
						'page'      => 'give-tools',
315
						'tab'       => 'import',
316
					);
317
					if ( $success ) {
318
319
320
						if ( $dry_run ) {
321
							$query_arg = array(
322
								'post_type'     => 'give_forms',
323
								'page'          => 'give-tools',
324
								'tab'           => 'import',
325
								'importer-type' => 'import_donations',
326
							);
327
328
							$text = __( 'Start Import', 'give' );
329
						} else {
330
							$query_arg = array(
331
								'post_type' => 'give_forms',
332
								'page'      => 'give-payment-history',
333
							);
334
							$text      = __( 'View Donations', 'give' );
335
						}
336
					}
337
338
					foreach ( $report as $key => $value ) {
339
						if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) {
340
							$key_name = $report_html[ $key ][2];
341
							if ( $value > 1 ) {
342
								$key_name = $report_html[ $key ][3];
343
							}
344
							?>
345
							<p>
346
								<?php printf( $report_html[ $key ][ $dry_run ], $value, $key_name ); ?>
347
							</p>
348
							<?php
349
						}
350
					}
351
					?>
352
353
					<p>
354
						<a class="button button-large button-secondary"
355
						   href="<?php echo add_query_arg( $query_arg, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a>
356
					</p>
357
				</th>
358
			</tr>
359
			<?php
360
		}
361
362
		/**
363
		 * Will start Import
364
		 *
365
		 * @since 1.8.14
366
		 */
367
		public function start_import() {
368
			// Reset the donation form report.
369
			give_import_donation_report_reset();
370
371
			$csv         = absint( $_REQUEST['csv'] );
372
			$delimiter   = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
373
			$index_start = 1;
374
			$next        = true;
375
			$total       = self::get_csv_total( $csv );
376
			if ( self::$per_page < $total ) {
377
				$total_ajax = ceil( $total / self::$per_page );
378
				$index_end  = self::$per_page;
379
			} else {
380
				$total_ajax = 1;
381
				$index_end  = $total;
382
				$next       = false;
383
			}
384
			$current_percentage = 100 / ( $total_ajax + 1 );
385
386
			?>
387
			<tr valign="top" class="give-import-dropdown">
388
				<th colspan="2">
389
					<h2 id="give-import-title"><?php _e( 'Importing', 'give' ) ?></h2>
390
					<p class="give-field-description"><?php _e( 'Your donations are now being imported...', 'give' ) ?></p>
391
				</th>
392
			</tr>
393
394
			<tr valign="top" class="give-import-dropdown">
395
				<th colspan="2">
396
					<span class="spinner is-active"></span>
397
					<div class="give-progress"
398
						 data-current="1"
399
						 data-total_ajax="<?php echo absint( $total_ajax ); ?>"
400
						 data-start="<?php echo absint( $index_start ); ?>"
401
						 data-end="<?php echo absint( $index_end ); ?>"
402
						 data-next="<?php echo absint( $next ); ?>"
403
						 data-total="<?php echo absint( $total ); ?>"
404
						 data-per_page="<?php echo absint( self::$per_page ); ?>">
405
406
						<div style="width: <?php echo (float) $current_percentage; ?>%"></div>
407
					</div>
408
					<input type="hidden" value="3" name="step">
409
					<input type="hidden" value='<?php echo esc_attr( maybe_serialize( $_REQUEST['mapto'] ) ); ?>' name="mapto" class="mapto">
410
					<input type="hidden" value="<?php echo $csv; ?>" name="csv" class="csv">
411
					<input type="hidden" value="<?php echo esc_attr( $_REQUEST['mode'] ); ?>" name="mode" class="mode">
412
					<input type="hidden" value="<?php echo esc_attr( $_REQUEST['create_user'] ); ?>" name="create_user" class="create_user">
413
					<input type="hidden" value="<?php echo esc_attr( $_REQUEST['delete_csv'] ); ?>" name="delete_csv" class="delete_csv">
414
					<input type="hidden" value="<?php echo esc_attr( $delimiter ); ?>" name="delimiter">
415
					<input type="hidden" value="<?php echo absint( $_REQUEST['dry_run'] ); ?>" name="dry_run">
416
					<input type="hidden" value='<?php echo esc_attr( maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ) ); ?>' name="main_key" class="main_key">
0 ignored issues
show
Bug introduced by
It seems like $delimiter defined by !empty($_REQUEST['delimi...T['delimiter']) : 'csv' on line 372 can also be of type array; however, Give_Import_Donations::get_importer() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
417
				</th>
418
			</tr>
419
			<?php
420
		}
421
422
		/**
423
		 * Will return true if importing can be started or not else false.
424
		 *
425
		 * @since 1.8.14
426
		 */
427
		public function check_for_dropdown_or_import() {
428
			$return = true;
429
			if ( isset( $_REQUEST['mapto'] ) ) {
430
				$mapto = (array) $_REQUEST['mapto'];
431
				if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) {
432
					Give_Admin_Settings::add_error( 'give-import-csv-form', __( 'In order to import donations, a column must be mapped to either the "Donation Form Title" or "Donation Form ID" field. Please map a column to one of those fields.', 'give' ) );
433
					$return = false;
434
				}
435
436
				if ( false === in_array( 'amount', $mapto ) ) {
437
					Give_Admin_Settings::add_error( 'give-import-csv-amount', __( 'In order to import donations, a column must be mapped to the "Amount" field. Please map a column to that field.', 'give' ) );
438
					$return = false;
439
				}
440
441
				if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) {
442
					Give_Admin_Settings::add_error( 'give-import-csv-donor', __( 'In order to import donations, a column must be mapped to either the "Donor Email" or "Donor ID" field. Please map a column to that field.', 'give' ) );
443
					$return = false;
444
				}
445
			} else {
446
				$return = false;
447
			}
448
449
			return $return;
450
		}
451
452
		/**
453
		 * Print the Dropdown option for CSV.
454
		 *
455
		 * @since 1.8.14
456
		 */
457
		public function render_dropdown() {
458
			$csv       = (int) $_GET['csv'];
459
			$delimiter = ( ! empty( $_GET['delimiter'] ) ? give_clean( $_GET['delimiter'] ) : 'csv' );
460
461
			// TO check if the CSV files that is being add is valid or not if not then redirect to first step again
462
			if ( ! $this->is_valid_csv( $csv ) ) {
463
				$url = give_import_page_url();
464
				?>
465
				<input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo $url; ?>"/>
466
				<?php
467
			} else {
468
				?>
469
				<tr valign="top" class="give-import-dropdown">
470
					<th colspan="2">
471
						<h2 id="give-import-title"><?php _e( 'Map CSV fields to donations', 'give' ) ?></h2>
472
473
						<p class="give-import-donation-required-fields-title"><?php _e( 'Required Fields' ); ?></p>
474
475
						<p class="give-field-description"><?php _e( 'These fields are required for the import to submitted' ); ?></p>
476
477
						<ul class="give-import-donation-required-fields">
478
							<li class="give-import-donation-required-email"
479
								title="Please configure all required fields to start the import process.">
480
								<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
481
								<span class="give-import-donation-required-text">
482
									<?php
483
									_e( 'Email Address', 'give' );
484
									?>
485
								</span>
486
							</li>
487
488
							<li class="give-import-donation-required-first"
489
								title="Please configure all required fields to start the import process.">
490
								<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
491
								<span class="give-import-donation-required-text">
492
									<?php
493
									_e( 'First Name', 'give' );
494
									?>
495
								</span>
496
							</li>
497
498
							<li class="give-import-donation-required-amount"
499
								title="Please configure all required fields to start the import process.">
500
								<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
501
								<span class="give-import-donation-required-text">
502
									<?php
503
									_e( 'Donation Amount', 'give' );
504
									?>
505
								</span>
506
							</li>
507
508
							<li class="give-import-donation-required-form"
509
								title="Please configure all required fields to start the import process.">
510
								<span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span>
511
								<span class="give-import-donation-required-text">
512
									<?php
513
									_e( 'Form Title or ID', 'give' );
514
									?>
515
								</span>
516
							</li>
517
						</ul>
518
519
						<p class="give-field-description"><?php _e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p>
520
					</th>
521
				</tr>
522
523
				<tr valign="top" class="give-import-dropdown">
524
					<th><b><?php _e( 'Column name', 'give' ); ?></b></th>
525
					<th><b><?php _e( 'Map to field', 'give' ); ?></b></th>
526
				</tr>
527
528
				<?php
529
				$raw_key = $this->get_importer( $csv, 0, $delimiter );
0 ignored issues
show
Bug introduced by
It seems like $delimiter defined by !empty($_GET['delimiter'...T['delimiter']) : 'csv' on line 459 can also be of type array; however, Give_Import_Donations::get_importer() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
530
				$mapto   = (array) ( isset( $_REQUEST['mapto'] ) ? $_REQUEST['mapto'] : array() );
531
532
				foreach ( $raw_key as $index => $value ) {
0 ignored issues
show
Bug introduced by
The expression $raw_key of type array<integer,string|nul...":"string|null"}>|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
533
					?>
534
					<tr valign="middle" class="give-import-option">
535
						<th><?php echo $value; ?></th>
536
						<th>
537
							<?php
538
							$this->get_columns( $index, $value, $mapto );
539
							?>
540
						</th>
541
					</tr>
542
					<?php
543
				}
544
			}
545
		}
546
547
		/**
548
		 * @param $option_value
549
		 * @param $value
550
		 *
551
		 * @return string
552
		 */
553
		public function selected( $option_value, $value ) {
554
			$option_value = strtolower( $option_value );
555
			$value        = strtolower( $value );
556
557
			$selected = '';
558
			if ( stristr( $value, $option_value ) ) {
559
				$selected = 'selected';
560
			} elseif ( strrpos( $value, 'give_' ) && stristr( $option_value, __( 'Import as Meta', 'give' ) ) ) {
561
				$selected = 'selected';
562
			}
563
564
			return $selected;
565
		}
566
567
		/**
568
		 * Print the columns from the CSV.
569
		 *
570
		 * @since  1.8.14
571
		 * @access private
572
		 *
573
		 * @param string $index
574
		 * @param bool   $value
575
		 * @param array  $mapto
576
		 *
577
		 * @return void
578
		 */
579
		private function get_columns( $index, $value = false, $mapto = array() ) {
580
			$default       = give_import_default_options();
581
			$current_mapto = (string) ( ! empty( $mapto[ $index ] ) ? $mapto[ $index ] : '' );
582
			?>
583
			<select name="mapto[<?php echo $index; ?>]">
584
				<?php $this->get_dropdown_option_html( $default, $current_mapto, $value ); ?>
585
586
				<optgroup label="<?php _e( 'Donations', 'give' ); ?>">
587
					<?php
588
					$this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value );
589
					?>
590
				</optgroup>
591
592
				<optgroup label="<?php _e( 'Donors', 'give' ); ?>">
593
					<?php
594
					$this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value );
595
					?>
596
				</optgroup>
597
598
				<optgroup label="<?php _e( 'Forms', 'give' ); ?>">
599
					<?php
600
					$this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value );
601
					?>
602
				</optgroup>
603
604
				<?php
605
				/**
606
				 * Fire the action
607
				 * You can use this filter to add new options.
608
				 *
609
				 * @since 1.8.15
610
				 */
611
				do_action( 'give_import_dropdown_option', $index, $value, $mapto, $current_mapto );
612
				?>
613
			</select>
614
			<?php
615
		}
616
617
		/**
618
		 * Print the option html for select in importer
619
		 *
620
		 * @since  1.8.15
621
		 * @access public
622
		 *
623
		 * @param  array $options
624
		 * @param  string $current_mapto
625
		 * @param bool $value
626
		 *
627
		 * @return void
628
		 */
629
		public function get_dropdown_option_html( $options, $current_mapto, $value = false ) {
630
631
			foreach ( $options as $option => $option_value ) {
632
				$ignore = array();
633
				if ( isset( $option_value['ignore'] ) && is_array( $option_value['ignore'] ) ) {
634
					$ignore = $option_value['ignore'];
635
					unset( $option_value['ignore'] );
636
				}
637
638
				$option_value_texts = (array) $option_value;
639
				$option_text        = $option_value_texts[0];
640
641
				$checked = ( ( $current_mapto === $option ) ? 'selected' : false );
642
				if ( empty( $checked ) && ! in_array( $value, $ignore ) ) {
643
					foreach ( $option_value_texts as $option_value_text ) {
644
						$checked = $this->selected( $option_value_text, $value );
645
						if ( $checked ) {
646
							break;
647
						}
648
					}
649
				}
650
651
				echo sprintf(
652
					'<option value="%1$s" %2$s >%3$s</option>',
653
					$option,
654
					$checked,
655
					$option_text
656
				);
657
			}
658
		}
659
660
		/**
661
		 * Get column count of csv file.
662
		 *
663
		 * @since 1.8.14
664
		 *
665
		 * @param $file_id
666
		 *
667
		 * @return bool|int
668
		 */
669
		public function get_csv_total( $file_id ) {
670
			$total = false;
671
			if ( $file_id ) {
672
				$file_dir = get_attached_file( $file_id );
673
				if ( $file_dir ) {
674
					$total = $this->get_csv_data_from_file_dir( $file_dir );
675
				}
676
			}
677
678
			return $total;
679
		}
680
681
		/**
682
		 * Get data from File
683
		 *
684
		 * @since 2.1
685
		 *
686
		 * @param $file_dir
687
		 *
688
		 * @return bool|int
689
		 */
690
		public function get_csv_data_from_file_dir( $file_dir ) {
691
			$total = false;
692
			if ( $file_dir ) {
693
				$file = new SplFileObject( $file_dir, 'r' );
694
				$file->seek( PHP_INT_MAX );
695
				$total = $file->key() + 1;
696
			}
697
698
			return $total;
699
		}
700
701
		/**
702
		 * Get the CSV fields title from the CSV.
703
		 *
704
		 * @since 1.8.14
705
		 *
706
		 * @param (int) $file_id
707
		 * @param int    $index
708
		 * @param string $delimiter
709
		 *
710
		 * @return array|bool $raw_data title of the CSV file fields
711
		 */
712
		public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) {
713
			/**
714
			 * Filter to modify delimiter of Import.
715
			 *
716
			 * @since 1.8.14
717
			 *
718
			 * Return string $delimiter.
719
			 */
720
			$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
721
722
			$raw_data = false;
723
			$file_dir = get_attached_file( $file_id );
724
			if ( $file_dir ) {
725
				if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
726
					$raw_data = fgetcsv( $handle, $index, $delimiter );
727
					// Remove BOM signature from the first item.
728
					if ( isset( $raw_data[0] ) ) {
729
						$raw_data[0] = $this->remove_utf8_bom( $raw_data[0] );
730
					}
731
				}
732
			}
733
734
			return $raw_data;
735
		}
736
737
		/**
738
		 * Remove UTF-8 BOM signature.
739
		 *
740
		 * @since 1.8.14
741
		 *
742
		 * @param  string $string String to handle.
743
		 *
744
		 * @return string
745
		 */
746
		public function remove_utf8_bom( $string ) {
747
			if ( 'efbbbf' === substr( bin2hex( $string ), 0, 6 ) ) {
748
				$string = substr( $string, 3 );
749
			}
750
751
			return $string;
752
		}
753
754
		/**
755
		 * Is used to show the process when user upload the donor form.
756
		 *
757
		 * @since 1.8.14
758
		 */
759
		public function render_progress() {
760
			$step = $this->get_step();
761
			?>
762
			<ol class="give-progress-steps">
763
				<li class="<?php echo( 1 === $step ? 'active' : '' ); ?>">
764
					<?php _e( 'Upload CSV file', 'give' ); ?>
765
				</li>
766
				<li class="<?php echo( 2 === $step ? 'active' : '' ); ?>">
767
					<?php _e( 'Column mapping', 'give' ); ?>
768
				</li>
769
				<li class="<?php echo( 3 === $step ? 'active' : '' ); ?>">
770
					<?php _e( 'Import', 'give' ); ?>
771
				</li>
772
				<li class="<?php echo( 4 === $step ? 'active' : '' ); ?>">
773
					<?php _e( 'Done!', 'give' ); ?>
774
				</li>
775
			</ol>
776
			<?php
777
		}
778
779
		/**
780
		 * Will return the import step.
781
		 *
782
		 * @since 1.8.14
783
		 *
784
		 * @return int $step on which step doest the import is on.
785
		 */
786 View Code Duplication
		public function get_step() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
787
			$step    = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 );
788
			$on_step = 1;
789
790
			if ( empty( $step ) || 1 === $step ) {
791
				$on_step = 1;
792
			} elseif ( $this->check_for_dropdown_or_import() ) {
793
				$on_step = 3;
794
			} elseif ( 2 === $step ) {
795
				$on_step = 2;
796
			} elseif ( 4 === $step ) {
797
				$on_step = 4;
798
			}
799
800
			return $on_step;
801
		}
802
803
		/**
804
		 * Render donations import page
805
		 *
806
		 * @since 1.8.14
807
		 */
808
		public function render_page() {
809
			include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-donations.php';
810
		}
811
812
		/**
813
		 * Print Dry Run HTML on donation import page
814
		 *
815
		 * @since 2.1
816
		 */
817
		public function give_import_donation_submit_button_render_media_csv() {
818
			$dry_run = isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 1;
819
			?>
820
			<div>
821
				<label for="dry_run">
822
					<input type="hidden" name="dry_run" value="0"/>
823
					<input type="checkbox" name="dry_run" id="dry_run" class="dry_run"
824
						   value="1" <?php checked( 1, $dry_run ); ?> >
825
					<strong><?php _e( 'Dry Run', 'give' ); ?></strong>
826
				</label>
827
				<p class="give-field-description">
828
					<?php
829
					_e( 'Preview what the import would look like without making any default changes to your site or your database.', 'give' );
830
					?>
831
				</p>
832
			</div>
833
			<?php
834
		}
835
836
		/**
837
		 * Change submit button text on first step of importing donation.
838
		 *
839
		 * @since 2.1
840
		 *
841
		 * @param $text
842
		 *
843
		 * @return string
844
		 */
845
		function give_import_donation_submit_text_render_media_csv( $text ) {
0 ignored issues
show
Unused Code introduced by
The parameter $text 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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
846
			return __( 'Begin Import', 'give' );
847
		}
848
849
		/**
850
		 * Add CSV upload HTMl
851
		 *
852
		 * Print the html of the file upload from which CSV will be uploaded.
853
		 *
854
		 * @since 1.8.14
855
		 * @return void
856
		 */
857
		public function render_media_csv() {
858
			add_filter( 'give_import_donation_submit_button_text', array(
859
				$this,
860
				'give_import_donation_submit_text_render_media_csv'
861
			) );
862
			add_action( 'give_import_donation_submit_button', array(
863
				$this,
864
				'give_import_donation_submit_button_render_media_csv'
865
			) );
866
			?>
867
			<tr valign="top">
868
				<th colspan="2">
869
					<h2 id="give-import-title"><?php _e( 'Import donations from a CSV file', 'give' ) ?></h2>
870
					<p class="give-field-description"><?php _e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ) ?></p>
871
				</th>
872
			</tr>
873
			<?php
874
			$csv         = ( isset( $_POST['csv'] ) ? give_clean( $_POST['csv'] ) : '' );
875
			$csv_id      = ( isset( $_POST['csv_id'] ) ? give_clean( $_POST['csv_id'] ) : '' );
876
			$delimiter   = ( isset( $_POST['delimiter'] ) ? give_clean( $_POST['delimiter'] ) : 'csv' );
877
			$mode        = empty( $_POST['mode'] ) ?
878
				'disabled' :
879
				( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? 'enabled' : 'disabled' );
880
			$create_user = empty( $_POST['create_user'] ) ?
881
				'disabled' :
882
				( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? 'enabled' : 'disabled' );
883
			$delete_csv  = empty( $_POST['delete_csv'] ) ?
884
				'enabled' :
885
				( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? 'enabled' : 'disabled' );
886
887
			// Reset csv and csv_id if csv
888
			if ( empty( $csv_id ) || ! $this->is_valid_csv( $csv_id, $csv ) ) {
0 ignored issues
show
Bug introduced by
It seems like $csv defined by isset($_POST['csv']) ? g...ean($_POST['csv']) : '' on line 874 can also be of type array; however, Give_Import_Donations::is_valid_csv() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
889
				$csv_id = $csv = '';
890
			}
891
			$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page;
892
893
			$sample_file_text = sprintf(
894
				'%s <a href="%s">%s</a>.',
895
				__( 'Download the sample file', 'give' ),
896
				esc_url( GIVE_PLUGIN_URL . 'sample-data/sample-data.csv' ),
897
				__( 'here', 'give' )
898
			);
899
900
			$csv_description = sprintf(
901
				'%1$s %2$s',
902
				__( 'The file must be a Comma Seperated Version (CSV) file type only.', 'give' ),
903
				$sample_file_text
904
			);
905
906
			$settings = array(
907
				array(
908
					'id'          => 'csv',
909
					'name'        => __( 'Choose a CSV file:', 'give' ),
910
					'type'        => 'file',
911
					'attributes'  => array( 'editing' => 'false', 'library' => 'text' ),
912
					'description' => $csv_description,
913
					'fvalue'      => 'url',
914
					'default'     => $csv,
915
				),
916
				array(
917
					'id'    => 'csv_id',
918
					'type'  => 'hidden',
919
					'value' => $csv_id,
920
				),
921
				array(
922
					'id'          => 'delimiter',
923
					'name'        => __( 'CSV Delimiter:', 'give' ),
924
					'description' => __( 'In case your CSV file supports a different type of separator (or delimiter) -- like a tab or space -- you can set that here.', 'give' ),
925
					'default'     => $delimiter,
926
					'type'        => 'select',
927
					'options'     => array(
928
						'csv'                  => __( 'Comma', 'give' ),
929
						'tab-separated-values' => __( 'Tab', 'give' ),
930
					),
931
				),
932
				array(
933
					'id'          => 'mode',
934
					'name'        => __( 'Test Mode:', 'give' ),
935
					'description' => __( 'Select whether you would like these donations to be marked as "test" donations within the database. By default, they will be marked as live donations.', 'give' ),
936
					'default'     => $mode,
937
					'type'        => 'radio_inline',
938
					'options'     => array(
939
						'enabled'  => __( 'Enabled', 'give' ),
940
						'disabled' => __( 'Disabled', 'give' ),
941
					),
942
				),
943
				array(
944
					'id'          => 'create_user',
945
					'name'        => __( 'Create WP users for new donors:', 'give' ),
946
					'description' => __( 'The importer can create WordPress user accounts based on the names and email addresses of the donations in your CSV file. Enable this option if you\'d like the importer to do that.', 'give' ),
947
					'default'     => $create_user,
948
					'type'        => 'radio_inline',
949
					'options'     => array(
950
						'enabled'  => __( 'Enabled', 'give' ),
951
						'disabled' => __( 'Disabled', 'give' ),
952
					),
953
				),
954
				array(
955
					'id'          => 'delete_csv',
956
					'name'        => __( 'Delete CSV after import:', 'give' ),
957
					'description' => __( 'Your CSV file will be uploaded via the WordPress Media Library. It\'s a good idea to delete it after the import is finished so that your sensitive data is not accessible on the web. Disable this only if you plan to delete the file manually later.', 'give' ),
958
					'default'     => $delete_csv,
959
					'type'        => 'radio_inline',
960
					'options'     => array(
961
						'enabled'  => __( 'Enabled', 'give' ),
962
						'disabled' => __( 'Disabled', 'give' ),
963
					),
964
				),
965
				array(
966
					'id'          => 'per_page',
967
					'name'        => __( 'Process Rows Per Batch:', 'give' ),
968
					'type'        => 'number',
969
					'description' => __( 'Determine how many rows you would like to import per cycle.', 'give' ),
970
					'default'     => $per_page,
971
					'class'       => 'give-text-small',
972
				),
973
			);
974
975
			$settings = apply_filters( 'give_import_file_upload_html', $settings );
976
977
			if ( empty( $this->is_csv_valid ) ) {
978
				Give_Admin_Settings::output_fields( $settings, 'give_settings' );
979
			} else {
980
				?>
981
				<input type="hidden" name="is_csv_valid" class="is_csv_valid"
982
					   value="<?php echo $this->is_csv_valid; ?>">
983
				<?php
984
			}
985
		}
986
987
		/**
988
		 * Run when user click on the submit button.
989
		 *
990
		 * @since 1.8.14
991
		 */
992
		public function save() {
993
			// Get the current step.
994
			$step = $this->get_step();
995
996
			// Validation for first step.
997
			if ( 1 === $step ) {
998
				$csv_id = absint( $_POST['csv_id'] );
999
1000
				if ( $this->is_valid_csv( $csv_id, esc_url( $_POST['csv'] ) ) ) {
1001
1002
					$url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array(
1003
						'step'          => '2',
1004
						'importer-type' => $this->importer_type,
1005
						'csv'           => $csv_id,
1006
						'delimiter'     => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv',
1007
						'mode'          => empty( $_POST['mode'] ) ?
1008
							'0' :
1009
							( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ),
1010
						'create_user'   => empty( $_POST['create_user'] ) ?
1011
							'0' :
1012
							( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ),
1013
						'delete_csv'    => empty( $_POST['delete_csv'] ) ?
1014
							'1' :
1015
							( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ),
1016
						'per_page'      => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page,
1017
						'dry_run'       => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 0,
1018
					) ) );
1019
1020
					$this->is_csv_valid = $url;
0 ignored issues
show
Documentation Bug introduced by
The property $is_csv_valid was declared of type integer, but $url is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1021
				}
1022
			}
1023
		}
1024
1025
		/**
1026
		 * Check if user uploaded csv is valid or not.
1027
		 *
1028
		 * @since  1.8.14
1029
		 * @access public
1030
		 *
1031
		 * @param mixed  $csv       ID of the CSV files.
1032
		 * @param string $match_url ID of the CSV files.
1033
		 *
1034
		 * @return bool $has_error CSV is valid or not.
1035
		 */
1036
		private function is_valid_csv( $csv = false, $match_url = '' ) {
1037
			$is_valid_csv = true;
1038
1039
			if ( $csv ) {
1040
				$csv_url = wp_get_attachment_url( $csv );
1041
1042
				$delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
1043
1044
				if (
1045
					! $csv_url ||
1046
					( ! empty( $match_url ) && ( $csv_url !== $match_url ) ) ||
1047
					( ( $mime_type = get_post_mime_type( $csv ) ) && ! strpos( $mime_type, $delimiter ) )
1048
				) {
1049
					$is_valid_csv = false;
1050
					Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
1051
				}
1052
			} else {
1053
				$is_valid_csv = false;
1054
				Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
1055
			}
1056
1057
			return $is_valid_csv;
1058
		}
1059
1060
1061
		/**
1062
		 * Render report import field
1063
		 *
1064
		 * @since  1.8.14
1065
		 * @access public
1066
		 *
1067
		 * @param $field
1068
		 * @param $option_value
1069
		 */
1070
		public function render_import_field( $field, $option_value ) {
1071
			include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-imports.php';
1072
		}
1073
1074
		/**
1075
		 * Get if current page import donations page or not
1076
		 *
1077
		 * @since 1.8.14
1078
		 * @return bool
1079
		 */
1080
		private function is_donations_import_page() {
1081
			return 'import' === give_get_current_setting_tab() &&
1082
			       isset( $_GET['importer-type'] ) &&
1083
			       $this->importer_type === give_clean( $_GET['importer-type'] );
1084
		}
1085
	}
1086
1087
	Give_Import_Donations::get_instance()->setup();
1088
}
1089