PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Shared / api / pluginsActivation.js

pluginsActivation.js in Extendify 3.1.0, at src/Shared/api/pluginsActivation.js

78 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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