| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP Notice display functions. |
| 4 |
* |
| 5 |
* All UsersWP notice display related functions can be found here. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 |
*/ |
| 10 |
class UsersWP_Import_Export { |
| 11 |
private $wp_filesystem; |
| 12 |
private $export_dir; |
| 13 |
private $export_url; |
| 14 |
public $per_page; |
| 15 |
public $meta_table_name; |
| 16 |
public $path; |
| 17 |
public $total_rows; |
| 18 |
public $imp_step; |
| 19 |
public $skipped; |
| 20 |
private $empty; |
| 21 |
private $step; |
| 22 |
private $file; |
| 23 |
private $filename; |
| 24 |
|
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
global $wp_filesystem; |
| 28 |
|
| 29 |
if ( empty( $wp_filesystem ) ) { |
| 30 |
require_once( ABSPATH . '/wp-admin/includes/file.php' ); |
| 31 |
WP_Filesystem(); |
| 32 |
global $wp_filesystem; |
| 33 |
} |
| 34 |
|
| 35 |
$this->wp_filesystem = $wp_filesystem; |
| 36 |
$this->export_dir = $this->export_location(); |
| 37 |
$this->export_url = $this->export_location( true ); |
| 38 |
$this->per_page = apply_filters('uwp_import_export_per_page', 20, $this); |
| 39 |
$this->meta_table_name = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 40 |
$this->path = ''; |
| 41 |
|
| 42 |
add_action( 'admin_init', array($this, 'process_settings_export') ); |
| 43 |
add_action( 'admin_init', array($this, 'process_settings_import') ); |
| 44 |
add_action( 'wp_ajax_uwp_ajax_export_users', array( $this, 'process_users_export' ) ); |
| 45 |
add_action( 'wp_ajax_uwp_ajax_import_users', array( $this, 'process_users_import' ) ); |
| 46 |
add_action( 'wp_ajax_uwp_ie_upload_file', array( $this, 'ie_upload_file' ) ); |
| 47 |
add_action( 'wp_ajax_nopriv_uwp_ie_upload_file', array( $this, 'ie_upload_file' ) ); |
| 48 |
add_action( 'admin_notices', array($this, 'ie_admin_notice') ); |
| 49 |
add_filter( 'uwp_get_export_users_status', array( $this, 'get_export_users_status' ) ); |
| 50 |
add_filter( 'uwp_get_import_users_status', array( $this, 'get_import_users_status' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns export file location |
| 55 |
* |
| 56 |
* @package userswp |
| 57 |
* |
| 58 |
* @param bool $relative |
| 59 |
* |
| 60 |
* @return string |
| 61 |
* |
| 62 |
*/ |
| 63 |
public function export_location( $relative = false ) { |
| 64 |
$upload_dir = wp_upload_dir(); |
| 65 |
$export_location = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache'; |
| 66 |
$export_location = apply_filters( 'uwp_export_location', $export_location, $relative ); |
| 67 |
|
| 68 |
return trailingslashit( $export_location ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Displays notice |
| 73 |
*/ |
| 74 |
public function ie_admin_notice(){ |
| 75 |
if(isset($_GET['imp-msg']) && 'success' == $_GET['imp-msg']){ |
| 76 |
?> |
| 77 |
<div class="notice notice-success is-dismissible"> |
| 78 |
<p><?php esc_html_e( 'Settings imported successfully!', 'userswp' ); ?></p> |
| 79 |
</div> |
| 80 |
<?php |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Process a settings export that generates a .json file of the settings |
| 86 |
*/ |
| 87 |
public function process_settings_export() { |
| 88 |
if( empty( $_POST['uwp_ie_action'] ) || 'export_settings' != $_POST['uwp_ie_action'] ) |
| 89 |
return; |
| 90 |
if( ! wp_verify_nonce( $_POST['uwp_export_nonce'], 'uwp_export_nonce' ) ) |
| 91 |
return; |
| 92 |
if( ! current_user_can( 'manage_options' ) ) |
| 93 |
return; |
| 94 |
$settings = get_option( 'uwp_settings' ); |
| 95 |
ignore_user_abort( true ); |
| 96 |
nocache_headers(); |
| 97 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 98 |
header( 'Content-Disposition: attachment; filename=uwp-settings-export-' . date( 'm-d-Y' ) . '.json' ); |
| 99 |
header( "Expires: 0" ); |
| 100 |
echo json_encode( $settings ); |
| 101 |
exit; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Process a settings import from a json file |
| 106 |
*/ |
| 107 |
public function process_settings_import() { |
| 108 |
if( empty( $_POST['uwp_ie_action'] ) || 'import_settings' != $_POST['uwp_ie_action'] ) |
| 109 |
return; |
| 110 |
if( ! wp_verify_nonce( $_POST['uwp_import_nonce'], 'uwp_import_nonce' ) ) |
| 111 |
return; |
| 112 |
if( ! current_user_can( 'manage_options' ) ) |
| 113 |
return; |
| 114 |
$extension = explode( '.', $_FILES['import_file']['name'] ); |
| 115 |
$extension = end( $extension ); |
| 116 |
if( $extension != 'json' ) { |
| 117 |
wp_die( esc_html( wp_sprintf( __( 'Please upload a valid .json file. %sGo Back%s', 'userswp' ), '<a href="' . esc_url( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings' ) ) . '">', '</a>' ) ) ); |
| 118 |
} |
| 119 |
$import_file = $_FILES['import_file']['tmp_name']; |
| 120 |
if( empty( $import_file ) ) { |
| 121 |
wp_die( esc_html( wp_sprintf( __( 'Please upload a file to import. %sGo Back%s', 'userswp' ), '<a href="' . esc_url( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings' ) ) . '">', '</a>' ) ) ); |
| 122 |
} |
| 123 |
// Retrieve the settings from the file and convert the json object to an array. |
| 124 |
$settings = (array) json_decode( file_get_contents( $import_file ), true ); |
| 125 |
update_option( 'uwp_settings', $settings ); |
| 126 |
wp_safe_redirect( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings&imp-msg=success' ) ); exit; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Processes users export |
| 131 |
*/ |
| 132 |
public function process_users_export(){ |
| 133 |
|
| 134 |
$response = array(); |
| 135 |
$response['success'] = false; |
| 136 |
$response['msg'] = __( 'Invalid export request found.', 'userswp' ); |
| 137 |
|
| 138 |
if ( empty( $_POST['data'] ) || !current_user_can( 'manage_options' ) ) { |
| 139 |
wp_send_json( $response ); |
| 140 |
} |
| 141 |
|
| 142 |
parse_str( $_POST['data'], $data ); |
| 143 |
|
| 144 |
$data['step'] = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
| 145 |
|
| 146 |
$_REQUEST = (array)$data; |
| 147 |
if ( !( !empty( $_REQUEST['uwp_export_users_nonce'] ) && wp_verify_nonce( $_REQUEST['uwp_export_users_nonce'], 'uwp_export_users_nonce' ) ) ) { |
| 148 |
$response['msg'] = __( 'Security check failed.', 'userswp' ); |
| 149 |
wp_send_json( $response ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( ( $error = $this->check_export_location() ) !== true ) { |
| 153 |
$response['msg'] = __( 'Filesystem ERROR: ' . $error, 'userswp' ); |
| 154 |
wp_send_json( $response ); |
| 155 |
} |
| 156 |
|
| 157 |
$this->set_export_params( $_REQUEST ); |
| 158 |
|
| 159 |
$return = $this->process_export_step(); |
| 160 |
$done = $this->get_export_status(); |
| 161 |
|
| 162 |
if ( $return ) { |
| 163 |
$this->step += 1; |
| 164 |
|
| 165 |
$response['success'] = true; |
| 166 |
$response['msg'] = ''; |
| 167 |
|
| 168 |
if ( $done >= 100 ) { |
| 169 |
$this->step = 'done'; |
| 170 |
$new_filename = 'uwp-users-export-' . date( 'y-m-d-H-i' ) . '-' . wp_generate_password(12, false ) . '.csv'; |
| 171 |
$new_file = $this->export_dir . $new_filename; |
| 172 |
|
| 173 |
if ( file_exists( $this->file ) ) { |
| 174 |
$this->wp_filesystem->move( $this->file, $new_file, true ); |
| 175 |
} |
| 176 |
|
| 177 |
if ( file_exists( $new_file ) ) { |
| 178 |
$response['data']['file'] = array( 'u' => $this->export_url . $new_filename, 's' => size_format( filesize( $new_file ), 2 ) ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
$response['data']['step'] = $this->step; |
| 183 |
$response['data']['done'] = $done; |
| 184 |
} else { |
| 185 |
$response['msg'] = __( 'No data found for export.', 'userswp' ); |
| 186 |
} |
| 187 |
|
| 188 |
wp_send_json( $response ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Sets export params |
| 194 |
* |
| 195 |
* @package userswp |
| 196 |
* |
| 197 |
* @param array $request |
| 198 |
* |
| 199 |
*/ |
| 200 |
public function set_export_params( $request ) { |
| 201 |
$this->empty = false; |
| 202 |
$this->step = !empty( $request['step'] ) ? absint( $request['step'] ) : 1; |
| 203 |
$this->filename = 'uwp-users-export-temp.csv'; |
| 204 |
$this->file = $this->export_dir . $this->filename; |
| 205 |
$chunk_per_page = !empty( $request['uwp_ie_chunk_size'] ) ? absint( $request['uwp_ie_chunk_size'] ) : 0; |
| 206 |
$this->per_page = $chunk_per_page < 50 || $chunk_per_page > 100000 ? 5000 : $chunk_per_page; |
| 207 |
|
| 208 |
do_action( 'uwp_export_users_set_params', $request ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Returns export file location |
| 213 |
* |
| 214 |
* @package userswp |
| 215 |
* |
| 216 |
* @return bool |
| 217 |
* |
| 218 |
*/ |
| 219 |
public function check_export_location() { |
| 220 |
try { |
| 221 |
if ( empty( $this->wp_filesystem ) ) { |
| 222 |
return __( 'Filesystem ERROR: Could not access filesystem.', 'userswp' ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( is_wp_error( $this->wp_filesystem ) ) { |
| 226 |
return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'userswp' ); |
| 227 |
} |
| 228 |
|
| 229 |
$is_dir = $this->wp_filesystem->is_dir( $this->export_dir ); |
| 230 |
$is_writeable = $is_dir && is_writeable( $this->export_dir ); |
| 231 |
|
| 232 |
if ( $is_dir && $is_writeable ) { |
| 233 |
|
| 234 |
} else if ( $is_dir && !$is_writeable ) { |
| 235 |
if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 236 |
return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'userswp' ), $this->export_dir ); |
| 237 |
} |
| 238 |
} else { |
| 239 |
if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 240 |
return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'userswp' ), $this->export_dir ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if(!$this->wp_filesystem->exists( $this->export_dir . '/index.php')){ |
| 245 |
$this->wp_filesystem->copy( USERSWP_PATH . 'assets/index.php', $this->export_dir . '/index.php' ); |
| 246 |
} |
| 247 |
|
| 248 |
return true; |
| 249 |
} catch ( Exception $e ) { |
| 250 |
return $e->getMessage(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Processes export step |
| 256 |
* |
| 257 |
* @return bool |
| 258 |
* |
| 259 |
*/ |
| 260 |
public function process_export_step() { |
| 261 |
if ( $this->step < 2 ) { |
| 262 |
@unlink( $this->file ); |
| 263 |
$this->print_columns(); |
| 264 |
} |
| 265 |
|
| 266 |
$return = $this->print_rows(); |
| 267 |
|
| 268 |
if ( $return ) { |
| 269 |
return true; |
| 270 |
} else { |
| 271 |
return false; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Prints columns |
| 277 |
* |
| 278 |
* @return array |
| 279 |
* |
| 280 |
*/ |
| 281 |
public function print_columns() { |
| 282 |
$column_data = ''; |
| 283 |
$columns = $this->get_columns(); |
| 284 |
$i = 1; |
| 285 |
foreach( $columns as $key => $column ) { |
| 286 |
$column_data .= '"' . addslashes( $column ) . '"'; |
| 287 |
$column_data .= $i == count( $columns ) ? '' : ','; |
| 288 |
$i++; |
| 289 |
} |
| 290 |
$column_data .= "\r\n"; |
| 291 |
|
| 292 |
$this->attach_export_data( $column_data ); |
| 293 |
|
| 294 |
return $column_data; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Returns export columns |
| 299 |
* |
| 300 |
* @return array |
| 301 |
* |
| 302 |
*/ |
| 303 |
public function get_columns() { |
| 304 |
global $wpdb; |
| 305 |
$columns = array(); |
| 306 |
|
| 307 |
foreach ( $wpdb->get_col( "DESC " . $this->meta_table_name, 0 ) as $column_name ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 308 |
$columns[] = $column_name; |
| 309 |
} |
| 310 |
|
| 311 |
return apply_filters( 'uwp_export_users_get_columns', $columns ); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Returns export data |
| 316 |
* |
| 317 |
* @package userswp |
| 318 |
* |
| 319 |
* @return array |
| 320 |
* |
| 321 |
*/ |
| 322 |
public function get_export_data() { |
| 323 |
global $wpdb; |
| 324 |
if(!$this->step){ |
| 325 |
$page = 1; |
| 326 |
} else { |
| 327 |
$page = $this->step; |
| 328 |
} |
| 329 |
|
| 330 |
$page_start = absint( ( $page - 1 ) * $this->per_page ); |
| 331 |
$data = $wpdb->get_results( "SELECT * FROM $this->meta_table_name WHERE 1=1 LIMIT $page_start,". $this->per_page); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 332 |
$i = 0; |
| 333 |
|
| 334 |
foreach ($data as $u){ |
| 335 |
$user = get_userdata($u->user_id); |
| 336 |
$data[$i]->username = $user->user_login; |
| 337 |
$data[$i]->email = $user->user_email; |
| 338 |
$data[$i]->bio = $user->description; |
| 339 |
$i++; |
| 340 |
} |
| 341 |
|
| 342 |
return apply_filters( 'uwp_export_users_get_data', $data ); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Returns export status |
| 347 |
* |
| 348 |
* @return int |
| 349 |
* |
| 350 |
*/ |
| 351 |
public function get_export_status() { |
| 352 |
$status = 100; |
| 353 |
return apply_filters( 'uwp_get_export_users_status', $status ); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Prints CSV rows |
| 358 |
* |
| 359 |
* @return string |
| 360 |
* |
| 361 |
*/ |
| 362 |
public function print_rows() { |
| 363 |
$row_data = ''; |
| 364 |
$data = $this->get_export_data(); |
| 365 |
$columns = $this->get_columns(); |
| 366 |
|
| 367 |
if ( is_array($data) && !empty($data) ) { |
| 368 |
foreach ( $data as $row ) { |
| 369 |
$i = 1; |
| 370 |
foreach ( $row as $key => $column ) { |
| 371 |
$column = $this->escape_data( $column ); |
| 372 |
$row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"'; |
| 373 |
$row_data .= $i == count( $columns ) ? '' : ','; |
| 374 |
$i++; |
| 375 |
} |
| 376 |
$row_data .= "\r\n"; |
| 377 |
} |
| 378 |
|
| 379 |
$this->attach_export_data( $row_data ); |
| 380 |
|
| 381 |
return $row_data; |
| 382 |
} |
| 383 |
|
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Returns export file content |
| 389 |
* |
| 390 |
* @return string |
| 391 |
* |
| 392 |
*/ |
| 393 |
protected function get_export_file() { |
| 394 |
$file = ''; |
| 395 |
|
| 396 |
if ( $this->wp_filesystem->exists( $this->file ) ) { |
| 397 |
$file = $this->wp_filesystem->get_contents( $this->file ); |
| 398 |
} else { |
| 399 |
$this->wp_filesystem->put_contents( $this->file, '' ); |
| 400 |
} |
| 401 |
|
| 402 |
return $file; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Adds export data to CSV file |
| 407 |
* |
| 408 |
* @package userswp |
| 409 |
* |
| 410 |
* @param string $data |
| 411 |
* |
| 412 |
*/ |
| 413 |
protected function attach_export_data( $data = '' ) { |
| 414 |
$filedata = $this->get_export_file(); |
| 415 |
$filedata .= $data; |
| 416 |
|
| 417 |
$this->wp_filesystem->put_contents( $this->file, $filedata ); |
| 418 |
|
| 419 |
$rows = file( $this->file, FILE_SKIP_EMPTY_LINES ); |
| 420 |
$columns = $this->get_columns(); |
| 421 |
$columns = empty( $columns ) ? 0 : 1; |
| 422 |
|
| 423 |
$this->empty = count( $rows ) == $columns ? true : false; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Returns export user status |
| 428 |
* |
| 429 |
* @return int |
| 430 |
* |
| 431 |
*/ |
| 432 |
public function get_export_users_status() { |
| 433 |
global $wpdb; |
| 434 |
$data = $wpdb->get_results("SELECT user_id FROM $this->meta_table_name WHERE 1=1"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 435 |
$total = !empty( $data ) ? count( $data ) : 0; |
| 436 |
$status = 100; |
| 437 |
|
| 438 |
if ( $this->per_page > $total ) { |
| 439 |
$this->per_page = $total; |
| 440 |
} |
| 441 |
|
| 442 |
if ( $total > 0 ) { |
| 443 |
$status = ( ( $this->per_page * $this->step ) / $total ) * 100; |
| 444 |
} |
| 445 |
|
| 446 |
if ( $status > 100 ) { |
| 447 |
$status = 100; |
| 448 |
} |
| 449 |
|
| 450 |
return $status; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Uploaded file handling |
| 455 |
* |
| 456 |
* @return string |
| 457 |
* |
| 458 |
*/ |
| 459 |
public function ie_upload_file(){ |
| 460 |
|
| 461 |
if ( !(!empty($_REQUEST['nonce']) && wp_verify_nonce( $_REQUEST['nonce'], 'uwp-ie-file-upload-nonce' )) ) { |
| 462 |
echo 'error';return; |
| 463 |
} |
| 464 |
|
| 465 |
$upload_data = array( |
| 466 |
'name' => $_FILES['import_file']['name'], |
| 467 |
'type' => $_FILES['import_file']['type'], |
| 468 |
'tmp_name' => $_FILES['import_file']['tmp_name'], |
| 469 |
'error' => $_FILES['import_file']['error'], |
| 470 |
'size' => $_FILES['import_file']['size'] |
| 471 |
); |
| 472 |
|
| 473 |
header('Content-Type: text/html; charset=' . get_option('blog_charset')); |
| 474 |
|
| 475 |
add_filter( 'upload_mimes', array( $this, 'allowed_upload_mimes' ) ); |
| 476 |
$uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) ); |
| 477 |
remove_filter( 'upload_mimes', array( $this, 'allowed_upload_mimes' ) ); |
| 478 |
|
| 479 |
if ( isset( $uploaded_file['url'] ) ) { |
| 480 |
$file_loc = $uploaded_file['url']; |
| 481 |
echo esc_url( $file_loc );exit; |
| 482 |
} else { |
| 483 |
echo 'error'; |
| 484 |
} |
| 485 |
exit; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Processes users import |
| 490 |
* |
| 491 |
*/ |
| 492 |
public function process_users_import(){ |
| 493 |
|
| 494 |
$response = array(); |
| 495 |
$response['success'] = false; |
| 496 |
$response['msg'] = __( 'Invalid import request found.', 'userswp' ); |
| 497 |
|
| 498 |
if ( empty( $_POST['data'] ) || !current_user_can( 'manage_options' ) ) { |
| 499 |
wp_send_json( $response ); |
| 500 |
} |
| 501 |
|
| 502 |
parse_str( $_POST['data'], $data ); |
| 503 |
|
| 504 |
$this->imp_step = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
| 505 |
|
| 506 |
$_REQUEST = (array)$data; |
| 507 |
if ( !( !empty( $_REQUEST['uwp_import_users_nonce'] ) && wp_verify_nonce( $_REQUEST['uwp_import_users_nonce'], 'uwp_import_users_nonce' ) ) ) { |
| 508 |
$response['msg'] = __( 'Security check failed.', 'userswp' ); |
| 509 |
wp_send_json( $response ); |
| 510 |
} |
| 511 |
|
| 512 |
$allowed = array('csv'); |
| 513 |
$import_file = $data['uwp_import_users_file']; |
| 514 |
$uploads = wp_upload_dir(); |
| 515 |
$csv_file_array = explode( '/', $import_file ); |
| 516 |
$csv_filename = end( $csv_file_array ); |
| 517 |
$this->path = $uploads['path'].'/'.$csv_filename; |
| 518 |
|
| 519 |
$ext = pathinfo($csv_filename, PATHINFO_EXTENSION); |
| 520 |
if (!in_array($ext, $allowed)) { |
| 521 |
$response['msg'] = __( 'Invalid file type, please upload .csv file.', 'userswp' ); |
| 522 |
wp_send_json( $response ); |
| 523 |
} |
| 524 |
|
| 525 |
$lc_all = setlocale( LC_ALL, 0 ); |
| 526 |
setlocale( LC_ALL, 'en_US.UTF-8' ); |
| 527 |
if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) { |
| 528 |
while ( ( $data = fgetcsv( $handle, 100000, "," ) ) !== false ) { |
| 529 |
if ( ! empty( $data ) ) { |
| 530 |
$file[] = $data; |
| 531 |
} |
| 532 |
} |
| 533 |
fclose( $handle ); |
| 534 |
} |
| 535 |
setlocale( LC_ALL, $lc_all ); |
| 536 |
|
| 537 |
$this->total_rows = ( ! empty( $file ) && count( $file ) > 1 ) ? count( $file ) - 1 : 0; |
| 538 |
|
| 539 |
$return = $this->process_import_step(); |
| 540 |
$done = $this->get_import_status(); |
| 541 |
|
| 542 |
if ( $return['success'] ) { |
| 543 |
$response['total']['msg'] = ''; |
| 544 |
if($this->total_rows > 0 && $this->imp_step == 1) { |
| 545 |
$response['total']['msg'] = __( 'Total '.$this->total_rows. ' item(s) found.', 'userswp' ); |
| 546 |
} |
| 547 |
|
| 548 |
$this->imp_step += 1; |
| 549 |
|
| 550 |
$response['success'] = true; |
| 551 |
$response['msg'] = ''; |
| 552 |
|
| 553 |
if ( $done >= 100 ) { |
| 554 |
$this->imp_step = 'done'; |
| 555 |
$response['msg'] = __( 'Users import completed.', 'userswp' ); |
| 556 |
} |
| 557 |
|
| 558 |
$response['data']['msg'] = ! empty( $return['msg'] ) ? $return['msg'] : $response['msg']; |
| 559 |
$response['data']['step'] = $this->imp_step; |
| 560 |
$response['data']['done'] = $done; |
| 561 |
} else { |
| 562 |
$response['msg'] = __( 'No valid data found for import.', 'userswp' ); |
| 563 |
} |
| 564 |
|
| 565 |
wp_send_json( $response ); |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Processes import step |
| 571 |
* |
| 572 |
* @return array |
| 573 |
* |
| 574 |
*/ |
| 575 |
public function process_import_step() { |
| 576 |
|
| 577 |
$errors = new WP_Error(); |
| 578 |
if(is_null($this->path)){ |
| 579 |
$errors->add('no_csv_file', __('No csv file found.','userswp')); |
| 580 |
} |
| 581 |
|
| 582 |
set_time_limit(0); |
| 583 |
|
| 584 |
$return = array('success' => false); |
| 585 |
|
| 586 |
$rows = $this->get_csv_rows($this->imp_step, 1); |
| 587 |
|
| 588 |
if ( ! empty( $rows ) ) { |
| 589 |
foreach ( $rows as $row ) { |
| 590 |
if( empty($row) ) { |
| 591 |
$return['msg'] = sprintf(__('Row - %s Error: '. 'Skipped due to invalid/no data.','userswp'), $this->imp_step); |
| 592 |
continue; |
| 593 |
} |
| 594 |
|
| 595 |
$username = isset($row['username']) ? $row['username'] : ''; |
| 596 |
$email = isset($row['email']) ? $row['email'] : ''; |
| 597 |
$first_name = isset($row['first_name']) ? $row['first_name'] : ''; |
| 598 |
$last_name = isset($row['last_name']) ? $row['last_name'] : ''; |
| 599 |
$bio = isset($row['bio']) ? $row['bio'] : ''; |
| 600 |
$display_name = isset($row['display_name']) ? $row['display_name'] : ''; |
| 601 |
$password = wp_generate_password(); |
| 602 |
$exclude = array('user_id'); |
| 603 |
$exclude = apply_filters('uwp_import_exclude_columns', $exclude, $row); |
| 604 |
|
| 605 |
if(isset($row['username']) && username_exists($row['username'])){ |
| 606 |
$user = get_user_by('login', $row['username']); |
| 607 |
$user_id = $user->ID; |
| 608 |
$email = $row['email']; |
| 609 |
if( !empty( $email ) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id) ) { |
| 610 |
$args = array( |
| 611 |
'ID' => $user_id, |
| 612 |
'user_email' => $email, |
| 613 |
'display_name' => $display_name, |
| 614 |
'first_name' => $first_name, |
| 615 |
'last_name' => $last_name, |
| 616 |
'description' => $bio, |
| 617 |
); |
| 618 |
wp_update_user( $args ); |
| 619 |
} |
| 620 |
} elseif(isset($row['email']) && email_exists($row['email'])){ |
| 621 |
$user = get_user_by('email', $row['email']); |
| 622 |
$user_id = $user->ID; |
| 623 |
} elseif((int)$row['user_id'] > 0){ |
| 624 |
$user = get_user_by('ID', $row['user_id']); |
| 625 |
if(false === $user){ |
| 626 |
$userdata = array( |
| 627 |
'user_login' => $row['username'], |
| 628 |
'user_email' => $email, |
| 629 |
'user_pass' => $password, |
| 630 |
'first_name' => $first_name, |
| 631 |
'last_name' => $last_name, |
| 632 |
'description' => $bio, |
| 633 |
'display_name'=> $display_name |
| 634 |
); |
| 635 |
$user_id = wp_insert_user( $userdata ); |
| 636 |
$notify = apply_filters('uwp_import_user_notify', 'user', $user_id); |
| 637 |
wp_new_user_notification($user_id,null, $notify); //send password reset link |
| 638 |
} else { |
| 639 |
if( $user->user_login == $row['username'] ) { //check id passed in csv and existing username are same |
| 640 |
$user_id = $row['user_id']; |
| 641 |
if( !empty( $email ) && $email != $user->user_email && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)) { |
| 642 |
$args = array( |
| 643 |
'ID' => $user_id, |
| 644 |
'user_email' => $email, |
| 645 |
'first_name' => $first_name, |
| 646 |
'last_name' => $last_name, |
| 647 |
'description' => $bio, |
| 648 |
'display_name' => $display_name |
| 649 |
); |
| 650 |
wp_update_user( $args ); |
| 651 |
} |
| 652 |
} else { |
| 653 |
$return['msg'] = sprintf(__('Row - %s Error: '. 'User could not be created.','userswp'), $this->imp_step); |
| 654 |
continue; |
| 655 |
} |
| 656 |
} |
| 657 |
} else { |
| 658 |
$user_id = wp_create_user( $username, $password, $email ); |
| 659 |
if( !is_wp_error( $user_id ) ) { |
| 660 |
$args = array( |
| 661 |
'ID' => $user_id, |
| 662 |
'first_name' => $first_name, |
| 663 |
'last_name' => $last_name, |
| 664 |
'description' => $bio, |
| 665 |
'display_name' => $display_name |
| 666 |
); |
| 667 |
wp_update_user( $args ); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
if( !is_wp_error( $user_id ) ){ |
| 672 |
foreach ($row as $key => $value){ |
| 673 |
if(!in_array($key, $exclude)){ |
| 674 |
$value = maybe_unserialize($value); |
| 675 |
uwp_update_usermeta($user_id, $key, $value); |
| 676 |
} |
| 677 |
} |
| 678 |
} else { |
| 679 |
$return['msg'] = sprintf(__('Row - %s Error: %s','userswp'), $this->imp_step, $user_id->get_error_message()); |
| 680 |
continue; |
| 681 |
} |
| 682 |
} |
| 683 |
$return['success'] = true; |
| 684 |
} |
| 685 |
|
| 686 |
return $return; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* Returns CSV row |
| 691 |
* |
| 692 |
* @package userswp |
| 693 |
* |
| 694 |
* @param int $row |
| 695 |
* @param int $count |
| 696 |
* |
| 697 |
* @return mixed |
| 698 |
* |
| 699 |
*/ |
| 700 |
public function get_csv_rows( $row = 0, $count = 1 ) { |
| 701 |
|
| 702 |
$lc_all = setlocale( LC_ALL, 0 ); // Fix issue of fgetcsv ignores special characters when they are at the beginning of line |
| 703 |
setlocale( LC_ALL, 'en_US.UTF-8' ); |
| 704 |
$l = $f =0; |
| 705 |
$headers = $file = array(); |
| 706 |
$userdata_fields = $this->get_columns(); |
| 707 |
if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) { |
| 708 |
while ( ( $line = fgetcsv( $handle, 100000, "," ) ) !== false ) { |
| 709 |
// If the first line is empty, abort |
| 710 |
// If another line is empty, just skip it |
| 711 |
if ( empty( $line ) ) { |
| 712 |
if ( $l === 0 ) |
| 713 |
break; |
| 714 |
else |
| 715 |
continue; |
| 716 |
} |
| 717 |
|
| 718 |
// If we are on the first line, the columns are the headers |
| 719 |
if ( $l === 0 ) { |
| 720 |
$headers = $line; |
| 721 |
$l ++; |
| 722 |
continue; |
| 723 |
} |
| 724 |
|
| 725 |
// only get the rows needed |
| 726 |
if ( $row && $count ) { |
| 727 |
|
| 728 |
// if we have everything we need then break; |
| 729 |
if ( $l == $row + $count ) { |
| 730 |
break; |
| 731 |
|
| 732 |
// if its less than the start row then continue; |
| 733 |
} elseif ( $l && $l < $row ) { |
| 734 |
$l ++; |
| 735 |
continue; |
| 736 |
|
| 737 |
// if we have the count we need then break; |
| 738 |
} elseif ( $f > $count ) { |
| 739 |
break; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
// Separate user data from meta |
| 744 |
$userdata = $usermeta = array(); |
| 745 |
foreach ( $line as $ckey => $column ) { |
| 746 |
$column_name = $headers[$ckey]; |
| 747 |
$column = trim( $column ); |
| 748 |
if ( in_array( $column_name, $userdata_fields ) ) { |
| 749 |
$userdata[$column_name] = $column; |
| 750 |
} else { |
| 751 |
$usermeta[$column_name] = $column; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
// A plugin may need to filter the data and meta |
| 756 |
$userdata = apply_filters( 'uwp_import_userdata', $userdata, $usermeta ); |
| 757 |
|
| 758 |
if ( ! empty( $userdata ) ) { |
| 759 |
$file[] = $userdata; |
| 760 |
$f ++; |
| 761 |
$l ++; |
| 762 |
} |
| 763 |
} |
| 764 |
fclose( $handle ); |
| 765 |
} |
| 766 |
setlocale( LC_ALL, $lc_all ); |
| 767 |
|
| 768 |
return $file; |
| 769 |
|
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* Returns import status |
| 774 |
* |
| 775 |
* @return int |
| 776 |
* |
| 777 |
*/ |
| 778 |
public function get_import_status() { |
| 779 |
$status = 100; |
| 780 |
return apply_filters( 'uwp_get_import_users_status', $status ); |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Returns import user status |
| 785 |
* |
| 786 |
* @return int |
| 787 |
* |
| 788 |
*/ |
| 789 |
public function get_import_users_status() { |
| 790 |
|
| 791 |
if ( $this->imp_step >= $this->total_rows ) { |
| 792 |
$status = 100; |
| 793 |
} else { |
| 794 |
$status = ( ( 1 * $this->imp_step ) / $this->total_rows ) * 100; |
| 795 |
} |
| 796 |
|
| 797 |
return $status; |
| 798 |
} |
| 799 |
|
| 800 |
public function allowed_upload_mimes($mimes = array()) { |
| 801 |
$mimes['csv'] = "text/csv"; |
| 802 |
return $mimes; |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Escape a string to be used in a CSV export. |
| 807 |
* |
| 808 |
* @see https://hackerone.com/reports/72785 |
| 809 |
* |
| 810 |
* @since 1.2.3.10 |
| 811 |
* |
| 812 |
* @param string $data Data to escape. |
| 813 |
* @return string |
| 814 |
*/ |
| 815 |
public function escape_data( $data ) { |
| 816 |
$escape_chars = array( '=', '+', '-', '@' ); |
| 817 |
|
| 818 |
if ( $data && in_array( substr( $data, 0, 1 ), $escape_chars, true ) ) { |
| 819 |
$data = " " . $data; |
| 820 |
} |
| 821 |
|
| 822 |
return $data; |
| 823 |
} |
| 824 |
|
| 825 |
} |
| 826 |
new UsersWP_Import_Export(); |