PluginProbe
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards / 5.5.29
WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards v5.5.29
5.5.85 5.5.84 5.5.83 5.5.82 5.5.81 5.5.80 5.5.79 5.5.77 5.5.76 5.5.75 5.5.73 5.5.72 5.5.22 5.5.23 5.5.29 5.5.3 5.5.31 5.5.32 5.5.34 5.5.35 5.5.36 5.5.37 5.5.4 5.5.40 5.5.41 All 161 releases
wp-data-access / WPDataAccess / CSV_Files / WPDA_CSV_Import.php

WPDA_CSV_Import.php in WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards 5.5.29, at WPDataAccess/CSV_Files/WPDA_CSV_Import.php

641 lines 22.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore Standard.Category.SniffName.ErrorCode
2
3 namespace WPDataAccess\CSV_Files {
4
5 use WPDataAccess\Connection\WPDADB;
6 use WPDataAccess\Dashboard\WPDA_Dashboard;
7 use WPDataAccess\Data_Dictionary\WPDA_List_Columns_Cache;
8 use WPDataAccess\Plugin_Table_Models\WPDA_CSV_Uploads_Model;
9 use WPDataAccess\Utilities\WPDA_Message_Box;
10 use WPDataAccess\List_Table\WPDA_List_View;
11 use WPDataAccess\WPDA;
12
13 /**
14 * CSV import class
15 */
16 class WPDA_CSV_Import {
17
18 /**
19 * Data schema name
20 *
21 * @var null
22 */
23 protected $schema_name = null;
24
25 /**
26 * Page action
27 *
28 * @var null
29 */
30 protected $action = null;
31
32 /**
33 * Constructor
34 */
35 public function __construct() {
36 global $wpdb;
37 $this->schema_name = $wpdb->dbname;
38
39 $this->action =
40 isset( $_REQUEST['action'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
41 sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification
42 }
43
44 /**
45 * Show HTML page
46 *
47 * @return void
48 */
49 public function show() {
50 ?>
51 <div class="wrap">
52 <h1 class="wp-heading-inline">
53 <?php
54 if (
55 isset( $_REQUEST['action'] ) && // phpcs:ignore WordPress.Security.NonceVerification
56 ! (
57 '-1' === $_REQUEST['action'] || // phpcs:ignore WordPress.Security.NonceVerification
58 'bulk-delete' === $_REQUEST['action'] || // phpcs:ignore WordPress.Security.NonceVerification
59 'delete' === $_REQUEST['action'] // phpcs:ignore WordPress.Security.NonceVerification
60 )
61 ) {
62 ?>
63 <a
64 href="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>?page=<?php echo esc_attr( \WP_Data_Access_Admin::PAGE_MAIN ); ?>&page_action=wpda_import_csv"
65 style="display: inline-block; vertical-align: unset; margin-top: 6px;"
66 class="dashicons dashicons-arrow-left-alt2 wpda_tooltip"
67 title="Back to CSV file list"
68 ></a>
69 <?php
70 }
71 ?>
72 <?php echo __( 'Import CSV ' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
73 </h1>
74 <?php $this->show_body(); ?>
75 </div>
76 <?php
77 }
78
79 /**
80 * Show page body
81 *
82 * @return void
83 */
84 protected function show_body() {
85 if ( 'upload' === $this->action ) { // phpcs:ignore WordPress.Security.NonceVerification
86 if ( isset( $_REQUEST['action2'] ) && 'save' === $_REQUEST['action2'] ) { // phpcs:ignore WordPress.Security.NonceVerification
87 $this->upload_file();
88 } else {
89 $this->show_body_upload();
90 }
91 } elseif ( 'mapping' === $this->action ) {
92 $this->show_body_mapping();
93 } elseif ( 'import_start' === $this->action ) {
94 $this->show_body_import_start();
95 } elseif ( 'import' === $this->action ) {
96 $this->show_body_import();
97 } elseif ( 'reload' === $this->action ) {
98 $this->show_body_reload();
99 } else {
100 $this->show_body_main();
101 }
102 }
103
104 /**
105 * Show mapping page
106 *
107 * @return void
108 */
109 protected function show_body_mapping() {
110 $csv_mapping = new WPDA_CSV_Mapping();
111 $csv_mapping->show();
112 }
113
114 /**
115 * Show main page body
116 *
117 * @return void
118 */
119 protected function show_body_main() {
120 $csv_list_view = new WPDA_List_View(
121 array(
122 'page_hook_suffix' => 'CSV',
123 'table_name' => WPDA_CSV_Uploads_Model::get_base_table_name(),
124 'list_table_class' => 'WPDataAccess\\CSV_Files\\WPDA_CSV_List_Table',
125 'edit_form_class' => 'WPDataAccess\\Simple_Form\\WPDA_Simple_Form',
126 'subtitle' => '',
127 )
128 );
129 $csv_list_view->show();
130 }
131
132 /**
133 * Show upload body
134 *
135 * @return void
136 */
137 protected function show_body_upload() {
138 $csv_id =
139 isset( $_REQUEST['csv_id'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
140 sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
141 $csv_name =
142 isset( $_REQUEST['csv_name'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
143 sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
144 ?>
145 <br/>
146 <fieldset class="wpda_fieldset">
147 <legend>
148 <?php echo __( 'Select a file and click upload', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
149 </legend>
150 <form id="form_import_table"
151 method="post"
152 action="?page=wpda&page_action=wpda_import_csv"
153 enctype="multipart/form-data">
154 <label for="csv_name">
155 Import name
156 <input id="csv_name"
157 name="csv_name"
158 type="text"
159 value="<?php echo esc_attr( $csv_name ); ?>"
160 <?php
161 if ( '' !== $csv_name ) {
162 echo 'disabled';
163 }
164 ?>
165 />
166 <?php if ( '' !== $csv_name ) { ?>
167 <input id="csv_name"
168 name="csv_name"
169 type="hidden"
170 value="<?php echo esc_attr( $csv_name ); ?>"
171 />
172 <?php } ?>
173 </label>
174 <br/><br/>
175 <input type="file" name="filename" id="filename" accept=".csv">
176 <button type="submit"
177 class="button button-primary"
178 onclick="if (jQuery('#csv_name').val()===''||jQuery('#filename').val()==='') { alert('Please enter an import name and select a file'); return false; }"
179 >
180 <i class="fas fa-check wpda_icon_on_button"></i>
181 <?php echo __( 'Upload', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
182 </button>
183 <button type="button"
184 onclick="window.location.href='?page=wpda&page_action=wpda_import_csv'"
185 class="button button-secondary"
186 >
187 <i class="fas fa-times-circle wpda_icon_on_button"></i>
188 <?php echo __( 'Cancel', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
189 </button>
190 <input type="hidden"
191 name="action"
192 value="upload"
193 />
194 <input type="hidden"
195 name="action2"
196 value="save"
197 />
198 <input type="hidden"
199 name="csv_id"
200 value="<?php echo esc_attr( $csv_id ); ?>"
201 />
202 <?php wp_nonce_field( "wpda-import-csv-{$this->schema_name}", '_wpnonce', false ); ?>
203 </form>
204 </fieldset>
205 <?php
206 }
207
208 /**
209 * Start import
210 *
211 * @return void
212 */
213 protected function show_body_import_start() {
214 $csv_id = isset( $_REQUEST['csv_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
215 $csv_name = isset( $_REQUEST['csv_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
216 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
217 $page = isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
218 ?>
219 <style type="text/css">
220 .wpda-label {
221 font-weight: bold;
222 display: inline-block;
223 width: 110px;
224 }
225 .wpda-import-form {
226 margin-left: 115px;
227 width: 110px;
228 }
229 </style>
230 <br/>
231 <form action="?page=<?php echo esc_attr( $page ); ?>&page_action=wpda_import_csv" method="post">
232 <fieldset class="wpda_fieldset">
233 <legend>
234 Start Import
235 </legend>
236 <p>
237 <label class="wpda-label">Import Name</label>
238 <input type="text" value="<?php echo esc_attr( $csv_name ); ?>" readonly />
239 </p>
240 <p>
241 <label class="wpda-label"></label>
242 <label style="padding-left:5px">
243 <input type="checkbox" name="truncate_table" />
244 <strong>Truncate table before import?</strong> (This will remove all rows from your table and cannot be undone!)
245 </label>
246 </p>
247 <p class="wpda-import-form">
248 <input type='hidden' name='action' value='import' />
249 <input type='hidden' name='csv_id' value='<?php echo esc_attr( $csv_id ); ?>' />
250 <input type='hidden' name='_wpnonce' value='<?php echo esc_attr( $wp_nonce ); ?>'>
251 <input type="submit" class="button" value="Start Import" />
252 </p>
253 </fieldset>
254 </form>
255 <?php
256 }
257
258 /**
259 * Import body
260 *
261 * @return void
262 */
263 protected function show_body_import() {
264 $csv_id =
265 isset( $_REQUEST['csv_id'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
266 sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
267
268 // Security check.
269 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // phpcs:ignore WordPress.Security.NonceVerification
270 if ( ! wp_verify_nonce( $wp_nonce, "wpda-import-csv-{$csv_id}" ) ) {
271 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
272 }
273
274 $truncate_table =
275 isset( $_REQUEST['truncate_table'] ) ? // phpcs:ignore WordPress.Security.NonceVerification
276 sanitize_text_field( wp_unslash( $_REQUEST['truncate_table'] ) ) : 'off'; // phpcs:ignore WordPress.Security.NonceVerification
277
278 echo '<p>Reading CSV file info...</p>';
279 $dbrow = WPDA_CSV_Uploads_Model::query( $csv_id );
280
281 global $wpdb;
282 if ( 1 === $wpdb->num_rows ) {
283 if ( ! isset( $dbrow[0]->csv_real_file_name ) ) {
284 echo '<p style="font-weight: bold">ERROR: No CSV file found for this import</p>';
285 return;
286 }
287
288 if ( ! isset( $dbrow[0]->csv_mapping ) ) {
289 echo '<p style="font-weight: bold">ERROR: Cannot import CSV file without column mapping</p>';
290 return;
291 }
292
293 echo '<p>Validating column mapping...</p>';
294 $upload_dir = WPDA::get_plugin_upload_dir();
295 $file_name = $upload_dir . $dbrow[0]->csv_real_file_name;
296 $mapping = json_decode( $dbrow[0]->csv_mapping, true );
297 $delimiter = isset( $mapping['settings']['delimiter'] ) ? $mapping['settings']['delimiter'] : ',';
298 $date_format = isset( $mapping['settings']['date_format'] ) ? $mapping['settings']['date_format'] : '%Y-%m-%d';
299 $has_header_columns = isset( $mapping['settings']['has_header_columns'] ) ? $mapping['settings']['has_header_columns'] : true;
300 $schema_name = isset( $mapping['database']['wpdaschema_name'] ) ? esc_attr( $mapping['database']['wpdaschema_name'] ) : '';
301 $table_name = isset( $mapping['database']['table_name'] ) ? esc_attr( $mapping['database']['table_name'] ) : '';
302 $table_name = str_replace( '`', '', $table_name );
303 $table_columns = isset( $mapping['columns'] ) ? $mapping['columns'] : array();
304
305 if ( 'on' === $truncate_table ) {
306 // Truncate table.
307 $wpdadb = WPDADB::get_db_connection( $schema_name );
308 $wpdadb->query(
309 $wpdb->prepare(
310 'truncate table `%1s`', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders
311 array(
312 WPDA::remove_backticks( $table_name ),
313 )
314 )
315 );
316 echo '<p><strong>Table `' . esc_attr( $table_name ) . '` truncated...</strong></p>';
317 }
318
319 $columns_inserted = '';
320 foreach ( $table_columns as $table_column ) {
321 $columns_inserted .= $table_column . ',';
322 }
323 $columns_inserted = substr( $columns_inserted, 0, strlen( $columns_inserted ) - 1 );
324
325 $data_type = array();
326 $data_type_before = array();
327 $data_type_after = array();
328 $wpda_list_columns = WPDA_List_Columns_Cache::get_list_columns( $schema_name, $table_name );
329 $column_data_types = $wpda_list_columns->get_table_columns();
330 foreach ( $column_data_types as $column ) {
331 $data_type[ $column['column_name'] ] = WPDA::get_type( $column['data_type'] );
332 switch ( $data_type[ $column['column_name'] ] ) {
333 case 'number':
334 $data_type_before[ $column['column_name'] ] = '';
335 $data_type_after[ $column['column_name'] ] = '';
336 break;
337 case 'date':
338 $data_type_before[ $column['column_name'] ] = "str_to_date('";
339 $data_type_after[ $column['column_name'] ] = "','$date_format')";
340 break;
341 default:
342 $data_type_before[ $column['column_name'] ] = "'";
343 $data_type_after[ $column['column_name'] ] = "'";
344 }
345 }
346
347 // phpcs:disable
348 echo '<p>Enabling buffering...</p>';
349 set_time_limit( 0 );
350 @ini_set( 'zlib.output_compression', false );
351 @ini_set( 'implicit_flush', true );
352 @ini_set( 'output_buffering', true );
353 @ini_set( 'display_errors', false );
354 ob_implicit_flush( true );
355 echo '<p>Reading CSV file...</p>';
356 @ini_set( 'auto_detect_line_endings', true );
357 // phpcs:enable
358
359 if ( false !== ( $fp = fopen( $file_name, 'rb' ) ) ) { // phpcs:ignore
360 $wpdadb = WPDADB::get_db_connection( $schema_name );
361 if ( null !== $wpdadb ) {
362 $row = 0;
363 $inserted = 0;
364 $errors = 0;
365 echo '<p>Connecting to database...</p>';
366 $suppress_errors = $wpdadb->suppress_errors( true );
367 while ( false !== ( $data = fgetcsv( $fp, 0, $delimiter, '"' ) ) ) { // phpcs:ignore
368 if ( 0 === $row && 'true' === $has_header_columns ) { // phpcs:ignore
369 // Skip first row.
370 } else {
371 // Prepare insert array.
372 $wpda_insert_column_values = array();
373 $row_values_valid = true;
374 for ( $column = 0; $column < count( $data ); $column++ ) { // phpcs:ignore
375 if ( isset( $table_columns[ $column ] ) ) {
376 // Add column to array.
377 if ( isset( $table_columns[ $column ] ) ) {
378 if (
379 'number' === $data_type[ $table_columns[ $column ] ] ||
380 'date' === $data_type[ $table_columns[ $column ] ]
381 ) {
382 if ( null === $data[ $column ] || 'null' === $data[ $column ] || '' === $data[ $column ] ) {
383 $wpda_insert_column_values[ $table_columns[ $column ] ] = null;
384 } else {
385 if ( 'number' === $data_type[ $table_columns[ $column ] ] ) {
386 $wpda_insert_column_values[ $table_columns[ $column ] ] = $data[ $column ];
387 } else {
388 try {
389 $date_value = substr( $data[ $column ], 0, 10 );
390 $convert_date = \DateTime::createFromFormat( str_replace( '%', '', $date_format ), $date_value );
391 if ( false === $convert_date ) {
392 $error_msg = 'Cannot convert ' . esc_attr( $date_value ) . ' to date (using format ' . esc_attr( $date_format ) . ')';
393 echo "<div>ERROR: {$error_msg}</div>";
394 $row_values_valid = false;
395
396 // Write error to log file
397 WPDA::wpda_log_wp_error( $error_msg );
398 } else {
399 $wpda_insert_column_values[ $table_columns[ $column ] ] = $convert_date->format( 'Y-m-d' );
400 }
401 } catch ( \Exception $e ) {
402 echo '<div>Cannot convert ' . esc_attr( $date_value ) . ' to date (using format ' . esc_attr( $date_format ) . ')</div>';
403 echo "<div>ERROR: {$error_msg}</div>";
404 $row_values_valid = false;
405
406 // Write error to log file
407 WPDA::wpda_log_wp_error( $error_msg );
408 }
409 }
410 }
411 } else {
412 $wpda_insert_column_values[ $table_columns[ $column ] ] = $data[ $column ];
413 }
414 }
415 }
416 }
417 if ( $row_values_valid ) {
418 // Insert row.
419 $result = $wpdadb->insert(
420 $table_name,
421 $wpda_insert_column_values
422 ); // db call ok; no-cache ok.
423 if ( 1 === $result ) {
424 $inserted++;
425 } else {
426 // Try to insert with plain sql.
427 $insert = "insert into `{$wpdadb->dbname}`.`{$table_name}` ";
428 $insert .= "({$columns_inserted}) values (";
429 for ( $column = 0; $column < count( $data ); $column++ ) { // phpcs:ignore
430 if ( isset( $table_columns[ $column ] ) ) {
431 if (
432 '' === $data[ $column ] &&
433 (
434 'number' === $data_type[ $table_columns[ $column ] ] ||
435 'date' === $data_type[ $table_columns[ $column ] ]
436 )
437 ) {
438 $insert .=
439 'null,';
440 } else {
441 $insert .=
442 $data_type_before[ $table_columns[ $column ] ] .
443 str_replace( "'", "\'", $data[ $column ] ) .
444 $data_type_after[ $table_columns[ $column ] ] .
445 ',';
446 }
447 }
448 }
449 $insert = substr( $insert, 0, strlen( $insert ) - 1 );
450 $insert .= ')';
451 if ( false === $wpdadb->query( $insert ) ) {
452 if ( '' === $wpdadb->last_error ) {
453 echo 'Error: ' . esc_attr( $insert ) . '<br/>';
454
455 // Write error to log file
456 WPDA::wpda_log_wp_error( $insert );
457 } else {
458 echo 'Error: ' . esc_attr( $wpdadb->last_error ) . '<br/>';
459
460 // Write error to log file
461 WPDA::wpda_log_wp_error( $wpdadb->last_error );
462 }
463 $errors++;
464 } else {
465 $inserted++;
466 }
467 }
468 } else {
469 $errors++;
470 }
471 }
472 $row++;
473 }
474 $wpdadb->suppress_errors( $suppress_errors );
475 }
476 fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
477
478 $row--;
479 echo "<p style='font-weight: bold'>Import ready!</p>";
480 echo esc_attr( $row ) . ' rows processed<br/>';
481 echo esc_attr( $inserted ) . ' rows inserted<br/>';
482 echo esc_attr( $errors ) . ' rows with errors<br/>';
483 } else {
484 echo '<p style="font-weight: bold">ERROR: File not found</p>';
485 }
486 }
487 }
488
489 /**
490 * Body reload
491 *
492 * @return void
493 */
494 protected function show_body_reload() {
495 $csv_id =
496 isset( $_REQUEST['csv_id'] ) ?
497 sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // input var okay.
498
499 // Security check.
500 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // input var okay.
501 if ( ! wp_verify_nonce( $wp_nonce, "wpda-reload-csv-{$csv_id}" ) ) {
502 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
503 }
504
505 $this->show_body_upload();
506 }
507
508 /**
509 * Upload file
510 *
511 * @return void
512 */
513 protected function upload_file() {
514 // Security check.
515 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '?'; // input var okay.
516 if ( ! wp_verify_nonce( $wp_nonce, "wpda-import-csv-{$this->schema_name}" ) ) {
517 wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); // phpcs:ignore WordPress.Security.EscapeOutput
518 }
519
520 // phpcs:disable
521 $temp_file_name = isset( $_FILES['filename']['tmp_name'] ) ?
522 sanitize_text_field( $_FILES['filename']['tmp_name'] ) : ''; // For Windows: do NOT unslash!
523 // phpcs:enable
524 $orig_file_name = isset( $_FILES['filename']['name'] ) ?
525 sanitize_text_field( wp_unslash( $_FILES['filename']['name'] ) ) : '';
526
527 if ( isset( $_FILES['filename'] ) && isset( $_REQUEST['csv_name'] ) ) {
528
529 if (
530 isset( $_FILES['filename']['error'] ) &&
531 UPLOAD_ERR_OK === $_FILES['filename']['error'] &&
532 is_uploaded_file( $temp_file_name )
533 ) {
534 echo '<br/>';
535 echo __( 'Uploading file', 'wp-data-access' ) . ' <strong>' . esc_attr( $orig_file_name ) . '</strong>...'; // phpcs:ignore WordPress.Security.EscapeOutput
536 echo '<br/><br/>';
537
538 $upload_dir = WPDA::get_plugin_upload_dir();
539 $real_file_name = 'wpda_csv_upload_' . gmdate( 'YmdHis' ) . '.csv';
540
541 // Process file and save a local copy.
542 $fp = $this->file_pointer = fopen( $temp_file_name, 'rb' ); // phpcs:ignore
543 if ( false !== $this->file_pointer ) {
544 if ( ! is_dir( $upload_dir ) ) {
545 mkdir( $upload_dir, 0755, true );
546 }
547
548 $fw = fopen( $upload_dir . "{$real_file_name}", 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
549 while ( ! feof( $this->file_pointer ) ) {
550 $file_content = fread( $this->file_pointer, 1024 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fread
551 fwrite( $fw, $file_content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
552 }
553 }
554 fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
555 fclose( $fw ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
556
557 echo __( 'Saving file info...', 'wp-data-access' ); // phpcs:ignore WordPress.Security.EscapeOutput
558 echo '<br/><br/>';
559
560 $csv_id =
561 isset( $_REQUEST['csv_id'] ) ?
562 sanitize_text_field( wp_unslash( $_REQUEST['csv_id'] ) ) : ''; // input var okay.
563 $csv_name = sanitize_text_field( wp_unslash( $_REQUEST['csv_name'] ) ); // input var okay.
564
565 if ( '' === $csv_id ) {
566 // New CSV import.
567 $result = WPDA_CSV_Uploads_Model::insert( $csv_name, $real_file_name, $orig_file_name );
568 } else {
569 // Reload CSV.
570 $oldrow = WPDA_CSV_Uploads_Model::query( $csv_id );
571 $result = WPDA_CSV_Uploads_Model::update( $csv_id, $real_file_name, $orig_file_name );
572 if ( $result ) {
573 // Remove old file.
574 if ( isset( $oldrow[0]->csv_real_file_name ) ) {
575 unlink( WPDA::get_plugin_upload_dir() . $oldrow[0]->csv_real_file_name );
576 }
577 }
578 }
579 if ( false === $result ) {
580 $msg = new WPDA_Message_Box(
581 array(
582 'message_text' => __( 'Processing CSV file failed', 'wp-data-access' ),
583 'message_type' => 'error',
584 'message_is_dismissible' => false,
585 )
586 );
587 $msg->box();
588 } else {
589 $wp_nonce_csv_id = '' === $csv_id ? $result : $csv_id;
590 $wp_nonce_action = "wpda-mapping-{$wp_nonce_csv_id}";
591 $wp_nonce = esc_attr( wp_create_nonce( $wp_nonce_action ) );
592 ?>
593 <strong>Upload successful</strong>
594 <br/><br/>
595 <form method="post"
596 action="?page=wpda&page_action=wpda_import_csv"
597 style="display: inline-block; vertical-align: baseline;">
598 <input type="hidden"
599 name="csv_id"
600 value="<?php echo '' === $csv_id ? esc_attr( $result ) : esc_attr( $csv_id ); ?>"
601 >
602 <input type="hidden"
603 name="action"
604 value="mapping"
605 >
606 <input type="submit"
607 class="page-title-action"
608 style="margin-left: 0;"
609 value="<?php echo __( 'Column mapping' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>"
610 />
611 <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $wp_nonce ); ?>" />
612 </form>&nbsp;
613 <form method="post"
614 action="?page=wpda&page_action=wpda_import_csv"
615 style="display: inline-block; vertical-align: baseline;">
616 <input type="submit"
617 class="page-title-action"
618 style="margin-left: 0;"
619 value="<?php echo __( 'CSV file list' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>"
620 />
621 </form>
622 <?php
623 }
624 }
625 } else {
626 // File upload failed: inform user.
627 $msg = new WPDA_Message_Box(
628 array(
629 'message_text' => __( 'File upload failed', 'wp-data-access' ),
630 'message_type' => 'error',
631 'message_is_dismissible' => false,
632 )
633 );
634 $msg->box();
635 }
636 }
637
638 }
639
640 }
641