| 1 |
jQuery(function () { |
| 2 |
jQuery('.mainwp-field-tab-connect input[name=tab_connect]').change(function () { |
| 3 |
const tab_active = this.value; |
| 4 |
if (tab_active !== '') { |
| 5 |
jQuery('#mainwp-qsw-connect-site-form').fadeIn(500); |
| 6 |
jQuery('#mainwp_managesites_add').show(); |
| 7 |
jQuery('#mainwp-qsw-toggle-verify-mainwp-child-active').show(); |
| 8 |
jQuery('#mainwp_addsite_continue_button').hide(); |
| 9 |
jQuery('.menu-connect-first-site .item').tab('change tab', tab_active); |
| 10 |
} |
| 11 |
}); |
| 12 |
|
| 13 |
jQuery('#mainwp-qsw-verify-mainwp-child-active').on('change', function () { |
| 14 |
if (jQuery(this).is(':checked')) { |
| 15 |
jQuery('#mainwp_managesites_add').removeClass('disabled'); |
| 16 |
jQuery('#mainwp_managesites_add_import').removeClass('disabled'); |
| 17 |
} else { |
| 18 |
jQuery('#mainwp_managesites_add').addClass('disabled'); |
| 19 |
jQuery('#mainwp_managesites_add_import').addClass('disabled'); |
| 20 |
} |
| 21 |
}); |
| 22 |
|
| 23 |
// Handle submit import file CVS. |
| 24 |
jQuery(document).on('click', '#mainwp_managesites_add_import', function () { |
| 25 |
let error_messages = mainwp_managesites_import_handle_form_before_submit(); |
| 26 |
// If there is an error, prevent submission and display the error |
| 27 |
if (error_messages.length > 0) { |
| 28 |
feedback('mainwp-message-zone', error_messages.join("<br/>"), "red"); |
| 29 |
} else { |
| 30 |
jQuery('#mainwp_connect_first_site_form').trigger('submit'); |
| 31 |
} |
| 32 |
|
| 33 |
return false; |
| 34 |
}); |
| 35 |
|
| 36 |
jQuery('#mainwp_qsw_client_name_field').on('keyup', function () { |
| 37 |
if (jQuery(this).val()) { |
| 38 |
jQuery('#bulk_add_createclient').show(); |
| 39 |
jQuery('#mainwp_qsw_add_client_continue_button').hide(); |
| 40 |
} else { |
| 41 |
jQuery('#bulk_add_createclient').hide(); |
| 42 |
jQuery('#mainwp_qsw_add_client_continue_button').show(); |
| 43 |
} |
| 44 |
}); |
| 45 |
|
| 46 |
jQuery('#mainwp-toggle-optional-settings').on('click', function () { |
| 47 |
jQuery('#mainwp-qsw-optional-settings-form').toggle(300); |
| 48 |
return false; |
| 49 |
}); |
| 50 |
|
| 51 |
jQuery('.ui.checkbox:not(.not-auto-init)').checkbox(); |
| 52 |
jQuery('.ui.dropdown:not(.not-auto-init)').dropdown(); |
| 53 |
|
| 54 |
jQuery('.mainwp-checkbox-showhide-elements').on('click', function () { |
| 55 |
let $el = jQuery(this); |
| 56 |
let hiels = $el.attr('hide-parent'); |
| 57 |
if (hiels !== undefined && hiels !== '') { |
| 58 |
let isChecked = $el.find('input').is(':checked'); |
| 59 |
let items = hiels.split(';'); |
| 60 |
items.forEach(function(hiel) { |
| 61 |
// if semantic ui checkbox is checked. |
| 62 |
if (isChecked) { |
| 63 |
jQuery('[hide-element=' + hiel + ']').fadeIn(500); |
| 64 |
} else { |
| 65 |
jQuery('[hide-element=' + hiel + ']').fadeOut(500); |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
}); |
| 70 |
|
| 71 |
jQuery(document).on('click', '#mainwp_managesites_add', function () { |
| 72 |
mainwp_setup_managesites_add(); |
| 73 |
}); |
| 74 |
|
| 75 |
jQuery(document).on('change', '#mainwp_managesites_add_wpurl', function () { |
| 76 |
let url = jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 77 |
let protocol = jQuery('#mainwp_managesites_add_wpurl_protocol').val(); |
| 78 |
|
| 79 |
if (url.lastIndexOf('http://') === 0) { |
| 80 |
protocol = 'http'; |
| 81 |
url = url.substring(7); |
| 82 |
} else if (url.lastIndexOf('https://') === 0) { |
| 83 |
protocol = 'https'; |
| 84 |
url = url.substring(8); |
| 85 |
} |
| 86 |
|
| 87 |
if (jQuery('#mainwp_managesites_add_wpname').val() == '') { |
| 88 |
jQuery('#mainwp_managesites_add_wpname').val(url); |
| 89 |
} |
| 90 |
|
| 91 |
jQuery('#mainwp_managesites_add_wpurl').val(url); |
| 92 |
jQuery('#mainwp_managesites_add_wpurl_protocol').val(protocol).trigger("change"); |
| 93 |
}); |
| 94 |
|
| 95 |
}); |
| 96 |
|
| 97 |
// Handle tab onvisible. |
| 98 |
const mainwp_menu_connect_first_site_onvisible_callback = function (objItem) { |
| 99 |
const tab = jQuery(objItem).attr("data-tab"); |
| 100 |
jQuery('.mainwp-field-tab-connect input[name=tab_connect]').filter(`[value="${tab}"]`).parent().trigger('click'); // set checked class ui checkbox |
| 101 |
if (tab === 'multiple-site') { |
| 102 |
jQuery('#mainwp_managesites_add_import').show(); |
| 103 |
jQuery('#mainwp_managesites_add').hide(); |
| 104 |
} else if (tab === 'single-site') { |
| 105 |
jQuery('#mainwp_managesites_add').show(); |
| 106 |
jQuery('#mainwp_managesites_add_import').hide(); |
| 107 |
} |
| 108 |
} |
| 109 |
// Connect a new website |
| 110 |
let mainwp_setup_managesites_add = function () { |
| 111 |
mainwp_set_message_zone('#mainwp-message-zone'); |
| 112 |
let errors = []; |
| 113 |
|
| 114 |
if (jQuery('#mainwp_managesites_add_wpname').val().trim() == '') { |
| 115 |
errors.push('Please enter a title for the website.'); |
| 116 |
} |
| 117 |
|
| 118 |
if (jQuery('#mainwp_managesites_add_wpurl').val().trim() == '') { |
| 119 |
errors.push('Please enter a valid URL for the site.'); |
| 120 |
} else { |
| 121 |
let url = jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 122 |
if (!url.endsWith('/')) { |
| 123 |
url += '/'; |
| 124 |
} |
| 125 |
|
| 126 |
jQuery('#mainwp_managesites_add_wpurl').val(url); |
| 127 |
|
| 128 |
if (!isUrl(jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val())) { |
| 129 |
errors.push('Please enter a valid URL for the site.'); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (jQuery('#mainwp_managesites_add_wpadmin').val().trim() == '') { |
| 134 |
errors.push('Please enter a username of the website administrator.'); |
| 135 |
} |
| 136 |
|
| 137 |
if (errors.length > 0) { |
| 138 |
mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'yellow'); |
| 139 |
} else { |
| 140 |
mainwp_set_message_zone('#mainwp-message-zone', '<i class="notched circle loading icon"></i> Adding the site to your MainWP Dashboard. Please wait...', ''); |
| 141 |
jQuery('#mainwp_managesites_add').attr('disabled', 'true'); //disable button to add.. |
| 142 |
|
| 143 |
let url = jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 144 |
|
| 145 |
if (!url.endsWith('/')) { |
| 146 |
url += '/'; |
| 147 |
} |
| 148 |
|
| 149 |
let name = jQuery('#mainwp_managesites_add_wpname').val().trim(); |
| 150 |
name = name.replace(/"/g, '"'); |
| 151 |
|
| 152 |
let data = mainwp_setup_secure_data({ |
| 153 |
action: 'mainwp_checkwp', |
| 154 |
name: name, |
| 155 |
url: url, |
| 156 |
admin: jQuery('#mainwp_managesites_add_wpadmin').val().trim(), |
| 157 |
}); |
| 158 |
|
| 159 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 160 |
let response = res_things.response; |
| 161 |
response = response.trim(); |
| 162 |
|
| 163 |
let url = jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 164 |
if (!url.endsWith('/')) { |
| 165 |
url += '/'; |
| 166 |
} |
| 167 |
|
| 168 |
url = url.replace(/"/g, '"'); |
| 169 |
|
| 170 |
if (response == 'HTTPERROR') { |
| 171 |
errors.push('This site can not be reached! Please use the Test Connection feature and see if the positive response will be returned. For additional help, please review <a href="https://docs.mainwp.com/">MainWP Knowledgebase</a>, and if you still have issues, please let us know in the <a href="https://community.mainwp.com/c/community-support/5">MainWP Community</a>.'); // NOSONAR - noopener - open safe. |
| 172 |
} else if (response == 'NOMAINWP') { |
| 173 |
errors.push(mainwp_js_get_error_not_detected_connect()); |
| 174 |
} else if (response.substring(0, 5) == 'ERROR') { |
| 175 |
if (response.length == 5) { |
| 176 |
errors.push('Undefined error occurred. Please try again. If the issue does not resolve, please review <a href="https://docs.mainwp.com/">MainWP Knowledgebase</a>, and if you still have issues, please let us know in the <a href="https://community.mainwp.com/c/community-support/5">MainWP Community</a>.'); // NOSONAR - noopener - open safe. |
| 177 |
} else { |
| 178 |
let error = response.substring(6); |
| 179 |
let err = mainwp_js_get_error_not_detected_connect(error, 'html_msg', false, true); // return text error. |
| 180 |
if (false === err) { |
| 181 |
errors.push(error); // it is not json string error. |
| 182 |
} else if (true !== err && '' != err) { |
| 183 |
errors.push(err); // decoded error. |
| 184 |
} |
| 185 |
} |
| 186 |
} else if (response == 'OK') { |
| 187 |
jQuery('#mainwp_managesites_add').attr('disabled', 'true'); |
| 188 |
|
| 189 |
let name = jQuery('#mainwp_managesites_add_wpname').val(); |
| 190 |
name = name.replace(/"/g, '"'); |
| 191 |
let group_ids = ''; |
| 192 |
let data = mainwp_setup_secure_data({ |
| 193 |
action: 'mainwp_addwp', |
| 194 |
managesites_add_wpname: name, |
| 195 |
managesites_add_wpurl: url, |
| 196 |
managesites_add_wpadmin: jQuery('#mainwp_managesites_add_wpadmin').val(), |
| 197 |
managesites_add_adminpwd: encodeURIComponent(jQuery('#mainwp_managesites_add_admin_pwd').val()), |
| 198 |
managesites_add_uniqueId: jQuery('#mainwp_managesites_add_uniqueId').val(), |
| 199 |
groupids: group_ids, |
| 200 |
qsw_page: true, |
| 201 |
}); |
| 202 |
|
| 203 |
// to support add client reports tokens values |
| 204 |
jQuery("input[name^='creport_token_']").each(function () { |
| 205 |
let tname = jQuery(this).attr('name'); |
| 206 |
let tvalue = jQuery(this).val(); |
| 207 |
data[tname] = tvalue; |
| 208 |
}); |
| 209 |
|
| 210 |
// support hooks fields |
| 211 |
jQuery(".mainwp_addition_fields_addsite input").each(function () { |
| 212 |
let tname = jQuery(this).attr('name'); |
| 213 |
let tvalue = jQuery(this).val(); |
| 214 |
data[tname] = tvalue; |
| 215 |
}); |
| 216 |
|
| 217 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 218 |
|
| 219 |
if (res_things.error) { |
| 220 |
response = res_things.error; |
| 221 |
} else { |
| 222 |
response = res_things.response; |
| 223 |
} |
| 224 |
|
| 225 |
response = response.trim(); |
| 226 |
|
| 227 |
mainwp_set_message_zone('#mainwp-message-zone'); |
| 228 |
jQuery('#mainwp-info-zone').hide(); |
| 229 |
|
| 230 |
if (response.substring(0, 5) == 'ERROR') { |
| 231 |
mainwp_set_message_zone('#mainwp-message-zone', response.substring(6), 'red'); |
| 232 |
} else { |
| 233 |
//Message the WP was added |
| 234 |
mainwp_set_message_zone('#mainwp-message-zone', response, 'green'); |
| 235 |
|
| 236 |
//Reset fields |
| 237 |
jQuery('#mainwp_managesites_add_wpname').val(''); |
| 238 |
jQuery('#mainwp_managesites_add_wpurl').val(''); |
| 239 |
jQuery('#mainwp_managesites_add_wpurl_protocol').val('https'); |
| 240 |
jQuery('#mainwp_managesites_add_wpadmin').val(''); |
| 241 |
jQuery('#mainwp_managesites_add_admin_pwd').val(''); |
| 242 |
jQuery('#mainwp_managesites_add_uniqueId').val(''); |
| 243 |
|
| 244 |
jQuery("input[name^='creport_token_']").each(function () { |
| 245 |
jQuery(this).val(''); |
| 246 |
}); |
| 247 |
|
| 248 |
// support hooks fields |
| 249 |
jQuery(".mainwp_addition_fields_addsite input").each(function () { |
| 250 |
jQuery(this).val(''); |
| 251 |
}); |
| 252 |
|
| 253 |
setTimeout(function () { |
| 254 |
mainwp_forceReload('admin.php?page=mainwp-setup&step=add_client'); |
| 255 |
}, 3000); |
| 256 |
} |
| 257 |
|
| 258 |
jQuery('#mainwp_managesites_add').prop("disabled", false); |
| 259 |
}, 'json'); |
| 260 |
} |
| 261 |
if (errors.length > 0) { |
| 262 |
mainwp_set_message_zone('#mainwp-message-zone', errors.join('<br />'), 'red'); |
| 263 |
jQuery('#mainwp_managesites_add').prop("disabled", false); |
| 264 |
} |
| 265 |
}, 'json'); |
| 266 |
} |
| 267 |
}; |
| 268 |
|
| 269 |
// Check if the URL field is valid value |
| 270 |
function isUrl(url) { |
| 271 |
try { |
| 272 |
new URL(url); |
| 273 |
return true; |
| 274 |
} catch (e) { |
| 275 |
console.log(e); |
| 276 |
} |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
let mainwp_setup_secure_data = function (data) { |
| 281 |
if (data['action'] == undefined) |
| 282 |
return data; |
| 283 |
data.security = jQuery('input[name="qsw_nonce[' + data.action + ']"]').val() || ''; |
| 284 |
return data; |
| 285 |
}; |
| 286 |
|