| 1 |
/** |
| 2 |
* Frontend functionality for subscribers and tags. |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Tags the given subscriber ID with the given tag |
| 12 |
* |
| 13 |
* @since 1.9.6 |
| 14 |
* |
| 15 |
* @param int subscriber_id Subscriber ID |
| 16 |
* @param string tag Tag |
| 17 |
* @param int post_id WordPress Post ID |
| 18 |
*/ |
| 19 |
function convertKitTagSubscriber( subscriber_id, tag, post_id ) { |
| 20 |
|
| 21 |
if ( convertkit.debug ) { |
| 22 |
console.log( 'convertKitTagSubscriber' ); |
| 23 |
console.log( convertkit ); |
| 24 |
console.log( subscriber_id ); |
| 25 |
console.log( tag ); |
| 26 |
console.log( post_id ); |
| 27 |
} |
| 28 |
|
| 29 |
( function( $ ) { |
| 30 |
|
| 31 |
$.ajax( |
| 32 |
{ |
| 33 |
type: 'POST', |
| 34 |
data: { |
| 35 |
action: 'convertkit_tag_subscriber', |
| 36 |
convertkit_nonce: convertkit.nonce, |
| 37 |
subscriber_id: subscriber_id, |
| 38 |
tag: tag, |
| 39 |
post_id: post_id |
| 40 |
}, |
| 41 |
url: convertkit.ajaxurl, |
| 42 |
success: function ( response ) { |
| 43 |
if ( convertkit.debug ) { |
| 44 |
console.log( response ); |
| 45 |
} |
| 46 |
convertKitRemoveSubscriberIDFromURL( window.location.href ); |
| 47 |
} |
| 48 |
} |
| 49 |
).fail( |
| 50 |
function (response) { |
| 51 |
if ( convertkit.debug ) { |
| 52 |
console.log( response ); |
| 53 |
} |
| 54 |
convertKitRemoveSubscriberIDFromURL( window.location.href ); |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
} )( jQuery ); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gets the subscriber ID for the given email address, storing |
| 64 |
* it in the `ck_subscriber_id` cookie if it exists. |
| 65 |
* |
| 66 |
* Typically called when the user completes a ConvertKit Form |
| 67 |
* that has either "Auto-confirm new subscribers" or |
| 68 |
* "Send subscriber to thank you page" enabled (both scenarios |
| 69 |
* include a ck_subscriber_id). |
| 70 |
* |
| 71 |
* @since 1.9.6 |
| 72 |
* |
| 73 |
* @param int id Subscriber ID |
| 74 |
*/ |
| 75 |
function convertStoreSubscriberIDInCookie( subscriber_id ) { |
| 76 |
|
| 77 |
if ( convertkit.debug ) { |
| 78 |
console.log( 'convertStoreSubscriberIDInCookie' ); |
| 79 |
console.log( subscriber_id ); |
| 80 |
} |
| 81 |
|
| 82 |
( function( $ ) { |
| 83 |
|
| 84 |
$.ajax( |
| 85 |
{ |
| 86 |
type: 'POST', |
| 87 |
data: { |
| 88 |
action: 'convertkit_store_subscriber_id_in_cookie', |
| 89 |
convertkit_nonce: convertkit.nonce, |
| 90 |
subscriber_id: subscriber_id |
| 91 |
}, |
| 92 |
url: convertkit.ajaxurl, |
| 93 |
success: function ( response ) { |
| 94 |
if ( convertkit.debug ) { |
| 95 |
console.log( response ); |
| 96 |
} |
| 97 |
|
| 98 |
convertKitRemoveSubscriberIDFromURL( window.location.href ); |
| 99 |
} |
| 100 |
} |
| 101 |
).fail( |
| 102 |
function (response) { |
| 103 |
if ( convertkit.debug ) { |
| 104 |
console.log( response ); |
| 105 |
} |
| 106 |
|
| 107 |
convertKitRemoveSubscriberIDFromURL( window.location.href ); |
| 108 |
} |
| 109 |
); |
| 110 |
|
| 111 |
} )( jQuery ); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Gets the subscriber ID for the given email address, storing |
| 117 |
* it in the `ck_subscriber_id` cookie if it exists. |
| 118 |
* |
| 119 |
* Typically called when the user completes a ConvertKit Form |
| 120 |
* that has either "Auto-confirm new subscribers" or |
| 121 |
* "Send subscriber to thank you page" enabled (both scenarios |
| 122 |
* include a ck_subscriber_id). |
| 123 |
* |
| 124 |
* @since 1.9.6 |
| 125 |
* |
| 126 |
* @param string emailAddress Email Address |
| 127 |
*/ |
| 128 |
function convertStoreSubscriberEmailAsIDInCookie( emailAddress ) { |
| 129 |
|
| 130 |
if ( convertkit.debug ) { |
| 131 |
console.log( 'convertStoreSubscriberEmailAsIDInCookie' ); |
| 132 |
console.log( emailAddress ); |
| 133 |
} |
| 134 |
|
| 135 |
( function( $ ) { |
| 136 |
|
| 137 |
$.ajax( |
| 138 |
{ |
| 139 |
type: 'POST', |
| 140 |
data: { |
| 141 |
action: 'convertkit_store_subscriber_email_as_id_in_cookie', |
| 142 |
convertkit_nonce: convertkit.nonce, |
| 143 |
email: emailAddress |
| 144 |
}, |
| 145 |
url: convertkit.ajaxurl, |
| 146 |
success: function ( response ) { |
| 147 |
if ( convertkit.debug ) { |
| 148 |
console.log( response ); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
).fail( |
| 153 |
function (response) { |
| 154 |
if ( convertkit.debug ) { |
| 155 |
console.log( response ); |
| 156 |
} |
| 157 |
} |
| 158 |
); |
| 159 |
|
| 160 |
} )( jQuery ); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Remove the url subscriber_id url param |
| 166 |
* |
| 167 |
* The 'ck_subscriber_id' should only be set on URLs included on |
| 168 |
* links from a ConvertKit email with no other URL parameters. |
| 169 |
* This function removes the parameters so a customer won't share |
| 170 |
* a URL with their subscriber ID in it. |
| 171 |
* |
| 172 |
* @param url |
| 173 |
*/ |
| 174 |
function convertKitRemoveSubscriberIDFromURL( url ) { |
| 175 |
|
| 176 |
var clean_url = url.substring( 0, url.indexOf( "?ck_subscriber_id" ) ); |
| 177 |
var title = document.getElementsByTagName( "title" )[0].innerHTML; |
| 178 |
if ( clean_url ) { |
| 179 |
window.history.pushState( null, title, clean_url ); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Utility function to pause for the given number of milliseconds |
| 186 |
* |
| 187 |
* @since 1.9.6 |
| 188 |
* |
| 189 |
* @param int milliseconds |
| 190 |
*/ |
| 191 |
function convertKitSleep( milliseconds ) { |
| 192 |
|
| 193 |
var start = new Date().getTime(); |
| 194 |
for (var i = 0; i < 1e7; i++) { |
| 195 |
if ((new Date().getTime() - start) > milliseconds) { |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Register events |
| 204 |
*/ |
| 205 |
jQuery( document ).ready( |
| 206 |
function( $ ) { |
| 207 |
|
| 208 |
if ( convertkit.subscriber_id > 0 && convertkit.tag && convertkit.post_id ) { |
| 209 |
// If the user can be detected as a ConvertKit Subscriber (i.e. their Subscriber ID is in a cookie or the URL), |
| 210 |
// and the Page/Post they are viewing has a Tag specified, subscribe them to the tag. |
| 211 |
convertKitTagSubscriber( convertkit.subscriber_id, convertkit.tag, convertkit.post_id ); |
| 212 |
} else if ( convertkit.subscriber_id > 0 ) { |
| 213 |
// If the user can be detected as a ConvertKit Subscriber (i.e. their Subscriber ID is in a cookie or the URL), |
| 214 |
// update the cookie now. |
| 215 |
convertStoreSubscriberIDInCookie( convertkit.subscriber_id ); |
| 216 |
} |
| 217 |
|
| 218 |
// Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted. |
| 219 |
$( document ).on( |
| 220 |
'click', |
| 221 |
'.formkit-submit', |
| 222 |
function() { |
| 223 |
var emailAddress = $( 'input[name="email_address"]' ).val(); |
| 224 |
|
| 225 |
// If the email address is empty, don't attempt to get the subscriber ID by email. |
| 226 |
if ( ! emailAddress.length ) { |
| 227 |
if ( convertkit.debug ) { |
| 228 |
console.log( 'email empty' ); |
| 229 |
} |
| 230 |
|
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
// If the email address is invalid, don't attempt to get the subscriber ID by email. |
| 235 |
var validator = /^(([^<>()\[\]\\.,;:\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,}))$/; |
| 236 |
if ( ! validator.test( emailAddress.toLowerCase() ) ) { |
| 237 |
if ( convertkit.debug ) { |
| 238 |
console.log( 'email not an email address' ); |
| 239 |
} |
| 240 |
|
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
// Wait a moment before sending the AJAX request. |
| 245 |
convertKitSleep( 2000 ); |
| 246 |
convertStoreSubscriberEmailAsIDInCookie( emailAddress ); |
| 247 |
} |
| 248 |
); |
| 249 |
|
| 250 |
} |
| 251 |
); |
| 252 |
|