| 1 |
jQuery(function () { |
| 2 |
jQuery('#mainwp-qsw-verify-mainwp-child-active').on('change', function () { |
| 3 |
if (jQuery(this).is(':checked')) { |
| 4 |
jQuery('#mainwp-qsw-connect-site-form').fadeIn(500); |
| 5 |
jQuery('#mainwp_managesites_add').show(); |
| 6 |
jQuery('#mainwp_addsite_continue_button').hide(); |
| 7 |
} else { |
| 8 |
jQuery('#mainwp-qsw-connect-site-form').fadeOut(500); |
| 9 |
jQuery('#mainwp_managesites_add').hide(); |
| 10 |
jQuery('#mainwp_addsite_continue_button').show(); |
| 11 |
} |
| 12 |
}); |
| 13 |
|
| 14 |
jQuery('#mainwp_qsw_client_name_field').on('keyup', function () { |
| 15 |
if (jQuery(this).val()) { |
| 16 |
jQuery('#bulk_add_createclient').show(); |
| 17 |
jQuery('#mainwp_qsw_add_client_continue_button').hide(); |
| 18 |
} else { |
| 19 |
jQuery('#bulk_add_createclient').hide(); |
| 20 |
jQuery('#mainwp_qsw_add_client_continue_button').show(); |
| 21 |
} |
| 22 |
}); |
| 23 |
|
| 24 |
jQuery('#mainwp-toggle-optional-settings').on('click', function () { |
| 25 |
jQuery('#mainwp-qsw-optional-settings-form').toggle(300); |
| 26 |
return false; |
| 27 |
}); |
| 28 |
|
| 29 |
jQuery('.ui.checkbox:not(.not-auto-init)').checkbox(); |
| 30 |
jQuery('.ui.dropdown:not(.not-auto-init)').dropdown(); |
| 31 |
|
| 32 |
jQuery('.mainwp-checkbox-showhide-elements').on('click', function () { |
| 33 |
var hiel = jQuery(this).attr('hide-parent'); |
| 34 |
// if semantic ui checkbox is checked. |
| 35 |
if (jQuery(this).find('input').is(':checked')) { |
| 36 |
jQuery('[hide-element=' + hiel + ']').fadeIn(500); |
| 37 |
} else { |
| 38 |
jQuery('[hide-element=' + hiel + ']').fadeOut(500); |
| 39 |
} |
| 40 |
}); |
| 41 |
|
| 42 |
jQuery(document).on('click', '#mainwp_managesites_add', function (event) { |
| 43 |
mainwp_setup_managesites_add(event); |
| 44 |
}); |
| 45 |
|
| 46 |
jQuery(document).on('change', '#mainwp_managesites_add_wpurl', function () { |
| 47 |
var url = jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 48 |
var protocol = jQuery('#mainwp_managesites_add_wpurl_protocol').val(); |
| 49 |
|
| 50 |
if (url.lastIndexOf('http://') === 0) { |
| 51 |
protocol = 'http'; |
| 52 |
url = url.substring(7); |
| 53 |
} else if (url.lastIndexOf('https://') === 0) { |
| 54 |
protocol = 'https'; |
| 55 |
url = url.substring(8); |
| 56 |
} |
| 57 |
|
| 58 |
if (jQuery('#mainwp_managesites_add_wpname').val() == '') { |
| 59 |
jQuery('#mainwp_managesites_add_wpname').val(url); |
| 60 |
} |
| 61 |
|
| 62 |
jQuery('#mainwp_managesites_add_wpurl').val(url); |
| 63 |
jQuery('#mainwp_managesites_add_wpurl_protocol').val(protocol).trigger("change"); |
| 64 |
}); |
| 65 |
|
| 66 |
}); |
| 67 |
|
| 68 |
// Connect a new website |
| 69 |
mainwp_setup_managesites_add = function () { |
| 70 |
|
| 71 |
jQuery('#mainwp-message-zone').hide(); |
| 72 |
|
| 73 |
var errors = []; |
| 74 |
|
| 75 |
if (jQuery('#mainwp_managesites_add_wpname').val().trim() == '') { |
| 76 |
errors.push('Please enter a title for the website.'); |
| 77 |
} |
| 78 |
|
| 79 |
if (jQuery('#mainwp_managesites_add_wpurl').val().trim() == '') { |
| 80 |
errors.push('Please enter a valid URL for the site.'); |
| 81 |
} else { |
| 82 |
var url = jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 83 |
if (url.substr(-1) != '/') { |
| 84 |
url += '/'; |
| 85 |
} |
| 86 |
|
| 87 |
jQuery('#mainwp_managesites_add_wpurl').val(url); |
| 88 |
|
| 89 |
if (!isUrl(jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val())) { |
| 90 |
errors.push('Please enter a valid URL for the site.'); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (jQuery('#mainwp_managesites_add_wpadmin').val().trim() == '') { |
| 95 |
errors.push('Please enter a username of the website administrator.'); |
| 96 |
} |
| 97 |
|
| 98 |
if (errors.length > 0) { |
| 99 |
jQuery('#mainwp-message-zone').html(errors.join('<br />')).addClass('yellow').show(); |
| 100 |
} else { |
| 101 |
jQuery('#mainwp-message-zone').html('Adding the site to your MainWP Dashboard. Please wait...').removeClass('green red yellow').show(); |
| 102 |
jQuery('#mainwp_managesites_add').attr('disabled', 'true'); //disable button to add.. |
| 103 |
|
| 104 |
var url = jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 105 |
|
| 106 |
if (url.substr(-1) != '/') { |
| 107 |
url += '/'; |
| 108 |
} |
| 109 |
|
| 110 |
var name = jQuery('#mainwp_managesites_add_wpname').val().trim(); |
| 111 |
name = name.replace(/"/g, '"'); |
| 112 |
|
| 113 |
var data = mainwp_setup_secure_data({ |
| 114 |
action: 'mainwp_checkwp', |
| 115 |
name: name, |
| 116 |
url: url, |
| 117 |
admin: jQuery('#mainwp_managesites_add_wpadmin').val().trim(), |
| 118 |
}); |
| 119 |
|
| 120 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 121 |
response = res_things.response; |
| 122 |
response = response.trim(); |
| 123 |
|
| 124 |
var url = jQuery('#mainwp_managesites_add_wpurl_protocol').val() + '://' + jQuery('#mainwp_managesites_add_wpurl').val().trim(); |
| 125 |
if (url.substr(-1) != '/') { |
| 126 |
url += '/'; |
| 127 |
} |
| 128 |
|
| 129 |
url = url.replace(/"/g, '"'); |
| 130 |
|
| 131 |
if (response == 'HTTPERROR') { |
| 132 |
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://kb.mainwp.com/">MainWP Knowledgebase</a>, and if you still have issues, please let us know in the <a href="https://managers.mainwp.com/c/community-support/5">MainWP Community</a>.'); |
| 133 |
} else if (response == 'NOMAINWP') { |
| 134 |
errors.push(mainwp_js_get_error_not_detected_connect()); |
| 135 |
} else if (response.substr(0, 5) == 'ERROR') { |
| 136 |
if (response.length == 5) { |
| 137 |
errors.push('Undefined error occurred. Please try again. If the issue does not resolve, please review <a href="https://kb.mainwp.com/">MainWP Knowledgebase</a>, and if you still have issues, please let us know in the <a href="https://managers.mainwp.com/c/community-support/5">MainWP Community</a>.'); |
| 138 |
} else { |
| 139 |
var error = response.substr(6); |
| 140 |
var err = mainwp_js_get_error_not_detected_connect(error, 'html_msg', false, true); // return text error. |
| 141 |
if (false === err) { |
| 142 |
errors.push(error); // it is not json string error. |
| 143 |
} else if (true !== err && '' != err) { |
| 144 |
errors.push(err); // decoded error. |
| 145 |
} |
| 146 |
} |
| 147 |
} else if (response == 'OK') { |
| 148 |
jQuery('#mainwp_managesites_add').attr('disabled', 'true'); |
| 149 |
|
| 150 |
var name = jQuery('#mainwp_managesites_add_wpname').val(); |
| 151 |
name = name.replace(/"/g, '"'); |
| 152 |
var group_ids = ''; |
| 153 |
var data = mainwp_setup_secure_data({ |
| 154 |
action: 'mainwp_addwp', |
| 155 |
managesites_add_wpname: name, |
| 156 |
managesites_add_wpurl: url, |
| 157 |
managesites_add_wpadmin: jQuery('#mainwp_managesites_add_wpadmin').val(), |
| 158 |
managesites_add_uniqueId: jQuery('#mainwp_managesites_add_uniqueId').val(), |
| 159 |
groupids: group_ids, |
| 160 |
qsw_page: true, |
| 161 |
}); |
| 162 |
|
| 163 |
// to support add client reports tokens values |
| 164 |
jQuery("input[name^='creport_token_']").each(function () { |
| 165 |
var tname = jQuery(this).attr('name'); |
| 166 |
var tvalue = jQuery(this).val(); |
| 167 |
data[tname] = tvalue; |
| 168 |
}); |
| 169 |
|
| 170 |
// support hooks fields |
| 171 |
jQuery(".mainwp_addition_fields_addsite input").each(function () { |
| 172 |
var tname = jQuery(this).attr('name'); |
| 173 |
var tvalue = jQuery(this).val(); |
| 174 |
data[tname] = tvalue; |
| 175 |
}); |
| 176 |
|
| 177 |
jQuery.post(ajaxurl, data, function (res_things) { |
| 178 |
|
| 179 |
if (res_things.error) { |
| 180 |
response = res_things.error; |
| 181 |
} else { |
| 182 |
response = res_things.response; |
| 183 |
} |
| 184 |
|
| 185 |
response = response.trim(); |
| 186 |
|
| 187 |
jQuery('#mainwp-message-zone').hide(); |
| 188 |
jQuery('#mainwp-info-zone').hide(); |
| 189 |
|
| 190 |
if (response.substr(0, 5) == 'ERROR') { |
| 191 |
jQuery('#mainwp-message-zone').removeClass('green yellow green'); |
| 192 |
jQuery('#mainwp-message-zone').html(response.substr(6)).addClass('red').show(); |
| 193 |
} else { |
| 194 |
//Message the WP was added |
| 195 |
jQuery('#mainwp-message-zone').removeClass('green yellow green'); |
| 196 |
jQuery('#mainwp-message-zone').html(response).addClass('green').show(); |
| 197 |
|
| 198 |
//Reset fields |
| 199 |
jQuery('#mainwp_managesites_add_wpname').val(''); |
| 200 |
jQuery('#mainwp_managesites_add_wpurl').val(''); |
| 201 |
jQuery('#mainwp_managesites_add_wpurl_protocol').val('https'); |
| 202 |
jQuery('#mainwp_managesites_add_wpadmin').val(''); |
| 203 |
jQuery('#mainwp_managesites_add_uniqueId').val(''); |
| 204 |
|
| 205 |
jQuery("input[name^='creport_token_']").each(function () { |
| 206 |
jQuery(this).val(''); |
| 207 |
}); |
| 208 |
|
| 209 |
// support hooks fields |
| 210 |
jQuery(".mainwp_addition_fields_addsite input").each(function () { |
| 211 |
jQuery(this).val(''); |
| 212 |
}); |
| 213 |
|
| 214 |
setTimeout(function () { |
| 215 |
window.location.href = 'admin.php?page=mainwp-setup&step=connect_first_site'; |
| 216 |
}, 1000); |
| 217 |
} |
| 218 |
|
| 219 |
jQuery('#mainwp_managesites_add').prop("disabled", false); |
| 220 |
}, 'json'); |
| 221 |
} |
| 222 |
if (errors.length > 0) { |
| 223 |
jQuery('#mainwp-message-zone').removeClass('green yellow green'); |
| 224 |
jQuery('#mainwp-message-zone').hide(); |
| 225 |
jQuery('#mainwp_managesites_add').prop("disabled", false); |
| 226 |
jQuery('#mainwp-message-zone').html(errors.join('<br />')).addClass('red').show(); |
| 227 |
} |
| 228 |
}, 'json'); |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
// Check if the URL field is valid value |
| 233 |
function isUrl(s) { |
| 234 |
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/; |
| 235 |
return regexp.test(s); |
| 236 |
} |
| 237 |
|
| 238 |
mainwp_setup_secure_data = function (data) { |
| 239 |
if (data['action'] == undefined) |
| 240 |
return data; |
| 241 |
|
| 242 |
data['security'] = jQuery('#nonce_secure_data').attr(data['action']); |
| 243 |
|
| 244 |
return data; |
| 245 |
}; |
| 246 |
|