| 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 |
|
| 21 |
public function __construct() { |
| 22 |
global $wp_filesystem; |
| 23 |
|
| 24 |
if ( empty( $wp_filesystem ) ) { |
| 25 |
require_once( ABSPATH . '/wp-admin/includes/file.php' ); |
| 26 |
WP_Filesystem(); |
| 27 |
global $wp_filesystem; |
| 28 |
} |
| 29 |
|
| 30 |
$this->wp_filesystem = $wp_filesystem; |
| 31 |
$this->export_dir = $this->export_location(); |
| 32 |
$this->export_url = $this->export_location( true ); |
| 33 |
$this->per_page = apply_filters('uwp_import_export_per_page', 20, $this); |
| 34 |
$this->meta_table_name = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 35 |
$this->path = ''; |
| 36 |
|
| 37 |
add_action( 'userswp_settings_import-export_tab_content', array($this, 'get_ie_content') ); |
| 38 |
add_action( 'admin_init', array($this, 'uwp_process_settings_export') ); |
| 39 |
add_action( 'admin_init', array($this, 'uwp_process_settings_import') ); |
| 40 |
add_action( 'wp_ajax_uwp_ajax_export_users', array( $this, 'uwp_process_users_export' ) ); |
| 41 |
add_action( 'wp_ajax_uwp_ajax_import_users', array( $this, 'uwp_process_users_import' ) ); |
| 42 |
add_action( 'wp_ajax_uwp_ie_upload_file', array( $this, 'uwp_ie_upload_file' ) ); |
| 43 |
add_action( 'wp_ajax_nopriv_uwp_ie_upload_file', array( $this, 'uwp_ie_upload_file' ) ); |
| 44 |
add_action( 'admin_notices', array($this, 'uwp_ie_admin_notice') ); |
| 45 |
add_filter( 'uwp_get_export_users_status', array( $this, 'uwp_get_export_users_status' ) ); |
| 46 |
add_filter( 'uwp_get_import_users_status', array( $this, 'uwp_get_import_users_status' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
public function get_ie_content() { |
| 50 |
$subtab = 'ie-users'; |
| 51 |
|
| 52 |
if (isset($_GET['subtab'])) { |
| 53 |
$subtab = $_GET['subtab']; |
| 54 |
} |
| 55 |
?> |
| 56 |
<div class="item-list-sub-tabs"> |
| 57 |
<ul class="item-list-sub-tabs-ul"> |
| 58 |
<li class="<?php if ($subtab == 'ie-users') { echo "current selected"; } ?>"> |
| 59 |
<a href="<?php echo add_query_arg(array('tab' => 'import-export', 'subtab' => 'ie-users')); ?>"><?php echo __( 'Users', 'userswp' ); ?></a> |
| 60 |
</li> |
| 61 |
<li class="<?php if ($subtab == 'ie-settings') { echo "current selected"; } ?>"> |
| 62 |
<a href="<?php echo add_query_arg(array('tab' => 'import-export', 'subtab' => 'ie-settings')); ?>"><?php echo __( 'Settings', 'userswp' ); ?></a> |
| 63 |
</li> |
| 64 |
</ul> |
| 65 |
</div> |
| 66 |
<?php |
| 67 |
if ($subtab == 'ie-users') { |
| 68 |
include_once( USERSWP_PATH . '/admin/settings/admin-settings-ie-users.php' ); |
| 69 |
} elseif ($subtab == 'ie-settings') { |
| 70 |
include_once( USERSWP_PATH . '/admin/settings/admin-settings-ie-settings.php' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function export_location( $relative = false ) { |
| 75 |
$upload_dir = wp_upload_dir(); |
| 76 |
$export_location = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache'; |
| 77 |
$export_location = apply_filters( 'uwp_export_location', $export_location, $relative ); |
| 78 |
|
| 79 |
return trailingslashit( $export_location ); |
| 80 |
} |
| 81 |
|
| 82 |
public function uwp_ie_admin_notice(){ |
| 83 |
if(isset($_GET['imp-msg']) && 'success' == $_GET['imp-msg']){ |
| 84 |
?> |
| 85 |
<div class="notice notice-success is-dismissible"> |
| 86 |
<p><?php _e( 'Settings imported successfully!', 'userswp' ); ?></p> |
| 87 |
</div> |
| 88 |
<?php |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Process a settings export that generates a .json file of the settings |
| 94 |
*/ |
| 95 |
public function uwp_process_settings_export() { |
| 96 |
if( empty( $_POST['uwp_ie_action'] ) || 'export_settings' != $_POST['uwp_ie_action'] ) |
| 97 |
return; |
| 98 |
if( ! wp_verify_nonce( $_POST['uwp_export_nonce'], 'uwp_export_nonce' ) ) |
| 99 |
return; |
| 100 |
if( ! current_user_can( 'manage_options' ) ) |
| 101 |
return; |
| 102 |
$settings = get_option( 'uwp_settings' ); |
| 103 |
ignore_user_abort( true ); |
| 104 |
nocache_headers(); |
| 105 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 106 |
header( 'Content-Disposition: attachment; filename=uwp-settings-export-' . date( 'm-d-Y' ) . '.json' ); |
| 107 |
header( "Expires: 0" ); |
| 108 |
echo json_encode( $settings ); |
| 109 |
exit; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Process a settings import from a json file |
| 114 |
*/ |
| 115 |
public function uwp_process_settings_import() { |
| 116 |
if( empty( $_POST['uwp_ie_action'] ) || 'import_settings' != $_POST['uwp_ie_action'] ) |
| 117 |
return; |
| 118 |
if( ! wp_verify_nonce( $_POST['uwp_import_nonce'], 'uwp_import_nonce' ) ) |
| 119 |
return; |
| 120 |
if( ! current_user_can( 'manage_options' ) ) |
| 121 |
return; |
| 122 |
$extension = end( explode( '.', $_FILES['import_file']['name'] ) ); |
| 123 |
if( $extension != 'json' ) { |
| 124 |
wp_die( sprintf(__( 'Please upload a valid .json file. %sGo Back%s' ), '<a href="'.admin_url( 'admin.php?page=userswp&tab=import-export&subtab=ie-settings' ).'">', '</a>' )); |
| 125 |
} |
| 126 |
$import_file = $_FILES['import_file']['tmp_name']; |
| 127 |
if( empty( $import_file ) ) { |
| 128 |
wp_die( sprintf(__( 'Please upload a file to import. %sGo Back%s' ), '<a href="'.admin_url( 'admin.php?page=userswp&tab=import-export&subtab=ie-settings' ).'">', '</a>' )); |
| 129 |
} |
| 130 |
// Retrieve the settings from the file and convert the json object to an array. |
| 131 |
$settings = (array) json_decode( file_get_contents( $import_file ), true ); |
| 132 |
update_option( 'uwp_settings', $settings ); |
| 133 |
wp_safe_redirect( admin_url( 'admin.php?page=userswp&tab=import-export&subtab=ie-settings&imp-msg=success' ) ); exit; |
| 134 |
} |
| 135 |
|
| 136 |
public function uwp_process_users_export(){ |
| 137 |
|
| 138 |
$response = array(); |
| 139 |
$response['success'] = false; |
| 140 |
$response['msg'] = __( 'Invalid export request found.', 'userswp' ); |
| 141 |
|
| 142 |
if ( empty( $_POST['data'] ) || !current_user_can( 'manage_options' ) ) { |
| 143 |
wp_send_json( $response ); |
| 144 |
} |
| 145 |
|
| 146 |
parse_str( $_POST['data'], $data ); |
| 147 |
|
| 148 |
$data['step'] = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
| 149 |
|
| 150 |
$_REQUEST = (array)$data; |
| 151 |
if ( !( !empty( $_REQUEST['uwp_export_users_nonce'] ) && wp_verify_nonce( $_REQUEST['uwp_export_users_nonce'], 'uwp_export_users_nonce' ) ) ) { |
| 152 |
$response['msg'] = __( 'Security check failed.', 'userswp' ); |
| 153 |
wp_send_json( $response ); |
| 154 |
} |
| 155 |
|
| 156 |
if ( ( $error = $this->check_export_location() ) !== true ) { |
| 157 |
$response['msg'] = __( 'Filesystem ERROR: ' . $error, 'userswp' ); |
| 158 |
wp_send_json( $response ); |
| 159 |
} |
| 160 |
|
| 161 |
$this->set_export_params( $_REQUEST ); |
| 162 |
|
| 163 |
$return = $this->process_export_step(); |
| 164 |
$done = $this->get_export_status(); |
| 165 |
|
| 166 |
if ( $return ) { |
| 167 |
$this->step += 1; |
| 168 |
|
| 169 |
$response['success'] = true; |
| 170 |
$response['msg'] = ''; |
| 171 |
|
| 172 |
if ( $done >= 100 ) { |
| 173 |
$this->step = 'done'; |
| 174 |
$new_filename = 'uwp-users-export-' . date( 'y-m-d-H-i' ) . '.csv'; |
| 175 |
$new_file = $this->export_dir . $new_filename; |
| 176 |
|
| 177 |
if ( file_exists( $this->file ) ) { |
| 178 |
$this->wp_filesystem->move( $this->file, $new_file, true ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( file_exists( $new_file ) ) { |
| 182 |
$response['data']['file'] = array( 'u' => $this->export_url . $new_filename, 's' => size_format( filesize( $new_file ), 2 ) ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
$response['data']['step'] = $this->step; |
| 187 |
$response['data']['done'] = $done; |
| 188 |
} else { |
| 189 |
$response['msg'] = __( 'No data found for export.', 'userswp' ); |
| 190 |
} |
| 191 |
|
| 192 |
wp_send_json( $response ); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
public function set_export_params( $request ) { |
| 197 |
$this->empty = false; |
| 198 |
$this->step = !empty( $request['step'] ) ? absint( $request['step'] ) : 1; |
| 199 |
$this->filename = 'uwp-users-export-temp.csv'; |
| 200 |
$this->file = $this->export_dir . $this->filename; |
| 201 |
$chunk_per_page = !empty( $request['uwp_ie_chunk_size'] ) ? absint( $request['uwp_ie_chunk_size'] ) : 0; |
| 202 |
$this->per_page = $chunk_per_page < 50 || $chunk_per_page > 100000 ? 5000 : $chunk_per_page; |
| 203 |
|
| 204 |
do_action( 'uwp_export_users_set_params', $request ); |
| 205 |
} |
| 206 |
|
| 207 |
public function check_export_location() { |
| 208 |
try { |
| 209 |
if ( empty( $this->wp_filesystem ) ) { |
| 210 |
return __( 'Filesystem ERROR: Could not access filesystem.', 'userswp' ); |
| 211 |
} |
| 212 |
|
| 213 |
if ( is_wp_error( $this->wp_filesystem ) ) { |
| 214 |
return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'userswp' ); |
| 215 |
} |
| 216 |
|
| 217 |
$is_dir = $this->wp_filesystem->is_dir( $this->export_dir ); |
| 218 |
$is_writeable = $is_dir && is_writeable( $this->export_dir ); |
| 219 |
|
| 220 |
if ( $is_dir && $is_writeable ) { |
| 221 |
return true; |
| 222 |
} else if ( $is_dir && !$is_writeable ) { |
| 223 |
if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 224 |
return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'userswp' ), $this->export_dir ); |
| 225 |
} |
| 226 |
|
| 227 |
return true; |
| 228 |
} else { |
| 229 |
if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 230 |
return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'userswp' ), $this->export_dir ); |
| 231 |
} |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
} catch ( Exception $e ) { |
| 236 |
return $e->getMessage(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
public function process_export_step() { |
| 241 |
if ( $this->step < 2 ) { |
| 242 |
@unlink( $this->file ); |
| 243 |
$this->print_columns(); |
| 244 |
} |
| 245 |
|
| 246 |
$return = $this->print_rows(); |
| 247 |
|
| 248 |
if ( $return ) { |
| 249 |
return true; |
| 250 |
} else { |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
public function print_columns() { |
| 256 |
$column_data = ''; |
| 257 |
$columns = $this->get_columns(); |
| 258 |
$i = 1; |
| 259 |
foreach( $columns as $key => $column ) { |
| 260 |
$column_data .= '"' . addslashes( $column ) . '"'; |
| 261 |
$column_data .= $i == count( $columns ) ? '' : ','; |
| 262 |
$i++; |
| 263 |
} |
| 264 |
$column_data .= "\r\n"; |
| 265 |
|
| 266 |
$this->attach_export_data( $column_data ); |
| 267 |
|
| 268 |
return $column_data; |
| 269 |
} |
| 270 |
|
| 271 |
public function get_columns() { |
| 272 |
global $wpdb; |
| 273 |
$columns = array(); |
| 274 |
|
| 275 |
foreach ( $wpdb->get_col( "DESC " . $this->meta_table_name, 0 ) as $column_name ) { |
| 276 |
$columns[] = $column_name; |
| 277 |
} |
| 278 |
|
| 279 |
return apply_filters( 'uwp_export_users_get_columns', $columns ); |
| 280 |
} |
| 281 |
|
| 282 |
public function get_export_data() { |
| 283 |
global $wpdb; |
| 284 |
if(!$this->step){ |
| 285 |
$page = 1; |
| 286 |
} else { |
| 287 |
$page = $this->step; |
| 288 |
} |
| 289 |
|
| 290 |
$page_start = absint( ( $page - 1 ) * $this->per_page ); |
| 291 |
$data = $wpdb->get_results( "SELECT * FROM $this->meta_table_name WHERE 1=1 LIMIT $page_start,". $this->per_page); |
| 292 |
$i = 0; |
| 293 |
|
| 294 |
foreach ($data as $u){ |
| 295 |
$user = get_userdata($u->user_id); |
| 296 |
$data[$i]->uwp_account_username = $user->user_login; |
| 297 |
$data[$i]->uwp_account_email = $user->user_email; |
| 298 |
$data[$i]->uwp_account_bio = $user->description; |
| 299 |
$i++; |
| 300 |
} |
| 301 |
|
| 302 |
return apply_filters( 'uwp_export_users_get_data', $data ); |
| 303 |
} |
| 304 |
|
| 305 |
public function get_export_status() { |
| 306 |
$status = 100; |
| 307 |
return apply_filters( 'uwp_get_export_users_status', $status ); |
| 308 |
} |
| 309 |
|
| 310 |
public function print_rows() { |
| 311 |
$row_data = ''; |
| 312 |
$data = $this->get_export_data(); |
| 313 |
$columns = $this->get_columns(); |
| 314 |
|
| 315 |
if ( $data ) { |
| 316 |
foreach ( $data as $row ) { |
| 317 |
$i = 1; |
| 318 |
foreach ( $row as $key => $column ) { |
| 319 |
$row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"'; |
| 320 |
$row_data .= $i == count( $columns ) ? '' : ','; |
| 321 |
$i++; |
| 322 |
} |
| 323 |
$row_data .= "\r\n"; |
| 324 |
} |
| 325 |
|
| 326 |
$this->attach_export_data( $row_data ); |
| 327 |
|
| 328 |
return $row_data; |
| 329 |
} |
| 330 |
|
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
protected function get_export_file() { |
| 335 |
$file = ''; |
| 336 |
|
| 337 |
if ( $this->wp_filesystem->exists( $this->file ) ) { |
| 338 |
$file = $this->wp_filesystem->get_contents( $this->file ); |
| 339 |
} else { |
| 340 |
$this->wp_filesystem->put_contents( $this->file, '' ); |
| 341 |
} |
| 342 |
|
| 343 |
return $file; |
| 344 |
} |
| 345 |
|
| 346 |
protected function attach_export_data( $data = '' ) { |
| 347 |
$filedata = $this->get_export_file(); |
| 348 |
$filedata .= $data; |
| 349 |
|
| 350 |
$this->wp_filesystem->put_contents( $this->file, $filedata ); |
| 351 |
|
| 352 |
$rows = file( $this->file, FILE_SKIP_EMPTY_LINES ); |
| 353 |
$columns = $this->get_columns(); |
| 354 |
$columns = empty( $columns ) ? 0 : 1; |
| 355 |
|
| 356 |
$this->empty = count( $rows ) == $columns ? true : false; |
| 357 |
} |
| 358 |
|
| 359 |
public function uwp_get_export_users_status() { |
| 360 |
global $wpdb; |
| 361 |
$data = $wpdb->get_results("SELECT user_id FROM $this->meta_table_name WHERE 1=1"); |
| 362 |
$total = !empty( $data ) ? count( $data ) : 0; |
| 363 |
$status = 100; |
| 364 |
|
| 365 |
if ( $this->per_page > $total ) { |
| 366 |
$this->per_page = $total; |
| 367 |
} |
| 368 |
|
| 369 |
if ( $total > 0 ) { |
| 370 |
$status = ( ( $this->per_page * $this->step ) / $total ) * 100; |
| 371 |
} |
| 372 |
|
| 373 |
if ( $status > 100 ) { |
| 374 |
$status = 100; |
| 375 |
} |
| 376 |
|
| 377 |
return $status; |
| 378 |
} |
| 379 |
|
| 380 |
public function uwp_ie_upload_file(){ |
| 381 |
$upload_data = array( |
| 382 |
'name' => $_FILES['import_file']['name'], |
| 383 |
'type' => $_FILES['import_file']['type'], |
| 384 |
'tmp_name' => $_FILES['import_file']['tmp_name'], |
| 385 |
'error' => $_FILES['import_file']['error'], |
| 386 |
'size' => $_FILES['import_file']['size'] |
| 387 |
); |
| 388 |
|
| 389 |
header('Content-Type: text/html; charset=' . get_option('blog_charset')); |
| 390 |
|
| 391 |
$uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) ); |
| 392 |
if ( isset( $uploaded_file['url'] ) ) { |
| 393 |
$file_loc = $uploaded_file['url']; |
| 394 |
echo $file_loc; |
| 395 |
} else { |
| 396 |
echo 'error'; |
| 397 |
} |
| 398 |
exit; |
| 399 |
} |
| 400 |
|
| 401 |
public function uwp_process_users_import(){ |
| 402 |
|
| 403 |
$response = array(); |
| 404 |
$response['success'] = false; |
| 405 |
$response['msg'] = __( 'Invalid import request found.', 'userswp' ); |
| 406 |
|
| 407 |
if ( empty( $_POST['data'] ) || !current_user_can( 'manage_options' ) ) { |
| 408 |
wp_send_json( $response ); |
| 409 |
} |
| 410 |
|
| 411 |
parse_str( $_POST['data'], $data ); |
| 412 |
|
| 413 |
$this->imp_step = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
| 414 |
|
| 415 |
$_REQUEST = (array)$data; |
| 416 |
if ( !( !empty( $_REQUEST['uwp_import_users_nonce'] ) && wp_verify_nonce( $_REQUEST['uwp_import_users_nonce'], 'uwp_import_users_nonce' ) ) ) { |
| 417 |
$response['msg'] = __( 'Security check failed.', 'userswp' ); |
| 418 |
wp_send_json( $response ); |
| 419 |
} |
| 420 |
|
| 421 |
$allowed = array('csv'); |
| 422 |
$import_file = $data['uwp_import_users_file']; |
| 423 |
$uploads = wp_upload_dir(); |
| 424 |
$csv_file_array = explode( '/', $import_file ); |
| 425 |
$csv_filename = end( $csv_file_array ); |
| 426 |
$this->path = $uploads['path'].'/'.$csv_filename; |
| 427 |
|
| 428 |
$ext = pathinfo($csv_filename, PATHINFO_EXTENSION); |
| 429 |
if (!in_array($ext, $allowed)) { |
| 430 |
$response['msg'] = __( 'Invalid file type, please upload .csv file.', 'userswp' ); |
| 431 |
wp_send_json( $response ); |
| 432 |
} |
| 433 |
|
| 434 |
$lc_all = setlocale( LC_ALL, 0 ); |
| 435 |
setlocale( LC_ALL, 'en_US.UTF-8' ); |
| 436 |
if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) { |
| 437 |
while ( ( $data = fgetcsv( $handle, 100000, "," ) ) !== false ) { |
| 438 |
if ( ! empty( $data ) ) { |
| 439 |
$file[] = $data; |
| 440 |
} |
| 441 |
} |
| 442 |
fclose( $handle ); |
| 443 |
} |
| 444 |
setlocale( LC_ALL, $lc_all ); |
| 445 |
|
| 446 |
$this->total_rows = ( ! empty( $file ) && count( $file ) > 1 ) ? count( $file ) - 1 : 0; |
| 447 |
|
| 448 |
$return = $this->process_import_step(); |
| 449 |
$done = $this->get_import_status(); |
| 450 |
|
| 451 |
if ( $return['success'] ) { |
| 452 |
$response['total']['msg'] = ''; |
| 453 |
if($this->total_rows > 0 && $this->imp_step == 1) { |
| 454 |
$response['total']['msg'] = __( 'Total '.$this->total_rows. ' item(s) found.', 'userswp' ); |
| 455 |
} |
| 456 |
|
| 457 |
$this->imp_step += 1; |
| 458 |
|
| 459 |
$response['success'] = true; |
| 460 |
$response['msg'] = ''; |
| 461 |
|
| 462 |
if ( $done >= 100 ) { |
| 463 |
$this->imp_step = 'done'; |
| 464 |
$response['msg'] = __( 'Users import completed.', 'userswp' ); |
| 465 |
} |
| 466 |
|
| 467 |
$response['data']['msg'] = $return['msg']; |
| 468 |
$response['data']['step'] = $this->imp_step; |
| 469 |
$response['data']['done'] = $done; |
| 470 |
} else { |
| 471 |
$response['msg'] = __( 'No valid data found for import.', 'userswp' ); |
| 472 |
} |
| 473 |
|
| 474 |
wp_send_json( $response ); |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
public function process_import_step() { |
| 479 |
|
| 480 |
$errors = new WP_Error(); |
| 481 |
if(is_null($this->path)){ |
| 482 |
$errors->add('no_csv_file', __('No csv file found.','userswp')); |
| 483 |
} |
| 484 |
|
| 485 |
set_time_limit(0); |
| 486 |
|
| 487 |
$return = array('success' => false); |
| 488 |
|
| 489 |
$rows = $this->get_csv_rows($this->imp_step, 1); |
| 490 |
|
| 491 |
if ( ! empty( $rows ) ) { |
| 492 |
foreach ( $rows as $row ) { |
| 493 |
if( empty($row) ) { |
| 494 |
$return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'Skipped due to invalid/no data.','userswp'); |
| 495 |
continue; |
| 496 |
} |
| 497 |
|
| 498 |
$username = isset($row['uwp_account_username']) ? $row['uwp_account_username'] : ''; |
| 499 |
$email = isset($row['uwp_account_email']) ? $row['uwp_account_email'] : ''; |
| 500 |
$first_name = isset($row['uwp_account_first_name']) ? $row['uwp_account_first_name'] : ''; |
| 501 |
$last_name = isset($row['uwp_account_last_name']) ? $row['uwp_account_last_name'] : ''; |
| 502 |
$bio = isset($row['uwp_account_bio']) ? $row['uwp_account_bio'] : ''; |
| 503 |
$display_name = isset($row['uwp_account_display_name']) ? $row['uwp_account_display_name'] : ''; |
| 504 |
$password = wp_generate_password(); |
| 505 |
$exclude = array('user_id'); |
| 506 |
$exclude = apply_filters('uwp_import_exclude_columns', $exclude, $row); |
| 507 |
|
| 508 |
if(isset($row['uwp_account_username']) && username_exists($row['uwp_account_username'])){ |
| 509 |
$user = get_user_by('login', $row['uwp_account_username']); |
| 510 |
$user_id = $user->ID; |
| 511 |
$email = $row['uwp_account_email']; |
| 512 |
if( !empty( $email ) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id) ) { |
| 513 |
$args = array( |
| 514 |
'ID' => $user_id, |
| 515 |
'user_email' => $email, |
| 516 |
'display_name' => $display_name, |
| 517 |
'first_name' => $first_name, |
| 518 |
'last_name' => $last_name, |
| 519 |
'description' => $bio, |
| 520 |
); |
| 521 |
wp_update_user( $args ); |
| 522 |
} |
| 523 |
} elseif(isset($row['uwp_account_email']) && email_exists($row['uwp_account_email'])){ |
| 524 |
$user = get_user_by('email', $row['uwp_account_email']); |
| 525 |
$user_id = $user->ID; |
| 526 |
} elseif((int)$row['user_id'] > 0){ |
| 527 |
$user = get_user_by('ID', $row['user_id']); |
| 528 |
if(false === $user){ |
| 529 |
$userdata = array( |
| 530 |
'user_login' => $row['uwp_account_username'], |
| 531 |
'user_email' => $email, |
| 532 |
'user_pass' => $password, |
| 533 |
'first_name' => $first_name, |
| 534 |
'last_name' => $last_name, |
| 535 |
'description' => $bio, |
| 536 |
'display_name'=> $display_name |
| 537 |
); |
| 538 |
$user_id = wp_insert_user( $userdata ); |
| 539 |
wp_new_user_notification($user_id,null, 'user'); //send password reset link |
| 540 |
} else { |
| 541 |
if( $user->user_login == $row['uwp_account_username'] ) { //check id passed in csv and existing username are same |
| 542 |
$user_id = $row['user_id']; |
| 543 |
if( !empty( $email ) && $email != $user->user_email && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)) { |
| 544 |
$args = array( |
| 545 |
'ID' => $user_id, |
| 546 |
'user_email' => $email, |
| 547 |
'first_name' => $first_name, |
| 548 |
'last_name' => $last_name, |
| 549 |
'description' => $bio, |
| 550 |
'display_name' => $display_name |
| 551 |
); |
| 552 |
wp_update_user( $args ); |
| 553 |
} |
| 554 |
} else { |
| 555 |
$return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'User could not be created.','userswp'); |
| 556 |
continue; |
| 557 |
} |
| 558 |
} |
| 559 |
} else { |
| 560 |
$user_id = wp_create_user( $username, $password, $email ); |
| 561 |
} |
| 562 |
|
| 563 |
if( !is_wp_error( $user_id ) ){ |
| 564 |
foreach ($row as $key => $value){ |
| 565 |
if(!in_array($key, $exclude) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)){ |
| 566 |
$value = maybe_unserialize($value); |
| 567 |
uwp_update_usermeta($user_id, $key, $value); |
| 568 |
} |
| 569 |
} |
| 570 |
} else { |
| 571 |
$return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. $user_id->get_error_message(),'userswp'); |
| 572 |
continue; |
| 573 |
} |
| 574 |
} |
| 575 |
$return['success'] = true; |
| 576 |
} |
| 577 |
|
| 578 |
return $return; |
| 579 |
} |
| 580 |
|
| 581 |
public function get_csv_rows( $row = 0, $count = 1 ) { |
| 582 |
|
| 583 |
$lc_all = setlocale( LC_ALL, 0 ); // Fix issue of fgetcsv ignores special characters when they are at the beginning of line |
| 584 |
setlocale( LC_ALL, 'en_US.UTF-8' ); |
| 585 |
$l = $f =0; |
| 586 |
$headers = $file = array(); |
| 587 |
$userdata_fields = $this->get_columns(); |
| 588 |
if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) { |
| 589 |
while ( ( $line = fgetcsv( $handle, 100000, "," ) ) !== false ) { |
| 590 |
// If the first line is empty, abort |
| 591 |
// If another line is empty, just skip it |
| 592 |
if ( empty( $line ) ) { |
| 593 |
if ( $l === 0 ) |
| 594 |
break; |
| 595 |
else |
| 596 |
continue; |
| 597 |
} |
| 598 |
|
| 599 |
// If we are on the first line, the columns are the headers |
| 600 |
if ( $l === 0 ) { |
| 601 |
$headers = $line; |
| 602 |
$l ++; |
| 603 |
continue; |
| 604 |
} |
| 605 |
|
| 606 |
// only get the rows needed |
| 607 |
if ( $row && $count ) { |
| 608 |
|
| 609 |
// if we have everything we need then break; |
| 610 |
if ( $l == $row + $count ) { |
| 611 |
break; |
| 612 |
|
| 613 |
// if its less than the start row then continue; |
| 614 |
} elseif ( $l && $l < $row ) { |
| 615 |
$l ++; |
| 616 |
continue; |
| 617 |
|
| 618 |
// if we have the count we need then break; |
| 619 |
} elseif ( $f > $count ) { |
| 620 |
break; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
// Separate user data from meta |
| 625 |
$userdata = $usermeta = array(); |
| 626 |
foreach ( $line as $ckey => $column ) { |
| 627 |
$column_name = $headers[$ckey]; |
| 628 |
$column = trim( $column ); |
| 629 |
if ( in_array( $column_name, $userdata_fields ) ) { |
| 630 |
$userdata[$column_name] = $column; |
| 631 |
} else { |
| 632 |
$usermeta[$column_name] = $column; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
// A plugin may need to filter the data and meta |
| 637 |
$userdata = apply_filters( 'uwp_import_userdata', $userdata, $usermeta ); |
| 638 |
$usermeta = apply_filters( 'uwp_import_usermeta', $usermeta, $userdata ); |
| 639 |
|
| 640 |
if ( ! empty( $userdata ) ) { |
| 641 |
$file[] = $userdata; |
| 642 |
$f ++; |
| 643 |
$l ++; |
| 644 |
} |
| 645 |
} |
| 646 |
fclose( $handle ); |
| 647 |
} |
| 648 |
setlocale( LC_ALL, $lc_all ); |
| 649 |
|
| 650 |
return $file; |
| 651 |
|
| 652 |
} |
| 653 |
|
| 654 |
public function get_import_status() { |
| 655 |
$status = 100; |
| 656 |
return apply_filters( 'uwp_get_import_users_status', $status ); |
| 657 |
} |
| 658 |
|
| 659 |
public function uwp_get_import_users_status() { |
| 660 |
|
| 661 |
if ( $this->imp_step >= $this->total_rows ) { |
| 662 |
$status = 100; |
| 663 |
} else { |
| 664 |
$status = ( ( 1 * $this->imp_step ) / $this->total_rows ) * 100; |
| 665 |
} |
| 666 |
|
| 667 |
return $status; |
| 668 |
} |
| 669 |
|
| 670 |
} |
| 671 |
new UsersWP_Import_Export(); |