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
34 changes: 0 additions & 34 deletions admin/section/class-convertkit-admin-section-restrict-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,6 @@ public function register_fields() {
)
);

add_settings_field(
'require_tag_login',
__( 'Require Login', 'convertkit' ),
array( $this, 'require_tag_login_callback' ),
$this->settings_key,
$this->name . '-tags',
array(
'name' => 'require_tag_login',
'label_for' => 'require_tag_login',
'label' => __( 'When checked, subscribers are sent a code in an email to login after being subscribed and tagged.', 'convertkit' ),
'description' => '',
)
);

// All.
add_settings_field(
'subscribe_button_label',
Expand Down Expand Up @@ -453,26 +439,6 @@ public function permit_crawlers_callback( $args ) {

}

/**
* Renders the input for the Require Login setting.
*
* @since 2.7.2
*
* @param array $args Setting field arguments (name,description).
*/
public function require_tag_login_callback( $args ) {

// Output field.
$this->output_checkbox_field(
$args['name'],
'on',
$this->settings->require_tag_login(),
$args['label'],
$args['description']
);

}

/**
* Renders the input for the text setting.
*
Expand Down
86 changes: 6 additions & 80 deletions includes/class-convertkit-output-restrict-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ public function initialize_classes() {

/**
* If the user isn't using JavaScript, or the Plugin's Disable JS is enabled, checks if the request is a Restrict Content request with an email address.
* Also runs if restrict content by tag and require login is disabled, as we immediately tag and redirect if this is the case.
* If so, calls the API depending on the Restrict Content resource that's required:
* - tag: subscribes the email address to the tag, storing the subscriber ID in a cookie and redirecting
* - product: calls the API to send the subscriber a magic link by email containing a code. See maybe_run_subscriber_verification()
* for logic once they click the link in the email or enter the code on screen.
* - tag: subscribes the email address to the tag, and calls the API to send the subscriber a magic link by email containing a code.
* - form + product: calls the API to send the subscriber a magic link by email containing a code.
*
* See maybe_run_subscriber_verification() for logic once they click the link in the email or enter the code on screen.
*
* @since 2.1.0
*/
Expand Down Expand Up @@ -413,26 +413,8 @@ public function maybe_run_subscriber_authentication() {
$this->error = $result;
return;
}

// If require login is disabled, return now.
if ( ! $this->restrict_content_settings->require_tag_login() ) {
// Clear any existing subscriber ID cookie, as the authentication flow has started by sending the email.
$subscriber = new ConvertKit_Subscriber();
$subscriber->forget();

// Fetch the subscriber ID from the result.
$subscriber_id = $result['subscriber']['id'];

// Store subscriber ID in cookie.
$this->store_subscriber_id_in_cookie( $subscriber_id );

// Redirect.
$this->redirect( $this->post_id );
return;
}
}

// If here, require login is enabled for tags or this is a product/form.
// Run subscriber authentication.
$result = $this->subscriber_authentication_send_code( $email, $this->post_id );

Expand Down Expand Up @@ -1077,32 +1059,14 @@ private function resource_exists() {
*/
private function subscriber_has_access( $subscriber_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter

// Depending on the resource type, determine if the subscriber has access to it.
// This is deliberately a switch statement, because we will likely add in support
// for restrict by tag and form later.
switch ( $this->resource_type ) {
case 'product':
// For products, the subscriber ID has to be a signed subscriber ID string.
return $this->subscriber_has_access_to_product_by_signed_subscriber_id( $subscriber_id, absint( $this->resource_id ) );

case 'form':
// For forms, the subscriber ID has to be a signed subscriber ID string.
return $this->subscriber_has_access_to_form_by_signed_subscriber_id( $subscriber_id, absint( $this->resource_id ) );

case 'tag':
// If the subscriber ID is numeric, check using get_subscriber_tags().
if ( is_numeric( $subscriber_id ) ) {
// If require login is enabled, only a signed subscriber ID is accepted, as this is generated
// via the subscriber verify email flow.
if ( $this->restrict_content_settings->require_tag_login() ) {
return false;
}

return $this->subscriber_has_access_to_tag_by_subscriber_id( $subscriber_id, absint( $this->resource_id ) );
}

// The subscriber ID is a signed subscriber ID string.
// Check using profile().
return $this->subscriber_has_access_to_tag_by_signed_subscriber_id( $subscriber_id, absint( $this->resource_id ) );

}
Expand Down Expand Up @@ -1202,44 +1166,6 @@ private function subscriber_has_access_to_tag_by_signed_subscriber_id( $signed_s

}

/**
* Determines if the given signed subscriber ID has an active subscription to
* the given tag.
*
* @since 2.7.1
*
* @param int $subscriber_id Subscriber ID.
* @param int $tag_id Tag ID.
* @return bool Has access to tag
*/
private function subscriber_has_access_to_tag_by_subscriber_id( $subscriber_id, $tag_id ) {

// Get tags that the subscriber has been assigned.
$tags = $this->api->get_subscriber_tags( $subscriber_id );

// If an error occurred, the subscriber ID is invalid.
if ( is_wp_error( $tags ) ) {
return false;
}

// If no tags exist, there's no access.
if ( ! count( $tags['tags'] ) ) {
return false;
}

// Iterate through the subscriber's tags to see if they have the required tag.
foreach ( $tags['tags'] as $tag ) {
if ( $tag['id'] === $tag_id ) {
// Subscriber has the required tag assigned to them - grant access.
return true;
}
}

// If here, the subscriber does not have the tag.
return false;

}

/**
* Gets the subscriber ID from the request (either the cookie or the URL).
*
Expand Down Expand Up @@ -1530,9 +1456,9 @@ function () use ( $post_id, $resource_id, $resource_type ) {
$heading = $this->restrict_content_settings->get_by_key( 'subscribe_heading_tag' );
$text = $this->restrict_content_settings->get_by_key( 'subscribe_text_tag' );

// If require login is enabled and scripts are enabled, output the email login form in a modal, which will be displayed
// If scripts are enabled, output the email login form in a modal, which will be displayed
// when the 'log in' link is clicked.
if ( $this->restrict_content_settings->require_tag_login() && ! $this->settings->scripts_disabled() ) {
if ( ! $this->settings->scripts_disabled() ) {
add_action(
'wp_footer',
function () use ( $post_id, $resource_id, $resource_type ) {
Expand Down
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
14 changes: 0 additions & 14 deletions includes/class-convertkit-settings-restrict-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ public function permit_crawlers() {

}

/**
* Returns whether login by email is required for Member Content by Tag functionality.
*
* @since 2.7.2
*
* @return bool
*/
public function require_tag_login() {

return ( $this->settings['require_tag_login'] === 'on' ? true : false );

}

/**
* Returns Restrict Content settings value for the given key.
*
Expand Down Expand Up @@ -140,7 +127,6 @@ public function get_defaults() {
// Restrict by Tag.
'subscribe_heading_tag' => __( 'Subscribe to keep reading', 'convertkit' ),
'subscribe_text_tag' => __( 'This post is free to read but only available to subscribers. Join today to get access to all posts.', 'convertkit' ),
'require_tag_login' => '', // blank|on.
'no_access_text_tag' => __( 'Your account does not have access to this content. Please use the form above to subscribe.', 'convertkit' ),

// All.
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
Loading
Loading