| @@ -16,9 +16,14 @@ | ||
| 16 | 16 | public $path; |
| 17 | 17 | public $total_rows; |
| 18 | 18 | public $imp_step; |
| 19 | 19 | public $skipped; |
| 20 | + private $empty; | |
| 21 | + private $step; | |
| 22 | + private $file; | |
| 23 | + private $filename; | |
| 20 | 24 | |
| 25 | + | |
| 21 | 26 | public function __construct() { |
| 22 | 27 | global $wp_filesystem; |
| 23 | 28 | |
| 24 | 29 | if ( empty( $wp_filesystem ) ) { |
| @@ -33,45 +38,29 @@ | ||
| 33 | 38 | $this->per_page = apply_filters('uwp_import_export_per_page', 20, $this); |
| 34 | 39 | $this->meta_table_name = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 35 | 40 | $this->path = ''; |
| 36 | 41 | |
| 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' ) ); | |
| 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' ) ); | |
| 47 | 51 | } |
| 48 | 52 | |
| 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 | - | |
| 53 | + /** | |
| 54 | + * Returns export file location | |
| 55 | + * | |
| 56 | + * @package userswp | |
| 57 | + * | |
| 58 | + * @param bool $relative | |
| 59 | + * | |
| 60 | + * @return string | |
| 61 | + * | |
| 62 | + */ | |
| 74 | 63 | public function export_location( $relative = false ) { |
| 75 | 64 | $upload_dir = wp_upload_dir(); |
| 76 | 65 | $export_location = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache'; |
| 77 | 66 | $export_location = apply_filters( 'uwp_export_location', $export_location, $relative ); |
| @@ -78,13 +67,16 @@ | ||
| 78 | 67 | |
| 79 | 68 | return trailingslashit( $export_location ); |
| 80 | 69 | } |
| 81 | 70 | |
| 82 | - public function uwp_ie_admin_notice(){ | |
| 71 | + /** | |
| 72 | + * Displays notice | |
| 73 | + */ | |
| 74 | + public function ie_admin_notice(){ | |
| 83 | 75 | if(isset($_GET['imp-msg']) && 'success' == $_GET['imp-msg']){ |
| 84 | 76 | ?> |
| 85 | 77 | <div class="notice notice-success is-dismissible"> |
| 86 | - <p><?php _e( 'Settings imported successfully!', 'userswp' ); ?></p> | |
| 78 | + <p><?php esc_html_e( 'Settings imported successfully!', 'userswp' ); ?></p> | |
| 87 | 79 | </div> |
| 88 | 80 | <?php |
| 89 | 81 | } |
| 90 | 82 | } |
| @@ -91,9 +83,9 @@ | ||
| 91 | 83 | |
| 92 | 84 | /** |
| 93 | 85 | * Process a settings export that generates a .json file of the settings |
| 94 | 86 | */ |
| 95 | - public function uwp_process_settings_export() { | |
| 87 | + public function process_settings_export() { | |
| 96 | 88 | if( empty( $_POST['uwp_ie_action'] ) || 'export_settings' != $_POST['uwp_ie_action'] ) |
| 97 | 89 | return; |
| 98 | 90 | if( ! wp_verify_nonce( $_POST['uwp_export_nonce'], 'uwp_export_nonce' ) ) |
| 99 | 91 | return; |
| @@ -111,9 +103,9 @@ | ||
| 111 | 103 | |
| 112 | 104 | /** |
| 113 | 105 | * Process a settings import from a json file |
| 114 | 106 | */ |
| 115 | - public function uwp_process_settings_import() { | |
| 107 | + public function process_settings_import() { | |
| 116 | 108 | if( empty( $_POST['uwp_ie_action'] ) || 'import_settings' != $_POST['uwp_ie_action'] ) |
| 117 | 109 | return; |
| 118 | 110 | if( ! wp_verify_nonce( $_POST['uwp_import_nonce'], 'uwp_import_nonce' ) ) |
| 119 | 111 | return; |
| @@ -118,23 +110,27 @@ | ||
| 118 | 110 | if( ! wp_verify_nonce( $_POST['uwp_import_nonce'], 'uwp_import_nonce' ) ) |
| 119 | 111 | return; |
| 120 | 112 | if( ! current_user_can( 'manage_options' ) ) |
| 121 | 113 | return; |
| 122 | - $extension = end( explode( '.', $_FILES['import_file']['name'] ) ); | |
| 114 | + $extension = explode( '.', $_FILES['import_file']['name'] ); | |
| 115 | + $extension = end( $extension ); | |
| 123 | 116 | 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>' )); | |
| 117 | + wp_die( esc_html( wp_sprintf( __( 'Please upload a valid .json file. %sGo Back%s' ), '<a href="' . esc_url( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings' ) ) . '">', '</a>' ) ) ); | |
| 125 | 118 | } |
| 126 | 119 | $import_file = $_FILES['import_file']['tmp_name']; |
| 127 | 120 | 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>' )); | |
| 121 | + wp_die( esc_html( wp_sprintf( __( 'Please upload a file to import. %sGo Back%s' ), '<a href="' . esc_url( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings' ) ) . '">', '</a>' ) ) ); | |
| 129 | 122 | } |
| 130 | 123 | // Retrieve the settings from the file and convert the json object to an array. |
| 131 | 124 | $settings = (array) json_decode( file_get_contents( $import_file ), true ); |
| 132 | 125 | 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; | |
| 126 | + wp_safe_redirect( admin_url( 'admin.php?page=userswp&tab=import-export§ion=settings&imp-msg=success' ) ); exit; | |
| 134 | 127 | } |
| 135 | 128 | |
| 136 | - public function uwp_process_users_export(){ | |
| 129 | + /** | |
| 130 | + * Processes users export | |
| 131 | + */ | |
| 132 | + public function process_users_export(){ | |
| 137 | 133 | |
| 138 | 134 | $response = array(); |
| 139 | 135 | $response['success'] = false; |
| 140 | 136 | $response['msg'] = __( 'Invalid export request found.', 'userswp' ); |
| @@ -170,9 +166,9 @@ | ||
| 170 | 166 | $response['msg'] = ''; |
| 171 | 167 | |
| 172 | 168 | if ( $done >= 100 ) { |
| 173 | 169 | $this->step = 'done'; |
| 174 | - $new_filename = 'uwp-users-export-' . date( 'y-m-d-H-i' ) . '.csv'; | |
| 170 | + $new_filename = 'uwp-users-export-' . date( 'y-m-d-H-i' ) . '-' . wp_generate_password(12, false ) . '.csv'; | |
| 175 | 171 | $new_file = $this->export_dir . $new_filename; |
| 176 | 172 | |
| 177 | 173 | if ( file_exists( $this->file ) ) { |
| 178 | 174 | $this->wp_filesystem->move( $this->file, $new_file, true ); |
| @@ -192,8 +188,16 @@ | ||
| 192 | 188 | wp_send_json( $response ); |
| 193 | 189 | |
| 194 | 190 | } |
| 195 | 191 | |
| 192 | + /** | |
| 193 | + * Sets export params | |
| 194 | + * | |
| 195 | + * @package userswp | |
| 196 | + * | |
| 197 | + * @param array $request | |
| 198 | + * | |
| 199 | + */ | |
| 196 | 200 | public function set_export_params( $request ) { |
| 197 | 201 | $this->empty = false; |
| 198 | 202 | $this->step = !empty( $request['step'] ) ? absint( $request['step'] ) : 1; |
| 199 | 203 | $this->filename = 'uwp-users-export-temp.csv'; |
| @@ -203,8 +207,16 @@ | ||
| 203 | 207 | |
| 204 | 208 | do_action( 'uwp_export_users_set_params', $request ); |
| 205 | 209 | } |
| 206 | 210 | |
| 211 | + /** | |
| 212 | + * Returns export file location | |
| 213 | + * | |
| 214 | + * @package userswp | |
| 215 | + * | |
| 216 | + * @return bool | |
| 217 | + * | |
| 218 | + */ | |
| 207 | 219 | public function check_export_location() { |
| 208 | 220 | try { |
| 209 | 221 | if ( empty( $this->wp_filesystem ) ) { |
| 210 | 222 | return __( 'Filesystem ERROR: Could not access filesystem.', 'userswp' ); |
| @@ -217,27 +229,35 @@ | ||
| 217 | 229 | $is_dir = $this->wp_filesystem->is_dir( $this->export_dir ); |
| 218 | 230 | $is_writeable = $is_dir && is_writeable( $this->export_dir ); |
| 219 | 231 | |
| 220 | 232 | if ( $is_dir && $is_writeable ) { |
| 221 | - return true; | |
| 233 | + | |
| 222 | 234 | } else if ( $is_dir && !$is_writeable ) { |
| 223 | 235 | if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 224 | 236 | return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'userswp' ), $this->export_dir ); |
| 225 | 237 | } |
| 226 | - | |
| 227 | - return true; | |
| 228 | 238 | } else { |
| 229 | 239 | if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
| 230 | 240 | return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'userswp' ), $this->export_dir ); |
| 231 | 241 | } |
| 242 | + } | |
| 232 | 243 | |
| 233 | - return true; | |
| 234 | - } | |
| 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; | |
| 235 | 249 | } catch ( Exception $e ) { |
| 236 | 250 | return $e->getMessage(); |
| 237 | 251 | } |
| 238 | 252 | } |
| 239 | 253 | |
| 254 | + /** | |
| 255 | + * Processes export step | |
| 256 | + * | |
| 257 | + * @return bool | |
| 258 | + * | |
| 259 | + */ | |
| 240 | 260 | public function process_export_step() { |
| 241 | 261 | if ( $this->step < 2 ) { |
| 242 | 262 | @unlink( $this->file ); |
| 243 | 263 | $this->print_columns(); |
| @@ -251,8 +271,14 @@ | ||
| 251 | 271 | return false; |
| 252 | 272 | } |
| 253 | 273 | } |
| 254 | 274 | |
| 275 | + /** | |
| 276 | + * Prints columns | |
| 277 | + * | |
| 278 | + * @return array | |
| 279 | + * | |
| 280 | + */ | |
| 255 | 281 | public function print_columns() { |
| 256 | 282 | $column_data = ''; |
| 257 | 283 | $columns = $this->get_columns(); |
| 258 | 284 | $i = 1; |
| @@ -267,13 +293,19 @@ | ||
| 267 | 293 | |
| 268 | 294 | return $column_data; |
| 269 | 295 | } |
| 270 | 296 | |
| 297 | + /** | |
| 298 | + * Returns export columns | |
| 299 | + * | |
| 300 | + * @return array | |
| 301 | + * | |
| 302 | + */ | |
| 271 | 303 | public function get_columns() { |
| 272 | 304 | global $wpdb; |
| 273 | 305 | $columns = array(); |
| 274 | 306 | |
| 275 | - foreach ( $wpdb->get_col( "DESC " . $this->meta_table_name, 0 ) as $column_name ) { | |
| 307 | + foreach ( $wpdb->get_col( "DESC " . $this->meta_table_name, 0 ) as $column_name ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 276 | 308 | $columns[] = $column_name; |
| 277 | 309 | } |
| 278 | 310 | |
| 279 | 311 | return apply_filters( 'uwp_export_users_get_columns', $columns ); |
| @@ -278,8 +310,16 @@ | ||
| 278 | 310 | |
| 279 | 311 | return apply_filters( 'uwp_export_users_get_columns', $columns ); |
| 280 | 312 | } |
| 281 | 313 | |
| 314 | + /** | |
| 315 | + * Returns export data | |
| 316 | + * | |
| 317 | + * @package userswp | |
| 318 | + * | |
| 319 | + * @return array | |
| 320 | + * | |
| 321 | + */ | |
| 282 | 322 | public function get_export_data() { |
| 283 | 323 | global $wpdb; |
| 284 | 324 | if(!$this->step){ |
| 285 | 325 | $page = 1; |
| @@ -287,16 +327,16 @@ | ||
| 287 | 327 | $page = $this->step; |
| 288 | 328 | } |
| 289 | 329 | |
| 290 | 330 | $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); | |
| 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 | |
| 292 | 332 | $i = 0; |
| 293 | 333 | |
| 294 | 334 | foreach ($data as $u){ |
| 295 | 335 | $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; | |
| 336 | + $data[$i]->username = $user->user_login; | |
| 337 | + $data[$i]->email = $user->user_email; | |
| 338 | + $data[$i]->bio = $user->description; | |
| 299 | 339 | $i++; |
| 300 | 340 | } |
| 301 | 341 | |
| 302 | 342 | return apply_filters( 'uwp_export_users_get_data', $data ); |
| @@ -301,22 +341,35 @@ | ||
| 301 | 341 | |
| 302 | 342 | return apply_filters( 'uwp_export_users_get_data', $data ); |
| 303 | 343 | } |
| 304 | 344 | |
| 345 | + /** | |
| 346 | + * Returns export status | |
| 347 | + * | |
| 348 | + * @return int | |
| 349 | + * | |
| 350 | + */ | |
| 305 | 351 | public function get_export_status() { |
| 306 | 352 | $status = 100; |
| 307 | 353 | return apply_filters( 'uwp_get_export_users_status', $status ); |
| 308 | 354 | } |
| 309 | 355 | |
| 356 | + /** | |
| 357 | + * Prints CSV rows | |
| 358 | + * | |
| 359 | + * @return string | |
| 360 | + * | |
| 361 | + */ | |
| 310 | 362 | public function print_rows() { |
| 311 | 363 | $row_data = ''; |
| 312 | 364 | $data = $this->get_export_data(); |
| 313 | 365 | $columns = $this->get_columns(); |
| 314 | 366 | |
| 315 | - if ( $data ) { | |
| 367 | + if ( is_array($data) && !empty($data) ) { | |
| 316 | 368 | foreach ( $data as $row ) { |
| 317 | 369 | $i = 1; |
| 318 | 370 | foreach ( $row as $key => $column ) { |
| 371 | + $column = $this->escape_data( $column ); | |
| 319 | 372 | $row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"'; |
| 320 | 373 | $row_data .= $i == count( $columns ) ? '' : ','; |
| 321 | 374 | $i++; |
| 322 | 375 | } |
| @@ -330,8 +383,14 @@ | ||
| 330 | 383 | |
| 331 | 384 | return false; |
| 332 | 385 | } |
| 333 | 386 | |
| 387 | + /** | |
| 388 | + * Returns export file content | |
| 389 | + * | |
| 390 | + * @return string | |
| 391 | + * | |
| 392 | + */ | |
| 334 | 393 | protected function get_export_file() { |
| 335 | 394 | $file = ''; |
| 336 | 395 | |
| 337 | 396 | if ( $this->wp_filesystem->exists( $this->file ) ) { |
| @@ -342,8 +401,16 @@ | ||
| 342 | 401 | |
| 343 | 402 | return $file; |
| 344 | 403 | } |
| 345 | 404 | |
| 405 | + /** | |
| 406 | + * Adds export data to CSV file | |
| 407 | + * | |
| 408 | + * @package userswp | |
| 409 | + * | |
| 410 | + * @param string $data | |
| 411 | + * | |
| 412 | + */ | |
| 346 | 413 | protected function attach_export_data( $data = '' ) { |
| 347 | 414 | $filedata = $this->get_export_file(); |
| 348 | 415 | $filedata .= $data; |
| 349 | 416 | |
| @@ -355,11 +422,17 @@ | ||
| 355 | 422 | |
| 356 | 423 | $this->empty = count( $rows ) == $columns ? true : false; |
| 357 | 424 | } |
| 358 | 425 | |
| 359 | - public function uwp_get_export_users_status() { | |
| 426 | + /** | |
| 427 | + * Returns export user status | |
| 428 | + * | |
| 429 | + * @return int | |
| 430 | + * | |
| 431 | + */ | |
| 432 | + public function get_export_users_status() { | |
| 360 | 433 | global $wpdb; |
| 361 | - $data = $wpdb->get_results("SELECT user_id FROM $this->meta_table_name WHERE 1=1"); | |
| 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 | |
| 362 | 435 | $total = !empty( $data ) ? count( $data ) : 0; |
| 363 | 436 | $status = 100; |
| 364 | 437 | |
| 365 | 438 | if ( $this->per_page > $total ) { |
| @@ -376,9 +449,20 @@ | ||
| 376 | 449 | |
| 377 | 450 | return $status; |
| 378 | 451 | } |
| 379 | 452 | |
| 380 | - public function uwp_ie_upload_file(){ | |
| 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 | + | |
| 381 | 465 | $upload_data = array( |
| 382 | 466 | 'name' => $_FILES['import_file']['name'], |
| 383 | 467 | 'type' => $_FILES['import_file']['type'], |
| 384 | 468 | 'tmp_name' => $_FILES['import_file']['tmp_name'], |
| @@ -387,12 +471,15 @@ | ||
| 387 | 471 | ); |
| 388 | 472 | |
| 389 | 473 | header('Content-Type: text/html; charset=' . get_option('blog_charset')); |
| 390 | 474 | |
| 475 | + add_filter( 'upload_mimes', array( $this, 'allowed_upload_mimes' ) ); | |
| 391 | 476 | $uploaded_file = wp_handle_upload( $upload_data, array('test_form' => false) ); |
| 477 | + remove_filter( 'upload_mimes', array( $this, 'allowed_upload_mimes' ) ); | |
| 478 | + | |
| 392 | 479 | if ( isset( $uploaded_file['url'] ) ) { |
| 393 | 480 | $file_loc = $uploaded_file['url']; |
| 394 | - echo $file_loc; | |
| 481 | + echo esc_url( $file_loc );exit; | |
| 395 | 482 | } else { |
| 396 | 483 | echo 'error'; |
| 397 | 484 | } |
| 398 | 485 | exit; |
| @@ -397,9 +484,13 @@ | ||
| 397 | 484 | } |
| 398 | 485 | exit; |
| 399 | 486 | } |
| 400 | 487 | |
| 401 | - public function uwp_process_users_import(){ | |
| 488 | + /** | |
| 489 | + * Processes users import | |
| 490 | + * | |
| 491 | + */ | |
| 492 | + public function process_users_import(){ | |
| 402 | 493 | |
| 403 | 494 | $response = array(); |
| 404 | 495 | $response['success'] = false; |
| 405 | 496 | $response['msg'] = __( 'Invalid import request found.', 'userswp' ); |
| @@ -463,9 +554,9 @@ | ||
| 463 | 554 | $this->imp_step = 'done'; |
| 464 | 555 | $response['msg'] = __( 'Users import completed.', 'userswp' ); |
| 465 | 556 | } |
| 466 | 557 | |
| 467 | - $response['data']['msg'] = $return['msg']; | |
| 558 | + $response['data']['msg'] = ! empty( $return['msg'] ) ? $return['msg'] : $response['msg']; | |
| 468 | 559 | $response['data']['step'] = $this->imp_step; |
| 469 | 560 | $response['data']['done'] = $done; |
| 470 | 561 | } else { |
| 471 | 562 | $response['msg'] = __( 'No valid data found for import.', 'userswp' ); |
| @@ -474,8 +565,14 @@ | ||
| 474 | 565 | wp_send_json( $response ); |
| 475 | 566 | |
| 476 | 567 | } |
| 477 | 568 | |
| 569 | + /** | |
| 570 | + * Processes import step | |
| 571 | + * | |
| 572 | + * @return array | |
| 573 | + * | |
| 574 | + */ | |
| 478 | 575 | public function process_import_step() { |
| 479 | 576 | |
| 480 | 577 | $errors = new WP_Error(); |
| 481 | 578 | if(is_null($this->path)){ |
| @@ -490,26 +587,26 @@ | ||
| 490 | 587 | |
| 491 | 588 | if ( ! empty( $rows ) ) { |
| 492 | 589 | foreach ( $rows as $row ) { |
| 493 | 590 | if( empty($row) ) { |
| 494 | - $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'Skipped due to invalid/no data.','userswp'); | |
| 591 | + $return['msg'] = sprintf(__('Row - %s Error: '. 'Skipped due to invalid/no data.','userswp'), $this->imp_step); | |
| 495 | 592 | continue; |
| 496 | 593 | } |
| 497 | 594 | |
| 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'] : ''; | |
| 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'] : ''; | |
| 504 | 601 | $password = wp_generate_password(); |
| 505 | 602 | $exclude = array('user_id'); |
| 506 | 603 | $exclude = apply_filters('uwp_import_exclude_columns', $exclude, $row); |
| 507 | 604 | |
| 508 | - if(isset($row['uwp_account_username']) && username_exists($row['uwp_account_username'])){ | |
| 509 | - $user = get_user_by('login', $row['uwp_account_username']); | |
| 605 | + if(isset($row['username']) && username_exists($row['username'])){ | |
| 606 | + $user = get_user_by('login', $row['username']); | |
| 510 | 607 | $user_id = $user->ID; |
| 511 | - $email = $row['uwp_account_email']; | |
| 608 | + $email = $row['email']; | |
| 512 | 609 | if( !empty( $email ) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id) ) { |
| 513 | 610 | $args = array( |
| 514 | 611 | 'ID' => $user_id, |
| 515 | 612 | 'user_email' => $email, |
| @@ -519,16 +616,16 @@ | ||
| 519 | 616 | 'description' => $bio, |
| 520 | 617 | ); |
| 521 | 618 | wp_update_user( $args ); |
| 522 | 619 | } |
| 523 | - } elseif(isset($row['uwp_account_email']) && email_exists($row['uwp_account_email'])){ | |
| 524 | - $user = get_user_by('email', $row['uwp_account_email']); | |
| 620 | + } elseif(isset($row['email']) && email_exists($row['email'])){ | |
| 621 | + $user = get_user_by('email', $row['email']); | |
| 525 | 622 | $user_id = $user->ID; |
| 526 | 623 | } elseif((int)$row['user_id'] > 0){ |
| 527 | 624 | $user = get_user_by('ID', $row['user_id']); |
| 528 | 625 | if(false === $user){ |
| 529 | 626 | $userdata = array( |
| 530 | - 'user_login' => $row['uwp_account_username'], | |
| 627 | + 'user_login' => $row['username'], | |
| 531 | 628 | 'user_email' => $email, |
| 532 | 629 | 'user_pass' => $password, |
| 533 | 630 | 'first_name' => $first_name, |
| 534 | 631 | 'last_name' => $last_name, |
| @@ -535,11 +632,12 @@ | ||
| 535 | 632 | 'description' => $bio, |
| 536 | 633 | 'display_name'=> $display_name |
| 537 | 634 | ); |
| 538 | 635 | $user_id = wp_insert_user( $userdata ); |
| 539 | - wp_new_user_notification($user_id,null, 'user'); //send password reset link | |
| 636 | + $notify = apply_filters('uwp_import_user_notify', 'user', $user_id); | |
| 637 | + wp_new_user_notification($user_id,null, $notify); //send password reset link | |
| 540 | 638 | } else { |
| 541 | - if( $user->user_login == $row['uwp_account_username'] ) { //check id passed in csv and existing username are same | |
| 639 | + if( $user->user_login == $row['username'] ) { //check id passed in csv and existing username are same | |
| 542 | 640 | $user_id = $row['user_id']; |
| 543 | 641 | if( !empty( $email ) && $email != $user->user_email && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)) { |
| 544 | 642 | $args = array( |
| 545 | 643 | 'ID' => $user_id, |
| @@ -551,25 +649,35 @@ | ||
| 551 | 649 | ); |
| 552 | 650 | wp_update_user( $args ); |
| 553 | 651 | } |
| 554 | 652 | } else { |
| 555 | - $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. 'User could not be created.','userswp'); | |
| 653 | + $return['msg'] = sprintf(__('Row - %s Error: '. 'User could not be created.','userswp'), $this->imp_step); | |
| 556 | 654 | continue; |
| 557 | 655 | } |
| 558 | 656 | } |
| 559 | 657 | } else { |
| 560 | 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 | + } | |
| 561 | 669 | } |
| 562 | 670 | |
| 563 | 671 | if( !is_wp_error( $user_id ) ){ |
| 564 | 672 | foreach ($row as $key => $value){ |
| 565 | - if(!in_array($key, $exclude) && $update_existing = apply_filters('uwp_import_update_users', false, $row, $user_id)){ | |
| 673 | + if(!in_array($key, $exclude)){ | |
| 566 | 674 | $value = maybe_unserialize($value); |
| 567 | 675 | uwp_update_usermeta($user_id, $key, $value); |
| 568 | 676 | } |
| 569 | 677 | } |
| 570 | 678 | } else { |
| 571 | - $return['msg'] = __('Row - ' .$this->imp_step. ' Error: '. $user_id->get_error_message(),'userswp'); | |
| 679 | + $return['msg'] = sprintf(__('Row - %s Error: %s','userswp'), $this->imp_step, $user_id->get_error_message()); | |
| 572 | 680 | continue; |
| 573 | 681 | } |
| 574 | 682 | } |
| 575 | 683 | $return['success'] = true; |
| @@ -577,8 +685,19 @@ | ||
| 577 | 685 | |
| 578 | 686 | return $return; |
| 579 | 687 | } |
| 580 | 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 | + */ | |
| 581 | 700 | public function get_csv_rows( $row = 0, $count = 1 ) { |
| 582 | 701 | |
| 583 | 702 | $lc_all = setlocale( LC_ALL, 0 ); // Fix issue of fgetcsv ignores special characters when they are at the beginning of line |
| 584 | 703 | setlocale( LC_ALL, 'en_US.UTF-8' ); |
| @@ -634,9 +753,8 @@ | ||
| 634 | 753 | } |
| 635 | 754 | |
| 636 | 755 | // A plugin may need to filter the data and meta |
| 637 | 756 | $userdata = apply_filters( 'uwp_import_userdata', $userdata, $usermeta ); |
| 638 | - $usermeta = apply_filters( 'uwp_import_usermeta', $usermeta, $userdata ); | |
| 639 | 757 | |
| 640 | 758 | if ( ! empty( $userdata ) ) { |
| 641 | 759 | $file[] = $userdata; |
| 642 | 760 | $f ++; |
| @@ -650,14 +768,26 @@ | ||
| 650 | 768 | return $file; |
| 651 | 769 | |
| 652 | 770 | } |
| 653 | 771 | |
| 772 | + /** | |
| 773 | + * Returns import status | |
| 774 | + * | |
| 775 | + * @return int | |
| 776 | + * | |
| 777 | + */ | |
| 654 | 778 | public function get_import_status() { |
| 655 | 779 | $status = 100; |
| 656 | 780 | return apply_filters( 'uwp_get_import_users_status', $status ); |
| 657 | 781 | } |
| 658 | 782 | |
| 659 | - public function uwp_get_import_users_status() { | |
| 783 | + /** | |
| 784 | + * Returns import user status | |
| 785 | + * | |
| 786 | + * @return int | |
| 787 | + * | |
| 788 | + */ | |
| 789 | + public function get_import_users_status() { | |
| 660 | 790 | |
| 661 | 791 | if ( $this->imp_step >= $this->total_rows ) { |
| 662 | 792 | $status = 100; |
| 663 | 793 | } else { |
| @@ -665,7 +795,32 @@ | ||
| 665 | 795 | } |
| 666 | 796 | |
| 667 | 797 | return $status; |
| 668 | 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 | + } | |
| 669 | 824 | |
| 670 | 825 | } |
| 671 | 826 | new UsersWP_Import_Export(); |