Commit b891a56f9ea for woocommerce
commit b891a56f9ea9919601ba9fc4344bd55e55e8e00e
Author: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>
Date: Mon Nov 30 13:25:54 2026 +0200
Add a Manage notifications link for guests in Back in Stock emails (#71454)
* fix: link guests to manage stock notifications instead of unsubscribing
* fix: add confirm link and expiry to plain-text stock sign-up email
* fix: remove extra blank lines in plain-text stock notification emails
* fix: keep unsubscribe only in the stock sign-up confirmation email
* fix: stop creating unsubscribe keys for back in stock emails
* fix: give stock email manage and unsubscribe links their own IDs
* fix: drop stale PHPStan baseline entry and set stock email templates to 11.4.0
* fix: point old stock email overrides' unsubscribe link to the account
* test: count duplicate links in the stock email footer assertions
diff --git a/plugins/woocommerce/changelog/fix-bis-guest-manage-notifications-link b/plugins/woocommerce/changelog/fix-bis-guest-manage-notifications-link
new file mode 100644
index 00000000000..fc737f72eaa
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-bis-guest-manage-notifications-link
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Replace the unsubscribe link in back in stock emails with a "Manage notifications" link, keeping unsubscribe for guests in the sign-up confirmation email.
diff --git a/plugins/woocommerce/changelog/fix-bis-plain-verify-expiry b/plugins/woocommerce/changelog/fix-bis-plain-verify-expiry
new file mode 100644
index 00000000000..5f9fc178e86
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-bis-plain-verify-expiry
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Add the confirm link and its expiry time to the plain-text back in stock sign-up confirmation email.
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 4e08fdbea2e..5783c1548fd 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -63556,12 +63556,6 @@ parameters:
count: 2
path: src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
- -
- message: '#^Cannot call method get_id\(\) on bool\|object\.$#'
- identifier: method.nonObject
- count: 1
- path: src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
-
-
message: '#^Cannot call method get_name\(\) on WC_Product\|false\.$#'
identifier: method.nonObject
diff --git a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
index eb1ffe36e49..8ebda7ffc38 100644
--- a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
+++ b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
@@ -15,13 +15,6 @@ use WC_Email;
*/
class CustomerStockNotificationEmail extends WC_Email {
- /**
- * Unsubscribe key for the email being sent. The caller's save persists its hash.
- *
- * @var string
- */
- private $unsubscribe_key = '';
-
/**
* Constructor.
*/
@@ -209,7 +202,9 @@ class CustomerStockNotificationEmail extends WC_Email {
$user = $notification instanceof Notification && $notification->get_user_id() ? get_user_by( 'id', $notification->get_user_id() ) : false;
$is_guest = ! is_a( $user, 'WP_User' );
$account_link = '';
- if ( ! $is_guest && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
+ // Guests can only manage sign-ups after creating an account, so skip the link when registration is off.
+ $can_reach_account = ! $is_guest || 'yes' === get_option( 'woocommerce_enable_myaccount_registration' );
+ if ( $can_reach_account && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
$account_link = UtmHelper::add_email_utm_params( MyAccountEndpoint::get_endpoint_url() );
}
@@ -217,16 +212,8 @@ class CustomerStockNotificationEmail extends WC_Email {
'button_text' => $button_text,
'button_link' => $button_link,
'account_link' => $account_link,
- 'unsubscribe_link' => UtmHelper::add_email_utm_params(
- add_query_arg(
- array(
- 'email_link_action' => EmailActionController::ACTION_UNSUBSCRIBE,
- 'email_link_action_key' => $this->unsubscribe_key,
- 'notification_id' => $notification->get_id(),
- ),
- home_url( '/' )
- )
- ),
+ // Unsubscribe is no longer used in the template, since the sign-up is sent with this email, but we're keeping it for backwards compatibility with custom templates, which now link to account management.
+ 'unsubscribe_link' => $account_link,
'is_guest' => $is_guest,
);
}
@@ -276,7 +263,6 @@ class CustomerStockNotificationEmail extends WC_Email {
public function prepare_email( Notification $notification ): void {
$this->object = $notification;
$this->recipient = $notification->get_user_email();
- $this->unsubscribe_key = $notification->get_unsubscribe_key( false );
$product = $notification->get_product();
$this->placeholders['{product_name}'] = preg_replace( $this->plain_search, $this->plain_replace, $product->get_name() );
$this->placeholders['{site_title}'] = preg_replace( $this->plain_search, $this->plain_replace, $this->get_blogname() );
diff --git a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
index d98ce8fc371..0ab05908534 100644
--- a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
+++ b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
@@ -175,7 +175,9 @@ class CustomerStockNotificationVerifiedEmail extends WC_Email {
$user = $notification instanceof Notification && $notification->get_user_id() ? get_user_by( 'id', $notification->get_user_id() ) : false;
$is_guest = ! is_a( $user, 'WP_User' );
$account_link = '';
- if ( ! $is_guest && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
+ // Guests can only manage sign-ups after creating an account, so skip the link when registration is off.
+ $can_reach_account = ! $is_guest || 'yes' === get_option( 'woocommerce_enable_myaccount_registration' );
+ if ( $can_reach_account && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
$account_link = UtmHelper::add_email_utm_params( MyAccountEndpoint::get_endpoint_url() );
}
diff --git a/plugins/woocommerce/src/Internal/StockNotifications/Emails/EmailManager.php b/plugins/woocommerce/src/Internal/StockNotifications/Emails/EmailManager.php
index 2dde1a9840a..0098f64d185 100644
--- a/plugins/woocommerce/src/Internal/StockNotifications/Emails/EmailManager.php
+++ b/plugins/woocommerce/src/Internal/StockNotifications/Emails/EmailManager.php
@@ -228,7 +228,8 @@ class EmailManager {
margin-top: 20px;
color: <?php echo esc_attr( $text ); ?>;
}
- #notification__unsubscribe_link {
+ #notification__unsubscribe_link,
+ #notification__manage_link {
color: <?php echo esc_attr( $text ); ?>;
}
#notification__product__price .screen-reader-text {
diff --git a/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php b/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
index 9615d60bd38..b93f67b2c2f 100644
--- a/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
+++ b/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
@@ -58,17 +58,21 @@ do_action( 'woocommerce_email_header', $email_heading, $email );
esc_html_e( 'You have received this message because your e-mail address was used to sign up for stock notifications on our store.', 'woocommerce' );
echo ' ';
- // Retain the legacy link ID for email styles and template overrides.
- if ( ! $is_guest && $account_link ) {
- // translators: %1$s placeholder is the account link, %2$s placeholder is the link text.
- $account_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $account_link ), _x( 'click here', 'account management cta for stock notifications for existing customers', 'woocommerce' ) );
- // translators: %s placeholder is the text part from above.
- echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $account_link_tag ) );
- } else {
+ // The sign-up is still active, so guests, and anyone who can't reach their account, can cancel it in one click.
+ if ( $is_guest || ! $account_link ) {
// translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
$unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for guests', 'woocommerce' ) );
// translators: %s placeholder is the text part from above.
echo wp_kses_post( sprintf( __( 'To stop receiving these messages, %s to unsubscribe.', 'woocommerce' ), $unsubscribe_link_tag ) );
+ echo ' ';
+ }
+
+ if ( $account_link ) {
+ if ( $is_guest ) {
+ esc_html_e( 'Create an account or log in with this email address to manage your notifications.', 'woocommerce' );
+ echo ' ';
+ }
+ printf( '<a href="%1$s" id="notification__manage_link">%2$s</a>', esc_url( $account_link ), esc_html__( 'Manage notifications', 'woocommerce' ) );
}
?>
<br><br>
diff --git a/plugins/woocommerce/templates/emails/customer-stock-notification.php b/plugins/woocommerce/templates/emails/customer-stock-notification.php
index 6c8254d8908..53ac87a09e5 100644
--- a/plugins/woocommerce/templates/emails/customer-stock-notification.php
+++ b/plugins/woocommerce/templates/emails/customer-stock-notification.php
@@ -63,17 +63,12 @@ do_action( 'woocommerce_email_header', $email_heading, $email );
echo esc_html( __( 'You have received this message because your e-mail address was used to sign up for stock notifications on our store.', 'woocommerce' ) );
echo ' ';
- // Retain the legacy link ID for email styles and template overrides.
- if ( ! $is_guest && $account_link ) {
- // translators: %1$s placeholder is the account link, %2$s placeholder is the link text.
- $account_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $account_link ), _x( 'click here', 'account management cta for stock notifications for existing customers', 'woocommerce' ) );
- // translators: %s placeholder is the text part from above.
- echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $account_link_tag ) );
- } else {
- // translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
- $unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for guests', 'woocommerce' ) );
- // translators: %s placeholder is the text part from above.
- echo wp_kses_post( sprintf( __( 'To stop receiving these messages, %s to unsubscribe.', 'woocommerce' ), $unsubscribe_link_tag ) );
+ if ( $account_link ) {
+ if ( $is_guest ) {
+ esc_html_e( 'Create an account or log in with this email address to manage your notifications.', 'woocommerce' );
+ echo ' ';
+ }
+ printf( '<a href="%1$s" id="notification__manage_link">%2$s</a>', esc_url( $account_link ), esc_html__( 'Manage notifications', 'woocommerce' ) );
}
?>
<br><br>
diff --git a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
index 1cf122abd65..6adfe5a642f 100644
--- a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
+++ b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
@@ -36,14 +36,20 @@ echo "\n\n----------------------------------------\n\n";
*/
do_action( 'woocommerce_email_stock_notification_product', $product, $notification, true, $email );
-echo "\n\n";
-if ( ! $is_guest && $account_link ) {
- // translators: %s: Account management URL.
- printf( esc_html__( 'To manage your notifications, log in to your account: %s', 'woocommerce' ), esc_url( $account_link ) );
-} else {
+$footer_lines = array();
+// The sign-up is still active, so guests, and anyone who can't reach their account, can cancel it in one click.
+if ( $is_guest || ! $account_link ) {
// translators: %s: Unsubscribe URL.
- printf( esc_html__( 'To stop receiving these messages, unsubscribe: %s', 'woocommerce' ), esc_url( $unsubscribe_link ) );
+ $footer_lines[] = sprintf( esc_html__( 'To stop receiving these messages, unsubscribe: %s', 'woocommerce' ), esc_url( $unsubscribe_link ) );
+}
+if ( $account_link ) {
+ if ( $is_guest ) {
+ $footer_lines[] = esc_html__( 'Create an account or log in with this email address to manage your notifications.', 'woocommerce' );
+ }
+ // translators: %s: Account management URL.
+ $footer_lines[] = sprintf( esc_html__( 'Manage notifications: %s', 'woocommerce' ), esc_url( $account_link ) );
}
+echo implode( "\n", $footer_lines ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Each line is escaped above.
echo "\n\n----------------------------------------\n\n";
/**
diff --git a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verify.php b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verify.php
index 0616d786828..3ed2fda8f74 100644
--- a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verify.php
+++ b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verify.php
@@ -12,7 +12,7 @@
*
* @see https://woocommerce.com/document/template-structure/
* @package WooCommerce\Templates\Emails\Plain
- * @version 10.2.0
+ * @version 11.4.0
*/
// Exit if accessed directly.
@@ -36,6 +36,13 @@ echo "\n\n----------------------------------------\n\n";
*/
do_action( 'woocommerce_email_stock_notification_product', $product, $notification, true, $email );
+// translators: %1$s: Confirm button text, %2$s: Confirm URL.
+printf( esc_html__( '%1$s: %2$s', 'woocommerce' ), esc_html( $verification_button_text ), esc_url( $verification_link ) );
+echo "\n";
+// translators: %s placeholder is the verification expiration datetime string.
+printf( esc_html__( 'This link will remain active for %s.', 'woocommerce' ), esc_html( $verification_expiration_threshold ) );
+echo "\n\n----------------------------------------\n\n";
+
/**
* Show user-defined additional content - this is set in each email's settings.
*/
diff --git a/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php b/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
index 3802abf04cf..86825302e39 100644
--- a/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
+++ b/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
@@ -36,13 +36,12 @@ echo "\n\n----------------------------------------\n\n";
*/
do_action( 'woocommerce_email_stock_notification_product', $product, $notification, true, $email );
-echo "\n\n";
-if ( ! $is_guest && $account_link ) {
+if ( $account_link ) {
+ if ( $is_guest ) {
+ echo esc_html__( 'Create an account or log in with this email address to manage your notifications.', 'woocommerce' ) . "\n";
+ }
// translators: %s: Account management URL.
- printf( esc_html__( 'To manage your notifications, log in to your account: %s', 'woocommerce' ), esc_url( $account_link ) );
-} else {
- // translators: %s: Unsubscribe URL.
- printf( esc_html__( 'To stop receiving these messages, unsubscribe: %s', 'woocommerce' ), esc_url( $unsubscribe_link ) );
+ printf( esc_html__( 'Manage notifications: %s', 'woocommerce' ), esc_url( $account_link ) );
}
echo "\n\n----------------------------------------\n\n";
diff --git a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
index 74a094859ae..75de2616053 100644
--- a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
@@ -155,8 +155,7 @@ test.describe(
'email'
);
- // A guest signup gets the unsubscribe wording in the footer, not
- // the account-management one.
+ // A guest can cancel the active signup in one click.
await openEmailInMailLog(
page,
email,
@@ -164,7 +163,7 @@ test.describe(
);
await expect(
bisEmailBody( page ).locator( 'body' )
- ).toContainText( BIS_EMAIL_FOOTER.guest );
+ ).toContainText( BIS_EMAIL_FOOTER.unsubscribe );
} );
test( 'following the unsubscribe link cancels the notification', async ( {
@@ -206,7 +205,56 @@ test.describe(
await expect( row.getByText( /Cancelled/i ) ).toBeVisible();
} );
- test( "a logged-in customer's confirmation email offers account management instead of unsubscribing", async ( {
+ test( 'an unsubscribe link with a tampered key changes nothing', async ( {
+ page,
+ product,
+ browser,
+ } ) => {
+ const email = uniqueGuestEmail( 'bis-confirm-bad-key' );
+
+ await signUpAsGuest( browser, product.permalink, email );
+
+ const verifyLink = await getEmailLinkById(
+ page,
+ email,
+ bisEmailSubject.verify( product.name ),
+ BIS_EMAIL_LINKS.actionButton
+ );
+ await page.goto( verifyLink );
+
+ const unsubscribeLink = await getEmailLinkById(
+ page,
+ email,
+ bisEmailSubject.verified( product.name ),
+ BIS_EMAIL_LINKS.unsubscribe
+ );
+
+ await page.goto( corruptEmailLinkKey( unsubscribeLink ) );
+
+ // A rejected key changes nothing, but the shopper is told why.
+ await expect(
+ page.getByText( bisNotice.unsubscribeLinkInvalid, {
+ exact: true,
+ } )
+ ).toBeVisible();
+
+ await page.goto( bisAdminListUrl( product.id ) );
+ const row = page
+ .getByRole( 'row' )
+ .filter( { has: page.getByText( email, { exact: true } ) } );
+ await expect( row ).toHaveCount( 1 );
+ await expect( row.getByText( /Cancelled/i ) ).toHaveCount( 0 );
+
+ // Positive control: the untouched link is accepted, so the page
+ // above rejected the link because of the key and not because the
+ // link is broken.
+ await page.goto( unsubscribeLink );
+ await expect(
+ page.getByText( bisNotice.unsubscribed( email, product.name ) )
+ ).toBeVisible();
+ } );
+
+ test( "a logged-in customer's signed-up email offers account management instead of unsubscribing", async ( {
page,
product,
browser,
@@ -232,12 +280,14 @@ test.describe(
// The templates fork on whether the signup belongs to an account;
// every other email test signs up as a guest, so this is the only
// place the account branch renders.
- await expect( body ).toContainText( BIS_EMAIL_FOOTER.loggedIn );
- await expect( body ).not.toContainText( BIS_EMAIL_FOOTER.guest );
+ await expect( body ).toContainText( BIS_EMAIL_FOOTER.manage );
+ await expect( body ).not.toContainText(
+ BIS_EMAIL_FOOTER.unsubscribe
+ );
// Account management must not cancel the notification.
const accountLink = bisEmailBody( page ).locator(
- `a${ BIS_EMAIL_LINKS.unsubscribe }`
+ `a${ BIS_EMAIL_LINKS.manage }`
);
await expect( accountLink ).toHaveAttribute(
'href',
diff --git a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
index 21d82e0288d..2577a535a0e 100644
--- a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
@@ -8,11 +8,8 @@ import {
BIS_EMAIL_FOOTER,
BIS_EMAIL_LINKS,
BIS_FEATURE_OPTION,
- bisAdminListUrl,
bisEmailBody,
bisEmailSubject,
- bisNotice,
- corruptEmailLinkKey,
getEmailLinkById,
openEmailInMailLog,
resetBISOptions,
@@ -100,7 +97,7 @@ test.describe(
new URL( product.permalink ).toString()
);
- // A guest signup gets the unsubscribe wording in the footer.
+ // The notification is sent with this email, so there is nothing left to unsubscribe from.
await openEmailInMailLog(
page,
email,
@@ -108,10 +105,15 @@ test.describe(
);
await expect(
bisEmailBody( page ).locator( 'body' )
- ).toContainText( BIS_EMAIL_FOOTER.guest );
+ ).not.toContainText( BIS_EMAIL_FOOTER.unsubscribe );
+ await expect(
+ bisEmailBody( page ).locator(
+ 'a[href*="email_link_action=unsubscribe"]'
+ )
+ ).toHaveCount( 0 );
} );
- test( "a logged-in customer's back-in-stock email offers account management instead of unsubscribing", async ( {
+ test( "a logged-in customer's back-in-stock email links to account management", async ( {
page,
product,
restApi,
@@ -130,14 +132,14 @@ test.describe(
const body = bisEmailBody( page ).locator( 'body' );
- // The template forks on whether the signup belongs to an account;
- // the guest fork is what every other email test exercises.
- await expect( body ).toContainText( BIS_EMAIL_FOOTER.loggedIn );
- await expect( body ).not.toContainText( BIS_EMAIL_FOOTER.guest );
+ await expect( body ).toContainText( BIS_EMAIL_FOOTER.manage );
+ await expect( body ).not.toContainText(
+ BIS_EMAIL_FOOTER.unsubscribe
+ );
// Account management must not cancel the notification.
const accountLink = bisEmailBody( page ).locator(
- `a${ BIS_EMAIL_LINKS.unsubscribe }`
+ `a${ BIS_EMAIL_LINKS.manage }`
);
await expect( accountLink ).toHaveAttribute(
'href',
@@ -148,95 +150,5 @@ test.describe(
/email_link_action=unsubscribe/
);
} );
-
- test( 'unsubscribe link in the back-in-stock email leaves the sent notification unchanged', async ( {
- page,
- product,
- restApi,
- browser,
- } ) => {
- const email = uniqueGuestEmail( 'bis-restock-unsub' );
-
- await signUpAsGuest( browser, product.permalink, email );
-
- await restockProduct( restApi, product.id );
- await triggerStockNotificationsBatch( page );
-
- await expectEmail(
- page,
- email,
- bisEmailSubject.backInStock( product.name )
- );
-
- const unsubscribeLink = await getEmailLinkById(
- page,
- email,
- bisEmailSubject.backInStock( product.name ),
- BIS_EMAIL_LINKS.unsubscribe
- );
- await page.goto( unsubscribeLink );
-
- // The shopper sees the outcome on the shop page they are sent to.
- await expect(
- page.getByText( bisNotice.alreadySent, { exact: true } )
- ).toBeVisible();
-
- await page.goto( bisAdminListUrl( product.id ) );
- const row = page
- .getByRole( 'row' )
- .filter( { has: page.getByText( email, { exact: true } ) } );
- await expect(
- row.getByText( 'Sent', { exact: true } )
- ).toBeVisible();
- await expect( row.getByText( /Cancelled/i ) ).toHaveCount( 0 );
- } );
-
- test( 'an unsubscribe link with a tampered key changes nothing', async ( {
- page,
- product,
- restApi,
- browser,
- } ) => {
- const email = uniqueGuestEmail( 'bis-restock-bad-key' );
-
- await signUpAsGuest( browser, product.permalink, email );
-
- await restockProduct( restApi, product.id );
- await triggerStockNotificationsBatch( page );
-
- const unsubscribeLink = await getEmailLinkById(
- page,
- email,
- bisEmailSubject.backInStock( product.name ),
- BIS_EMAIL_LINKS.unsubscribe
- );
-
- await page.goto( corruptEmailLinkKey( unsubscribeLink ) );
-
- // A rejected key changes nothing, but the shopper is told why.
- await expect(
- page.getByText( bisNotice.unsubscribeLinkInvalid, {
- exact: true,
- } )
- ).toBeVisible();
- await expect(
- page.getByText( bisNotice.alreadySent, { exact: true } )
- ).toHaveCount( 0 );
-
- await page.goto( bisAdminListUrl( product.id ) );
- const row = page
- .getByRole( 'row' )
- .filter( { has: page.getByText( email, { exact: true } ) } );
- await expect( row ).toHaveCount( 1 );
- await expect( row.getByText( /Cancelled/i ) ).toHaveCount( 0 );
-
- // Positive control: the untouched link is accepted, so the page
- // above rejected the link because of the key and not because the
- // link is broken.
- await page.goto( unsubscribeLink );
- await expect(
- page.getByText( bisNotice.alreadySent, { exact: true } )
- ).toBeVisible();
- } );
}
);
diff --git a/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts b/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
index 0f917815a9b..bd8febaaa2e 100644
--- a/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
+++ b/plugins/woocommerce/tests/e2e/utils/back-in-stock-notifications.ts
@@ -656,15 +656,17 @@ export function corruptEmailLinkKey( link: string ): string {
}
/**
- * Text the email footer renders for a signup bound to an account.
+ * Footer text in the signed-up and back-in-stock emails.
+ *
+ * `unsubscribe` only renders in the signed-up email, for guests or when the account page can't be reached.
*
* @see templates/emails/customer-stock-notification.php
* @see templates/emails/customer-stock-notification-verified.php
*/
export const BIS_EMAIL_FOOTER = {
- loggedIn:
- /To manage your notifications, click here to log in to your account\./,
- guest: /To stop receiving these messages, click here to unsubscribe\./,
+ manage: /Manage notifications/,
+ unsubscribe:
+ /To stop receiving these messages, click here to unsubscribe\./,
} as const;
/**
@@ -798,6 +800,7 @@ export const BIS_EMAIL_LINKS = {
// Product CTA in the back-in-stock email; verification link in the verify email.
actionButton: '#notification__action_button',
unsubscribe: '#notification__unsubscribe_link',
+ manage: '#notification__manage_link',
} as const;
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php b/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
index 61995a39624..d79ffa705f6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
@@ -3,6 +3,7 @@
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\StockNotifications\Emails;
+use Automattic\WooCommerce\Internal\StockNotifications\Config;
use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailManager;
use Automattic\WooCommerce\Internal\StockNotifications\Notification;
use Automattic\WooCommerce\Internal\StockNotifications\Frontend\MyAccountEndpoint;
@@ -305,9 +306,9 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
}
/**
- * @testdox Should not save the notification while sending the stock email, leaving the unsubscribe key to the caller's save.
+ * @testdox Should not save the notification or create an unsubscribe key while sending the stock email.
*/
- public function test_send_stock_notification_email_leaves_saving_the_unsubscribe_key_to_the_caller() {
+ public function test_send_stock_notification_email_does_not_save_or_link_to_unsubscribe() {
$notification = $this->build_notification();
$updates = 0;
$count = function () use ( &$updates ) {
@@ -320,11 +321,8 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
remove_action( 'woocommerce_update_stock_notification', $count );
$this->assertSame( 0, $updates );
$this->assertCount( 1, $this->sent_messages );
- $this->assertSame( 1, preg_match( '/email_link_action_key=([A-Za-z0-9]+)/', $this->sent_messages[0], $matches ) );
-
- $notification->save();
- $notification = new Notification( $notification->get_id() );
- $this->assertTrue( $notification->check_unsubscribe_key( $matches[1] ) );
+ $this->assertStringNotContainsString( 'email_link_action=unsubscribe', $this->sent_messages[0] );
+ $this->assertEmpty( $notification->get_meta( 'unsubscribe_action_key' ), 'The stock email must not create an unsubscribe key.' );
}
/**
@@ -352,42 +350,53 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
*/
public function provider_email_send_methods(): array {
return array(
- 'stock notification' => array( 'send_stock_notification_email' ),
- 'verify' => array( 'send_verify_email' ),
- 'verified' => array( 'send_verified_email' ),
+ 'verify' => array( 'send_verify_email' ),
+ 'verified' => array( 'send_verified_email' ),
);
}
/**
- * @testdox Should link registered recipients to account management and preserve guest unsubscribe links.
- * @testWith [false, false, "stock-notifications"]
- * [true, false, "stock-notifications"]
- * [false, false, "restock-alerts"]
- * [true, false, "restock-alerts"]
- * [false, true, "stock-notifications"]
- * [true, true, "stock-notifications"]
- * [false, false, ""]
- * [true, false, ""]
- * [false, true, ""]
- * [true, true, ""]
- * [false, false, "stock-notifications", true]
- * [true, false, "stock-notifications", true]
- * [false, false, "restock-alerts", true]
- * [true, false, "restock-alerts", true]
- * [false, true, "stock-notifications", true]
- * [true, true, "stock-notifications", true]
- * [false, false, "", true]
- * [true, false, "", true]
- * [false, true, "", true]
- * [true, true, "", true]
+ * @testdox Should show the manage link when the account page is reachable, and unsubscribe only in the signed-up email for guests or when it isn't.
+ * @testWith [false, false, "stock-notifications", false, false, true, false]
+ * [false, false, "restock-alerts", false, false, true, false]
+ * [false, false, "", false, false, false, false]
+ * [false, true, "stock-notifications", false, false, false, false]
+ * [false, true, "", false, false, false, false]
+ * [false, true, "stock-notifications", false, true, true, false]
+ * [false, true, "", false, true, false, false]
+ * [true, false, "stock-notifications", false, false, true, false]
+ * [true, false, "restock-alerts", false, false, true, false]
+ * [true, false, "", false, false, false, true]
+ * [true, true, "stock-notifications", false, false, false, true]
+ * [true, true, "", false, false, false, true]
+ * [true, true, "stock-notifications", false, true, true, true]
+ * [true, true, "", false, true, false, true]
+ * [false, false, "stock-notifications", true, false, true, false]
+ * [false, false, "restock-alerts", true, false, true, false]
+ * [false, false, "", true, false, false, false]
+ * [false, true, "stock-notifications", true, false, false, false]
+ * [false, true, "", true, false, false, false]
+ * [false, true, "stock-notifications", true, true, true, false]
+ * [false, true, "", true, true, false, false]
+ * [true, false, "stock-notifications", true, false, true, false]
+ * [true, false, "restock-alerts", true, false, true, false]
+ * [true, false, "", true, false, false, true]
+ * [true, true, "stock-notifications", true, false, false, true]
+ * [true, true, "", true, false, false, true]
+ * [true, true, "stock-notifications", true, true, true, true]
+ * [true, true, "", true, true, false, true]
*
- * @param bool $verified Whether to render the confirmation email.
- * @param bool $guest Whether the signup belongs to a guest.
- * @param string $endpoint The configured account endpoint.
- * @param bool $plain_text Whether to render the plain-text email instead of HTML.
+ * @param bool $verified Whether to render the signed-up email instead of the back-in-stock email.
+ * @param bool $guest Whether the signup belongs to a guest.
+ * @param string $endpoint The configured account endpoint.
+ * @param bool $plain_text Whether to render the plain-text email instead of HTML.
+ * @param bool $registration Whether customers can create an account on the My Account page.
+ * @param bool $expect_manage Whether the email should link to the notifications page.
+ * @param bool $expect_unsubscribe Whether the email should have an unsubscribe link.
*/
- public function test_email_footer_links( bool $verified, bool $guest, string $endpoint, bool $plain_text = false ): void {
+ public function test_email_footer_links( bool $verified, bool $guest, string $endpoint, bool $plain_text, bool $registration, bool $expect_manage, bool $expect_unsubscribe ): void {
update_option( MyAccountEndpoint::ENDPOINT_OPTION, $endpoint );
+ update_option( 'woocommerce_enable_myaccount_registration', $registration ? 'yes' : 'no' );
new MyAccountEndpoint();
WC()->query->init_query_vars();
$notification = $this->build_notification();
@@ -401,28 +410,69 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
$sut->prepare_email( $notification );
$notification->save();
$content = $plain_text ? $sut->get_content_plain() : $sut->get_content_html();
- $pattern = $plain_text ? '/^.*: (https?:\/\/\S+)$/m' : '/href="([^"]+)" id="notification__unsubscribe_link"/';
- $this->assertSame( 1, preg_match( $pattern, $content, $matches ), 'The footer must contain its management link.' );
- $link = html_entity_decode( $matches[1], ENT_QUOTES );
- parse_str( (string) wp_parse_url( $link, PHP_URL_QUERY ), $query );
-
- $this->assertSame( 'back-in-stock-notifications', $query['utm_source'] );
- $this->assertSame( 'email', $query['utm_medium'] );
- if ( $guest || '' === $endpoint ) {
- $this->assertSame( 'unsubscribe', $query['email_link_action'] );
+ $prompt = 'Create an account or log in with this email address to manage your notifications.';
+
+ $manage_links = $this->get_links( $content, '/(?<!back-in-)stock-notifications|restock-alerts/' );
+ if ( $expect_manage ) {
+ $this->assertCount( 1, $manage_links, 'The email must link to the notifications page once.' );
+ parse_str( (string) wp_parse_url( $manage_links[0], PHP_URL_QUERY ), $query );
+ $this->assertSame( wc_get_account_endpoint_url( $endpoint ), remove_query_arg( array( 'utm_source', 'utm_medium' ), $manage_links[0] ) );
+ $this->assertSame( 'back-in-stock-notifications', $query['utm_source'] );
+ $this->assertSame( 'email', $query['utm_medium'] );
+ $this->assertStringContainsString( 'Manage notifications', $content );
+ } else {
+ $this->assertSame( array(), $manage_links, 'The email must not link to the notifications page.' );
+ $this->assertStringNotContainsString( 'Manage notifications', $content );
+ }
+ if ( $guest && $expect_manage ) {
+ $this->assertStringContainsString( $prompt, $content );
+ } else {
+ $this->assertStringNotContainsString( $prompt, $content );
+ }
+
+ $unsubscribe_links = $this->get_links( $content, '/email_link_action=unsubscribe/' );
+ if ( $expect_unsubscribe ) {
+ $this->assertCount( 1, $unsubscribe_links, 'The email must have one unsubscribe link.' );
+ parse_str( (string) wp_parse_url( $unsubscribe_links[0], PHP_URL_QUERY ), $query );
$this->assertTrue( ( new Notification( $notification->get_id() ) )->check_unsubscribe_key( $query['email_link_action_key'] ), 'The unsubscribe link must contain the persisted unsubscribe key.' );
$this->assertSame( (string) $notification->get_id(), $query['notification_id'] );
- $this->assertStringContainsString( 'To stop receiving these messages,', $content );
- $this->assertStringNotContainsString( 'To manage your notifications,', $content );
} else {
- $this->assertSame( wc_get_account_endpoint_url( $endpoint ), remove_query_arg( array( 'utm_source', 'utm_medium' ), $link ) );
- $this->assertArrayNotHasKey( 'email_link_action', $query );
- $this->assertArrayNotHasKey( 'email_link_action_key', $query );
- $this->assertStringContainsString( 'To manage your notifications,', $content );
+ $this->assertSame( array(), $unsubscribe_links, 'The email must not have an unsubscribe link.' );
$this->assertStringNotContainsString( 'To stop receiving these messages,', $content );
}
}
+ /**
+ * Find the URLs in an HTML or plain-text email that match a pattern.
+ *
+ * @param string $content The email content.
+ * @param string $pattern Regex the decoded URL must match.
+ * @return string[] The matching, decoded URLs.
+ */
+ private function get_links( string $content, string $pattern ): array {
+ preg_match_all( '#https?://[^\s"<]+#', $content, $matches );
+ $urls = array_map(
+ fn( $url ) => html_entity_decode( $url, ENT_QUOTES ),
+ $matches[0]
+ );
+ return array_values( array_filter( $urls, fn( $url ) => 1 === preg_match( $pattern, $url ) ) );
+ }
+
+ /**
+ * @testdox Should include the confirm link and how long it stays active in the plain-text verify email.
+ */
+ public function test_plain_verify_email_includes_confirm_link_and_expiration(): void {
+ $notification = $this->build_notification();
+ $sut = WC()->mailer()->get_emails()['WC_Email_Customer_Stock_Notification_Verify'];
+ $sut->prepare_email( $notification );
+
+ $content = $sut->get_content_plain();
+
+ $this->assertMatchesRegularExpression( '/^Confirm: https?:\/\/\S*email_link_action=verify\S*$/m', html_entity_decode( $content, ENT_QUOTES ) );
+ $minutes = (int) floor( Config::get_verification_expiration_time_threshold() / 60 );
+ $this->assertStringContainsString( "This link will remain active for {$minutes} minutes.", $content );
+ }
+
/**
* Build a persisted notification for an in-stock simple product.
*