From 7312f137d40c432f2ce8a912ef73694033304c02 Mon Sep 17 00:00:00 2001 From: Manzur Ahammed Date: Mon, 20 May 2024 05:06:03 +0200 Subject: [PATCH] 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 --- .../woo-enable-autorenew/index.js | 20 +++++ .../woo-renew-subscription/index.js | 20 +++++ plugins/woocommerce-admin/webpack.config.js | 2 + .../add-show-expiring-notice-on-plugin-update | 4 + .../admin/helper/class-wc-helper-updater.php | 73 +++++++++++++++++++ .../woocommerce/src/Admin/PluginsHelper.php | 10 ++- 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce-admin/client/wp-admin-scripts/woo-enable-autorenew/index.js create mode 100644 plugins/woocommerce-admin/client/wp-admin-scripts/woo-renew-subscription/index.js create mode 100644 plugins/woocommerce/changelog/add-show-expiring-notice-on-plugin-update diff --git a/plugins/woocommerce-admin/client/wp-admin-scripts/woo-enable-autorenew/index.js b/plugins/woocommerce-admin/client/wp-admin-scripts/woo-enable-autorenew/index.js new file mode 100644 index 00000000000..8429cb41919 --- /dev/null +++ b/plugins/woocommerce-admin/client/wp-admin-scripts/woo-enable-autorenew/index.js @@ -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' ); + } ); + } ); + } +} ); diff --git a/plugins/woocommerce-admin/client/wp-admin-scripts/woo-renew-subscription/index.js b/plugins/woocommerce-admin/client/wp-admin-scripts/woo-renew-subscription/index.js new file mode 100644 index 00000000000..3eff82fb7c0 --- /dev/null +++ b/plugins/woocommerce-admin/client/wp-admin-scripts/woo-renew-subscription/index.js @@ -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' ); + } ); + } ); + } +} ); diff --git a/plugins/woocommerce-admin/webpack.config.js b/plugins/woocommerce-admin/webpack.config.js index 01b53eeb368..0333d758445 100644 --- a/plugins/woocommerce-admin/webpack.config.js +++ b/plugins/woocommerce-admin/webpack.config.js @@ -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 = { diff --git a/plugins/woocommerce/changelog/add-show-expiring-notice-on-plugin-update b/plugins/woocommerce/changelog/add-show-expiring-notice-on-plugin-update new file mode 100644 index 00000000000..216a67b432c --- /dev/null +++ b/plugins/woocommerce/changelog/add-show-expiring-notice-on-plugin-update @@ -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. diff --git a/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php b/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php index 629c15d318e..850d765c860 100644 --- a/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php +++ b/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php @@ -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, renew %2$sto 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, enable auto-renew 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. * diff --git a/plugins/woocommerce/src/Admin/PluginsHelper.php b/plugins/woocommerce/src/Admin/PluginsHelper.php index ab24dc5f87e..09f0c694894 100644 --- a/plugins/woocommerce/src/Admin/PluginsHelper.php +++ b/plugins/woocommerce/src/Admin/PluginsHelper.php @@ -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' ); } }