| 1 |
<?php |
| 2 |
wp_enqueue_script( 'plupload' ); |
| 3 |
|
| 4 |
?> |
| 5 |
<style type="text/css"> |
| 6 |
.uwp-export-submit-wrap [type="submit"] { |
| 7 |
vertical-align: top; |
| 8 |
line-height: normal; |
| 9 |
} |
| 10 |
.uwp-ie-container .uwp-progress { |
| 11 |
width: calc(100% - 50px); |
| 12 |
height: 16px; |
| 13 |
background-color: #ddd; |
| 14 |
float: left; |
| 15 |
position: relative; |
| 16 |
} |
| 17 |
.uwp-ie-container .uwp-progress div { |
| 18 |
background-color: orange; |
| 19 |
height: 100%; |
| 20 |
width: 0; |
| 21 |
display: block |
| 22 |
} |
| 23 |
.uwp-ie-container .uwp-progress > span { |
| 24 |
position: absolute; |
| 25 |
height: 100%; |
| 26 |
width: 100%; |
| 27 |
display: block; |
| 28 |
font-size: 13px; |
| 29 |
line-height: 14px; |
| 30 |
text-align: center; |
| 31 |
vertical-align: top; |
| 32 |
top: 0; |
| 33 |
} |
| 34 |
.uwp-ie-container .uwp-msg-wrap { |
| 35 |
padding: 12px; |
| 36 |
background-color: #f4f4f4; |
| 37 |
border-style: solid; |
| 38 |
border-width: 1px 0; |
| 39 |
border-color: #eae9e9; |
| 40 |
overflow: auto; |
| 41 |
margin: 20px -12px -23px; |
| 42 |
position: relative; |
| 43 |
} |
| 44 |
|
| 45 |
.uwp-ie-container #import-submit{ |
| 46 |
display: none; |
| 47 |
} |
| 48 |
.uwp-ie-container .uwp-msg-wrap{ |
| 49 |
margin: 20px 0; |
| 50 |
} |
| 51 |
.uwp-ie-container .uwp-msg-wrap .uwp-export-loader, .uwp-ie-container .uwp-msg-wrap .uwp-import-loader { |
| 52 |
display: inline-block; |
| 53 |
float: right; |
| 54 |
margin: 0; |
| 55 |
vertical-align: middle; |
| 56 |
width: 40px; |
| 57 |
height: 16px; |
| 58 |
text-align: center; |
| 59 |
} |
| 60 |
.uwp-ie-container .uwp-export-loader .fa, .uwp-ie-container .uwp-import-loader .fa { |
| 61 |
font-size: 17px; |
| 62 |
color: orange; |
| 63 |
} |
| 64 |
.uwp-ie-container .uwp-export-loader .fa-spin, .uwp-ie-container .uwp-import-loader .fa-spin { |
| 65 |
color: #000; |
| 66 |
} |
| 67 |
.uwp-msg-wrap .updated { |
| 68 |
margin: 0 0 2px 0 !important; |
| 69 |
} |
| 70 |
.uwp-export-file, .uwp-import-file { |
| 71 |
display: block; |
| 72 |
padding-top: 7px; |
| 73 |
clear: both; |
| 74 |
display: block; |
| 75 |
} |
| 76 |
.uwp-export-file a, .uwp-import-file a { |
| 77 |
text-decoration: none; |
| 78 |
} |
| 79 |
.uwp-ie-report { |
| 80 |
max-height: 120px; |
| 81 |
overflow-y: scroll; |
| 82 |
width: auto; |
| 83 |
background-color: #fffbcc; |
| 84 |
padding: 0px 10px; |
| 85 |
margin: 30px 0 0 0; |
| 86 |
} |
| 87 |
|
| 88 |
.uwp-ie-report p { |
| 89 |
font-size: 12px; |
| 90 |
margin: 7px 0px; |
| 91 |
padding: 0; |
| 92 |
} |
| 93 |
</style> |
| 94 |
<?php |
| 95 |
$uwp_chunk_sizes = array( |
| 96 |
50 => 50, |
| 97 |
100 => 100, |
| 98 |
200 => 200, |
| 99 |
500 => 500, |
| 100 |
1000 => 1000, |
| 101 |
2000 => 2000, |
| 102 |
5000 => 5000, |
| 103 |
10000 => 10000, |
| 104 |
20000 => 20000, |
| 105 |
50000 => 50000, |
| 106 |
100000 => 100000, |
| 107 |
); |
| 108 |
|
| 109 |
$uwp_chunk_sizes = apply_filters('uwp_ie_csv_chunks_options', $uwp_chunk_sizes); |
| 110 |
$uwp_chunk_sizes_opts = ''; |
| 111 |
foreach ($uwp_chunk_sizes as $value => $title) { |
| 112 |
$uwp_chunk_sizes_opts .= '<option value="' . $value . '" ' . selected($value, 5000, false) . '>' . $title . '</option>'; |
| 113 |
} |
| 114 |
|
| 115 |
$users_count = count_users(); |
| 116 |
$total_users = $users_count['total_users']; |
| 117 |
?> |
| 118 |
<div class="metabox-holder uwp-ie-container"> |
| 119 |
<div class="postbox uwp-export-users"> |
| 120 |
<h3><span><?php esc_html_e( 'Export Users Data', 'userswp' ); ?></span></h3> |
| 121 |
<div class="inside uwp-export-users-form"> |
| 122 |
<p><?php esc_html_e( 'Download a CSV of all users data for usersWP.', 'userswp' ); ?></p> |
| 123 |
<table class="form-table"> |
| 124 |
<tbody> |
| 125 |
<tr> |
| 126 |
<th class=""><label for="uwp_ie_chunk_size"><?php esc_html_e( 'Max entries per csv file:', 'userswp' );?></label></th> |
| 127 |
<td><select name="uwp_ie_chunk_size" class="aui-select2" id="uwp_ie_chunk_size" data-ucount = "<?php echo esc_attr( $total_users );?>" style="min-width:140px"><?php echo esc_attr( $uwp_chunk_sizes_opts );?></select><p class="description"><?php esc_html_e( 'The maximum number of entries per csv file (default to 5000, you might want to lower this to prevent memory issues.)', 'userswp' );?></p></td> |
| 128 |
</tr> |
| 129 |
</tbody> |
| 130 |
</table> |
| 131 |
<div class="uwp-export-submit-wrap"> |
| 132 |
<input type="hidden" name="uwp_ie_action" value="export_users" /> |
| 133 |
<?php wp_nonce_field( 'uwp_export_users_nonce', 'uwp_export_users_nonce' ); ?> |
| 134 |
<?php submit_button( __( 'Export CSV', 'userswp' ), 'primary', 'export-submit', false ); ?> |
| 135 |
|
| 136 |
</div> |
| 137 |
</div> |
| 138 |
</div> |
| 139 |
|
| 140 |
<div class="postbox"> |
| 141 |
<h3><span><?php esc_html_e( 'Import Users', 'userswp' ); ?></span></h3> |
| 142 |
<div class="inside" id="uwp-imp-container"> |
| 143 |
<p><?php esc_html_e( 'Import the users data from a .csv file. This file can be obtained by exporting the data on another site using the form above.', 'userswp' ); ?></p> |
| 144 |
<p class="uwp-imp-uploaded-file"> |
| 145 |
<a id="uwp-imp-browse" class="button file-selector button-primary" href="#"><?php esc_html_e( 'Select File', 'userswp' ); ?></a> |
| 146 |
</p> |
| 147 |
<p> |
| 148 |
<input type="hidden" value="" name="uwp_import_users_file" class="uwp_import_users_file"> |
| 149 |
<?php wp_nonce_field( 'uwp_import_users_nonce', 'uwp_import_users_nonce' ); ?> |
| 150 |
<?php submit_button( __( 'Import', 'userswp' ), 'primary', 'import-submit', false ); ?> |
| 151 |
</p> |
| 152 |
</div> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
|
| 156 |
<script type="text/javascript"> |
| 157 |
jQuery(function($) { |
| 158 |
var UWP_IE = { |
| 159 |
init: function() { |
| 160 |
var $self = this; |
| 161 |
this.submit(); |
| 162 |
this.clearMessage(); |
| 163 |
this.form = jQuery('form#mainform'); |
| 164 |
this.form.attr('action', 'javascript:void(0);'); |
| 165 |
jQuery("#save_style", this.el).on("click", function(e) { |
| 166 |
$self.saveStyle(e); |
| 167 |
}); |
| 168 |
}, |
| 169 |
submit: function() { |
| 170 |
var $this = this; |
| 171 |
$('#export-submit').click(function(e) { |
| 172 |
e.preventDefault(); |
| 173 |
var $btn = jQuery(this); |
| 174 |
var $form = jQuery('form#mainform'); |
| 175 |
if (!$btn.attr('disabled')) { |
| 176 |
var data = $form.serialize(); |
| 177 |
$btn.attr('disabled', true); |
| 178 |
$btn.find('.uwp-msg-wrap').remove(); |
| 179 |
$btn.after('<div class="uwp-msg-wrap"><div class="uwp-progress"><div></div><span>0%</span></div><span class="uwp-export-loader"><i class="fas fa-spin fa-spinner"></i></span></div>'); |
| 180 |
// start the process |
| 181 |
$this.step(1, data, $form, $this); |
| 182 |
} |
| 183 |
}); |
| 184 |
$('#import-submit').click(function(e) { |
| 185 |
e.preventDefault(); |
| 186 |
var $btn = $(this); |
| 187 |
var $form = jQuery('form#mainform'); |
| 188 |
$btn.hide(); |
| 189 |
if (!$btn.attr('disabled')) { |
| 190 |
var data = $form.serialize(); |
| 191 |
$btn.attr('disabled', true); |
| 192 |
$form.find('.uwp-msg-wrap').remove(); |
| 193 |
$btn.after('<div class="uwp-msg-wrap"><div class="uwp-progress"><div></div><span>0%</span></div><span class="uwp-import-loader"><i class="fas fa-spin fa-spinner"></i></span></div>'); |
| 194 |
$form.find('.uwp-msg-wrap').append('<div class="uwp-ie-report"></div>'); |
| 195 |
// start the process |
| 196 |
$this.imp_step(1, data, $form, $this); |
| 197 |
} |
| 198 |
}); |
| 199 |
}, |
| 200 |
step: function(step, data, $form, $this) { |
| 201 |
var message = $form.find('.uwp-msg-wrap'); |
| 202 |
var post_data = { |
| 203 |
action: 'uwp_ajax_export_users', |
| 204 |
step: step, |
| 205 |
data: data, |
| 206 |
}; |
| 207 |
$.ajax({ |
| 208 |
url: ajaxurl, |
| 209 |
type: 'POST', |
| 210 |
cache: false, |
| 211 |
dataType: 'json', |
| 212 |
data: post_data, |
| 213 |
beforeSend: function(jqXHR, settings) {}, |
| 214 |
success: function(res) { |
| 215 |
if (res && typeof res == 'object') { |
| 216 |
if (res.success) { |
| 217 |
if ('done' == res.data.step || res.data.done >= 100) { |
| 218 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 219 |
$('.uwp-progress > span').text(parseInt(res.data.done) + '%'); |
| 220 |
$('.uwp-progress div').animate({ |
| 221 |
width: res.data.done + '%' |
| 222 |
}, 100, function() {}); |
| 223 |
if (res.msg) { |
| 224 |
message.html('<div id="uwp-export-success" class="updated notice is-dismissible"><p>' + msg + '<span class="notice-dismiss"></span></p></div>'); |
| 225 |
} |
| 226 |
if (res.data.file && res.data.file.u) { |
| 227 |
message.append('<span class="uwp-export-file"><a href="' + res.data.file.u + '" target="_blank" download><i class="fas fa-download"></i> ' + res.data.file.u + '</a><span> - ' + res.data.file.s + '<span><span>'); |
| 228 |
} |
| 229 |
message.find('.uwp-export-loader').html('<i class="fas fa-check-circle"></i>'); |
| 230 |
} else { |
| 231 |
var next = parseInt(res.data.step) > 0 ? parseInt(res.data.step) : 1; |
| 232 |
$('.uwp-progress > span').text(parseInt(res.data.done) + '%'); |
| 233 |
$('.uwp-progress div').animate({ |
| 234 |
width: res.data.done + '%' |
| 235 |
}, 100, function() {}); |
| 236 |
$this.step(parseInt(next), data, $form, $this); |
| 237 |
} |
| 238 |
} else { |
| 239 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 240 |
if (res.msg) { |
| 241 |
message.html('<div class="updated error"><p>' + res.msg + '</p></div>'); |
| 242 |
} |
| 243 |
} |
| 244 |
} else { |
| 245 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 246 |
message.html('<div class="updated error">' + res + '</div>'); |
| 247 |
} |
| 248 |
} |
| 249 |
}).fail(function(res) { |
| 250 |
if (window.console && window.console.log) { |
| 251 |
console.log(res); |
| 252 |
} |
| 253 |
}); |
| 254 |
}, |
| 255 |
imp_step: function(step, data, $form, $this) { |
| 256 |
var message = $form.find('.uwp-msg-wrap'); |
| 257 |
var post_data = { |
| 258 |
action: 'uwp_ajax_import_users', |
| 259 |
step: step, |
| 260 |
data: data, |
| 261 |
}; |
| 262 |
$.ajax({ |
| 263 |
url: ajaxurl, |
| 264 |
type: 'POST', |
| 265 |
cache: false, |
| 266 |
dataType: 'json', |
| 267 |
data: post_data, |
| 268 |
beforeSend: function(jqXHR, settings) {}, |
| 269 |
success: function(res) { |
| 270 |
if (res && typeof res == 'object') { |
| 271 |
if (res.success) { |
| 272 |
if (res.total.msg) { |
| 273 |
message.find('.uwp-ie-report').append('<p>' + res.total.msg + '</p>'); |
| 274 |
} |
| 275 |
if ('done' == res.data.step || res.data.done >= 100) { |
| 276 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 277 |
$('.uwp-progress > span').text(parseInt(res.data.done) + '%'); |
| 278 |
$('.uwp-progress div').animate({ |
| 279 |
width: res.data.done + '%' |
| 280 |
}, 100, function() {}); |
| 281 |
if (res.msg) { |
| 282 |
message.append('<div id="uwp-export-success" class="updated notice is-dismissible"><p>' + res.msg + '<span class="notice-dismiss"></span></p></div>'); |
| 283 |
} |
| 284 |
message.find('.uwp-import-loader').html('<i class="fas fa-check-circle"></i>'); |
| 285 |
} else { |
| 286 |
var next = parseInt(res.data.step) > 0 ? parseInt(res.data.step) : 1; |
| 287 |
$('.uwp-progress > span').text(parseInt(res.data.done) + '%'); |
| 288 |
$('.uwp-progress div').animate({ |
| 289 |
width: res.data.done + '%' |
| 290 |
}, 100, function() {}); |
| 291 |
if (res.data.msg) { |
| 292 |
message.find('.uwp-ie-report').append('<p>' + res.data.msg + '</p>'); |
| 293 |
} |
| 294 |
$this.imp_step(parseInt(next), data, $form, $this); |
| 295 |
} |
| 296 |
} else { |
| 297 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 298 |
if (res.msg) { |
| 299 |
message.html('<div class="updated error"><p>' + res.msg + '</p></div>'); |
| 300 |
} |
| 301 |
} |
| 302 |
} else { |
| 303 |
$form.find('input[type="submit"]').removeAttr('disabled'); |
| 304 |
message.html('<div class="updated error">' + res + '</div>'); |
| 305 |
} |
| 306 |
} |
| 307 |
}).fail(function(res) { |
| 308 |
if (window.console && window.console.log) { |
| 309 |
console.log(res); |
| 310 |
} |
| 311 |
}); |
| 312 |
}, |
| 313 |
clearMessage: function() { |
| 314 |
$('body').on('click', '#uwp-export-success .notice-dismiss', function() { |
| 315 |
$(this).closest('#uwp-export-success').parent().slideUp('fast'); |
| 316 |
}); |
| 317 |
$('body').on('click', '#uwp-import-success .notice-dismiss', function() { |
| 318 |
$(this).closest('#uwp-import-success').parent().slideUp('fast'); |
| 319 |
}); |
| 320 |
} |
| 321 |
}; |
| 322 |
UWP_IE.init(); |
| 323 |
|
| 324 |
window.UWP_IE_Uploader = function (browse_button, container, max, type, allowed_type, max_file_size) { |
| 325 |
this.removed_files = []; |
| 326 |
this.container = container; |
| 327 |
this.browse_button = browse_button; |
| 328 |
this.max = max || 1; |
| 329 |
this.count = $('#' + container).find('.uwp-attachment-list > li').length; //count how many items are there |
| 330 |
this.perFileCount = 0; |
| 331 |
if( !$('#'+browse_button).length ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
this.uploader = new plupload.Uploader({ |
| 336 |
runtimes: 'html5,html4', |
| 337 |
browse_button: browse_button, |
| 338 |
container: container, |
| 339 |
multipart: true, |
| 340 |
multipart_params: { |
| 341 |
action: 'uwp_ie_upload_file', |
| 342 |
}, |
| 343 |
max_file_count : 1, |
| 344 |
urlstream_upload: true, |
| 345 |
file_data_name: 'import_file', |
| 346 |
max_file_size: max_file_size + 'kb', |
| 347 |
url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) . '?nonce=' . wp_create_nonce( 'uwp-ie-file-upload-nonce' ) ); ?>' + '&type=' + type, |
| 348 |
filters: [{ |
| 349 |
title: '<?php echo esc_js( __('Allowed Files', 'userswp') ); ?>', |
| 350 |
extensions: allowed_type |
| 351 |
}] |
| 352 |
}); |
| 353 |
|
| 354 |
//attach event handlers |
| 355 |
this.uploader.bind('Init', $.proxy(this, 'init')); |
| 356 |
this.uploader.bind('FilesAdded', $.proxy(this, 'added')); |
| 357 |
this.uploader.bind('QueueChanged', $.proxy(this, 'upload')); |
| 358 |
this.uploader.bind('UploadProgress', $.proxy(this, 'progress')); |
| 359 |
this.uploader.bind('Error', $.proxy(this, 'error')); |
| 360 |
this.uploader.bind('FileUploaded', $.proxy(this, 'uploaded')); |
| 361 |
|
| 362 |
this.uploader.init(); |
| 363 |
|
| 364 |
return this.uploader; |
| 365 |
}; |
| 366 |
|
| 367 |
UWP_IE_Uploader.prototype = { |
| 368 |
|
| 369 |
init: function (up, params) { |
| 370 |
this.showHide(); |
| 371 |
$('#' + this.container).prepend('<div class="uwp-file-warning"></div>'); |
| 372 |
}, |
| 373 |
|
| 374 |
showHide: function () { |
| 375 |
|
| 376 |
if ( this.count >= this.max) { |
| 377 |
|
| 378 |
if ( this.count > this.max ) { |
| 379 |
$('#' + this.container + ' .uwp-file-warning').addClass('error').html( '<?php echo esc_js( __( 'Maximum number of files reached!', 'userswp' ) ); ?>' ); |
| 380 |
$('#' + this.container).find('.file-selector').hide(); |
| 381 |
|
| 382 |
return; |
| 383 |
} |
| 384 |
}; |
| 385 |
$('#' + this.container + ' .uwp-file-warning').removeClass('error').html( '' ); |
| 386 |
$('#' + this.container).find('.file-selector').show(); |
| 387 |
}, |
| 388 |
|
| 389 |
added: function (up, files) { |
| 390 |
var $container = $('#' + this.container).find('.uwp-imp-uploaded-file'); |
| 391 |
|
| 392 |
this.showHide(); |
| 393 |
$('form#mainform').find('.uwp-msg-wrap').remove(); |
| 394 |
|
| 395 |
$.each(files, function(i, file) { |
| 396 |
$container.append( |
| 397 |
'<div class="upload-item uwp-msg-wrap" id="' + file.id + '"><div class="uwp-progress"><div class="bar"></div><span class="percent">0%</span></div><span class="uwp-import-loader"><i class="fas fa-spin fa-spinner"></i></span><span class="uwp-import-file filename original">' + |
| 398 |
file.name + '<span> - ' + plupload.formatSize(file.size) + '</span></span>' + |
| 399 |
'</div></div>'); |
| 400 |
}); |
| 401 |
|
| 402 |
up.refresh(); |
| 403 |
up.start(); |
| 404 |
}, |
| 405 |
|
| 406 |
upload: function (uploader) { |
| 407 |
|
| 408 |
this.count = uploader.files.length - this.removed_files.length ; |
| 409 |
this.showHide(); |
| 410 |
|
| 411 |
}, |
| 412 |
|
| 413 |
progress: function (up, file) { |
| 414 |
var item = $('#' + file.id); |
| 415 |
|
| 416 |
$('.bar', item).css({ width: file.percent + '%' }); |
| 417 |
$('.percent', item).html( file.percent + '%' ); |
| 418 |
}, |
| 419 |
|
| 420 |
error: function (up, error) { |
| 421 |
$('#' + this.container).find('#' + error.file.id).remove(); |
| 422 |
|
| 423 |
var msg = ''; |
| 424 |
switch (error.code) { |
| 425 |
case -600: |
| 426 |
msg = '<?php esc_attr_e( 'The file you have uploaded exceeds the file size limit. Please try again.', 'userswp' ); ?>' |
| 427 |
break; |
| 428 |
|
| 429 |
case -601: |
| 430 |
msg = '<?php esc_attr_e( 'Invalid file uploaded. Please upload .csv file. Please try again.', 'userswp' ); ?>' |
| 431 |
break; |
| 432 |
|
| 433 |
default: |
| 434 |
msg = 'Error #' + error.code + ': ' + error.message; |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
alert(msg); |
| 439 |
|
| 440 |
this.count -= 1; |
| 441 |
this.showHide(); |
| 442 |
this.uploader.refresh(); |
| 443 |
}, |
| 444 |
|
| 445 |
uploaded: function (up, file, response) { |
| 446 |
var self = this; |
| 447 |
|
| 448 |
$('#' + file.id + " .percent").html("100%"); |
| 449 |
$('#' + file.id).find('.uwp-import-loader').html('<i class="fas fa-check-circle"></i>'); |
| 450 |
|
| 451 |
if(response.response !== 'error') { |
| 452 |
$('#' + this.container).find('.file-selector').hide(); |
| 453 |
$('form#mainform').find('#import-submit').show(); |
| 454 |
$('form#mainform').find('.uwp_import_users_file').val(response.response); |
| 455 |
|
| 456 |
} else { |
| 457 |
console.log(response); |
| 458 |
alert(response.response); |
| 459 |
|
| 460 |
this.count -= 1; |
| 461 |
this.showHide(); |
| 462 |
} |
| 463 |
}, |
| 464 |
|
| 465 |
}; |
| 466 |
|
| 467 |
$(document).ready( function(){ |
| 468 |
var uploader = new UWP_IE_Uploader('uwp-imp-browse', 'uwp-imp-container', 1, 'uwp_ie_import_file', 'csv', 2048); |
| 469 |
}); |
| 470 |
}); |
| 471 |
</script> |