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
15 changes: 15 additions & 0 deletions admin/class-convertkit-admin-refresh-resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@ public function register_routes() {
'/resources/refresh/(?P<resource>[a-zA-Z0-9-_]+)',
array(
'methods' => WP_REST_Server::CREATABLE,
'args' => array(
// Resource: Validate resource is included in the request, a valid resource
// and sanitize the resource.
'resource' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_string( $param ) && in_array( $param, array( 'forms', 'landing_pages', 'tags', 'posts', 'products', 'restrict_content' ), true );

},
'sanitize_callback' => 'sanitize_text_field',
),
),
'callback' => array( $this, 'refresh_resources' ),

// Only refresh resources for users who can edit posts.
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
Expand Down
75 changes: 0 additions & 75 deletions includes/class-convertkit-ajax.php

This file was deleted.

1 change: 1 addition & 0 deletions includes/class-convertkit-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function register_routes() {
return rest_ensure_response( convertkit_get_blocks() );
},

// Only refresh resources for users who can edit posts.
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
Expand Down
84 changes: 84 additions & 0 deletions includes/class-convertkit-output-restrict-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,52 @@ public function register_routes() {
'/restrict-content/subscriber-authentication',
array(
'methods' => WP_REST_Server::CREATABLE,
'args' => array(
// Email: Validate email is included in the request, is a valid email address
// and sanitize the email address.
'convertkit_email' => array(
'required' => true,
'validate_callback' => function ( $param ) {

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

},
'sanitize_callback' => 'sanitize_email',
),

// Post ID: Validate post ID is included in the request and is an integer.
'convertkit_post_id' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_numeric( $param );

},
'sanitize_callback' => 'absint',
),

// Resource Type: Validate resource type is included in the request and is a string.
'convertkit_resource_type' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_string( $param );

},
'sanitize_callback' => 'sanitize_text_field',
),

// Resource ID: Validate resource ID is included in the request and is an integer.
'convertkit_resource_id' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_numeric( $param );

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

// Initialize classes that will be used.
Expand Down Expand Up @@ -181,6 +227,8 @@ public function register_routes() {
)
);
},

// No authentication required, as this is on the frontend site.
'permission_callback' => '__return_true',
)
);
Expand All @@ -191,6 +239,40 @@ public function register_routes() {
'/restrict-content/subscriber-verification',
array(
'methods' => WP_REST_Server::CREATABLE,
'args' => array(
// Post ID: Validate post ID is an integer if included in the request.
'convertkit_post_id' => array(
'required' => false,
'validate_callback' => function ( $param ) {

return is_numeric( $param );

},
'sanitize_callback' => 'absint',
),

// Token: Validate token is included in the request and is a string.
'token' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_string( $param );

},
'sanitize_callback' => 'sanitize_text_field',
),

// Subscriber Code: Validate subscriber code is included in the request and is a string.
'subscriber_code' => array(
'required' => true,
'validate_callback' => function ( $param ) {

return is_string( $param );

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

// Initialize classes that will be used.
Expand Down Expand Up @@ -234,6 +316,8 @@ public function register_routes() {
)
);
},

// No authentication required, as this is on the frontend site.
'permission_callback' => '__return_true',
)
);
Expand Down
61 changes: 59 additions & 2 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ConvertKit_Output {
*/
public function __construct() {

add_action( 'rest_api_init', array( $this, 'register_routes' ) );
add_action( 'init', array( $this, 'get_subscriber_id_from_request' ) );
add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) );
add_action( 'template_redirect', array( $this, 'output_form' ) );
Expand All @@ -80,6 +81,62 @@ 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 occured 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 @@ -756,9 +813,9 @@ public function enqueue_scripts() {
'convertkit-js',
'convertkit',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxurl' => rest_url( 'kit/v1/subscriber/store-email-as-id-in-cookie' ),
'debug' => $settings->debug_enabled(),
'nonce' => wp_create_nonce( 'convertkit' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'subscriber_id' => $this->subscriber_id,
)
);
Expand Down
1 change: 0 additions & 1 deletion includes/class-wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ private function initialize_global() {

$this->classes['admin_notices'] = new ConvertKit_Admin_Notices();
$this->classes['admin_refresh_resources'] = new ConvertKit_Admin_Refresh_Resources();
$this->classes['ajax'] = new ConvertKit_AJAX();
$this->classes['blocks_convertkit_broadcasts'] = new ConvertKit_Block_Broadcasts();
$this->classes['blocks_convertkit_content'] = new ConvertKit_Block_Content();
$this->classes['blocks_convertkit_formtrigger'] = new ConvertKit_Block_Form_Trigger();
Expand Down
5 changes: 2 additions & 3 deletions resources/frontend/js/convertkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ function convertStoreSubscriberEmailAsIDInCookie(emailAddress) {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-WP-Nonce': convertkit.nonce,
},
body: new URLSearchParams({
action: 'convertkit_store_subscriber_email_as_id_in_cookie',
convertkit_nonce: convertkit.nonce,
email: emailAddress,
}),
})
Expand All @@ -50,7 +49,7 @@ function convertStoreSubscriberEmailAsIDInCookie(emailAddress) {

// Emit custom event with subscriber ID.
convertKitEmitCustomEvent('convertkit_user_subscribed', {
id: result.data.id,
id: result.id,
email: emailAddress,
});
})
Expand Down
Loading
Loading