| 1 |
/** |
| 2 |
* Yatra reCAPTCHA v3 helper. |
| 3 |
* |
| 4 |
* Exposes window.yatraRecaptcha.execute(action) -> Promise<token> and |
| 5 |
* window.yatraRecaptcha.protects(form) -> bool. Form submit handlers call |
| 6 |
* execute() right before sending, then include the token as `recaptcha_token`. |
| 7 |
* |
| 8 |
* The config object (siteKey, enabled, forms) is injected by |
| 9 |
* wp_localize_script before this file runs; we augment it with methods. |
| 10 |
* |
| 11 |
* @package Yatra |
| 12 |
*/ |
| 13 |
(function () { |
| 14 |
var cfg = (window.yatraRecaptcha = window.yatraRecaptcha || {}); |
| 15 |
|
| 16 |
/** |
| 17 |
* Get a fresh v3 token for the given action. Resolves to '' (never rejects) |
| 18 |
* if reCAPTCHA is unavailable, so a script/network failure can be handled by |
| 19 |
* the caller / server rather than throwing in the submit path. |
| 20 |
* |
| 21 |
* @param {string} action |
| 22 |
* @returns {Promise<string>} |
| 23 |
*/ |
| 24 |
cfg.execute = function (action) { |
| 25 |
return new Promise(function (resolve) { |
| 26 |
try { |
| 27 |
if (!cfg.enabled || !cfg.siteKey || typeof grecaptcha === 'undefined') { |
| 28 |
resolve(''); |
| 29 |
return; |
| 30 |
} |
| 31 |
grecaptcha.ready(function () { |
| 32 |
grecaptcha |
| 33 |
.execute(cfg.siteKey, { action: action || 'submit' }) |
| 34 |
.then(function (token) { |
| 35 |
resolve(token || ''); |
| 36 |
}) |
| 37 |
.catch(function () { |
| 38 |
resolve(''); |
| 39 |
}); |
| 40 |
}); |
| 41 |
} catch (e) { |
| 42 |
resolve(''); |
| 43 |
} |
| 44 |
}); |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* Whether a given form is configured to be protected. |
| 49 |
* @param {string} form |
| 50 |
* @returns {boolean} |
| 51 |
*/ |
| 52 |
cfg.protects = function (form) { |
| 53 |
return !!(cfg.enabled && cfg.forms && cfg.forms[form]); |
| 54 |
}; |
| 55 |
})(); |
| 56 |
|