| @@ -6,62 +6,8 @@ | ||
| 6 | 6 | * @author ConvertKit |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Gets the subscriber ID for the given email address, storing | |
| 11 | - * it in the `ck_subscriber_id` cookie if it exists. | |
| 12 | - * | |
| 13 | - * Typically called when the user completes a ConvertKit Form | |
| 14 | - * that has either "Auto-confirm new subscribers" or | |
| 15 | - * "Send subscriber to thank you page" enabled (both scenarios | |
| 16 | - * include a ck_subscriber_id). | |
| 17 | - * | |
| 18 | - * @since 1.9.6 | |
| 19 | - * | |
| 20 | - * @param {string} emailAddress Email Address | |
| 21 | - */ | |
| 22 | -function convertStoreSubscriberEmailAsIDInCookie(emailAddress) { | |
| 23 | - if (convertkit.debug) { | |
| 24 | - console.log('convertStoreSubscriberEmailAsIDInCookie'); | |
| 25 | - console.log(emailAddress); | |
| 26 | - } | |
| 27 | - | |
| 28 | - fetch(convertkit.ajaxurl, { | |
| 29 | - method: 'POST', | |
| 30 | - headers: { | |
| 31 | - 'Content-Type': 'application/x-www-form-urlencoded', | |
| 32 | - 'X-WP-Nonce': convertkit.nonce, | |
| 33 | - }, | |
| 34 | - body: new URLSearchParams({ | |
| 35 | - email: emailAddress, | |
| 36 | - }), | |
| 37 | - }) | |
| 38 | - .then(function (response) { | |
| 39 | - if (convertkit.debug) { | |
| 40 | - console.log(response); | |
| 41 | - } | |
| 42 | - | |
| 43 | - return response.json(); | |
| 44 | - }) | |
| 45 | - .then(function (result) { | |
| 46 | - if (convertkit.debug) { | |
| 47 | - console.log(result); | |
| 48 | - } | |
| 49 | - | |
| 50 | - // Emit custom event with subscriber ID. | |
| 51 | - convertKitEmitCustomEvent('convertkit_user_subscribed', { | |
| 52 | - id: result.id, | |
| 53 | - email: emailAddress, | |
| 54 | - }); | |
| 55 | - }) | |
| 56 | - .catch(function (error) { | |
| 57 | - if (convertkit.debug) { | |
| 58 | - console.error(error); | |
| 59 | - } | |
| 60 | - }); | |
| 61 | -} | |
| 62 | - | |
| 63 | -/** | |
| 64 | 10 | * Remove the url subscriber_id url param |
| 65 | 11 | * |
| 66 | 12 | * The 'ck_subscriber_id' should only be set on URLs included on |
| 67 | 13 | * links from a ConvertKit email with no other URL parameters. |
| @@ -106,23 +52,8 @@ | ||
| 106 | 52 | }); |
| 107 | 53 | } |
| 108 | 54 | |
| 109 | 55 | /** |
| 110 | - * Utility function to pause for the given number of milliseconds | |
| 111 | - * | |
| 112 | - * @since 1.9.6 | |
| 113 | - * @param {number} milliseconds Number of milliseconds to pause for. | |
| 114 | - */ | |
| 115 | -function convertKitSleep(milliseconds) { | |
| 116 | - const start = new Date().getTime(); | |
| 117 | - for (let i = 0; i < 1e7; i++) { | |
| 118 | - if (new Date().getTime() - start > milliseconds) { | |
| 119 | - break; | |
| 120 | - } | |
| 121 | - } | |
| 122 | -} | |
| 123 | - | |
| 124 | -/** | |
| 125 | 56 | * Emit a custom event with optional detail data. |
| 126 | 57 | * |
| 127 | 58 | * This function creates and dispatches a custom event with the specified |
| 128 | 59 | * event name and detail data. |
| @@ -151,15 +82,51 @@ | ||
| 151 | 82 | |
| 152 | 83 | // Get the parent form of the submit button. |
| 153 | 84 | const form = submitButton.closest('form'); |
| 154 | 85 | |
| 155 | - // Submit the form. | |
| 156 | - form.submit(); | |
| 86 | + // Submit the form, using requestSubmit() so any submit event listeners are honored | |
| 87 | + // e.g. the Member Content login form, which submits using AJAX. | |
| 88 | + form.requestSubmit(); | |
| 157 | 89 | } |
| 158 | 90 | |
| 159 | 91 | // Scope the function to the window object as webpack will wrap everything in a closure, |
| 160 | 92 | // resulting in the function not being available globally. |
| 161 | 93 | window.convertKitRecaptchaFormSubmit = convertKitRecaptchaFormSubmit; |
| 94 | + | |
| 95 | +/** | |
| 96 | + * Handles form submissions when Cloudflare Turnstile is enabled. | |
| 97 | + * | |
| 98 | + * Turnstile auto-populates a hidden `cf-turnstile-response` input inside the | |
| 99 | + * enclosing form once the challenge is solved, so we just need to submit the | |
| 100 | + * containing form when the callback fires. | |
| 101 | + * | |
| 102 | + * @param {string} token Turnstile response token. | |
| 103 | + */ | |
| 104 | +function convertKitTurnstileFormSubmit(token) { | |
| 105 | + // Find the Turnstile widget div with the data-callback attribute. | |
| 106 | + const widget = document.querySelector( | |
| 107 | + '.cf-turnstile[data-callback="convertKitTurnstileFormSubmit"]' | |
| 108 | + ); | |
| 109 | + | |
| 110 | + if (!widget) { | |
| 111 | + return; | |
| 112 | + } | |
| 113 | + | |
| 114 | + // Get the parent form of the widget. | |
| 115 | + const form = widget.closest('form'); | |
| 116 | + | |
| 117 | + if (!form) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + | |
| 121 | + // Submit the form, using requestSubmit() so any submit event listeners are honored | |
| 122 | + // e.g. the Member Content login form, which submits using AJAX. | |
| 123 | + form.requestSubmit(); | |
| 124 | +} | |
| 125 | + | |
| 126 | +// Scope the function to the window object as webpack will wrap everything in a closure, | |
| 127 | +// resulting in the function not being available globally. | |
| 128 | +window.convertKitTurnstileFormSubmit = convertKitTurnstileFormSubmit; | |
| 162 | 129 | /* eslint-enable no-unused-vars */ |
| 163 | 130 | |
| 164 | 131 | /** |
| 165 | 132 | * Register events on frontend. |
| @@ -169,53 +136,8 @@ | ||
| 169 | 136 | if (typeof convertkit !== 'undefined') { |
| 170 | 137 | document.addEventListener('DOMContentLoaded', function () { |
| 171 | 138 | // Removes `ck_subscriber_id` from the URI. |
| 172 | 139 | convertKitRemoveSubscriberIDFromURL(window.location.href); |
| 173 | - | |
| 174 | - // Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted. | |
| 175 | - document.addEventListener('click', function (e) { | |
| 176 | - // Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button. | |
| 177 | - if ( | |
| 178 | - !e.target.matches('.formkit-submit') && | |
| 179 | - (!e.target.parentElement || | |
| 180 | - !e.target.parentElement.matches('.formkit-submit')) | |
| 181 | - ) { | |
| 182 | - if (convertkit.debug) { | |
| 183 | - console.log('not a ck form'); | |
| 184 | - } | |
| 185 | - | |
| 186 | - return; | |
| 187 | - } | |
| 188 | - | |
| 189 | - // Get email address. | |
| 190 | - const emailAddress = document.querySelector( | |
| 191 | - 'input[name="email_address"]' | |
| 192 | - ).value; | |
| 193 | - | |
| 194 | - // If the email address is empty, don't attempt to get the subscriber ID by email. | |
| 195 | - if (!emailAddress.length) { | |
| 196 | - if (convertkit.debug) { | |
| 197 | - console.log('email empty'); | |
| 198 | - } | |
| 199 | - | |
| 200 | - return; | |
| 201 | - } | |
| 202 | - | |
| 203 | - // If the email address is invalid, don't attempt to get the subscriber ID by email. | |
| 204 | - const validator = | |
| 205 | - /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
| 206 | - if (!validator.test(emailAddress.toLowerCase())) { | |
| 207 | - if (convertkit.debug) { | |
| 208 | - console.log('email not an email address'); | |
| 209 | - } | |
| 210 | - | |
| 211 | - return; | |
| 212 | - } | |
| 213 | - | |
| 214 | - // Wait a moment before sending the AJAX request. | |
| 215 | - convertKitSleep(2000); | |
| 216 | - convertStoreSubscriberEmailAsIDInCookie(emailAddress); | |
| 217 | - }); | |
| 218 | 140 | |
| 219 | 141 | // Set a cookie if any scripts with data-kit-limit-per-session attribute exist. |
| 220 | 142 | if ( |
| 221 | 143 | document.querySelectorAll('script[data-kit-limit-per-session="1"]') |