Commit 31563191fe8 for woocommerce

commit 31563191fe8fd4f3f16ca4ded616a054a0c5736a
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Fri Nov 20 15:22:29 2026 +0100

    Hide billing details on the Review Order page from non-owners (#71228)

    (cherry picked from commit 911bc2bc41b06ec8715b00d898a6b5a9265013f2)

    Co-authored-by: Taha Paksu <3295+tpaksu@users.noreply.github.com>

diff --git a/plugins/woocommerce/src/Internal/OrderReviews/Meta.php b/plugins/woocommerce/src/Internal/OrderReviews/Meta.php
index 2f93b03dd47..8d072892548 100644
--- a/plugins/woocommerce/src/Internal/OrderReviews/Meta.php
+++ b/plugins/woocommerce/src/Internal/OrderReviews/Meta.php
@@ -26,7 +26,8 @@ class Meta {

 	/**
 	 * Build the meta-line parts shown above the heading on both the form and
-	 * empty-state views (customer name, billing email, order #/date).
+	 * empty-state views: the order number and date, preceded by the customer's name
+	 * and billing email only when the viewer is signed in as that customer.
 	 *
 	 * @param WC_Order $order Order being reviewed.
 	 * @return array<int, string> Non-empty parts ready to be joined with a separator.
@@ -53,14 +54,28 @@ class Meta {
 			);
 		}

-		return array_values(
-			array_filter(
-				array(
-					$customer_name,
-					$customer_email,
-					$order_summary,
-				)
-			)
-		);
+		$parts = self::viewer_is_signed_in_owner( $order )
+			? array( $customer_name, $customer_email, $order_summary )
+			: array( $order_summary );
+
+		return array_values( array_filter( $parts ) );
+	}
+
+	/**
+	 * Whether the viewer is signed in as the customer who owns the order.
+	 *
+	 * The billing email is the second proof the Store API order route and the
+	 * order-received page ask for on top of the key, so the page must not print it to
+	 * everyone holding one.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param WC_Order $order Order being reviewed.
+	 * @return bool
+	 */
+	private static function viewer_is_signed_in_owner( WC_Order $order ): bool {
+		$customer_id = $order->get_customer_id();
+
+		return $customer_id > 0 && is_user_logged_in() && get_current_user_id() === $customer_id;
 	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Internal/OrderReviews/EndpointTest.php b/plugins/woocommerce/tests/php/src/Internal/OrderReviews/EndpointTest.php
index 5b51cce1dd9..290b94df64c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/OrderReviews/EndpointTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/OrderReviews/EndpointTest.php
@@ -327,6 +327,103 @@ class EndpointTest extends WC_Unit_Test_Case {
 		$this->assertStringContainsString( 'Order #' . $order->get_order_number(), $html );
 	}

+	/**
+	 * @testdox A visitor holding only the order key is not shown the customer's name or billing email.
+	 */
+	public function test_meta_line_hides_billing_details_from_key_only_visitor(): void {
+		$order = OrderHelper::create_order();
+		$order->set_customer_id( 0 );
+		$order->set_billing_first_name( 'Gina' );
+		$order->set_billing_last_name( 'Guest' );
+		$order->set_billing_email( 'gina.guest@example.test' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+		$_GET = array( 'key' => $order->get_order_key() );
+
+		$html = $this->render( $order->get_id() );
+
+		$this->assertStringNotContainsString(
+			'gina.guest@example.test',
+			$html,
+			'The billing email is the second proof the order routes ask for on top of the key.'
+		);
+		$this->assertStringNotContainsString( 'Gina Guest', $html );
+		$this->assertStringContainsString( 'Order #' . $order->get_order_number(), $html );
+	}
+
+	/**
+	 * @testdox The signed-in customer the order belongs to still sees their name and billing email.
+	 */
+	public function test_meta_line_shows_billing_details_to_signed_in_owner(): void {
+		$customer_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+
+		$order = OrderHelper::create_order();
+		$order->set_customer_id( $customer_id );
+		$order->set_billing_first_name( 'Carl' );
+		$order->set_billing_last_name( 'Customer' );
+		$order->set_billing_email( 'carl.customer@example.test' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+
+		wp_set_current_user( $customer_id );
+		$_GET = array( 'key' => $order->get_order_key() );
+
+		$html = $this->render( $order->get_id() );
+
+		$this->assertStringContainsString( 'carl.customer@example.test', $html );
+		$this->assertStringContainsString( 'Carl Customer', $html );
+	}
+
+	/**
+	 * @testdox A logged-out visitor with the key is not shown a registered customer's billing email when the sign-in requirement is waived.
+	 */
+	public function test_meta_line_hides_billing_details_from_logged_out_visitor_on_a_customer_order(): void {
+		$customer_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+
+		$order = OrderHelper::create_order();
+		$order->set_customer_id( $customer_id );
+		$order->set_billing_first_name( 'Carl' );
+		$order->set_billing_last_name( 'Customer' );
+		$order->set_billing_email( 'carl.customer@example.test' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+
+		wp_set_current_user( 0 );
+		$_GET = array( 'key' => $order->get_order_key() );
+
+		add_filter( 'woocommerce_review_order_verify_known_shoppers', '__return_false' );
+		$html = $this->render( $order->get_id() );
+		remove_filter( 'woocommerce_review_order_verify_known_shoppers', '__return_false' );
+
+		$this->assertStringNotContainsString(
+			'carl.customer@example.test',
+			$html,
+			'Owning the key is not the same as being signed in as the customer.'
+		);
+		$this->assertStringContainsString( 'Order #' . $order->get_order_number(), $html );
+	}
+
+	/**
+	 * @testdox A signed-in visitor who is not the customer is not shown a guest order's billing email.
+	 */
+	public function test_meta_line_hides_guest_billing_details_from_a_signed_in_stranger(): void {
+		$stranger_id = self::factory()->user->create( array( 'role' => 'customer' ) );
+
+		$order = OrderHelper::create_order();
+		$order->set_customer_id( 0 );
+		$order->set_billing_email( 'gina.guest@example.test' );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+
+		wp_set_current_user( $stranger_id );
+		$_GET = array( 'key' => $order->get_order_key() );
+
+		$html = $this->render( $order->get_id() );
+
+		$this->assertStringNotContainsString( 'gina.guest@example.test', $html );
+		$this->assertStringContainsString( 'Order #' . $order->get_order_number(), $html );
+	}
+
 	/**
 	 * @testdox render_shortcode() only renders on the managed page with a matching order key.
 	 */
@@ -726,6 +823,11 @@ class EndpointTest extends WC_Unit_Test_Case {
 		$this->assertStringContainsString( 'Order #' . $order->get_order_number(), $html );
 		$this->assertStringContainsString( 'Thank you for your reviews', $html );
 		$this->assertStringContainsString( 'Your feedback helps', $html );
+		$this->assertStringNotContainsString(
+			'thanks@example.test',
+			$html,
+			'The empty-state template withholds the billing email too.'
+		);
 	}

 	/**