Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class ConvertKit_Output {
*/
public function __construct() {

add_action( 'rest_api_init', array( $this, 'register_routes' ) );
add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) );
add_action( 'template_redirect', array( $this, 'output_form' ) );
add_action( 'template_redirect', array( $this, 'page_takeover' ) );
Expand All @@ -80,62 +79,6 @@ public function __construct() {

}

/**
* Register REST API routes.
*
* @since 3.1.7
*/
public function register_routes() {

// Register route to store the Kit subscriber's email's ID in a cookie.
register_rest_route(
'kit/v1',
'/subscriber/store-email-as-id-in-cookie',
array(
'methods' => WP_REST_Server::CREATABLE,
'args' => array(
// Email: Validate email is included in the request, a valid email address
// and sanitize the email address.
'email' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_string( $param ) && is_email( $param );

},
'sanitize_callback' => 'sanitize_email',
),
),
'callback' => function ( $request ) {

// Get email address.
$email = $request->get_param( 'email' );

// Get subscriber ID.
$subscriber = new ConvertKit_Subscriber();
$subscriber_id = $subscriber->validate_and_store_subscriber_email( $email );

// Bail if an error occurred i.e. API hasn't been configured.
if ( is_wp_error( $subscriber_id ) ) {
return rest_ensure_response( $subscriber_id );
}

// Return the subscriber ID.
return rest_ensure_response(
array(
'id' => $subscriber_id,
)
);

},

// No authentication required, as this is on the frontend site.
'permission_callback' => '__return_true',
)
);

}

/**
* Tags the subscriber, if:
* - a subscriber ID exists in the cookie or URL,
Expand Down Expand Up @@ -820,9 +763,7 @@ public function enqueue_scripts() {
'convertkit-js',
'convertkit',
array(
'ajaxurl' => rest_url( 'kit/v1/subscriber/store-email-as-id-in-cookie' ),
'debug' => $settings->debug_enabled(),
'nonce' => wp_create_nonce( 'wp_rest' ),
'subscriber_id' => $this->subscriber_id,
)
);
Expand Down
50 changes: 0 additions & 50 deletions includes/class-convertkit-subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,56 +50,6 @@ public function get_subscriber_id() {

}

/**
* Validates the given subscriber email by querying the API to confirm
* the subscriber exists before storing their ID in a cookie.
*
* @since 2.0.0
*
* @param string $subscriber_email Possible Subscriber Email.
* @return WP_Error|int|string Error | Confirmed Subscriber ID or Signed Subscriber ID
*/
public function validate_and_store_subscriber_email( $subscriber_email ) {

// Bail if the API hasn't been configured.
$settings = new ConvertKit_Settings();
if ( ! $settings->has_access_and_refresh_token() ) {
return new WP_Error(
'convertkit_subscriber_get_subscriber_id_from_request_error',
__( 'Access Token not configured in Plugin Settings.', 'convertkit' )
);
}

// Initialize the API.
$api = new ConvertKit_API_V4(
CONVERTKIT_OAUTH_CLIENT_ID,
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
$settings->get_access_token(),
$settings->get_refresh_token(),
$settings->debug_enabled(),
'subscriber'
);

// Get subscriber by email, to ensure they exist.
$subscriber_id = $api->get_subscriber_id( $subscriber_email );

// Bail if no subscriber exists with the given subscriber ID, or an error occurred.
if ( is_wp_error( $subscriber_id ) ) {
// Delete the cookie.
$this->forget();

// Return error.
return $subscriber_id;
}

// Store the subscriber ID as a cookie.
$this->set( $subscriber_id );

// Return subscriber ID.
return $subscriber_id;

}

/**
* Gets the subscriber ID from the `ck_subscriber_id` cookie.
*
Expand Down
114 changes: 0 additions & 114 deletions resources/frontend/js/convertkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,6 @@
* @author ConvertKit
*/

/**
* Gets the subscriber ID for the given email address, storing
* it in the `ck_subscriber_id` cookie if it exists.
*
* Typically called when the user completes a ConvertKit Form
* that has either "Auto-confirm new subscribers" or
* "Send subscriber to thank you page" enabled (both scenarios
* include a ck_subscriber_id).
*
* @since 1.9.6
*
* @param {string} emailAddress Email Address
*/
function convertStoreSubscriberEmailAsIDInCookie(emailAddress) {
if (convertkit.debug) {
console.log('convertStoreSubscriberEmailAsIDInCookie');
console.log(emailAddress);
}

fetch(convertkit.ajaxurl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-WP-Nonce': convertkit.nonce,
},
body: new URLSearchParams({
email: emailAddress,
}),
})
.then(function (response) {
if (convertkit.debug) {
console.log(response);
}

return response.json();
})
.then(function (result) {
if (convertkit.debug) {
console.log(result);
}

// Emit custom event with subscriber ID.
convertKitEmitCustomEvent('convertkit_user_subscribed', {
id: result.id,
email: emailAddress,
});
})
.catch(function (error) {
if (convertkit.debug) {
console.error(error);
}
});
}

/**
* Remove the url subscriber_id url param
*
Expand Down Expand Up @@ -106,21 +52,6 @@ function convertKitRemoveSubscriberIDFromURL(url) {
});
}

/**
* Utility function to pause for the given number of milliseconds
*
* @since 1.9.6
* @param {number} milliseconds Number of milliseconds to pause for.
*/
function convertKitSleep(milliseconds) {
const start = new Date().getTime();
for (let i = 0; i < 1e7; i++) {
if (new Date().getTime() - start > milliseconds) {
break;
}
}
}

/**
* Emit a custom event with optional detail data.
*
Expand Down Expand Up @@ -171,51 +102,6 @@ if (typeof convertkit !== 'undefined') {
// Removes `ck_subscriber_id` from the URI.
convertKitRemoveSubscriberIDFromURL(window.location.href);

// Store subscriber ID as a cookie from the email address used when a ConvertKit Form is submitted.
document.addEventListener('click', function (e) {
// Check if the form submit button was clicked, or the span element was clicked and its parent is the form submit button.
if (
!e.target.matches('.formkit-submit') &&
(!e.target.parentElement ||
!e.target.parentElement.matches('.formkit-submit'))
) {
if (convertkit.debug) {
console.log('not a ck form');
}

return;
}

// Get email address.
const emailAddress = document.querySelector(
'input[name="email_address"]'
).value;

// If the email address is empty, don't attempt to get the subscriber ID by email.
if (!emailAddress.length) {
if (convertkit.debug) {
console.log('email empty');
}

return;
}

// If the email address is invalid, don't attempt to get the subscriber ID by email.
const 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,}))$/;
if (!validator.test(emailAddress.toLowerCase())) {
if (convertkit.debug) {
console.log('email not an email address');
}

return;
}

// Wait a moment before sending the AJAX request.
convertKitSleep(2000);
convertStoreSubscriberEmailAsIDInCookie(emailAddress);
});

// Set a cookie if any scripts with data-kit-limit-per-session attribute exist.
if (
document.querySelectorAll('script[data-kit-limit-per-session="1"]')
Expand Down
Loading