Skip to content

Commit

Permalink
Monetize: Update cache key to prevent false hits (#36925)
Browse files Browse the repository at this point in the history
* Cache by all consumed parameters
  • Loading branch information
pottedmeat authored Apr 25, 2024
1 parent 71b37bf commit f633338
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-paid-subscriber-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Ensure that the paid subscriber cache is unique to the parameters it consumes.
Original file line number Diff line number Diff line change
Expand Up @@ -634,17 +634,23 @@ public static function clear_cache( int $user_id = null ) {
public static function user_is_paid_subscriber( $valid_plan_ids = array(), $user_id = null ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
if ( empty( $user_id ) ) {
return false;
}
}
if ( ! isset( self::$user_is_paid_subscriber_cache[ $user_id ] ) ) {
// sort and stringify sorted valid plan ids to use as a cache key
sort( $valid_plan_ids );
$cache_key = $user_id . '_' . implode( ',', $valid_plan_ids );
if ( ! isset( self::$user_is_paid_subscriber_cache[ $cache_key ] ) ) {
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
if ( empty( $valid_plan_ids ) ) {
$valid_plan_ids = self::get_all_newsletter_plan_ids();
}
$paywall = \Automattic\Jetpack\Extensions\Premium_Content\subscription_service( $user_id );
$is_paid_subscriber = $paywall->visitor_can_view_content( $valid_plan_ids, Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS );
self::$user_is_paid_subscriber_cache[ $user_id ] = $is_paid_subscriber;
self::$user_is_paid_subscriber_cache[ $cache_key ] = $is_paid_subscriber;
}
return self::$user_is_paid_subscriber_cache[ $user_id ];
return self::$user_is_paid_subscriber_cache[ $cache_key ];
}

/**
Expand Down

0 comments on commit f633338

Please sign in to comment.