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