| 1 |
(function( $ ) { |
| 2 |
|
| 3 |
$(document).ready(function() { |
| 4 |
|
| 5 |
var $_CookedMigrationButton = $('#cooked-migration-button'), |
| 6 |
$_CookedImportButton = $('#cooked-import-button'), |
| 7 |
$_CookedCSVImportButton = $('#cooked-csv-import-button'), |
| 8 |
$_CookedCSVImportForm = $('#cooked-csv-import-form'), |
| 9 |
$_CookedMigrationProgress = $('#cooked-migration-progress'), |
| 10 |
$_CookedMigrationProgressText = $('#cooked-migration-progress-text'), |
| 11 |
$_CookedCSVImportProgress = $('#cooked-csv-import-progress'), |
| 12 |
$_CookedCSVImportProgressText = $('#cooked-csv-import-progress-text'); |
| 13 |
|
| 14 |
// Migration Button Exists? |
| 15 |
if ($_CookedMigrationButton.length) { |
| 16 |
$_CookedMigrationButton.on('click',function(e) { |
| 17 |
e.preventDefault(); |
| 18 |
|
| 19 |
var thisButton = $(this), |
| 20 |
confirm_migrate = confirm(cooked_migration_js_vars.i18n_confirm_migrate_recipes); |
| 21 |
|
| 22 |
if (confirm_migrate && !thisButton.hasClass('disabled')) { |
| 23 |
thisButton.addClass('disabled').attr('disabled', true); |
| 24 |
thisButton.hide(); |
| 25 |
|
| 26 |
var ajax__bulk_migrate_recipes = $.post( |
| 27 |
cooked_migration_js_vars.ajax_url, |
| 28 |
{ |
| 29 |
action: 'cooked_get_migrate_ids', |
| 30 |
nonce: cooked_migration_js_vars.cooked_migrate_nonce |
| 31 |
}, |
| 32 |
function (json_recipe_ids) { |
| 33 |
if (json_recipe_ids) { |
| 34 |
var recipe_ids = JSON.parse(json_recipe_ids), |
| 35 |
total_recipes = Object.keys(recipe_ids).length; |
| 36 |
|
| 37 |
if (total_recipes > 0) { |
| 38 |
cooked_migrate_recipes(json_recipe_ids, total_recipes); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
); |
| 43 |
} |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
// Import Button Exists? |
| 48 |
if ($_CookedImportButton.length) { |
| 49 |
$_CookedImportButton.on('click', function(e) { |
| 50 |
e.preventDefault(); |
| 51 |
|
| 52 |
var thisButton = $(this), |
| 53 |
import_type = thisButton.data('import-type'), |
| 54 |
confirm_import = confirm(cooked_migration_js_vars.i18n_confirm_import_recipes); |
| 55 |
|
| 56 |
if (confirm_import && !thisButton.hasClass('disabled')) { |
| 57 |
thisButton.addClass('disabled').attr('disabled', true); |
| 58 |
thisButton.hide(); |
| 59 |
|
| 60 |
var ajax__bulk_import_recipes = $.post( |
| 61 |
cooked_migration_js_vars.ajax_url, |
| 62 |
{ |
| 63 |
action: 'cooked_get_import_ids', |
| 64 |
import_type: import_type, |
| 65 |
nonce: cooked_migration_js_vars.cooked_import_recipes_nonce |
| 66 |
}, |
| 67 |
function (json_recipe_ids) { |
| 68 |
if (json_recipe_ids) { |
| 69 |
var recipe_ids = JSON.parse(json_recipe_ids), |
| 70 |
total_recipes = Object.keys(recipe_ids).length; |
| 71 |
|
| 72 |
if (total_recipes > 0) { |
| 73 |
cooked_import_recipes(json_recipe_ids, total_recipes, import_type); |
| 74 |
} |
| 75 |
} else { |
| 76 |
console.log(cooked_migration_js_vars.i18n_something_wrong); |
| 77 |
thisButton.addClass('disabled').attr('disabled', false); |
| 78 |
thisButton.show(); |
| 79 |
} |
| 80 |
}, |
| 81 |
); |
| 82 |
} |
| 83 |
}); |
| 84 |
} |
| 85 |
|
| 86 |
// CSV Import Button Exists? |
| 87 |
if ($_CookedCSVImportButton.length) { |
| 88 |
$_CookedCSVImportButton.on('click', function(e) { |
| 89 |
e.preventDefault(); |
| 90 |
|
| 91 |
var thisButton = $(this), |
| 92 |
fileInput = $('#cooked-csv-file'), |
| 93 |
file = fileInput[0].files[0], |
| 94 |
errorsDiv = $('#cooked-csv-import-errors'); |
| 95 |
|
| 96 |
errorsDiv.hide().empty(); |
| 97 |
|
| 98 |
if (!file) { |
| 99 |
errorsDiv.html('<p>' + cooked_migration_js_vars.i18n_csv_no_file + '</p>').show(); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
if (file.type !== 'text/csv' && !file.name.endsWith('.csv')) { |
| 104 |
errorsDiv.html('<p>' + cooked_migration_js_vars.i18n_csv_invalid_file + '</p>').show(); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if (thisButton.hasClass('disabled')) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
var confirm_import = confirm(cooked_migration_js_vars.i18n_confirm_csv_import); |
| 113 |
if (!confirm_import) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
thisButton.addClass('disabled').attr('disabled', true); |
| 118 |
fileInput.attr('disabled', true); |
| 119 |
|
| 120 |
var formData = new FormData(); |
| 121 |
formData.append('action', 'cooked_upload_csv'); |
| 122 |
formData.append('nonce', cooked_migration_js_vars.cooked_import_nonce); |
| 123 |
formData.append('csv_file', file); |
| 124 |
|
| 125 |
// Show progress |
| 126 |
if (!$_CookedCSVImportProgress.hasClass('cooked-active')) { |
| 127 |
$_CookedCSVImportProgress.addClass('cooked-active'); |
| 128 |
$_CookedCSVImportProgressText.addClass('cooked-active'); |
| 129 |
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "0%" }); |
| 130 |
$_CookedCSVImportProgressText.text(cooked_migration_js_vars.i18n_uploading); |
| 131 |
} |
| 132 |
|
| 133 |
// Upload file |
| 134 |
$.ajax({ |
| 135 |
url: cooked_migration_js_vars.ajax_url, |
| 136 |
type: 'POST', |
| 137 |
data: formData, |
| 138 |
processData: false, |
| 139 |
contentType: false, |
| 140 |
success: function(response) { |
| 141 |
if (response.success) { |
| 142 |
$_CookedCSVImportProgressText.text(cooked_migration_js_vars.i18n_processing); |
| 143 |
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "50%" }); |
| 144 |
|
| 145 |
// Process CSV |
| 146 |
$.post( |
| 147 |
cooked_migration_js_vars.ajax_url, |
| 148 |
{ |
| 149 |
action: 'cooked_process_csv', |
| 150 |
transient_key: response.data.transient_key, |
| 151 |
nonce: cooked_migration_js_vars.cooked_import_nonce |
| 152 |
}, |
| 153 |
function(processResponse) { |
| 154 |
if (processResponse.success) { |
| 155 |
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "100%" }); |
| 156 |
$_CookedCSVImportProgressText.text(processResponse.data.success + ' / ' + processResponse.data.total + ' ' + cooked_migration_js_vars.i18n_recipes_imported); |
| 157 |
|
| 158 |
// Show errors if any |
| 159 |
if (processResponse.data.errors && processResponse.data.errors.length > 0) { |
| 160 |
var errorHtml = '<p><strong>' + cooked_migration_js_vars.i18n_errors + '</strong></p><ul>'; |
| 161 |
processResponse.data.errors.forEach(function(error) { |
| 162 |
errorHtml += '<li>' + error + '</li>'; |
| 163 |
}); |
| 164 |
errorHtml += '</ul>'; |
| 165 |
errorsDiv.html(errorHtml).show(); |
| 166 |
} |
| 167 |
|
| 168 |
// Show success message |
| 169 |
setTimeout(function() { |
| 170 |
$_CookedCSVImportProgress.hide(); |
| 171 |
$_CookedCSVImportProgressText.hide(); |
| 172 |
$('#cooked-csv-import-completed').show(); |
| 173 |
thisButton.hide(); |
| 174 |
fileInput.hide(); |
| 175 |
}, 2000); |
| 176 |
} else { |
| 177 |
errorsDiv.html('<p>' + (processResponse.data.message || cooked_migration_js_vars.i18n_import_failed) + '</p>').show(); |
| 178 |
thisButton.removeClass('disabled').attr('disabled', false); |
| 179 |
fileInput.attr('disabled', false); |
| 180 |
$_CookedCSVImportProgress.removeClass('cooked-active'); |
| 181 |
$_CookedCSVImportProgressText.removeClass('cooked-active'); |
| 182 |
} |
| 183 |
}, |
| 184 |
'json' |
| 185 |
).fail(function() { |
| 186 |
errorsDiv.html('<p>' + cooked_migration_js_vars.i18n_failed_process_csv + '</p>').show(); |
| 187 |
thisButton.removeClass('disabled').attr('disabled', false); |
| 188 |
fileInput.attr('disabled', false); |
| 189 |
$_CookedCSVImportProgress.removeClass('cooked-active'); |
| 190 |
$_CookedCSVImportProgressText.removeClass('cooked-active'); |
| 191 |
}); |
| 192 |
} else { |
| 193 |
errorsDiv.html('<p>' + (response.data.message || cooked_migration_js_vars.i18n_file_upload_failed) + '</p>').show(); |
| 194 |
thisButton.removeClass('disabled').attr('disabled', false); |
| 195 |
fileInput.attr('disabled', false); |
| 196 |
$_CookedCSVImportProgress.removeClass('cooked-active'); |
| 197 |
$_CookedCSVImportProgressText.removeClass('cooked-active'); |
| 198 |
} |
| 199 |
}, |
| 200 |
error: function() { |
| 201 |
errorsDiv.html('<p>' + cooked_migration_js_vars.i18n_failed_upload_csv + '</p>').show(); |
| 202 |
thisButton.removeClass('disabled').attr('disabled', false); |
| 203 |
fileInput.attr('disabled', false); |
| 204 |
$_CookedCSVImportProgress.removeClass('cooked-active'); |
| 205 |
$_CookedCSVImportProgressText.removeClass('cooked-active'); |
| 206 |
} |
| 207 |
}); |
| 208 |
}); |
| 209 |
} |
| 210 |
}); |
| 211 |
})( jQuery ); |
| 212 |
|
| 213 |
if (typeof cookedDecimalAdjust != 'function') { |
| 214 |
function cookedDecimalAdjust(type, value, exp) { |
| 215 |
// If the exp is undefined or zero... |
| 216 |
if (typeof exp === 'undefined' || +exp === 0) { |
| 217 |
return Math[type](value); |
| 218 |
} |
| 219 |
value = +value; |
| 220 |
exp = +exp; |
| 221 |
// If the value is not a number or the exp is not an integer... |
| 222 |
if (value === null || isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { |
| 223 |
return NaN; |
| 224 |
} |
| 225 |
// If the value is negative... |
| 226 |
if (value < 0) { |
| 227 |
return -cookedDecimalAdjust(type, -value, exp); |
| 228 |
} |
| 229 |
// Shift |
| 230 |
value = value.toString().split('e'); |
| 231 |
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))); |
| 232 |
// Shift back |
| 233 |
value = value.toString().split('e'); |
| 234 |
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if ( !Math.round10 ) { |
| 239 |
Math.round10 = function(value, exp) { |
| 240 |
var percent = cookedDecimalAdjust('round', value, exp); |
| 241 |
return percent; |
| 242 |
}; |
| 243 |
} |
| 244 |
|
| 245 |
var cooked_recipe_migrate_counter = 0; |
| 246 |
var progressIterations = 0; |
| 247 |
|
| 248 |
function cooked_migrate_recipes(recipe_ids, total_recipes ) { |
| 249 |
var temp_counter = 0, |
| 250 |
total_counter = 0, |
| 251 |
progress_percent = 0; |
| 252 |
|
| 253 |
if (total_recipes > 0) { |
| 254 |
var progress = jQuery( '#cooked-migration-progress' ); |
| 255 |
var progress_bar = progress.find( '.cooked-progress-bar' ); |
| 256 |
var progress_text = jQuery( '#cooked-migration-progress-text' ); |
| 257 |
|
| 258 |
if ( !progress.hasClass('cooked-active') ) { |
| 259 |
progress.addClass('cooked-active'); |
| 260 |
progress_text.addClass('cooked-active'); |
| 261 |
progress_bar.css( { "width" : "0%" } ); |
| 262 |
} |
| 263 |
|
| 264 |
var this_recipe_ids = JSON.parse( recipe_ids ), |
| 265 |
this_total_recipe_ids = Object.keys(this_recipe_ids).length; |
| 266 |
|
| 267 |
var formattedTotal = total_recipes; |
| 268 |
formattedTotal.toLocaleString(); |
| 269 |
|
| 270 |
var ajax__bulk_migrate_recipes = jQuery.post( |
| 271 |
cooked_migration_js_vars.ajax_url, |
| 272 |
{ |
| 273 |
action: 'cooked_migrate_recipes', |
| 274 |
recipe_ids: recipe_ids, |
| 275 |
nonce: cooked_migration_js_vars.cooked_migrate_nonce |
| 276 |
}, |
| 277 |
function( new_recipe_ids ) { |
| 278 |
if ( new_recipe_ids && new_recipe_ids != 'false' && new_recipe_ids != false ){ |
| 279 |
var leftover_recipe_ids = JSON.parse( new_recipe_ids ), |
| 280 |
leftover_recipes = Object.keys(leftover_recipe_ids).length; |
| 281 |
|
| 282 |
cooked_recipe_migrate_counter = total_recipes - leftover_recipes; |
| 283 |
|
| 284 |
var formattedTotal = total_recipes; |
| 285 |
formattedTotal.toLocaleString(); |
| 286 |
|
| 287 |
var formattedComplete = cooked_recipe_migrate_counter; |
| 288 |
formattedComplete.toLocaleString(); |
| 289 |
|
| 290 |
progress_percent = Math.round10( ( cooked_recipe_migrate_counter / total_recipes ) * 100, -1 ); |
| 291 |
if ( progress_percent < 2 ) { progress_percent = 2; } |
| 292 |
progress_bar.css( { "width" : progress_percent + "%" } ); |
| 293 |
|
| 294 |
var remainingProgress = 100 - progress_percent; |
| 295 |
var estimatedCompletionTime = Math.round( ( remainingProgress / progress_percent ) * progressIterations ); |
| 296 |
var estimatedHours, estimatedMinutes; |
| 297 |
progressIterations += 1; |
| 298 |
|
| 299 |
if ( progress_percent < 100 && progress_percent > 3 && isFinite( estimatedCompletionTime ) ) { |
| 300 |
estimatedHours = Math.floor(estimatedCompletionTime / 3600); |
| 301 |
estimatedMinutes = Math.floor((estimatedCompletionTime / 60) % 60); |
| 302 |
if ( estimatedHours >= 1 ){ |
| 303 |
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " " + cooked_migration_js_vars.i18n_hrs + ", " + estimatedMinutes + " " + cooked_migration_js_vars.i18n_mins + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" ); |
| 304 |
} else if ( estimatedMinutes >= 1 ){ |
| 305 |
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " " + cooked_migration_js_vars.i18n_mins + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" ); |
| 306 |
} else { |
| 307 |
progress_text.text( formattedComplete + " / " + formattedTotal ); |
| 308 |
} |
| 309 |
|
| 310 |
} else { |
| 311 |
progress_text.text( formattedComplete + " / " + formattedTotal ); |
| 312 |
} |
| 313 |
|
| 314 |
cooked_migrate_recipes( new_recipe_ids, total_recipes ); |
| 315 |
} else { |
| 316 |
progress.hide(); |
| 317 |
progress_text.hide(); |
| 318 |
|
| 319 |
jQuery('.recipe-setting-block.migrate_button').find('h3').hide(); |
| 320 |
jQuery('.recipe-setting-block.migrate_button').find('p:nth-child(2)').hide(); |
| 321 |
jQuery('.recipe-setting-block.migrate_button').find('ul.cooked-admin-ul').hide(); |
| 322 |
jQuery('#cooked-migration-button').hide(); |
| 323 |
jQuery('#cooked-migration-completed').addClass('cooked-active'); |
| 324 |
} |
| 325 |
} |
| 326 |
) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
function cooked_import_recipes(recipe_ids, total_recipes, import_type) { |
| 332 |
var progress_percent = 0; |
| 333 |
|
| 334 |
if (total_recipes > 0) { |
| 335 |
var progress = jQuery( '#cooked-import-progress' ); |
| 336 |
var progress_bar = progress.find( '.cooked-progress-bar' ); |
| 337 |
var progress_text = jQuery( '#cooked-import-progress-text' ); |
| 338 |
|
| 339 |
if (!progress.hasClass('cooked-active')) { |
| 340 |
progress.addClass('cooked-active'); |
| 341 |
progress_text.addClass('cooked-active'); |
| 342 |
progress_bar.css( { "width" : "0%" } ); |
| 343 |
} |
| 344 |
|
| 345 |
var this_recipe_ids = JSON.parse(recipe_ids), |
| 346 |
this_total_recipe_ids = Object.keys(this_recipe_ids).length; |
| 347 |
|
| 348 |
var formattedTotal = total_recipes; |
| 349 |
formattedTotal.toLocaleString(); |
| 350 |
|
| 351 |
var ajax__bulk_migrate_recipes = jQuery.post( |
| 352 |
cooked_migration_js_vars.ajax_url, |
| 353 |
{ |
| 354 |
action: 'cooked_import_recipes', |
| 355 |
recipe_ids: recipe_ids, |
| 356 |
import_type: import_type, |
| 357 |
nonce: cooked_migration_js_vars.cooked_import_recipes_nonce |
| 358 |
}, |
| 359 |
function (new_recipe_ids) { |
| 360 |
if (new_recipe_ids && new_recipe_ids != 'false' && new_recipe_ids != false) { |
| 361 |
var leftover_recipe_ids = JSON.parse( new_recipe_ids ), |
| 362 |
leftover_recipes = Object.keys(leftover_recipe_ids).length; |
| 363 |
|
| 364 |
cooked_recipe_migrate_counter = total_recipes - leftover_recipes; |
| 365 |
|
| 366 |
var formattedTotal = total_recipes; |
| 367 |
formattedTotal.toLocaleString(); |
| 368 |
|
| 369 |
var formattedComplete = cooked_recipe_migrate_counter; |
| 370 |
formattedComplete.toLocaleString(); |
| 371 |
|
| 372 |
progress_percent = Math.round10( ( cooked_recipe_migrate_counter / total_recipes ) * 100, -1 ); |
| 373 |
if ( progress_percent < 2 ) { progress_percent = 2; } |
| 374 |
progress_bar.css({ "width" : progress_percent + "%" }); |
| 375 |
|
| 376 |
var remainingProgress = 100 - progress_percent; |
| 377 |
var estimatedCompletionTime = Math.round( ( remainingProgress / progress_percent ) * progressIterations ); |
| 378 |
var estimatedHours, estimatedMinutes; |
| 379 |
progressIterations += 1; |
| 380 |
|
| 381 |
if ( progress_percent < 100 && progress_percent > 3 && isFinite( estimatedCompletionTime ) ) { |
| 382 |
estimatedHours = Math.floor(estimatedCompletionTime / 3600); |
| 383 |
estimatedMinutes = Math.floor((estimatedCompletionTime / 60) % 60); |
| 384 |
if ( estimatedHours >= 1 ) { |
| 385 |
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " " + cooked_migration_js_vars.i18n_hrs + ", " + estimatedMinutes + " " + cooked_migration_js_vars.i18n_mins + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" ); |
| 386 |
} else if ( estimatedMinutes >= 1 ) { |
| 387 |
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " " + cooked_migration_js_vars.i18n_mins + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" ); |
| 388 |
} else { |
| 389 |
progress_text.text( formattedComplete + " / " + formattedTotal ); |
| 390 |
} |
| 391 |
|
| 392 |
} else { |
| 393 |
progress_text.text( formattedComplete + " / " + formattedTotal ); |
| 394 |
} |
| 395 |
|
| 396 |
cooked_import_recipes( new_recipe_ids, total_recipes, import_type ); |
| 397 |
} else { |
| 398 |
progress.hide(); |
| 399 |
progress_text.hide(); |
| 400 |
|
| 401 |
jQuery('.recipe-setting-block.import_button').find('h3').hide(); |
| 402 |
jQuery('.recipe-setting-block.import_button').find('p:nth-child(2)').hide(); |
| 403 |
jQuery('.recipe-setting-block.import_button').find('.cooked-import-note').hide(); |
| 404 |
jQuery('.recipe-setting-block.import_button').find('ul.cooked-admin-ul').hide(); |
| 405 |
jQuery('#cooked-import-button').hide(); |
| 406 |
jQuery('#cooked-import-completed').addClass('cooked-active'); |
| 407 |
} |
| 408 |
}) |
| 409 |
} |
| 410 |
} |
| 411 |
|