Display a notice for expired/expiring subscriptions along with a plugin update message. (#47076)

* add subscription expiry message on plugin list

* Update changelog

* fix lint

* refactor code

* update product price meta

* add tracks event for enable autorenew and renew subscription notices in plugin lists

* fix lint error

* check product price is set or not

* check product price is set or not

* added translator comment

* fix typo

---------

Co-authored-by: prahesa.setia <prahesa.kusuma.setia@automattic.com>
This commit is contained in:
Manzur Ahammed 2024-05-20 05:06:03 +02:00 committed by GitHub
parent 8538ca3498
commit 7312f137d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 126 additions and 3 deletions

View File

@ -0,0 +1,20 @@
/**
* External dependencies
*/
import domReady from '@wordpress/dom-ready';
import { recordEvent } from '@woocommerce/tracks';
domReady( () => {
const enableAutorenewLink = document.querySelectorAll(
'.woocommerce-enable-autorenew'
);
if ( enableAutorenewLink.length > 0 ) {
recordEvent( 'woo_enable_autorenew_in_plugins_shown' );
enableAutorenewLink.forEach( ( link ) => {
link.addEventListener( 'click', function () {
recordEvent( 'woo_enable_autorenew_in_plugins_clicked' );
} );
} );
}
} );

View File

@ -0,0 +1,20 @@
/**
* External dependencies
*/
import domReady from '@wordpress/dom-ready';
import { recordEvent } from '@woocommerce/tracks';
domReady( () => {
const renewSubscriptionLink = document.querySelectorAll(
'.woocommerce-renew-subscription'
);
if ( renewSubscriptionLink.length > 0 ) {
recordEvent( 'woo_renew_subscription_in_plugins_shown' );
renewSubscriptionLink.forEach( ( link ) => {
link.addEventListener( 'click', function () {
recordEvent( 'woo_renew_subscription_in_plugins_clicked' );
} );
} );
}
} );

View File

@ -75,6 +75,8 @@ const wpAdminScripts = [
'command-palette-analytics',
'woo-connect-notice',
'woo-plugin-update-connect-notice',
'woo-enable-autorenew',
'woo-renew-subscription',
];
const getEntryPoints = () => {
const entryPoints = {

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Show an message in the plugin table list for WooCommerce extensions that are either expired or expiring.

View File

@ -36,6 +36,18 @@ class WC_Helper_Updater {
if ( ! WC_Woo_Update_Manager_Plugin::is_plugin_active() || ! WC_Helper::is_site_connected() ) {
add_action( 'load-plugins.php', array( __CLASS__, 'setup_update_plugins_messages' ), 11 );
}
if ( WC_Helper::is_site_connected() ) {
add_action( 'load-plugins.php', array( __CLASS__, 'setup_message_for_expired_and_expiring_subscriptions' ), 11 );
}
}
/**
* Add the hook for modifying default WPCore update notices on the plugins management page.
*/
public static function setup_message_for_expired_and_expiring_subscriptions() {
foreach ( WC_Helper::get_local_woo_plugins() as $plugin ) {
add_action( 'in_plugin_update_message-' . $plugin['_filename'], array( __CLASS__, 'display_notice_for_expired_and_expiring_subscriptions' ), 10, 2 );
}
}
/**
@ -231,6 +243,67 @@ class WC_Helper_Updater {
}
}
/**
* Runs on in_plugin_update_message-{file-name}, show a message if plugins subscription expired or expiring soon.
*
* @param object $plugin_data An array of plugin metadata.
* @param object $response An object of metadata about the available plugin update.
*
* @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 ) ) {
return;
}
// Prepare the expiry notice based on subscription status.
$expiry_notice = '';
if ( ! empty( $subscription['expired'] ) && ! $subscription['lifetime'] ) {
/* translators: 1: Product regular price */
$product_price = ! empty( $subscription['product_regular_price'] ) ? sprintf( __( 'for %s ', 'woocommerce' ), esc_html( $subscription['product_regular_price'] ) ) : '';
$expiry_notice = sprintf(
/* translators: 1: URL to My Subscriptions page 2: Product price */
__( ' Your subscription expired, <a href="%1$s" class="woocommerce-renew-subscription">renew %2$s</a>to update.', 'woocommerce' ),
esc_url( 'https://woocommerce.com/my-account/my-subscriptions/' ),
$product_price
);
} elseif ( ! empty( $subscription['expiring'] ) && ! $subscription['autorenew'] ) {
$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'] ),
esc_url( 'https://woocommerce.com/my-account/my-subscriptions/' )
);
}
// Display the expiry notice.
if ( ! empty( $expiry_notice ) ) {
echo wp_kses(
$expiry_notice,
array(
'a' => array(
'href' => array(),
'class' => array(),
),
)
);
}
}
/**
* Get update data for all plugins.
*

View File

@ -40,7 +40,7 @@ class PluginsHelper {
add_action( 'woocommerce_plugins_activate_callback', array( __CLASS__, 'activate_plugins' ), 10, 2 );
add_action( 'admin_notices', array( __CLASS__, 'maybe_show_connect_notice_in_plugin_list' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_scripts_for_connect_notice' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_scripts_for_connect_notice_in_plugins' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_scripts_for_notices_in_plugins' ) );
}
/**
@ -602,16 +602,20 @@ class PluginsHelper {
}
/**
* Enqueue scripts for connect notice in plugin list page.
* Enqueue scripts for notices in plugin list page.
*
* @return void
*/
public static function maybe_enqueue_scripts_for_connect_notice_in_plugins() {
public static function maybe_enqueue_scripts_for_notices_in_plugins() {
if ( 'plugins' !== get_current_screen()->id ) {
return;
}
WCAdminAssets::register_script( 'wp-admin-scripts', 'woo-plugin-update-connect-notice' );
WCAdminAssets::register_script( 'wp-admin-scripts', 'woo-enable-autorenew' );
WCAdminAssets::register_script( 'wp-admin-scripts', 'woo-renew-subscription' );
wp_enqueue_script( 'woo-plugin-update-connect-notice' );
wp_enqueue_script( 'woo-enable-autorenew' );
wp_enqueue_script( 'woo-renew-subscription' );
}
}