| 1 |
jQuery(document).ready(function($) { |
| 2 |
var migrationSteps = [ |
| 3 |
'settings', |
| 4 |
'clients', |
| 5 |
'post_types', |
| 6 |
'meta', |
| 7 |
'pro_options', |
| 8 |
'cleanup' |
| 9 |
]; |
| 10 |
|
| 11 |
var stepLabels = { |
| 12 |
'settings': 'Settings', |
| 13 |
'clients': 'Clients', |
| 14 |
'post_types': 'Posts', |
| 15 |
'meta': 'Meta Data', |
| 16 |
'pro_options': 'Pro Options', |
| 17 |
'cleanup': 'Cleanup' |
| 18 |
}; |
| 19 |
|
| 20 |
var currentStep = 0; |
| 21 |
var migrationRunning = false; |
| 22 |
var migrationLogs = []; |
| 23 |
|
| 24 |
function updateProgress(step, status, message) { |
| 25 |
var $step = $('#migration-step-' + step); |
| 26 |
$step.removeClass('pending running success error') |
| 27 |
.addClass(status); |
| 28 |
|
| 29 |
if (message) { |
| 30 |
$step.find('.step-message').text(message); |
| 31 |
} |
| 32 |
|
| 33 |
// Update progress bar |
| 34 |
var progress = ((currentStep + 1) / migrationSteps.length) * 100; |
| 35 |
$('#migration-progress').css('width', progress + '%'); |
| 36 |
} |
| 37 |
|
| 38 |
function appendLog(message, level) { |
| 39 |
var $log = $('#migration-log'); |
| 40 |
var timestamp = new Date().toLocaleTimeString(); |
| 41 |
var levelClass = level === 'error' ? 'text-danger' : |
| 42 |
level === 'success' ? 'text-success' : 'text-info'; |
| 43 |
var logEntry = '[' + timestamp + '] ' + message + '\n'; |
| 44 |
|
| 45 |
// Store log for copying |
| 46 |
migrationLogs.push(logEntry); |
| 47 |
|
| 48 |
$log.append('<div class="log-entry ' + levelClass + '">' + |
| 49 |
'<span class="log-time">[' + timestamp + ']</span> ' + |
| 50 |
'<span class="log-message">' + message + '</span>' + |
| 51 |
'</div>' |
| 52 |
); |
| 53 |
|
| 54 |
// Auto-scroll to bottom |
| 55 |
$log.scrollTop($log[0].scrollHeight); |
| 56 |
} |
| 57 |
|
| 58 |
function showError(message) { |
| 59 |
$('#migration-error').html(message).show(); |
| 60 |
appendLog(message, 'error'); |
| 61 |
} |
| 62 |
|
| 63 |
function runMigrationStep() { |
| 64 |
if (currentStep >= migrationSteps.length) { |
| 65 |
migrationRunning = false; |
| 66 |
$('#start-migration').prop('disabled', false).text('Migration Complete'); |
| 67 |
appendLog('Migration completed successfully', 'success'); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
var step = migrationSteps[currentStep]; |
| 72 |
updateProgress(step, 'running', 'Running...'); |
| 73 |
appendLog('Starting ' + stepLabels[step] + ' migration...', 'info'); |
| 74 |
|
| 75 |
$.ajax({ |
| 76 |
url: easyInvoiceMigration.ajaxurl, |
| 77 |
type: 'POST', |
| 78 |
data: { |
| 79 |
action: 'easy_invoice_migration', |
| 80 |
nonce: easyInvoiceMigration.nonce, |
| 81 |
step: step |
| 82 |
}, |
| 83 |
success: function(response) { |
| 84 |
if (response.success) { |
| 85 |
var result = response.data; |
| 86 |
|
| 87 |
// Add logs |
| 88 |
if (result.log) { |
| 89 |
result.log.forEach(function(entry) { |
| 90 |
appendLog(entry.message, entry.level); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
if (result.success) { |
| 95 |
updateProgress(step, 'success', result.message); |
| 96 |
currentStep++; |
| 97 |
runMigrationStep(); |
| 98 |
} else { |
| 99 |
updateProgress(step, 'error', result.message); |
| 100 |
showError(stepLabels[step] + ' migration failed: ' + result.message); |
| 101 |
migrationRunning = false; |
| 102 |
$('#start-migration').prop('disabled', false).text('Retry Migration'); |
| 103 |
} |
| 104 |
} else { |
| 105 |
updateProgress(step, 'error', response.data.message); |
| 106 |
showError(stepLabels[step] + ' migration failed: ' + response.data.message); |
| 107 |
migrationRunning = false; |
| 108 |
$('#start-migration').prop('disabled', false).text('Retry Migration'); |
| 109 |
} |
| 110 |
}, |
| 111 |
error: function(xhr, status, error) { |
| 112 |
var errorMessage = error; |
| 113 |
try { |
| 114 |
var response = JSON.parse(xhr.responseText); |
| 115 |
if (response.message) { |
| 116 |
errorMessage = response.message; |
| 117 |
} |
| 118 |
} catch (e) {} |
| 119 |
|
| 120 |
updateProgress(step, 'error', 'AJAX error: ' + errorMessage); |
| 121 |
showError(stepLabels[step] + ' migration failed: ' + errorMessage); |
| 122 |
migrationRunning = false; |
| 123 |
$('#start-migration').prop('disabled', false).text('Retry Migration'); |
| 124 |
|
| 125 |
// Log the full error details |
| 126 |
console.error('Migration AJAX error:', { |
| 127 |
status: status, |
| 128 |
error: error, |
| 129 |
xhr: xhr |
| 130 |
}); |
| 131 |
} |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
$('#start-migration').click(function(e) { |
| 136 |
e.preventDefault(); |
| 137 |
|
| 138 |
if (migrationRunning) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
// Reset UI |
| 143 |
migrationRunning = true; |
| 144 |
currentStep = 0; |
| 145 |
migrationLogs = []; |
| 146 |
$('#migration-error').hide(); |
| 147 |
$('#migration-log').empty(); |
| 148 |
$('.migration-step').removeClass('success error').addClass('pending'); |
| 149 |
$(this).prop('disabled', true).text('Migration Running...'); |
| 150 |
|
| 151 |
// Get migration counts first |
| 152 |
$.ajax({ |
| 153 |
url: easyInvoiceMigration.ajaxurl, |
| 154 |
type: 'GET', |
| 155 |
data: { |
| 156 |
action: 'easy_invoice_migration_counts', |
| 157 |
nonce: easyInvoiceMigration.nonce |
| 158 |
}, |
| 159 |
success: function(response) { |
| 160 |
if (response.success) { |
| 161 |
var counts = response.data; |
| 162 |
appendLog('Found items to migrate:', 'info'); |
| 163 |
appendLog('- Invoices: ' + counts.invoices, 'info'); |
| 164 |
appendLog('- Quotes: ' + counts.quotes, 'info'); |
| 165 |
appendLog('- Payments: ' + counts.payments, 'info'); |
| 166 |
appendLog('- Settings: ' + counts.options, 'info'); |
| 167 |
|
| 168 |
// Show detailed counts if available |
| 169 |
if (counts.details) { |
| 170 |
appendLog('\nDetailed counts:', 'info'); |
| 171 |
appendLog('- Invoice meta entries: ' + counts.details.invoice_meta_count, 'info'); |
| 172 |
appendLog('- Quote meta entries: ' + counts.details.quote_meta_count, 'info'); |
| 173 |
appendLog('- Payment meta entries: ' + counts.details.payment_meta_count, 'info'); |
| 174 |
} |
| 175 |
|
| 176 |
runMigrationStep(); |
| 177 |
} else { |
| 178 |
showError('Failed to get migration counts: ' + (response.data ? response.data.message : 'Unknown error')); |
| 179 |
migrationRunning = false; |
| 180 |
$('#start-migration').prop('disabled', false).text('Start Migration'); |
| 181 |
} |
| 182 |
}, |
| 183 |
error: function(xhr, status, error) { |
| 184 |
var errorMessage = error; |
| 185 |
try { |
| 186 |
var response = JSON.parse(xhr.responseText); |
| 187 |
if (response.message) { |
| 188 |
errorMessage = response.message; |
| 189 |
} |
| 190 |
} catch (e) {} |
| 191 |
|
| 192 |
showError('Failed to get migration counts: ' + errorMessage); |
| 193 |
migrationRunning = false; |
| 194 |
$('#start-migration').prop('disabled', false).text('Start Migration'); |
| 195 |
|
| 196 |
// Log the full error details |
| 197 |
console.error('Migration counts AJAX error:', { |
| 198 |
status: status, |
| 199 |
error: error, |
| 200 |
xhr: xhr |
| 201 |
}); |
| 202 |
} |
| 203 |
}); |
| 204 |
}); |
| 205 |
|
| 206 |
// Copy log functionality |
| 207 |
$('#copy-log').click(function() { |
| 208 |
var $button = $(this); |
| 209 |
var logText = migrationLogs.join(''); |
| 210 |
|
| 211 |
// Create temporary textarea |
| 212 |
var $temp = $('<textarea>'); |
| 213 |
$('body').append($temp); |
| 214 |
$temp.val(logText).select(); |
| 215 |
|
| 216 |
try { |
| 217 |
// Copy text |
| 218 |
document.execCommand('copy'); |
| 219 |
|
| 220 |
// Update button text |
| 221 |
$button.addClass('copied').html('<span class="dashicons dashicons-yes"></span> Copied!'); |
| 222 |
|
| 223 |
// Reset button after 2 seconds |
| 224 |
setTimeout(function() { |
| 225 |
$button.removeClass('copied').html('<span class="dashicons dashicons-clipboard"></span> Copy Log'); |
| 226 |
}, 2000); |
| 227 |
} catch (err) { |
| 228 |
console.error('Failed to copy log:', err); |
| 229 |
} |
| 230 |
|
| 231 |
// Remove temporary textarea |
| 232 |
$temp.remove(); |
| 233 |
}); |
| 234 |
}); |