From 17ca74e0255cdd18002abbf3a20a6f4bb2f90aaa Mon Sep 17 00:00:00 2001 From: MILLER/F Date: Thu, 15 Feb 2024 14:24:32 +0000 Subject: [PATCH] Add support for WP Super Cache and Boost Cache (#35598) * Add support for WP Super Cache and Boost Cache * Fix string * Prevent redefining a constant --------- Co-authored-by: Fabien MILLERAND --- ...support for WP Super Cache and Boost Cache | 4 +++ .../blocks/subscriptions/subscriptions.php | 35 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/[Memberships] Add support for WP Super Cache and Boost Cache diff --git a/projects/plugins/jetpack/changelog/[Memberships] Add support for WP Super Cache and Boost Cache b/projects/plugins/jetpack/changelog/[Memberships] Add support for WP Super Cache and Boost Cache new file mode 100644 index 0000000000000..fcaca31daff7d --- /dev/null +++ b/projects/plugins/jetpack/changelog/[Memberships] Add support for WP Super Cache and Boost Cache @@ -0,0 +1,4 @@ +Significance: patch +Type: compat + +Add support for WP Super Cache and Boost Cache diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php b/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php index 52ff80ea15969..82e631ba33b14 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/subscriptions.php @@ -153,6 +153,8 @@ function ( $allowed_meta ) { add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 ); add_action( 'admin_head', __NAMESPACE__ . '\newsletter_access_column_styles' ); } + + add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' ); } add_action( 'init', __NAMESPACE__ . '\register_block', 9 ); @@ -895,6 +897,32 @@ function maybe_gate_existing_comments( $comment ) { return ''; } +/** + * Is the Jetpack_Token_Subscription_Service class loaded + * + * @return bool + */ +function is_jetpack_token_subscription_service_loaded(): bool { + return class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' ); +} + +/** + * Adds support for WP Super cache and Boost cache + */ +function maybe_prevent_super_cache_caching() { + // Prevents cached page to be served if the Membership cookie is present + if ( is_jetpack_token_subscription_service_loaded() ) { + do_action( 'wpsc_add_cookie', Jetpack_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME ); + } + + if ( is_user_auth() ) { + // Do not cache the page if user is auth with Membership token + if ( ! defined( 'DONOTCACHEPAGE' ) ) { + define( 'DONOTCACHEPAGE', true ); + } + } +} + /** * Returns paywall content blocks * @@ -1111,16 +1139,17 @@ function get_paywall_access_question( $post_access_level ) { /** * Returns true if user is auth for subscriptions check, otherwise returns false. * - * @return boolean + * @return bool */ -function is_user_auth() { +function is_user_auth(): bool { if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) { return true; } if ( current_user_can( 'manage_options' ) ) { return true; } - if ( class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' ) ) { + + if ( is_jetpack_token_subscription_service_loaded() ) { if ( Jetpack_Token_Subscription_Service::has_token_from_cookie() ) { return true; }