Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion assets/admin/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ body.post-type-cp_recipe #titlediv #title-prompt-text { font-size:15px; color:#8
position: relative;
border-radius: 10px;
margin: 40px 40px 40px 20px;
/* max-width:1000px; */
max-width: 1000px;
min-width: 1000px;
background: #fff;
box-shadow:
Expand Down Expand Up @@ -258,6 +258,14 @@ body.post-type-cp_recipe .switchery-small > small,
#cooked-migration-progress-text.cooked-progress-text, #cooked-import-progress-text.cooked-progress-text { font-size:11px; color:#888; max-width:600px; }
#cooked-migration-completed, #cooked-import-completed { display:none; }
#cooked-migration-completed.cooked-active, #cooked-import-completed.cooked-active { display:block; }
#cooked-csv-import-progress.cooked-progress { background:#ccc; margin:25px 0 0 0; border-radius:6px; height:12px; max-width:600px; }
#cooked-csv-import-progress.cooked-progress .cooked-progress-bar { border-radius:6px; height:12px; }
#cooked-csv-import-progress-text.cooked-progress-text { font-size:11px; color:#888; max-width:600px; }
#cooked-csv-import-completed { display:none; }
#cooked-csv-import-completed.cooked-active { display:block; }
#cooked-csv-import-form { max-width:100%; box-sizing:border-box; }
#cooked-csv-import-form input[type="file"] { max-width:100%; box-sizing:border-box; width:100%; }
#cooked-csv-import-errors { max-width:100%; box-sizing:border-box; word-wrap:break-word; }

/* Save as Default */
#cooked_recipe_settings .cooked-layout-save-default { position:relative; top:-2px; z-index:10; margin:0 0 0 15px; padding:0 8px 1px; }
Expand Down
2 changes: 1 addition & 1 deletion assets/admin/css/style.min.css

Large diffs are not rendered by default.

159 changes: 153 additions & 6 deletions assets/admin/js/cooked-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

var $_CookedMigrationButton = $('#cooked-migration-button'),
$_CookedImportButton = $('#cooked-import-button'),
$_CookedCSVImportButton = $('#cooked-csv-import-button'),
$_CookedCSVImportForm = $('#cooked-csv-import-form'),
$_CookedMigrationProgress = $('#cooked-migration-progress'),
$_CookedMigrationProgressText = $('#cooked-migration-progress-text');
$_CookedMigrationProgressText = $('#cooked-migration-progress-text'),
$_CookedCSVImportProgress = $('#cooked-csv-import-progress'),
$_CookedCSVImportProgressText = $('#cooked-csv-import-progress-text');

// Migration Button Exists?
if ($_CookedMigrationButton.length) {
Expand Down Expand Up @@ -67,7 +71,7 @@
cooked_import_recipes(json_recipe_ids, total_recipes, import_type);
}
} else {
console.log('Something went wrong');
console.log(cooked_migration_js_vars.i18n_something_wrong || 'Something went wrong');
thisButton.addClass('disabled').attr('disabled', false);
thisButton.show();
}
Expand All @@ -76,6 +80,145 @@
}
});
}

// CSV Import Button Exists?
if ($_CookedCSVImportButton.length) {
$_CookedCSVImportButton.on('click', function(e) {
e.preventDefault();

var thisButton = $(this),
fileInput = $('#cooked-csv-file'),
file = fileInput[0].files[0],
errorsDiv = $('#cooked-csv-import-errors');

errorsDiv.hide().empty();

if (!file) {
var errorMsg = (cooked_migration_js_vars.i18n_csv_no_file || 'Please select a CSV file.');
errorsDiv.html('<p>' + errorMsg + '</p>').show();
return;
}

if (file.type !== 'text/csv' && !file.name.endsWith('.csv')) {
var invalidFileMsg = (cooked_migration_js_vars.i18n_csv_invalid_file || 'Please select a valid CSV file.');
errorsDiv.html('<p>' + invalidFileMsg + '</p>').show();
return;
}

if (thisButton.hasClass('disabled')) {
return;
}

var confirm_import = confirm(cooked_migration_js_vars.i18n_confirm_csv_import || cooked_migration_js_vars.i18n_confirm_import_recipes || 'Are you sure you want to import recipes from this CSV file?');
if (!confirm_import) {
return;
}

thisButton.addClass('disabled').attr('disabled', true);
fileInput.attr('disabled', true);

var formData = new FormData();
formData.append('action', 'cooked_upload_csv');
formData.append('csv_file', file);

// Show progress
if (!$_CookedCSVImportProgress.hasClass('cooked-active')) {
$_CookedCSVImportProgress.addClass('cooked-active');
$_CookedCSVImportProgressText.addClass('cooked-active');
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "0%" });
$_CookedCSVImportProgressText.text(cooked_migration_js_vars.i18n_uploading || 'Uploading...');
}

// Upload file
$.ajax({
url: cooked_migration_js_vars.ajax_url,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
if (response.success) {
$_CookedCSVImportProgressText.text(cooked_migration_js_vars.i18n_processing || 'Processing...');
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "50%" });

// Process CSV
$.post(
cooked_migration_js_vars.ajax_url,
{
action: 'cooked_process_csv',
transient_key: response.data.transient_key
},
function(processResponse) {
if (processResponse.success) {
$_CookedCSVImportProgress.find('.cooked-progress-bar').css({ "width" : "100%" });
var recipesImportedText = cooked_migration_js_vars.i18n_recipes_imported || 'recipes imported';
$_CookedCSVImportProgressText.text(processResponse.data.success + ' / ' + processResponse.data.total + ' ' + recipesImportedText);

// Show errors if any
if (processResponse.data.errors && processResponse.data.errors.length > 0) {
var errorsLabel = cooked_migration_js_vars.i18n_errors || 'Errors:';
var errorHtml = '<p><strong>' + errorsLabel + '</strong></p><ul>';
processResponse.data.errors.forEach(function(error) {
errorHtml += '<li>' + error + '</li>';
});
errorHtml += '</ul>';
errorsDiv.html(errorHtml).show();
}

// Show success message
setTimeout(function() {
$_CookedCSVImportProgress.hide();
$_CookedCSVImportProgressText.hide();
$('#cooked-csv-import-completed').show();
thisButton.hide();
fileInput.hide();
}, 2000);
} else {
var importFailedText = cooked_migration_js_vars.i18n_import_failed || 'Import failed.';
errorsDiv.html('<p>' + (processResponse.data.message || importFailedText) + '</p>').show();
if (processResponse.data.errors && processResponse.data.errors.length > 0) {
var errorHtml = '<ul>';
processResponse.data.errors.forEach(function(error) {
errorHtml += '<li>' + error + '</li>';
});
errorHtml += '</ul>';
errorsDiv.append(errorHtml);
}
thisButton.removeClass('disabled').attr('disabled', false);
fileInput.attr('disabled', false);
$_CookedCSVImportProgress.removeClass('cooked-active');
$_CookedCSVImportProgressText.removeClass('cooked-active');
}
},
'json'
).fail(function() {
var failedProcessText = cooked_migration_js_vars.i18n_failed_process_csv || 'Failed to process CSV file.';
errorsDiv.html('<p>' + failedProcessText + '</p>').show();
thisButton.removeClass('disabled').attr('disabled', false);
fileInput.attr('disabled', false);
$_CookedCSVImportProgress.removeClass('cooked-active');
$_CookedCSVImportProgressText.removeClass('cooked-active');
});
} else {
var fileUploadFailedText = cooked_migration_js_vars.i18n_file_upload_failed || 'File upload failed.';
errorsDiv.html('<p>' + (response.data.message || fileUploadFailedText) + '</p>').show();
thisButton.removeClass('disabled').attr('disabled', false);
fileInput.attr('disabled', false);
$_CookedCSVImportProgress.removeClass('cooked-active');
$_CookedCSVImportProgressText.removeClass('cooked-active');
}
},
error: function() {
var failedUploadText = cooked_migration_js_vars.i18n_failed_upload_csv || 'Failed to upload CSV file.';
errorsDiv.html('<p>' + failedUploadText + '</p>').show();
thisButton.removeClass('disabled').attr('disabled', false);
fileInput.attr('disabled', false);
$_CookedCSVImportProgress.removeClass('cooked-active');
$_CookedCSVImportProgressText.removeClass('cooked-active');
}
});
});
}
});
})( jQuery );

Expand Down Expand Up @@ -167,10 +310,12 @@ function cooked_migrate_recipes(recipe_ids, total_recipes ) {
if ( progress_percent < 100 && progress_percent > 3 && isFinite( estimatedCompletionTime ) ){
estimatedHours = Math.floor(estimatedCompletionTime / 3600);
estimatedMinutes = Math.floor((estimatedCompletionTime / 60) % 60);
var hrsText = cooked_migration_js_vars.i18n_hrs || 'hrs';
var minsText = cooked_migration_js_vars.i18n_mins || 'mins';
if ( estimatedHours >= 1 ){
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " hrs, " + estimatedMinutes + " mins " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " " + hrsText + ", " + estimatedMinutes + " " + minsText + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
} else if ( estimatedMinutes >= 1 ){
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " mins " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " " + minsText + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
} else {
progress_text.text( formattedComplete + " / " + formattedTotal );
}
Expand Down Expand Up @@ -250,10 +395,12 @@ function cooked_import_recipes(recipe_ids, total_recipes, import_type) {
if ( progress_percent < 100 && progress_percent > 3 && isFinite( estimatedCompletionTime ) ) {
estimatedHours = Math.floor(estimatedCompletionTime / 3600);
estimatedMinutes = Math.floor((estimatedCompletionTime / 60) % 60);
var hrsText = cooked_migration_js_vars.i18n_hrs || 'hrs';
var minsText = cooked_migration_js_vars.i18n_mins || 'mins';
if ( estimatedHours >= 1 ){
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " hrs, " + estimatedMinutes + " mins " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedHours + " " + hrsText + ", " + estimatedMinutes + " " + minsText + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
} else if ( estimatedMinutes >= 1 ){
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " mins " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
progress_text.html( formattedComplete + " / " + formattedTotal + "<strong style='display:inline-block; float:right;'>" + estimatedMinutes + " " + minsText + " " + cooked_migration_js_vars.i18n_remaining + "</strong>" );
} else {
progress_text.text( formattedComplete + " / " + formattedTotal );
}
Expand Down
Loading