| 1 |
/** |
| 2 |
* Client for the abj404_support_request AJAX endpoint. |
| 3 |
* |
| 4 |
* Exposes window.abj404SupportRequest.send({triggered_from, user_message, |
| 5 |
* reply_email}) for the reusable button UI added in task B. Returns a |
| 6 |
* Promise that resolves to {ok, reference_id, fallback_used} on success |
| 7 |
* or rejects with {status, message, retry_after_seconds?} on error. |
| 8 |
* |
| 9 |
* The nonce is read from a global ABJ404 namespace populated server-side: |
| 10 |
* ABJ404.nonces.support_request = '<wp_create_nonce(...)>'; |
| 11 |
* Callers do NOT have to pass the nonce; it is injected automatically. |
| 12 |
*/ |
| 13 |
|
| 14 |
(function (window) { |
| 15 |
'use strict'; |
| 16 |
|
| 17 |
function resolveNonce() { |
| 18 |
if (window.ABJ404 && window.ABJ404.nonces && window.ABJ404.nonces.support_request) { |
| 19 |
return String(window.ABJ404.nonces.support_request); |
| 20 |
} |
| 21 |
return ''; |
| 22 |
} |
| 23 |
|
| 24 |
function resolveAjaxUrl() { |
| 25 |
if (typeof window.ajaxurl === 'string' && window.ajaxurl) { |
| 26 |
return window.ajaxurl; |
| 27 |
} |
| 28 |
if (window.ABJ404 && window.ABJ404.ajaxurl) { |
| 29 |
return String(window.ABJ404.ajaxurl); |
| 30 |
} |
| 31 |
return '/wp-admin/admin-ajax.php'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Send a support request. Returns a Promise. |
| 36 |
* |
| 37 |
* @param {Object} args |
| 38 |
* @param {string} args.triggered_from Required. Must match the server |
| 39 |
* allowlist (redirects_page, captured_404s_page, plugins_row_action, |
| 40 |
* settings_debug, system_corrupt_install). |
| 41 |
* @param {string} [args.user_message] Optional, max 2000 chars. |
| 42 |
* @param {string} [args.reply_email] Optional. |
| 43 |
* @return {Promise<Object>} |
| 44 |
*/ |
| 45 |
function send(args) { |
| 46 |
args = args || {}; |
| 47 |
var formData = new FormData(); |
| 48 |
formData.append('action', 'abj404_support_request'); |
| 49 |
formData.append('nonce', resolveNonce()); |
| 50 |
formData.append('triggered_from', String(args.triggered_from || '')); |
| 51 |
if (typeof args.user_message === 'string') { |
| 52 |
formData.append('user_message', args.user_message); |
| 53 |
} |
| 54 |
if (typeof args.reply_email === 'string') { |
| 55 |
formData.append('reply_email', args.reply_email); |
| 56 |
} |
| 57 |
|
| 58 |
return fetch(resolveAjaxUrl(), { |
| 59 |
method: 'POST', |
| 60 |
credentials: 'same-origin', |
| 61 |
body: formData |
| 62 |
}).then(function (response) { |
| 63 |
return response.json().then(function (json) { |
| 64 |
return { status: response.status, body: json }; |
| 65 |
}).catch(function () { |
| 66 |
return { status: response.status, body: null }; |
| 67 |
}); |
| 68 |
}).then(function (wrapped) { |
| 69 |
var body = wrapped.body || {}; |
| 70 |
var data = body.data || {}; |
| 71 |
if (body.success === true) { |
| 72 |
return data; |
| 73 |
} |
| 74 |
var err = { |
| 75 |
status: wrapped.status, |
| 76 |
message: data.message || 'Support request failed.' |
| 77 |
}; |
| 78 |
if (typeof data.retry_after_seconds === 'number') { |
| 79 |
err.retry_after_seconds = data.retry_after_seconds; |
| 80 |
} |
| 81 |
if (typeof data.fallback_used === 'boolean') { |
| 82 |
err.fallback_used = data.fallback_used; |
| 83 |
} |
| 84 |
throw err; |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
window.abj404SupportRequest = { send: send }; |
| 89 |
|
| 90 |
})(window); |
| 91 |
|