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