| 1 |
|
| 2 |
/** |
| 3 |
* Bulk upload sites |
| 4 |
*/ |
| 5 |
|
| 6 |
let import_current = 0; |
| 7 |
let import_stop_by_user = false; |
| 8 |
let import_total = 0; |
| 9 |
let import_count_success = 0; |
| 10 |
let import_count_fails = 0; |
| 11 |
|
| 12 |
globalThis.mainwpVars = globalThis.mainwpVars || {}; |
| 13 |
|
| 14 |
jQuery(function () { |
| 15 |
import_total = Number.parseInt( jQuery('#mainwp_managesites_total_import').val(), 10 ) || 0; |
| 16 |
|
| 17 |
jQuery(document).on('click', '#mainwp_managesites_btn_import', function () { |
| 18 |
if (import_stop_by_user) { |
| 19 |
import_stop_by_user = false; |
| 20 |
jQuery('#mainwp_managesites_import_logging .log').append(MainWP.I18n.t('Continue import.') + "\n"); |
| 21 |
jQuery('#mainwp_managesites_btn_import').val(MainWP.I18n.t('Pause')); |
| 22 |
jQuery('#mainwp_managesites_btn_save_csv').attr('disabled', 'true'); // Disable |
| 23 |
mainwp_managesites_import_sites(); |
| 24 |
} else { |
| 25 |
import_stop_by_user = true; |
| 26 |
jQuery('#mainwp_managesites_import_logging .log').append(MainWP.I18n.t('Paused import by user.') + "\n"); |
| 27 |
jQuery('#mainwp_managesites_btn_import').val(MainWP.I18n.t('Continue')); |
| 28 |
jQuery('#mainwp_managesites_btn_save_csv').prop("disabled", false); //Enable |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
jQuery(document).on('click', '#mainwp_managesites_btn_save_csv', function () { |
| 33 |
let fail_data = ''; |
| 34 |
jQuery('#mainwp_managesites_import_fail_logging span').each(function () { |
| 35 |
fail_data += jQuery(this).html() + "\r"; |
| 36 |
}); |
| 37 |
let blob = new Blob([fail_data], { type: "text/plain;charset=utf-8" }); |
| 38 |
saveAs(blob, "import_sites_fails.csv"); |
| 39 |
}); |
| 40 |
|
| 41 |
if (jQuery('#mainwp_managesites_do_import').val() == 1) { |
| 42 |
mainwp_managesites_import_sites(); |
| 43 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
// eslint-disable-next-line complexity |
| 47 |
let mainwp_managesites_import_sites = function () { // NOSONAR - to compatible. |
| 48 |
if (import_stop_by_user) |
| 49 |
return; |
| 50 |
|
| 51 |
let page_href = jQuery("#mainwp-import-sites-modal").attr('data-page-url'); |
| 52 |
|
| 53 |
jQuery('#mainwp-importing-sites').hide(); |
| 54 |
|
| 55 |
import_current++; |
| 56 |
|
| 57 |
if (import_current > import_total || 0 === import_total ) { |
| 58 |
jQuery('#mainwp-import-sites-status-message').hide(); |
| 59 |
jQuery('#mainwp_managesites_btn_import').attr('disabled', 'true'); //Disable |
| 60 |
if (import_count_success < import_total || 0 === import_total) { |
| 61 |
jQuery('#mainwp_managesites_btn_save_csv').prop("disabled", false); //Enable |
| 62 |
} |
| 63 |
|
| 64 |
if ( 0 === import_total ) { |
| 65 |
jQuery( '#mainwp_managesites_import_logging .log' ).html( |
| 66 |
'<div style="text-align:center;margin:50px 0;"><h2 class="ui icon header"><i class="orange exclamation triangle icon"></i><div class="content">' + |
| 67 |
MainWP.I18n.t( 'No sites to import' ) + |
| 68 |
'<div class="sub header">' + |
| 69 |
MainWP.I18n.t( 'No valid data rows were found in the import file.' ) + |
| 70 |
'</div></div></h2></div>' |
| 71 |
); |
| 72 |
jQuery( '#mainwp-import-sites-modal > .close.icon' ).show(); |
| 73 |
} else if ( 0 === import_count_fails ) { |
| 74 |
jQuery( '#mainwp_managesites_import_logging .log' ).html( |
| 75 |
'<div style="text-align:center;margin:50px 0;"><h2 class="ui icon header"><i class="green check icon"></i><div class="content">' + |
| 76 |
MainWP.I18n.t( 'Congratulations!' ) + |
| 77 |
'<div class="sub header">' + |
| 78 |
import_count_success + ' ' + |
| 79 |
MainWP.I18n.t( 'sites imported successfully.' ) + |
| 80 |
'</div></div></h2></div>' |
| 81 |
); |
| 82 |
jQuery( '#mainwp_managesites_btn_import' ).hide(); |
| 83 |
|
| 84 |
if ( page_href !== undefined && '' !== page_href ) { |
| 85 |
setTimeout( function () { |
| 86 |
mainwp_forceReload( page_href ); |
| 87 |
}, 2000 ); |
| 88 |
} else { |
| 89 |
setTimeout( function () { |
| 90 |
mainwp_forceReload(); |
| 91 |
}, 2000 ); |
| 92 |
} |
| 93 |
} else { |
| 94 |
jQuery( '#mainwp_managesites_import_logging .log' ).append( |
| 95 |
'<div class="ui yellow message">' + |
| 96 |
MainWP.I18n.t( 'Process completed with errors.' ) + ' ' + |
| 97 |
import_count_fails + ' ' + |
| 98 |
MainWP.I18n.t( 'site(s) failed to import.' ) + ' ' + |
| 99 |
MainWP.I18n.t( 'Please review logs to resolve problems and try again.' ) + |
| 100 |
'</div>' |
| 101 |
); |
| 102 |
jQuery( '#mainwp_managesites_btn_import' ).hide(); |
| 103 |
jQuery( '#mainwp-import-sites-modal-try-again' ).show(); |
| 104 |
jQuery( '#mainwp-import-sites-modal-continue' ).show(); |
| 105 |
} |
| 106 |
|
| 107 |
jQuery('#mainwp_managesites_import_logging').scrollTop(jQuery('#mainwp_managesites_import_logging .log').height()); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Call the customer constructor without the website on the last run. |
| 112 |
if (import_current == import_total) { |
| 113 |
mainwp_managesites_import_client_no_website(); |
| 114 |
} |
| 115 |
|
| 116 |
let import_data = jQuery('#mainwp_managesites_import_csv_line_' + import_current).attr('encoded-data'); |
| 117 |
let import_line_orig = jQuery('#mainwp_managesites_import_csv_line_' + import_current).attr('original'); |
| 118 |
const is_page_managesites = jQuery('#mainwp_managesites_do_managesites_import').val(); // Get value page managesites |
| 119 |
|
| 120 |
let decodedVal = JSON.parse(import_data); |
| 121 |
|
| 122 |
let import_wpname = decodedVal.name; |
| 123 |
let import_wpurl = decodedVal.url; |
| 124 |
let import_wpadmin = decodedVal.adminname; |
| 125 |
let import_wpadmin_pwd = decodedVal.adminpwd; |
| 126 |
let import_wpgroups = decodedVal.wpgroups; |
| 127 |
let import_uniqueId = decodedVal.uniqueId; |
| 128 |
let import_http_username = decodedVal.http_user; |
| 129 |
let import_http_password = decodedVal.http_pass; |
| 130 |
let import_verify_certificate = decodedVal.verify_certificate; |
| 131 |
|
| 132 |
if (import_wpname === undefined) |
| 133 |
import_wpname = ''; |
| 134 |
if (import_wpurl === undefined) |
| 135 |
import_wpurl = ''; |
| 136 |
if (import_wpadmin === undefined) |
| 137 |
import_wpadmin = ''; |
| 138 |
if (import_wpgroups === undefined ) |
| 139 |
import_wpgroups = ''; |
| 140 |
if (import_uniqueId === undefined || import_uniqueId == null) { |
| 141 |
import_uniqueId = ''; |
| 142 |
} |
| 143 |
if (import_wpadmin_pwd === undefined || import_wpadmin_pwd == null) { |
| 144 |
import_wpadmin_pwd = ''; |
| 145 |
} |
| 146 |
|
| 147 |
jQuery('#mainwp_managesites_import_logging .log').append('<strong>[' + import_current + '] << ' + import_line_orig + '</strong><br/>'); |
| 148 |
|
| 149 |
let errors = []; |
| 150 |
|
| 151 |
if (import_wpname == '') { |
| 152 |
errors.push(MainWP.I18n.t('Please enter the site name.')); |
| 153 |
} |
| 154 |
|
| 155 |
if (import_wpurl == '') { |
| 156 |
errors.push(MainWP.I18n.t('Please enter the site URL.')); |
| 157 |
} |
| 158 |
|
| 159 |
if (import_wpadmin == '') { |
| 160 |
errors.push(MainWP.I18n.t('Please enter username of the site administrator.')); |
| 161 |
} |
| 162 |
|
| 163 |
if (errors.length > 0) { |
| 164 |
jQuery('#mainwp_managesites_import_logging .log').append('[' + import_current + '] >> Error - ' + errors.join(" ") + '<br/>'); |
| 165 |
jQuery('#mainwp_managesites_import_fail_logging').append('<span>' + import_line_orig + '</span>'); |
| 166 |
import_count_fails++; |
| 167 |
mainwp_managesites_import_sites(); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
let data = mainwp_secure_data({ |
| 172 |
action: 'mainwp_checkwp', |
| 173 |
name: import_wpname, |
| 174 |
url: import_wpurl, |
| 175 |
admin: import_wpadmin, |
| 176 |
check_me: import_current, |
| 177 |
verify_certificate: import_verify_certificate, |
| 178 |
http_user: import_http_username, |
| 179 |
http_pass: import_http_password |
| 180 |
}); |
| 181 |
|
| 182 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 183 |
let response = res_things.response ?? ''; |
| 184 |
|
| 185 |
let check_result = '[' + res_things.check_me + '] >> '; |
| 186 |
|
| 187 |
response = response.trim(); |
| 188 |
let url = import_wpurl; |
| 189 |
if (url.substring(0, 4) != 'http') { |
| 190 |
url = 'https://' + url; // default https://. |
| 191 |
} |
| 192 |
if (!url.endsWith('/')) { |
| 193 |
url += '/'; |
| 194 |
} |
| 195 |
|
| 196 |
url = url.replaceAll('"', '"'); |
| 197 |
|
| 198 |
if (response == 'HTTPERROR') { |
| 199 |
errors.push(check_result + MainWP.I18n.t('HTTP error: website does not exist!')); |
| 200 |
} else if (response == 'NOMAINWP') { |
| 201 |
errors.push(check_result + MainWP.I18n.t('MainWP Child plugin not detected! First install and activate the MainWP Child plugin and add your site to MainWP afterwards. Click <a href="%1" target="_blank">here</a> to install <a href="%2" target="_blank">MainWP</a> plugin (do not forget to activate it after installation)', url + 'wp-admin/plugin-install.php?tab=search&type=term&s=mainwp&plugin-search-input=Search+Plugins', url + 'wp-admin/plugin-install.php?tab=search&type=term&s=mainwp&plugin-search-input=Search+Plugins')); |
| 202 |
} else if (response.substring(0, 5) == 'ERROR') { |
| 203 |
if (response.length == 5) { |
| 204 |
errors.push(check_result + MainWP.I18n.t('Undefined error!')); |
| 205 |
} else { |
| 206 |
errors.push(check_result + 'ERROR: ' + response.substring(6)); |
| 207 |
} |
| 208 |
} else if (response == 'OK') { |
| 209 |
let groupids = []; |
| 210 |
let data = mainwp_secure_data({ |
| 211 |
action: "mainwp_addwp", |
| 212 |
managesites_add_wpname: import_wpname, |
| 213 |
managesites_add_wpurl: url, |
| 214 |
managesites_add_wpadmin: import_wpadmin, |
| 215 |
managesites_add_adminpwd: encodeURIComponent(import_wpadmin_pwd), |
| 216 |
managesites_add_uniqueId: import_uniqueId, |
| 217 |
"groupids[]": groupids, |
| 218 |
groupnames_import: import_wpgroups, |
| 219 |
add_me: import_current, |
| 220 |
verify_certificate: import_verify_certificate, |
| 221 |
managesites_add_http_user: import_http_username, |
| 222 |
managesites_add_http_pass: import_http_password, |
| 223 |
bulk: true, |
| 224 |
}); |
| 225 |
|
| 226 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 227 |
if (res_things.error) { |
| 228 |
response = 'ERROR: ' + res_things.error; |
| 229 |
} else { |
| 230 |
response = res_things.response; |
| 231 |
} |
| 232 |
let add_result = '[' + res_things.add_me + '] >> '; |
| 233 |
|
| 234 |
response = response.trim(); |
| 235 |
|
| 236 |
if (response.substring(0, 5) == 'ERROR') { |
| 237 |
jQuery('#mainwp_managesites_import_fail_logging').append('<span>' + import_line_orig + '</span>'); |
| 238 |
jQuery('#mainwp_managesites_import_logging .log').append(add_result + response.substring(6) + "<br/>"); |
| 239 |
import_count_fails++; |
| 240 |
} else { |
| 241 |
//Message the WP was added. |
| 242 |
jQuery('#mainwp_managesites_import_logging .log').append(add_result + response + "<br/>"); |
| 243 |
// Check if you are on page managesites and import by form. |
| 244 |
if (is_page_managesites !== undefined && Number(is_page_managesites) === 1) { |
| 245 |
let data_temp_managesites = mainwp_secure_data({ |
| 246 |
action: 'mainwp_delete_temp_import_website', |
| 247 |
managesites_add_wpurl: url, |
| 248 |
managesites_add_wpadmin: import_wpadmin, |
| 249 |
}); |
| 250 |
jQuery.post(ajaxurl, data_temp_managesites, function () { }); |
| 251 |
} |
| 252 |
|
| 253 |
// Create Client |
| 254 |
mainwp_managesites_import_client(import_current, res_things.siteid); |
| 255 |
|
| 256 |
import_count_success++; |
| 257 |
} |
| 258 |
mainwp_managesites_import_sites(); |
| 259 |
}, 'json').fail(function (xhr, textStatus, errorThrown) { |
| 260 |
jQuery('#mainwp_managesites_import_fail_logging').append('<span>' + import_line_orig + '</span>'); |
| 261 |
jQuery('#mainwp_managesites_import_logging .log').append("error: " + errorThrown + "<br/>"); |
| 262 |
import_count_fails++; |
| 263 |
mainwp_managesites_import_sites(); |
| 264 |
}); |
| 265 |
} |
| 266 |
|
| 267 |
if (errors.length > 0) { |
| 268 |
jQuery('#mainwp_managesites_import_fail_logging').append('<span>' + import_line_orig + '</span>'); |
| 269 |
jQuery('#mainwp_managesites_import_logging .log').append(errors.join("\n") + '<br/>'); |
| 270 |
import_count_fails++; |
| 271 |
mainwp_managesites_import_sites(); |
| 272 |
} |
| 273 |
jQuery('#mainwp_managesites_import_logging').scrollTop(jQuery('#mainwp_managesites_import_logging .log').height()); |
| 274 |
}, 'json'); |
| 275 |
|
| 276 |
}; |
| 277 |
|
| 278 |
// Handle add new client |
| 279 |
const mainwp_managesites_import_client = function (index, siteid) { |
| 280 |
let import_client_data = jQuery('#mainwp_managesites_import_client_line_' + index).attr('encoded-data'); |
| 281 |
|
| 282 |
if (import_client_data === undefined || import_client_data == null) { |
| 283 |
import_client_data = ''; |
| 284 |
} |
| 285 |
|
| 286 |
if (import_client_data !== '') { |
| 287 |
const data = mainwp_secure_data({ |
| 288 |
action: 'mainwp_import_website_add_client', |
| 289 |
client: import_client_data, |
| 290 |
site_id: siteid |
| 291 |
}); |
| 292 |
|
| 293 |
jQuery.post(ajaxurl, data, function () { }); |
| 294 |
} |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
// Handle add new client no website |
| 300 |
const mainwp_managesites_import_client_no_website = function () { |
| 301 |
let client_data = []; |
| 302 |
jQuery('.mainwp_managesites_import_client_no_site_lines').each(function () { |
| 303 |
let data = jQuery(this).attr('encoded-data'); |
| 304 |
client_data.push(data); |
| 305 |
}); |
| 306 |
|
| 307 |
if (client_data.length > 0) { |
| 308 |
const data = mainwp_secure_data({ |
| 309 |
action: 'mainwp_import_website_add_client_no_site', |
| 310 |
client: client_data, |
| 311 |
}); |
| 312 |
|
| 313 |
jQuery.post(ajaxurl, data, function () { }); |
| 314 |
} |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
// Handle page import website |
| 319 |
jQuery(document).ready(function ($) { |
| 320 |
// Get default value |
| 321 |
let import_index = $("#mainwp-managesites-import-row").attr( |
| 322 |
"data-default-row" |
| 323 |
); |
| 324 |
let is_page_setup = $("#mainwp-managesites-import-row").attr( |
| 325 |
"data-page-setup" |
| 326 |
); |
| 327 |
// Store the initial value of the input |
| 328 |
let initial_value = ""; |
| 329 |
// Add new row by clicking Add New Row button |
| 330 |
$("#mainwp-managesites-import-row").on("click", function (e) { |
| 331 |
e.preventDefault(); |
| 332 |
import_index++; // Update index before create row. |
| 333 |
|
| 334 |
let new_row = ""; |
| 335 |
if (Number(is_page_setup) === 1) { |
| 336 |
new_row = |
| 337 |
mainwp_managesites_import_sites_page_setup_add_row(import_index); |
| 338 |
} else { |
| 339 |
new_row = mainwp_managesites_import_sites_add_row(import_index); |
| 340 |
} |
| 341 |
|
| 342 |
$(this).parent().parent().before(new_row); |
| 343 |
}); |
| 344 |
|
| 345 |
// When user focuses on input, save initial value |
| 346 |
$("#mainwp-managesites-row-import-sites").on("focus", "input", function () { |
| 347 |
initial_value = $(this).val(); |
| 348 |
}); |
| 349 |
|
| 350 |
// Attach blur event to all input fields whose name is site_url |
| 351 |
$("#mainwp-managesites-row-import-sites").on( |
| 352 |
"blur", |
| 353 |
".mainwp-managesites-import-site-url", |
| 354 |
function () { |
| 355 |
const input = $(this); |
| 356 |
if (input.val() === initial_value) { |
| 357 |
return; |
| 358 |
} |
| 359 |
const row_index = input.attr("data-row-index"); |
| 360 |
const input_site_name = $( |
| 361 |
"#mainwp-managesites-import-site-name-" + row_index |
| 362 |
); |
| 363 |
const parsed_url = mainwp_managesites_import_sites_extract_domain( |
| 364 |
input.val() |
| 365 |
); |
| 366 |
|
| 367 |
if (parsed_url !== "" && parsed_url !== undefined) { |
| 368 |
// Update input value with domain name only |
| 369 |
input.val(`${parsed_url.origin}${parsed_url.pathname}`); |
| 370 |
// Set value site name |
| 371 |
input_site_name.val(`${parsed_url.origin}`); |
| 372 |
} else { |
| 373 |
const user_confirmed = confirm( |
| 374 |
MainWP.I18n.t( |
| 375 |
"Please enter a valid URL. Example: http://example.com\nClick OK to stay and correct, or Cancel to continue without correcting." |
| 376 |
) |
| 377 |
); |
| 378 |
if (user_confirmed) { |
| 379 |
setTimeout(function () { |
| 380 |
input.focus(); |
| 381 |
}, 1); // Reset focus immediately after confirm |
| 382 |
} |
| 383 |
input_site_name.val(input.val()); |
| 384 |
} |
| 385 |
} |
| 386 |
); |
| 387 |
|
| 388 |
// Catch paste event to check URL as soon as data is pasted |
| 389 |
$(document).on( |
| 390 |
"paste", |
| 391 |
".mainwp-managesites-import-site-url", |
| 392 |
function () { |
| 393 |
const input = $(this); |
| 394 |
if (input.val() === initial_value) { |
| 395 |
return; |
| 396 |
} |
| 397 |
const row_index = input.attr("data-row-index"); |
| 398 |
const input_site_name = $( |
| 399 |
"#mainwp-managesites-import-site-name-" + row_index |
| 400 |
); |
| 401 |
const parsed_url = mainwp_managesites_import_sites_extract_domain( |
| 402 |
input.val() |
| 403 |
); |
| 404 |
if (parsed_url !== "" && parsed_url !== undefined) { |
| 405 |
// Update input value with domain name only |
| 406 |
input.val(`${parsed_url.origin}${parsed_url.pathname}`); |
| 407 |
// Set value site name |
| 408 |
input_site_name.val(`${parsed_url.origin}`); |
| 409 |
} else { |
| 410 |
// Show confirm, asking the user if they want to edit or not |
| 411 |
const user_confirmed = confirm( |
| 412 |
MainWP.I18n.t( |
| 413 |
"The pasted URL is invalid. Example: http://example.com\nClick OK to stay and correct, or Cancel to continue without correcting." |
| 414 |
) |
| 415 |
); |
| 416 |
if (user_confirmed) { |
| 417 |
setTimeout(function () { |
| 418 |
input.focus(); |
| 419 |
}, 1); // Reset focus if user selects OK |
| 420 |
input_site_name.val(input.val()); |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
); |
| 425 |
|
| 426 |
// Save data when leaving inputs in a row. |
| 427 |
$("#mainwp-managesites-row-import-sites").on("blur", "input", function () { |
| 428 |
const current_value = $(this).val(); |
| 429 |
// Check if value has changed before calling AJAX |
| 430 |
if (current_value !== initial_value) { |
| 431 |
const row_index = $(this).attr("data-row-index"); |
| 432 |
mainwp_managesites_save_row_temp_data(row_index); |
| 433 |
} |
| 434 |
}); |
| 435 |
}); |
| 436 |
|
| 437 |
// Function to send temporary data of a row to the server. |
| 438 |
const mainwp_managesites_save_row_temp_data = function (row_index) { |
| 439 |
const row = jQuery("#mainwp-managesites-import-row-" + row_index); |
| 440 |
const site_url = row |
| 441 |
.find(`input[name="mainwp_managesites_import[${row_index}][site_url]"]`) |
| 442 |
.val(); |
| 443 |
const admin_name = row |
| 444 |
.find(`input[name="mainwp_managesites_import[${row_index}][admin_name]"]`) |
| 445 |
.val(); |
| 446 |
const admin_password = row |
| 447 |
.find( |
| 448 |
`input[name="mainwp_managesites_import[${row_index}][admin_password]"]` |
| 449 |
) |
| 450 |
.val(); |
| 451 |
|
| 452 |
// Check if the fields site_url, admin_name and admin_password are not empty. |
| 453 |
if (site_url && admin_name && admin_password) { |
| 454 |
const site_name = row |
| 455 |
.find(`input[name="mainwp_managesites_import[${row_index}][site_name]"]`) |
| 456 |
.val(); |
| 457 |
const tag = row |
| 458 |
.find(`input[name="mainwp_managesites_import[${row_index}][tag]"]`) |
| 459 |
.val(); |
| 460 |
const security_id = row |
| 461 |
.find( |
| 462 |
`input[name="mainwp_managesites_import[${row_index}][security_id]"]` |
| 463 |
) |
| 464 |
.val(); |
| 465 |
const http_username = row |
| 466 |
.find( |
| 467 |
`input[name="mainwp_managesites_import[${row_index}][http_username]"]` |
| 468 |
) |
| 469 |
.val(); |
| 470 |
const http_password = row |
| 471 |
.find( |
| 472 |
`input[name="mainwp_managesites_import[${row_index}][http_password]"]` |
| 473 |
) |
| 474 |
.val(); |
| 475 |
const verify_certificate = row |
| 476 |
.find( |
| 477 |
`input[name="mainwp_managesites_import[${row_index}][verify_certificate]"]` |
| 478 |
) |
| 479 |
.val(); |
| 480 |
const ssl_version = row |
| 481 |
.find( |
| 482 |
`input[name="mainwp_managesites_import[${row_index}][ssl_version]"]` |
| 483 |
) |
| 484 |
.val(); |
| 485 |
const data = mainwp_secure_data({ |
| 486 |
action: "mainwp_save_temp_import_website", |
| 487 |
status: "save_temp", |
| 488 |
row_index: Number(row_index), |
| 489 |
site_url: site_url, |
| 490 |
admin_name: admin_name, |
| 491 |
admin_password: admin_password, |
| 492 |
site_name: site_name, |
| 493 |
tag: tag, |
| 494 |
security_id: security_id, |
| 495 |
http_username: http_username, |
| 496 |
http_password: http_password, |
| 497 |
verify_certificate: verify_certificate, |
| 498 |
ssl_version: ssl_version, |
| 499 |
}); |
| 500 |
|
| 501 |
// Send row save data to server. |
| 502 |
jQuery.post(ajaxurl, data, function () { }); |
| 503 |
} |
| 504 |
}; |
| 505 |
// Function to get the domain part from the entered URL |
| 506 |
const mainwp_managesites_import_sites_extract_domain = function (url) { // NOSONAR - to compatible. |
| 507 |
|
| 508 |
try { |
| 509 |
// Use URL API to parse URL and get only protocol and host part |
| 510 |
return new URL(url); |
| 511 |
} catch (e) { |
| 512 |
console.log(e); |
| 513 |
// If the URL is invalid or contains an error, return an empty string. |
| 514 |
} |
| 515 |
return ""; |
| 516 |
}; |
| 517 |
|
| 518 |
// Delete row when pressing Delete button. |
| 519 |
const mainwp_managesites_import_sites_delete_row = function (index) { |
| 520 |
const row = jQuery("#mainwp-managesites-import-row-" + index); |
| 521 |
const site_url = row |
| 522 |
.find(`input[name="mainwp_managesites_import[${index}][site_url]"]`) |
| 523 |
.val(); |
| 524 |
const admin_name = row |
| 525 |
.find(`input[name="mainwp_managesites_import[${index}][admin_name]"]`) |
| 526 |
.val(); |
| 527 |
// Check if the fields site_url, admin_name are not empty. |
| 528 |
if (site_url && admin_name) { |
| 529 |
const data = mainwp_secure_data({ |
| 530 |
action: "mainwp_save_temp_import_website", |
| 531 |
status: "delete_temp", |
| 532 |
row_index: Number(index), |
| 533 |
}); |
| 534 |
|
| 535 |
jQuery.post(ajaxurl, data, function (response) { }); |
| 536 |
} |
| 537 |
jQuery("#mainwp-managesites-import-row-" + index).remove(); |
| 538 |
return true; |
| 539 |
}; |
| 540 |
// Toggle form field in page setup wizard. |
| 541 |
const mainwp_managesites_import_sites_more_row = function (index) { |
| 542 |
jQuery(".mainwp-managesites-import-column-more-" + index).fadeToggle("slow"); |
| 543 |
jQuery("#icon-visible-" + index).toggle(); |
| 544 |
jQuery("#icon-hidden-" + index).toggle(); |
| 545 |
return false; |
| 546 |
}; |
| 547 |
|
| 548 |
const mainwp_managesites_import_sites_page_setup_add_row = function (row_index) { |
| 549 |
row_index--; |
| 550 |
return `<div class="row mainwp-managesites-import-rows" id="mainwp-managesites-import-row-${row_index}" data-index="${row_index}"> |
| 551 |
<div class="four wide column"> |
| 552 |
${mainwp_managesites_import_sites_render_input( |
| 553 |
row_index, |
| 554 |
"site_url", |
| 555 |
"mainwp-managesites-import-site-url-" + row_index, |
| 556 |
"mainwp-managesites-import-site-url" |
| 557 |
)} |
| 558 |
</div> |
| 559 |
<div class="four wide column"> |
| 560 |
${mainwp_managesites_import_sites_render_input( |
| 561 |
row_index, |
| 562 |
"site_name", |
| 563 |
"mainwp-managesites-import-site-name-" + row_index, |
| 564 |
"mainwp-managesites-import-site-name" |
| 565 |
)} |
| 566 |
</div> |
| 567 |
<div class="four wide column"> |
| 568 |
${mainwp_managesites_import_sites_render_input( |
| 569 |
row_index, |
| 570 |
"admin_name", |
| 571 |
"mainwp-managesites-import-admin-name-" + row_index, |
| 572 |
"mainwp-managesites-import-admin-name" |
| 573 |
)} |
| 574 |
</div> |
| 575 |
<div class="three wide column"> |
| 576 |
${mainwp_managesites_import_sites_render_input( |
| 577 |
row_index, |
| 578 |
"admin_password", |
| 579 |
"mainwp-managesites-import-admin-password-" + row_index, |
| 580 |
"mainwp-managesites-import-admin-password", |
| 581 |
"", |
| 582 |
"", |
| 583 |
"password" |
| 584 |
)} |
| 585 |
</div> |
| 586 |
<div class="one wide column"> |
| 587 |
<div class="ui mini fluid input"> |
| 588 |
<a class="mainwp-managesites-more-import-row" href="javascript:void(0)" onclick="mainwp_managesites_import_sites_more_row(${row_index})" style="margin-right: 10px !important;"> |
| 589 |
<i class="eye outline icon" id="icon-visible-${row_index}" ></i> |
| 590 |
<i class="eye slash outline icon" id="icon-hidden-${row_index}" style="display: none;"></i> |
| 591 |
</a> |
| 592 |
${mainwp_managesites_import_sites_render_button_remove(row_index)} |
| 593 |
</div> |
| 594 |
</div> |
| 595 |
<div class="three wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 596 |
${mainwp_managesites_import_sites_render_column( |
| 597 |
row_index, |
| 598 |
"Tag", |
| 599 |
"tag", |
| 600 |
"tag" |
| 601 |
)} |
| 602 |
</div> |
| 603 |
<div class="three wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 604 |
${mainwp_managesites_import_sites_render_column( |
| 605 |
row_index, |
| 606 |
"Security ID", |
| 607 |
"security_id", |
| 608 |
"security-id" |
| 609 |
)} |
| 610 |
</div> |
| 611 |
<div class="three wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 612 |
${mainwp_managesites_import_sites_render_column( |
| 613 |
row_index, |
| 614 |
"HTTP Username", |
| 615 |
"http_username", |
| 616 |
"http-username" |
| 617 |
)} |
| 618 |
</div> |
| 619 |
<div class="three wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 620 |
${mainwp_managesites_import_sites_render_column( |
| 621 |
row_index, |
| 622 |
"HTTP Password", |
| 623 |
"http_password", |
| 624 |
"http-password" |
| 625 |
)} |
| 626 |
</div> |
| 627 |
<div class="two wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 628 |
${mainwp_managesites_import_sites_render_column( |
| 629 |
row_index, |
| 630 |
"Verify Certificate", |
| 631 |
"verify_certificate", |
| 632 |
"verify-certificate", |
| 633 |
"number", |
| 634 |
1 |
| 635 |
)} |
| 636 |
</div> |
| 637 |
<div class="two wide column mainwp-managesites-import-column-more-${row_index}" style="display:none"> |
| 638 |
${mainwp_managesites_import_sites_render_column( |
| 639 |
row_index, |
| 640 |
"SSL Version", |
| 641 |
"ssl_version", |
| 642 |
"ssl-version", |
| 643 |
"", |
| 644 |
"auto" |
| 645 |
)} |
| 646 |
</div> |
| 647 |
</div>`; |
| 648 |
}; |
| 649 |
// Add new row by clicking Add New Row button. |
| 650 |
const mainwp_managesites_import_sites_add_row = function (row_index) { |
| 651 |
row_index--; |
| 652 |
return ` |
| 653 |
<div class="row mainwp-managesites-import-rows" id="mainwp-managesites-import-row-${row_index}" data-index="${row_index}" data-temp-id="${row_index}"> |
| 654 |
<div class="two wide column"> |
| 655 |
${mainwp_managesites_import_sites_render_input( |
| 656 |
row_index, |
| 657 |
"site_url", |
| 658 |
"mainwp-managesites-import-site-url-" + row_index, |
| 659 |
"mainwp-managesites-import-site-url" |
| 660 |
)} |
| 661 |
</div> |
| 662 |
<div class="two wide column"> |
| 663 |
${mainwp_managesites_import_sites_render_input( |
| 664 |
row_index, |
| 665 |
"site_name", |
| 666 |
"mainwp-managesites-import-site-name-" + row_index, |
| 667 |
"mainwp-managesites-import-site-name" |
| 668 |
)} |
| 669 |
</div> |
| 670 |
<div class="two wide column"> |
| 671 |
${mainwp_managesites_import_sites_render_input( |
| 672 |
row_index, |
| 673 |
"admin_name", |
| 674 |
"mainwp-managesites-import-admin-name-" + row_index, |
| 675 |
"mainwp-managesites-import-admin-name" |
| 676 |
)} |
| 677 |
</div> |
| 678 |
<div class="two wide column"> |
| 679 |
${mainwp_managesites_import_sites_render_input( |
| 680 |
row_index, |
| 681 |
"admin_password", |
| 682 |
"mainwp-managesites-import-admin-password-" + row_index, |
| 683 |
"mainwp-managesites-import-admin-password", |
| 684 |
"", |
| 685 |
"", |
| 686 |
"password" |
| 687 |
)} |
| 688 |
</div> |
| 689 |
|
| 690 |
<div class="one wide column"> |
| 691 |
${mainwp_managesites_import_sites_render_input( |
| 692 |
row_index, |
| 693 |
"tag", |
| 694 |
"mainwp-managesites-import-tag-" + row_index, |
| 695 |
"mainwp-managesites-import-tag" |
| 696 |
)} |
| 697 |
</div> |
| 698 |
<div class="one wide column"> |
| 699 |
${mainwp_managesites_import_sites_render_input( |
| 700 |
row_index, |
| 701 |
"security_id", |
| 702 |
"mainwp-managesites-import-security-id-" + row_index, |
| 703 |
"mainwp-managesites-import-security-id" |
| 704 |
)} |
| 705 |
</div> |
| 706 |
<div class="two wide column"> |
| 707 |
${mainwp_managesites_import_sites_render_input( |
| 708 |
row_index, |
| 709 |
"http_username", |
| 710 |
"mainwp-managesites-import-http-username-" + row_index, |
| 711 |
"mainwp-managesites-import-http-username" |
| 712 |
)} |
| 713 |
</div> |
| 714 |
<div class="one wide column"> |
| 715 |
${mainwp_managesites_import_sites_render_input( |
| 716 |
row_index, |
| 717 |
"http_password", |
| 718 |
"mainwp-managesites-import-http-password-" + row_index, |
| 719 |
"mainwp-managesites-import-http-password" |
| 720 |
)} |
| 721 |
</div> |
| 722 |
<div class="one wide column"> |
| 723 |
${mainwp_managesites_import_sites_render_input( |
| 724 |
row_index, |
| 725 |
"verify_certificate", |
| 726 |
"mainwp-managesites-import-verify-certificate-" + row_index, |
| 727 |
"mainwp-managesites-import-verify-certificate", |
| 728 |
"number", |
| 729 |
1 |
| 730 |
)} |
| 731 |
</div> |
| 732 |
<div class="one wide column"> |
| 733 |
${mainwp_managesites_import_sites_render_input( |
| 734 |
row_index, |
| 735 |
"ssl_version", |
| 736 |
"mainwp-managesites-import-ssl-version" + row_index, |
| 737 |
"mainwp-managesites-import-ssl-version", |
| 738 |
"", |
| 739 |
"auto" |
| 740 |
)} |
| 741 |
</div> |
| 742 |
<div class="one wide column"> |
| 743 |
${mainwp_managesites_import_sites_render_button_remove(row_index)} |
| 744 |
</div> |
| 745 |
</div>`; |
| 746 |
}; |
| 747 |
|
| 748 |
// Render column. |
| 749 |
const mainwp_managesites_import_sites_render_column = function ( |
| 750 |
row_index, |
| 751 |
label, |
| 752 |
input_name, |
| 753 |
input_class, |
| 754 |
input_type = "", |
| 755 |
value = "" |
| 756 |
) { |
| 757 |
return ` |
| 758 |
<div class=""> |
| 759 |
<span class="ui small text">${label}</span> |
| 760 |
${mainwp_managesites_import_sites_render_input( |
| 761 |
row_index, |
| 762 |
input_name, |
| 763 |
"mainwp-managesites-import-" + input_class + "-" + row_index, |
| 764 |
"mainwp-managesites-import-" + input_class, |
| 765 |
input_type, |
| 766 |
value |
| 767 |
)} |
| 768 |
</div>`; |
| 769 |
}; |
| 770 |
|
| 771 |
// Render input. |
| 772 |
const mainwp_managesites_import_sites_render_input = function ( |
| 773 |
row_index, |
| 774 |
name, |
| 775 |
id = "", |
| 776 |
class_st = "", |
| 777 |
type = "", |
| 778 |
value = "", |
| 779 |
filed_type = "text", |
| 780 |
) { |
| 781 |
return ` |
| 782 |
<div class="ui mini fluid input"> |
| 783 |
<input type="${filed_type}" value="${value}" id="${id}" class="mini ${class_st}" name="mainwp_managesites_import[${row_index}][${name}]" data-row-index="${row_index}" ${type === "number" ? "oninput=\"this.value = this.value.replace(/[^0-9]/g, '')\"" : ""}/> |
| 784 |
</div>`; |
| 785 |
}; |
| 786 |
|
| 787 |
// Render button remove row. |
| 788 |
const mainwp_managesites_import_sites_render_button_remove = function ( |
| 789 |
row_index |
| 790 |
) { |
| 791 |
return ` |
| 792 |
<a class="mainwp-managesites-delete-import-row" href="#" onclick="mainwp_managesites_import_sites_delete_row(${row_index})"> |
| 793 |
<i class="trash alternate outline icon"></i> |
| 794 |
</a>`; |
| 795 |
}; |
| 796 |
// validate form before submitting form managesites_import. |
| 797 |
const mainwp_managesites_import_handle_form_before_submit = function () { |
| 798 |
let has_table_data = false; |
| 799 |
let csv_selected = jQuery(`input[name="mainwp_managesites_file_bulkupload"]`).val() !== ""; // Check if CSV file is selected |
| 800 |
let error_messages = []; |
| 801 |
|
| 802 |
// Iterate through each row in the rows |
| 803 |
has_table_data = mainwp_managesites_validate_import_rows(error_messages); |
| 804 |
|
| 805 |
// Check if both CSV and table have data |
| 806 |
if (csv_selected && has_table_data) { |
| 807 |
error_messages.push(MainWP.I18n.t("You can only submit either the table data or a CSV file, not both at the same time")); |
| 808 |
} |
| 809 |
|
| 810 |
// Check if both are empty |
| 811 |
if (!csv_selected && !has_table_data) { |
| 812 |
error_messages.push(MainWP.I18n.t("Please fill in the table or select a CSV file.")); |
| 813 |
} |
| 814 |
|
| 815 |
return error_messages; |
| 816 |
}; |
| 817 |
|
| 818 |
// Check data in model table add new website. |
| 819 |
const mainwp_managesites_validate_import_rows = function (error_messages, is_valid_row = false) { |
| 820 |
let has_table_data = false; |
| 821 |
let valid_row_count = 0; |
| 822 |
jQuery( |
| 823 |
"#mainwp-managesites-row-import-sites .mainwp-managesites-import-rows" |
| 824 |
).each(function (index) { |
| 825 |
let site_url = jQuery( |
| 826 |
`input[name="mainwp_managesites_import[${index}][site_url]"]` |
| 827 |
).val(); |
| 828 |
let admin_name = jQuery( |
| 829 |
`input[name="mainwp_managesites_import[${index}][admin_name]"]` |
| 830 |
).val(); |
| 831 |
let admin_password = jQuery( |
| 832 |
`input[name="mainwp_managesites_import[${index}][admin_password]"]` |
| 833 |
).val(); |
| 834 |
|
| 835 |
// If there is data in any row of the table, check the required fields |
| 836 |
if (site_url || admin_name || admin_password) { |
| 837 |
has_table_data = true; |
| 838 |
|
| 839 |
let msg = ""; |
| 840 |
if (!site_url) { |
| 841 |
msg = MainWP.I18n.t("Site URL is required in row %1", index + 1); |
| 842 |
error_messages.push(msg); |
| 843 |
} |
| 844 |
if (!admin_name) { |
| 845 |
msg = MainWP.I18n.t("Admin Name is required in row %1", index + 1); |
| 846 |
error_messages.push(msg); |
| 847 |
} |
| 848 |
if (!admin_password) { |
| 849 |
msg = MainWP.I18n.t("Admin Password is required in row %1", index + 1); |
| 850 |
error_messages.push(msg); |
| 851 |
} |
| 852 |
|
| 853 |
// If both site_url, admin_name and admin_password have values, increment the counter variable. |
| 854 |
if (site_url && admin_name && admin_password) { |
| 855 |
valid_row_count++; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
}); |
| 860 |
if (is_valid_row && error_messages.length === 0 && valid_row_count === 0) { |
| 861 |
error_messages.push(MainWP.I18n.t("At least one row must have both Site URL, Admin Name and Admin Password.")); |
| 862 |
} |
| 863 |
|
| 864 |
return has_table_data; |
| 865 |
} |