| 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 |
if (!siteKey) { |
| 7 |
reject(new Error(`No reCAPTCHA site key for the ${action} action`)); |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
const existing = document.querySelector( |
| 12 |
`script[src*="recaptcha/enterprise"]`, |
| 13 |
); |
| 14 |
const load = () => |
| 15 |
window.grecaptcha.enterprise.ready(async () => { |
| 16 |
try { |
| 17 |
resolve( |
| 18 |
await window.grecaptcha.enterprise.execute(siteKey, { action }), |
| 19 |
); |
| 20 |
} catch (error) { |
| 21 |
reject(error); |
| 22 |
} |
| 23 |
}); |
| 24 |
|
| 25 |
if (existing) { |
| 26 |
load(); |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
const script = document.createElement('script'); |
| 31 |
script.src = `https://www.google.com/recaptcha/enterprise.js?render=${siteKey}`; |
| 32 |
script.async = true; |
| 33 |
script.onload = load; |
| 34 |
document.head.appendChild(script); |
| 35 |
}); |
| 36 |
|
| 37 |
const createAccount = async ({ |
| 38 |
slug, |
| 39 |
email, |
| 40 |
marketingConsent, |
| 41 |
termsAgreed, |
| 42 |
signal, |
| 43 |
scriptData, |
| 44 |
}) => { |
| 45 |
await apiFetch({ |
| 46 |
path: `extendify/v1/${slug}/create-account`, |
| 47 |
method: 'POST', |
| 48 |
data: { |
| 49 |
email, |
| 50 |
marketingConsent, |
| 51 |
termsAgreed, |
| 52 |
...scriptData, |
| 53 |
}, |
| 54 |
signal, |
| 55 |
}); |
| 56 |
}; |
| 57 |
|
| 58 |
/* |
| 59 |
* Plugin entries shape: |
| 60 |
* createAccountCallback: (data) => Promise<void> — performs the account creation request |
| 61 |
* idempotent: boolean (default true) — false skips retries; use when re-sending the same request could cause errors |
| 62 |
*/ |
| 63 |
export const pluginsActivation = { |
| 64 |
simplybook: { |
| 65 |
idempotent: false, |
| 66 |
createAccountCallback: async ({ |
| 67 |
scriptData, |
| 68 |
email, |
| 69 |
marketingConsent, |
| 70 |
termsAgreed, |
| 71 |
signal, |
| 72 |
}) => { |
| 73 |
const captchaToken = await getRecaptchaToken( |
| 74 |
scriptData?.recaptchaAction, |
| 75 |
scriptData?.recaptchaSiteKey, |
| 76 |
); |
| 77 |
|
| 78 |
// Hit the endpoint via ?rest_route= so the request URL contains "simplybook" — |
| 79 |
// SimplyBook only registers its onboarding routes when it does, else they 404. |
| 80 |
const url = addQueryArgs(`${window.extSharedData.homeUrl}/`, { |
| 81 |
rest_route: '/extendify/v1/simplybook/create-account', |
| 82 |
}); |
| 83 |
|
| 84 |
await apiFetch({ |
| 85 |
url, |
| 86 |
method: 'POST', |
| 87 |
data: { |
| 88 |
email, |
| 89 |
marketingConsent, |
| 90 |
termsAgreed, |
| 91 |
captcha_token: captchaToken, |
| 92 |
}, |
| 93 |
signal, |
| 94 |
}); |
| 95 |
}, |
| 96 |
}, |
| 97 |
'translatepress-multilingual': { |
| 98 |
createAccountCallback: (data) => |
| 99 |
createAccount({ slug: 'translatepress-multilingual', ...data }), |
| 100 |
}, |
| 101 |
imagify: { |
| 102 |
createAccountCallback: (data) => |
| 103 |
createAccount({ slug: 'imagify', ...data }), |
| 104 |
}, |
| 105 |
metricool: { |
| 106 |
idempotent: false, |
| 107 |
createAccountCallback: async ({ |
| 108 |
scriptData, |
| 109 |
email, |
| 110 |
marketingConsent, |
| 111 |
termsAgreed, |
| 112 |
signal, |
| 113 |
}) => { |
| 114 |
const captchaToken = await getRecaptchaToken( |
| 115 |
scriptData?.recaptchaAction, |
| 116 |
scriptData?.recaptchaSiteKey, |
| 117 |
); |
| 118 |
|
| 119 |
// The "/v1" segment is what makes Metricool register its logout route. |
| 120 |
await apiFetch({ |
| 121 |
path: 'extendify/v1/metricool/v1/create-account', |
| 122 |
method: 'POST', |
| 123 |
data: { |
| 124 |
email, |
| 125 |
marketingConsent, |
| 126 |
termsAgreed, |
| 127 |
captcha_token: captchaToken, |
| 128 |
}, |
| 129 |
signal, |
| 130 |
}); |
| 131 |
}, |
| 132 |
}, |
| 133 |
}; |
| 134 |
|