| 1 |
/* global metasyncImportData, jQuery, ajaxurl */ |
| 2 |
/** |
| 3 |
* MetaSync Import External Data |
| 4 |
* |
| 5 |
* Extracted for Phase 5, #887. |
| 6 |
* Handles importing external plugin data (redirections, SEO metadata) |
| 7 |
* with batch progress tracking for SEO metadata imports. |
| 8 |
* |
| 9 |
* Localized object: metasyncImportData |
| 10 |
* - importNonce (string) Nonce for metasync_import_external_data |
| 11 |
* - seoImportNonce (string) Nonce for metasync_import_seo_metadata |
| 12 |
* |
| 13 |
* @since Phase 5 |
| 14 |
*/ |
| 15 |
jQuery(document).ready(function ($) { |
| 16 |
var importNonce = metasyncImportData.importNonce; |
| 17 |
var seoImportNonce = metasyncImportData.seoImportNonce; |
| 18 |
var currentImportData = null; |
| 19 |
|
| 20 |
// Handle import button clicks |
| 21 |
$('.metasync-import-btn').on('click', function () { |
| 22 |
var btn = $(this); |
| 23 |
var type = btn.data('type'); |
| 24 |
var plugin = btn.data('plugin'); |
| 25 |
var card = btn.closest('.metasync-plugin-card'); |
| 26 |
|
| 27 |
// Special handling for SEO metadata - show options modal |
| 28 |
if (type === 'seo_metadata') { |
| 29 |
currentImportData = { |
| 30 |
btn: btn, |
| 31 |
type: type, |
| 32 |
plugin: plugin, |
| 33 |
card: card |
| 34 |
}; |
| 35 |
$('#metasync-seo-options-modal').addClass('active'); |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// For other import types, proceed directly |
| 40 |
performImport(btn, type, plugin, card); |
| 41 |
}); |
| 42 |
|
| 43 |
// Modal close handlers |
| 44 |
$('.metasync-modal-close, #modal-cancel').on('click', function () { |
| 45 |
$('#metasync-seo-options-modal').removeClass('active'); |
| 46 |
$('.metasync-progress-container').removeClass('active'); |
| 47 |
$('.metasync-progress-fill').css('width', '0%'); |
| 48 |
$('.metasync-progress-text').text('0%'); |
| 49 |
$('.metasync-progress-status').text(''); |
| 50 |
currentImportData = null; |
| 51 |
}); |
| 52 |
|
| 53 |
// Click outside modal to close |
| 54 |
$('.metasync-modal-overlay').on('click', function (e) { |
| 55 |
if (e.target === this) { |
| 56 |
$(this).find('.metasync-modal-close').click(); |
| 57 |
} |
| 58 |
}); |
| 59 |
|
| 60 |
// Start Import button in modal |
| 61 |
$('#modal-start-import').on('click', function () { |
| 62 |
if (!currentImportData) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
var options = { |
| 67 |
import_titles: $('#import-titles').is(':checked'), |
| 68 |
import_descriptions: $('#import-descriptions').is(':checked'), |
| 69 |
overwrite_existing: $('#overwrite-existing').is(':checked') |
| 70 |
}; |
| 71 |
|
| 72 |
// Disable form elements |
| 73 |
$('#modal-start-import, #modal-cancel, #import-titles, #import-descriptions, #overwrite-existing') |
| 74 |
.prop('disabled', true); |
| 75 |
|
| 76 |
// Show progress |
| 77 |
$('.metasync-progress-container').addClass('active'); |
| 78 |
|
| 79 |
// Start batch import |
| 80 |
performBatchImport(currentImportData, options); |
| 81 |
}); |
| 82 |
|
| 83 |
// Perform direct import (for non-SEO metadata types) |
| 84 |
function performImport(btn, type, plugin, card) { |
| 85 |
var resultDiv = card.find('.metasync-import-result'); |
| 86 |
|
| 87 |
btn.prop('disabled', true).text('Importing...'); |
| 88 |
resultDiv.removeClass('success error').hide(); |
| 89 |
|
| 90 |
$.ajax({ |
| 91 |
url: ajaxurl, |
| 92 |
type: 'POST', |
| 93 |
data: { |
| 94 |
action: 'metasync_import_external_data', |
| 95 |
nonce: importNonce, |
| 96 |
type: type, |
| 97 |
plugin: plugin |
| 98 |
}, |
| 99 |
success: function (response) { |
| 100 |
if (response.success) { |
| 101 |
btn.addClass('success').text('\u2713 Imported'); |
| 102 |
resultDiv.addClass('success').text(response.data.message).slideDown(); |
| 103 |
} else { |
| 104 |
btn.prop('disabled', false).text('Import ' + type.charAt(0).toUpperCase() + type.slice(1)); |
| 105 |
resultDiv.addClass('error').text(response.data.message || 'Import failed.').slideDown(); |
| 106 |
} |
| 107 |
}, |
| 108 |
error: function () { |
| 109 |
btn.prop('disabled', false).text('Retry Import'); |
| 110 |
resultDiv.addClass('error').text('Network error. Please try again.').slideDown(); |
| 111 |
} |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
// Perform batch import with progress tracking (for SEO metadata) |
| 116 |
function performBatchImport(importData, options) { |
| 117 |
var offset = 0; |
| 118 |
var totalImported = 0; |
| 119 |
var totalSkipped = 0; |
| 120 |
|
| 121 |
function processBatch() { |
| 122 |
$.ajax({ |
| 123 |
url: ajaxurl, |
| 124 |
type: 'POST', |
| 125 |
data: { |
| 126 |
action: 'metasync_import_seo_metadata', |
| 127 |
nonce: seoImportNonce, |
| 128 |
plugin: importData.plugin, |
| 129 |
import_titles: options.import_titles ? 1 : 0, |
| 130 |
import_descriptions: options.import_descriptions ? 1 : 0, |
| 131 |
overwrite_existing: options.overwrite_existing ? 1 : 0, |
| 132 |
offset: offset |
| 133 |
}, |
| 134 |
success: function (response) { |
| 135 |
if (response.success && response.data) { |
| 136 |
var data = response.data; |
| 137 |
totalImported += data.imported || 0; |
| 138 |
totalSkipped += data.skipped || 0; |
| 139 |
|
| 140 |
// Update progress bar |
| 141 |
var percent = data.progress_percent || 0; |
| 142 |
$('.metasync-progress-fill').css('width', percent + '%'); |
| 143 |
$('.metasync-progress-text').text(percent + '%'); |
| 144 |
$('.metasync-progress-status').text( |
| 145 |
'Processing... ' + (data.processed || 0) + ' of ' + (data.total || 0) + ' posts' |
| 146 |
); |
| 147 |
|
| 148 |
if (data.is_complete) { |
| 149 |
// Import complete |
| 150 |
$('.metasync-progress-status').empty() |
| 151 |
.append($('<strong>').css('color', 'var(--dashboard-success)').text('\u2713 Import Complete!')) |
| 152 |
.append(document.createTextNode(' Imported: ' + totalImported + ' posts | Skipped: ' + totalSkipped + ' posts')); |
| 153 |
|
| 154 |
importData.btn.addClass('success').text('\u2713 Imported'); |
| 155 |
|
| 156 |
// Close modal after 2 seconds |
| 157 |
setTimeout(function () { |
| 158 |
$('#metasync-seo-options-modal .metasync-modal-close').click(); |
| 159 |
// Re-enable button for future imports |
| 160 |
$('#modal-start-import, #modal-cancel, #import-titles, #import-descriptions, #overwrite-existing') |
| 161 |
.prop('disabled', false); |
| 162 |
}, 2000); |
| 163 |
} else { |
| 164 |
// Continue with next batch |
| 165 |
offset = data.processed || 0; |
| 166 |
processBatch(); |
| 167 |
} |
| 168 |
} else { |
| 169 |
// Error occurred |
| 170 |
var importErrMsg = (response.data && response.data.message ? response.data.message : 'Unknown error occurred'); |
| 171 |
var $importStatus = $('.metasync-progress-status').empty(); |
| 172 |
$importStatus.append($('<strong>').css('color', 'var(--dashboard-error)').text('\u2717 Import Failed')); |
| 173 |
$importStatus[0].appendChild(document.createTextNode(' ' + importErrMsg)); |
| 174 |
$('#modal-start-import, #modal-cancel, #import-titles, #import-descriptions, #overwrite-existing') |
| 175 |
.prop('disabled', false); |
| 176 |
} |
| 177 |
}, |
| 178 |
error: function () { |
| 179 |
$('.metasync-progress-status').empty() |
| 180 |
.append($('<strong>').css('color', 'var(--dashboard-error)').text('\u2717 Network Error')) |
| 181 |
.append(document.createTextNode(' Please try again')); |
| 182 |
$('#modal-start-import, #modal-cancel, #import-titles, #import-descriptions, #overwrite-existing') |
| 183 |
.prop('disabled', false); |
| 184 |
} |
| 185 |
}); |
| 186 |
} |
| 187 |
|
| 188 |
// Start first batch |
| 189 |
processBatch(); |
| 190 |
} |
| 191 |
}); |
| 192 |
|