Skip to content

Commit

Permalink
Paywall: fix pending subscription check (#34543)
Browse files Browse the repository at this point in the history
  • Loading branch information
lezama authored Dec 12, 2023
1 parent d93b8b1 commit 791dd02
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: bugfix

Paywall: Fix pending subscription state
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ public function get_key() {
}
return $token->secret;
}

/**
* Returns true if the current authenticated user has a pending subscription to the current site.
*
* @return boolean
*/
public function is_current_user_pending_subscriber() {

return $this->get_token_property( 'blog_sub' ) === 'pending';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function initialize();
*/
public function visitor_can_view_content( $valid_plan_ids, $access_level );

/**
* Is the current user a pending subscriber for the current site?
*
* @return boolean
*/
public function is_current_user_pending_subscriber();

/**
* The current visitor would like to obtain access. Where do they go?
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public function is_current_user_subscribed() {
return $this->get_token_property( 'blog_sub' ) === 'active';
}

/**
* Returns true if the current authenticated user has a pending subscription to the current site.
*
* @return boolean
*/
public function is_current_user_pending_subscriber() {
return false;
}

/**
* Return if the user has access to the content depending on the access level and the user rights
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public function visitor_can_view_content( $valid_plan_ids, $access_level ) {
return false;
}

/**
* is the current user a pending subscriber for the current site?
*
* @return boolean
*/
public function is_current_user_pending_subscriber() {
return false;
}

/**
* The current visitor would like to obtain access. Where do they go?
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ public function is_current_user_subscribed() {
return true;
}

/**
* Returns true if the current authenticated user has a pending subscription to the current site.
*
* @return boolean
*/
public function is_current_user_pending_subscriber() {
include_once WP_CONTENT_DIR . '/mu-plugins/email-subscriptions/subscriptions.php';
$email = wp_get_current_user()->user_email;
$subscriber_object = \Blog_Subscriber::get( $email );
if ( empty( $subscriber_object ) ) {
return false;
}
$blog_id = $this->get_site_id();
$subscription_status = \Blog_Subscription::get_subscription_status_for_blog( $subscriber_object, $blog_id );
if ( 'pending' !== $subscription_status ) {
return false;
}
return true;
}

/**
* Lookup users subscriptions for a site and determine if the user has a valid subscription to match the plan ID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Jetpack_Gutenberg;
use Jetpack_Memberships;
use Jetpack_Subscriptions_Widget;
use function Automattic\Jetpack\Extensions\Premium_Content\subscription_service;

require_once __DIR__ . '/constants.php';

Expand Down Expand Up @@ -806,14 +805,7 @@ function add_paywall( $the_content ) {
return $the_content;
}

require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
$token_service = subscription_service();
$token = $token_service->get_and_set_token_from_request();
$payload = $token_service->decode_token( $token );
$is_valid_token = ! empty( $payload );
$email_confirmation_pending = $is_valid_token && isset( $payload['blog_sub'] ) && $payload['blog_sub'] === 'pending';

$paywalled_content = get_paywall_content( $post_access_level, $email_confirmation_pending ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$paywalled_content = get_paywall_content( $post_access_level ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) {
if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) {
Expand Down Expand Up @@ -869,11 +861,10 @@ function maybe_gate_existing_comments( $comment ) {
* Returns paywall content blocks
*
* @param string $post_access_level The newsletter access level.
* @param string $email_confirmation_pending True if the current user needs to validate their email.
* @return string
*/
function get_paywall_content( $post_access_level, $email_confirmation_pending = false ) {
if ( $email_confirmation_pending ) {
function get_paywall_content( $post_access_level ) {
if ( Jetpack_Memberships::user_is_pending_subscriber() ) {
return get_paywall_blocks_subscribe_pending();
}
if ( doing_filter( 'get_the_excerpt' ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ public static function user_is_paid_subscriber() {
return $is_paid_subscriber;
}

/**
* Determines whether the current user has a pending subscription.
*
* @return bool Whether the user has a pending subscription
*/
public static function user_is_pending_subscriber() {
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
$subscription_service = \Automattic\Jetpack\Extensions\Premium_Content\subscription_service();
return $subscription_service->is_current_user_pending_subscriber();
}

/**
* Determines whether the current user can view the post based on the newsletter access level
* and caches the result.
Expand Down

0 comments on commit 791dd02

Please sign in to comment.