| 1 |
/** |
| 2 |
* Setup wizard - step navigation and actions. |
| 3 |
* Follows the same AJAX fragment-loading pattern used throughout wp-admin.js. |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Load a wizard step's body via AJAX and reflect it in the progress nav. |
| 8 |
*/ |
| 9 |
function wpsc_onboarding_load_step(stepId) { |
| 10 |
jQuery(".wpsc-onboarding-step-nav").removeClass("active"); |
| 11 |
jQuery(".wpsc-onboarding-step-nav." + stepId).addClass("active"); |
| 12 |
|
| 13 |
window.history.replaceState( |
| 14 |
{}, |
| 15 |
null, |
| 16 |
"admin.php?page=wpsc-onboarding&step=" + stepId |
| 17 |
); |
| 18 |
|
| 19 |
jQuery(".wpsc-onboarding-body").html(supportcandy.loader_html); |
| 20 |
wpsc_scroll_top(); |
| 21 |
|
| 22 |
var data = { action: "wpsc_get_onboarding_step", step: stepId }; |
| 23 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 24 |
jQuery(".wpsc-onboarding-body").html(response); |
| 25 |
wpsc_reset_responsive_style(); |
| 26 |
}); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Move to the next step in order, or leave the wizard if this was the last one. |
| 31 |
*/ |
| 32 |
function wpsc_onboarding_next_step(currentStepId) { |
| 33 |
var steps = supportcandy.onboarding_steps || []; |
| 34 |
var idx = steps.indexOf(currentStepId); |
| 35 |
if (idx > -1 && idx < steps.length - 1) { |
| 36 |
wpsc_onboarding_load_step(steps[idx + 1]); |
| 37 |
} else { |
| 38 |
window.location.href = "admin.php?page=wpsc-tickets"; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Reflect a step's completed state in the progress nav without a reload. |
| 44 |
*/ |
| 45 |
function wpsc_onboarding_mark_step(stepId, status) { |
| 46 |
jQuery(".wpsc-onboarding-step-nav." + stepId) |
| 47 |
.removeClass("pending completed") |
| 48 |
.addClass(status); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Mark a step complete and move on. Setup must be completed, so a step can |
| 53 |
* only be marked done, never skipped - the server enforces the same rule |
| 54 |
* (e.g. the Support Page requirement on step 1) regardless of what the UI did. |
| 55 |
* |
| 56 |
* The progress nav (see wpsc_onboarding_load_step) lets an admin jump straight |
| 57 |
* to any step, so this can be rejected because an earlier step was never |
| 58 |
* completed - the server then sends back which step to go complete first |
| 59 |
* (redirect_step). Not every step has a ".wpsc-onboarding-inline-message" in |
| 60 |
* its markup to show that in, so this is surfaced with an alert instead and |
| 61 |
* the admin is sent back to that step. |
| 62 |
*/ |
| 63 |
function wpsc_onboarding_complete_step(stepId, nonce) { |
| 64 |
var data = { action: "wpsc_onboarding_complete_step", step: stepId, _ajax_nonce: nonce }; |
| 65 |
jQuery |
| 66 |
.post(supportcandy.ajax_url, data, function () { |
| 67 |
wpsc_onboarding_mark_step(stepId, "completed"); |
| 68 |
wpsc_onboarding_next_step(stepId); |
| 69 |
}) |
| 70 |
.fail(function (xhr) { |
| 71 |
var msg = supportcandy.translations.something_wrong; |
| 72 |
var redirectStep = null; |
| 73 |
try { |
| 74 |
var res = JSON.parse(xhr.responseText); |
| 75 |
if (res && res.data) { |
| 76 |
msg = typeof res.data === "string" ? res.data : res.data.message || msg; |
| 77 |
redirectStep = res.data && res.data.redirect_step ? res.data.redirect_step : null; |
| 78 |
} |
| 79 |
} catch (e) { |
| 80 |
// ignore - fall back to generic message. |
| 81 |
} |
| 82 |
if (redirectStep) { |
| 83 |
alert(msg); |
| 84 |
wpsc_onboarding_load_step(redirectStep); |
| 85 |
} else { |
| 86 |
jQuery(".wpsc-onboarding-inline-message").text(msg).addClass("error"); |
| 87 |
} |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Step 1: create the Support/Open Ticket page in one click, then reload the step. |
| 93 |
*/ |
| 94 |
function wpsc_onboarding_create_page(type, el, nonce) { |
| 95 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 96 |
var data = { action: "wpsc_onboarding_create_page", type: type, _ajax_nonce: nonce }; |
| 97 |
jQuery.post(supportcandy.ajax_url, data, function () { |
| 98 |
wpsc_onboarding_load_step("support-pages"); |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Step 1: the Support Page is required - block Continue and show a notice |
| 104 |
* until it's been created, then save guest-ticket / OTP settings and mark |
| 105 |
* the step complete. |
| 106 |
*/ |
| 107 |
function wpsc_onboarding_save_pages_step(el, completeNonce) { |
| 108 |
var configured = jQuery(".wpsc-onboarding-support-page-configured").val() === "1"; |
| 109 |
if (!configured) { |
| 110 |
jQuery(".wpsc-onboarding-support-page-notice").show(); |
| 111 |
return; |
| 112 |
} |
| 113 |
jQuery(".wpsc-onboarding-support-page-notice").hide(); |
| 114 |
|
| 115 |
var form = jQuery(".wpsc-frm-onboarding-ticket-access")[0]; |
| 116 |
var dataform = new FormData(form); |
| 117 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 118 |
jQuery |
| 119 |
.ajax({ |
| 120 |
url: supportcandy.ajax_url, |
| 121 |
type: "POST", |
| 122 |
data: dataform, |
| 123 |
processData: false, |
| 124 |
contentType: false, |
| 125 |
}) |
| 126 |
.done(function () { |
| 127 |
wpsc_onboarding_complete_step("support-pages", completeNonce); |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Step 2: save the AI provider/API key via the existing save endpoint, then |
| 133 |
* reload the step to reflect the new connected/error state. |
| 134 |
*/ |
| 135 |
function wpsc_onboarding_save_ai(el) { |
| 136 |
var $btn = jQuery(el); |
| 137 |
var originalText = $btn.text(); |
| 138 |
var form = jQuery(".wpsc-frm-onboarding-ai")[0]; |
| 139 |
var dataform = new FormData(form); |
| 140 |
|
| 141 |
jQuery(".wpsc-onboarding-inline-message").text("").removeClass("error"); |
| 142 |
$btn.prop("disabled", true).text(supportcandy.translations.please_wait); |
| 143 |
|
| 144 |
jQuery |
| 145 |
.ajax({ |
| 146 |
url: supportcandy.ajax_url, |
| 147 |
type: "POST", |
| 148 |
data: dataform, |
| 149 |
processData: false, |
| 150 |
contentType: false, |
| 151 |
}) |
| 152 |
.done(function () { |
| 153 |
wpsc_onboarding_load_step("ai-assistant"); |
| 154 |
}) |
| 155 |
.fail(function (xhr) { |
| 156 |
var msg = supportcandy.translations.something_wrong; |
| 157 |
try { |
| 158 |
var res = JSON.parse(xhr.responseText); |
| 159 |
if (res && res.data) { |
| 160 |
msg = typeof res.data === "string" ? res.data : res.data.message || msg; |
| 161 |
} |
| 162 |
} catch (e) { |
| 163 |
// ignore - fall back to generic message. |
| 164 |
} |
| 165 |
jQuery(".wpsc-onboarding-inline-message").text(msg).addClass("error"); |
| 166 |
$btn.prop("disabled", false).text(originalText); |
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Step 3: add a category via the existing save endpoint, then reload the step. |
| 172 |
*/ |
| 173 |
function wpsc_onboarding_add_category(el, nonce) { |
| 174 |
var input = jQuery(".wpsc-frm-onboarding-add-category input[name='label']"); |
| 175 |
var label = input.val().trim(); |
| 176 |
if (!label) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
jQuery(el).prop("disabled", true).text(supportcandy.translations.please_wait); |
| 181 |
var data = { action: "wpsc_set_add_category", label: label, _ajax_nonce: nonce }; |
| 182 |
jQuery.post(supportcandy.ajax_url, data, function () { |
| 183 |
wpsc_onboarding_load_step("categories"); |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Step 7: save From Name / From Email onto the existing email-notifications |
| 189 |
* general settings (merged there server-side, not overwritten - including its |
| 190 |
* From Name / From Email required + valid-email validation), then mark the |
| 191 |
* step complete. |
| 192 |
*/ |
| 193 |
function wpsc_onboarding_save_email_step(el, completeNonce) { |
| 194 |
var $btn = jQuery(el); |
| 195 |
var originalText = $btn.text(); |
| 196 |
var form = jQuery(".wpsc-frm-onboarding-email-general")[0]; |
| 197 |
|
| 198 |
// The fields are marked required/type="email" in markup - reportValidity() |
| 199 |
// surfaces the browser's native bubble for those before we even hit the |
| 200 |
// server, which still re-checks everything itself regardless. |
| 201 |
if (!form.reportValidity()) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
var dataform = new FormData(form); |
| 206 |
|
| 207 |
jQuery(".wpsc-onboarding-inline-message").text("").removeClass("error"); |
| 208 |
$btn.prop("disabled", true).text(supportcandy.translations.please_wait); |
| 209 |
|
| 210 |
jQuery |
| 211 |
.ajax({ |
| 212 |
url: supportcandy.ajax_url, |
| 213 |
type: "POST", |
| 214 |
data: dataform, |
| 215 |
processData: false, |
| 216 |
contentType: false, |
| 217 |
}) |
| 218 |
.done(function () { |
| 219 |
wpsc_onboarding_complete_step("emails", completeNonce); |
| 220 |
}) |
| 221 |
.fail(function (xhr) { |
| 222 |
var msg = supportcandy.translations.something_wrong; |
| 223 |
try { |
| 224 |
var res = JSON.parse(xhr.responseText); |
| 225 |
if (res && res.data) { |
| 226 |
msg = typeof res.data === "string" ? res.data : res.data.message || msg; |
| 227 |
} |
| 228 |
} catch (e) { |
| 229 |
// ignore - fall back to generic message. |
| 230 |
} |
| 231 |
jQuery(".wpsc-onboarding-inline-message").text(msg).addClass("error"); |
| 232 |
$btn.prop("disabled", false).text(originalText); |
| 233 |
}); |
| 234 |
} |
| 235 |
|