| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
const getRecaptchaToken = (action, siteKey) => |
| 4 |
new Promise((resolve, reject) => { |
| 5 |
const existing = document.querySelector( |
| 6 |
`script[src*="recaptcha/enterprise"]`, |
| 7 |
); |
| 8 |
const load = () => |
| 9 |
window.grecaptcha.enterprise.ready(async () => { |
| 10 |
try { |
| 11 |
resolve( |
| 12 |
await window.grecaptcha.enterprise.execute(siteKey, { action }), |
| 13 |
); |
| 14 |
} catch (error) { |
| 15 |
reject(error); |
| 16 |
} |
| 17 |
}); |
| 18 |
|
| 19 |
if (existing) { |
| 20 |
load(); |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
const script = document.createElement('script'); |
| 25 |
script.src = `https://www.google.com/recaptcha/enterprise.js?render=${siteKey}`; |
| 26 |
script.async = true; |
| 27 |
script.onload = load; |
| 28 |
document.head.appendChild(script); |
| 29 |
}); |
| 30 |
|
| 31 |
const createAccount = async ({ |
| 32 |
slug, |
| 33 |
email, |
| 34 |
marketingConsent, |
| 35 |
termsAgreed, |
| 36 |
signal, |
| 37 |
scriptData, |
| 38 |
}) => { |
| 39 |
await apiFetch({ |
| 40 |
path: `extendify/v1/${slug}/create-account`, |
| 41 |
method: 'POST', |
| 42 |
data: { |
| 43 |
email, |
| 44 |
marketingConsent, |
| 45 |
termsAgreed, |
| 46 |
...scriptData, |
| 47 |
}, |
| 48 |
signal, |
| 49 |
}); |
| 50 |
}; |
| 51 |
|
| 52 |
/* |
| 53 |
* Plugin entries shape: |
| 54 |
* createAccountCallback: (data) => Promise<void> — performs the account creation request |
| 55 |
* idempotent: boolean (default true) — false skips retries; use when re-sending the same request could cause errors |
| 56 |
*/ |
| 57 |
export const pluginsActivation = { |
| 58 |
simplybook: { |
| 59 |
idempotent: false, |
| 60 |
createAccountCallback: async ({ scriptData, ...data }) => { |
| 61 |
const captchaToken = await getRecaptchaToken( |
| 62 |
scriptData?.recaptchaAction, |
| 63 |
scriptData?.recaptchaSiteKey, |
| 64 |
); |
| 65 |
|
| 66 |
await createAccount({ |
| 67 |
slug: 'simplybook', |
| 68 |
...data, |
| 69 |
scriptData: { captcha_token: captchaToken }, |
| 70 |
}); |
| 71 |
}, |
| 72 |
}, |
| 73 |
'translatepress-multilingual': { |
| 74 |
createAccountCallback: (data) => |
| 75 |
createAccount({ slug: 'translatepress-multilingual', ...data }), |
| 76 |
}, |
| 77 |
}; |
| 78 |
|