Fixes issue where expired and expiring notice displayed incorrectly (#49717)

Fixes issue where expired and expiring notice displayed incorrectly

Consider connected (to the current site) product when checking expired
and expiring notice.

* Cache site subscriptions instead of just site_id
* Add helper method get_installed_subscriptions
* Bail out of installed subscriptions are empty
* Return early for invalid site ID
This commit is contained in:
Akeda Bagus 2024-07-23 13:46:02 +07:00 committed by GitHub
parent 7f23702018
commit 5fdc8fa520
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 16 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: fix
Display admin notice of expired and expiring Woo subscription when the product
is connected / activated on the site.

View File

@ -254,26 +254,36 @@ class WC_Helper_Updater {
* @return void.
*/
public static function display_notice_for_expired_and_expiring_subscriptions( $plugin_data, $response ) {
// Extract product ID from the response.
$product_id = preg_replace( '/[^0-9]/', '', $response->id );
// Get the subscription details based on product ID.
$subscription = current(
wp_list_filter(
WC_Helper::get_subscriptions(),
array( 'product_id' => $product_id )
)
);
// Check if subscription is empty.
if ( empty( $subscription ) ) {
// Product subscriptions.
$subscriptions = wp_list_filter( WC_Helper::get_installed_subscriptions(), array( 'product_id' => $product_id ) );
if ( empty( $subscriptions ) ) {
return;
}
$expired_subscription = current(
array_filter(
$subscriptions,
function ( $subscription ) {
return ! empty( $subscription['expired'] ) && ! $subscription['lifetime'];
}
)
);
$expiring_subscription = current(
array_filter(
$subscriptions,
function ( $subscription ) {
return ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'];
}
)
);
// Prepare the expiry notice based on subscription status.
$expiry_notice = '';
if ( ! empty( $subscription['expired'] ) && ! $subscription['lifetime'] ) {
if ( ! empty( $expired_subscription ) ) {
$renew_link = add_query_arg(
array(
@ -284,7 +294,7 @@ class WC_Helper_Updater {
);
/* translators: 1: Product regular price */
$product_price = ! empty( $subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $subscription['product_regular_price'] ) ) : '';
$product_price = ! empty( $expired_subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $expired_subscription['product_regular_price'] ) ) : '';
$expiry_notice = sprintf(
/* translators: 1: URL to My Subscriptions page 2: Product price */
@ -292,18 +302,19 @@ class WC_Helper_Updater {
esc_url( $renew_link ),
$product_price
);
} elseif ( ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'] ) {
$renew_link = add_query_arg(
} elseif ( ! empty( $expiring_subscription ) ) {
$renew_link = add_query_arg(
array(
'utm_source' => 'pu',
'utm_campaign' => 'pu_plugin_screen_enable_autorenew',
),
PluginsHelper::WOO_SUBSCRIPTION_PAGE_URL
);
$expiry_notice = sprintf(
/* translators: 1: Expiry date 1: URL to My Subscriptions page */
__( ' Your subscription expires on %1$s, <a href="%2$s" class="woocommerce-enable-autorenew">enable auto-renew</a> to continue receiving updates.', 'woocommerce' ),
date_i18n( 'F jS', $subscription['expires'] ),
date_i18n( 'F jS', $expiring_subscription['expires'] ),
esc_url( $renew_link )
);
}

View File

@ -1297,6 +1297,35 @@ class WC_Helper {
return ! empty( $subscription );
}
/**
* Get the user's connected subscriptions that are installed on the current
* site.
*
* @return array
*/
public static function get_installed_subscriptions() {
static $installed_subscriptions = null;
// Cache installed_subscriptions in the current request.
if ( is_null( $installed_subscriptions ) ) {
$auth = WC_Helper_Options::get( 'auth' );
$site_id = isset( $auth['site_id'] ) ? absint( $auth['site_id'] ) : 0;
if ( 0 === $site_id ) {
$installed_subscriptions = array();
return $installed_subscriptions;
}
$installed_subscriptions = array_filter(
self::get_subscriptions(),
function ( $subscription ) use ( $site_id ) {
return in_array( $site_id, $subscription['connections'], true );
}
);
}
return $installed_subscriptions;
}
/**
* Get subscription state of a given product ID.
*

View File

@ -827,6 +827,7 @@ class PluginsHelper {
$subscriptions,
function ( $sub ) {
return ( ! empty( $sub['local']['installed'] ) && ! empty( $sub['product_key'] ) )
&& $sub['active']
&& $sub['expiring']
&& ! $sub['autorenew'];
},
@ -898,6 +899,7 @@ class PluginsHelper {
$subscriptions,
function ( $sub ) {
return ( ! empty( $sub['local']['installed'] ) && ! empty( $sub['product_key'] ) )
&& $sub['active']
&& $sub['expired']
&& ! $sub['lifetime'];
},