PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.0.18
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.0.18
1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 All 173 releases
userswp / includes / class-import-export.php

class-import-export.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.0.18, at includes/class-import-export.php

636 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
202 do_action( 'uwp_export_users_set_params', $request );
203 }
204
205 public function check_export_location() {
206 try {
207 if ( empty( $this->wp_filesystem ) ) {
208 return __( 'Filesystem ERROR: Could not access filesystem.', 'userswp' );
209 }
210
211 if ( is_wp_error( $this->wp_filesystem ) ) {
212 return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'userswp' );
213 }
214
215 $is_dir = $this->wp_filesystem->is_dir( $this->export_dir );
216 $is_writeable = $is_dir && is_writeable( $this->export_dir );
217
218 if ( $is_dir && $is_writeable ) {
219 return true;
220 } else if ( $is_dir && !$is_writeable ) {
221 if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) {
222 return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'userswp' ), $this->export_dir );
223 }
224
225 return true;
226 } else {
227 if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) {
228 return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'userswp' ), $this->export_dir );
229 }
230
231 return true;
232 }
233 } catch ( Exception $e ) {
234 return $e->getMessage();
235 }
236 }
237
238 public function process_export_step() {
239 if ( $this->step < 2 ) {
240 @unlink( $this->file );
241 $this->print_columns();
242 }
243
244 $return = $this->print_rows();
245
246 if ( $return ) {
247 return true;
248 } else {
249 return false;
250 }
251 }
252
253 public function print_columns() {
254 $column_data = '';
255 $columns = $this->get_columns();
256 $i = 1;
257 foreach( $columns as $key => $column ) {
258 $column_data .= '"' . addslashes( $column ) . '"';
259 $column_data .= $i == count( $columns ) ? '' : ',';
260 $i++;
261 }
262 $column_data .= "\r\n";
263
264 $this->attach_export_data( $column_data );
265
266 return $column_data;
267 }
268
269 public function get_columns() {
270 global $wpdb;
271 $columns = array();
272
273 foreach ( $wpdb->get_col( "DESC " . $this->meta_table_name, 0 ) as $column_name ) {
274 $columns[] = $column_name;
275 }
276
277 return apply_filters( 'uwp_export_users_get_columns', $columns );
278 }
279
280 public function get_export_data() {
281 global $wpdb;
282 $data = $wpdb->get_results( "SELECT * FROM $this->meta_table_name WHERE 1=1 LIMIT 0,". $this->per_page);
283
284 return apply_filters( 'uwp_export_users_get_data', $data );
285 }
286
287 public function get_export_status() {
288 $status = 100;
289 return apply_filters( 'uwp_get_export_users_status', $status );
290 }
291
292 public function print_rows() {
293 $row_data = '';
294 $data = $this->get_export_data();
295 $columns = $this->get_columns();
296
297 if ( $data ) {
298 foreach ( $data as $row ) {
299 $i = 1;
300 foreach ( $row as $key => $column ) {
301 $row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"';
302 $row_data .= $i == count( $columns ) ? '' : ',';
303 $i++;
304 }
305 $row_data .= "\r\n";
306 }
307
308 $this->attach_export_data( $row_data );
309
310 return $row_data;
311 }
312
313 return false;
314 }
315
316 protected function get_export_file() {
317 $file = '';
318
319 if ( $this->wp_filesystem->exists( $this->file ) ) {
320 $file = $this->wp_filesystem->get_contents( $this->file );
321 } else {
322 $this->wp_filesystem->put_contents( $this->file, '' );
323 }
324
325 return $file;
326 }
327
328 protected function attach_export_data( $data = '' ) {
329 $filedata = $this->get_export_file();
330 $filedata .= $data;
331
332 $this->wp_filesystem->put_contents( $this->file, $filedata );
333
334 $rows = file( $this->file, FILE_SKIP_EMPTY_LINES );
335 $columns = $this->get_columns();
336 $columns = empty( $columns ) ? 0 : 1;
337
338 $this->empty = count( $rows ) == $columns ? true : false;
339 }
340
341 public function uwp_get_export_users_status() {
342 global $wpdb;
343 $data = $wpdb->get_results("SELECT user_id FROM $this->meta_table_name WHERE 1=1");
344 $total = !empty( $data ) ? count( $data ) : 0;
345 $status = 100;
346
347 if ( $total > 0 ) {
348 $status = ( ( $this->per_page * $this->step ) / $total ) * 100;
349 }
350
351 if ( $status > 100 ) {
352 $status = 100;
353 }
354
355 return $status;
356 }
357
358 public function uwp_ie_upload_file(){
359 $upload_data = array(
360 'name' => $_FILES['import_file']['name'],
361 'type' => $_FILES['import_file']['type'],
362 'tmp_name' => $_FILES['import_file']['tmp_name'],
363 'error' => $_FILES['import_file']['error'],
364 'size' => $_FILES['import_file']['size']
365 );
366
367 header('Content-Type: text/html; charset=' . get_option('blog_charset'));
368
369 $uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) );
370 if ( isset( $uploaded_file['url'] ) ) {
371 $file_loc = $uploaded_file['url'];
372 echo $file_loc;
373 } else {
374 echo 'error';
375 }
376 exit;
377 }
378
379 public function uwp_process_users_import(){
380
381 $response = array();
382 $response['success'] = false;
383 $response['msg'] = __( 'Invalid import request found.', 'userswp' );
384
385 if ( empty( $_POST['data'] ) || !current_user_can( 'manage_options' ) ) {
386 wp_send_json( $response );
387 }
388
389 parse_str( $_POST['data'], $data );
390
391 $this->imp_step = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1;
392
393 $_REQUEST = (array)$data;
394 if ( !( !empty( $_REQUEST['uwp_import_users_nonce'] ) && wp_verify_nonce( $_REQUEST['uwp_import_users_nonce'], 'uwp_import_users_nonce' ) ) ) {
395 $response['msg'] = __( 'Security check failed.', 'userswp' );
396 wp_send_json( $response );
397 }
398
399 $allowed = array('csv');
400 $import_file = $data['uwp_import_users_file'];
401 $uploads = wp_upload_dir();
402 $csv_file_array = explode( '/', $import_file );
403 $csv_filename = end( $csv_file_array );
404 $this->path = $uploads['path'].'/'.$csv_filename;
405
406 $ext = pathinfo($csv_filename, PATHINFO_EXTENSION);
407 if (!in_array($ext, $allowed)) {
408 $response['msg'] = __( 'Invalid file type, please upload .csv file.', 'userswp' );
409 wp_send_json( $response );
410 }
411
412 $lc_all = setlocale( LC_ALL, 0 );
413 setlocale( LC_ALL, 'en_US.UTF-8' );
414 if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) {
415 while ( ( $data = fgetcsv( $handle, 100000, "," ) ) !== false ) {
416 if ( ! empty( $data ) ) {
417 $file[] = $data;
418 }
419 }
420 fclose( $handle );
421 }
422 setlocale( LC_ALL, $lc_all );
423
424 $this->total_rows = ( ! empty( $file ) && count( $file ) > 1 ) ? count( $file ) - 1 : 0;
425
426 $return = $this->process_import_step();
427 $done = $this->get_import_status();
428
429 if ( $return['success'] ) {
430 $response['total']['msg'] = '';
431 if($this->total_rows > 0 && $this->imp_step == 1) {
432 $response['total']['msg'] = __( 'Total '.$this->total_rows. ' item(s) found.', 'userswp' );
433 }
434
435 $this->imp_step += 1;
436
437 $response['success'] = true;
438 $response['msg'] = '';
439
440 if ( $done >= 100 ) {
441 $this->imp_step = 'done';
442 $response['msg'] = __( 'Users import completed.', 'userswp' );
443 }
444
445 $response['data']['msg'] = $return['msg'];
446 $response['data']['step'] = $this->imp_step;
447 $response['data']['done'] = $done;
448 } else {
449 $response['msg'] = __( 'No valid data found for import.', 'userswp' );
450 }
451
452 wp_send_json( $response );
453
454 }
455
456 public function process_import_step() {
457
458 $errors = new WP_Error();
459 if(is_null($this->path)){
460 $errors->add('no_csv_file', __('No csv file found.','userswp'));
461 }
462
463 set_time_limit(0);
464
465 $return = array('success' => false);
466
467 $rows = $this->get_csv_rows($this->imp_step, 1);
468
469 if ( ! empty( $rows ) ) {
470 foreach ( $rows as $row ) {
471 if( empty($row) ) {
472 $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'Skipped due to invalid/no data.','userswp');
473 continue;
474 }
475
476 $username = isset($row['uwp_account_username']) ? $row['uwp_account_username'] : '';
477 $email = isset($row['uwp_account_email']) ? $row['uwp_account_email'] : '';
478 $display_name = isset($row['uwp_account_display_name']) ? $row['uwp_account_display_name'] : '';
479 $password = wp_generate_password();
480 $exclude = array('user_id');
481 $exclude = apply_filters('uwp_import_exclude_columns', $exclude, $row);
482
483 if(isset($row['uwp_account_username']) && username_exists($row['uwp_account_username'])){
484 $user = get_user_by('login', $row['uwp_account_username']);
485 $user_id = $user->ID;
486 $email = $row['uwp_account_email'];
487 if( !empty( $email ) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id) ) {
488 $args = array(
489 'ID' => $user_id,
490 'user_email' => $email,
491 'display_name' => $display_name
492 );
493 wp_update_user( $args );
494 }
495 } elseif(isset($row['uwp_account_email']) && email_exists($row['uwp_account_email'])){
496 $user = get_user_by('email', $row['uwp_account_email']);
497 $user_id = $user->ID;
498 } elseif((int)$row['user_id'] > 0){
499 $user = get_user_by('ID', $row['user_id']);
500 if(false === $user){
501 $userdata = array(
502 'user_login' => $row['uwp_account_username'],
503 'user_email' => $email,
504 'user_pass' => $password,
505 'display_name'=> $display_name
506 );
507 $user_id = wp_insert_user( $userdata );
508 } else {
509 if( $user->user_login == $row['uwp_account_username'] ) { //check id passed in csv and existing username are same
510 $user_id = $row['user_id'];
511 if( !empty( $email ) && $email != $user->user_email && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)) {
512 $args = array(
513 'ID' => $user_id,
514 'user_email' => $email,
515 'display_name' => $display_name
516 );
517 wp_update_user( $args );
518 }
519 } else {
520 $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'User could not be created.','userswp');
521 continue;
522 }
523 }
524 } else {
525 $user_id = wp_create_user( $username, $password, $email );
526 }
527
528 if( !is_wp_error( $user_id ) ){
529 foreach ($row as $key => $value){
530 if(!in_array($key, $exclude) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)){
531 $value = maybe_unserialize($value);
532 uwp_update_usermeta($user_id, $key, $value);
533 }
534 }
535 } else {
536 $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. $user_id->get_error_message(),'userswp');
537 continue;
538 }
539 }
540 $return['success'] = true;
541 }
542
543 return $return;
544 }
545
546 public function get_csv_rows( $row = 0, $count = 1 ) {
547
548 $lc_all = setlocale( LC_ALL, 0 ); // Fix issue of fgetcsv ignores special characters when they are at the beginning of line
549 setlocale( LC_ALL, 'en_US.UTF-8' );
550 $l = $f =0;
551 $headers = $file = array();
552 $userdata_fields = $this->get_columns();
553 if ( ( $handle = fopen( $this->path, "r" ) ) !== false ) {
554 while ( ( $line = fgetcsv( $handle, 100000, "," ) ) !== false ) {
555 // If the first line is empty, abort
556 // If another line is empty, just skip it
557 if ( empty( $line ) ) {
558 if ( $l === 0 )
559 break;
560 else
561 continue;
562 }
563
564 // If we are on the first line, the columns are the headers
565 if ( $l === 0 ) {
566 $headers = $line;
567 $l ++;
568 continue;
569 }
570
571 // only get the rows needed
572 if ( $row && $count ) {
573
574 // if we have everything we need then break;
575 if ( $l == $row + $count ) {
576 break;
577
578 // if its less than the start row then continue;
579 } elseif ( $l && $l < $row ) {
580 $l ++;
581 continue;
582
583 // if we have the count we need then break;
584 } elseif ( $f > $count ) {
585 break;
586 }
587 }
588
589 // Separate user data from meta
590 $userdata = $usermeta = array();
591 foreach ( $line as $ckey => $column ) {
592 $column_name = $headers[$ckey];
593 $column = trim( $column );
594 if ( in_array( $column_name, $userdata_fields ) ) {
595 $userdata[$column_name] = $column;
596 } else {
597 $usermeta[$column_name] = $column;
598 }
599 }
600
601 // A plugin may need to filter the data and meta
602 $userdata = apply_filters( 'uwp_import_userdata', $userdata, $usermeta );
603 $usermeta = apply_filters( 'uwp_import_usermeta', $usermeta, $userdata );
604
605 if ( ! empty( $userdata ) ) {
606 $file[] = $userdata;
607 $f ++;
608 $l ++;
609 }
610 }
611 fclose( $handle );
612 }
613 setlocale( LC_ALL, $lc_all );
614
615 return $file;
616
617 }
618
619 public function get_import_status() {
620 $status = 100;
621 return apply_filters( 'uwp_get_import_users_status', $status );
622 }
623
624 public function uwp_get_import_users_status() {
625
626 if ( $this->imp_step >= $this->total_rows ) {
627 $status = 100;
628 } else {
629 $status = ( ( 1 * $this->imp_step ) / $this->total_rows ) * 100;
630 }
631
632 return $status;
633 }
634
635 }
636 new UsersWP_Import_Export();