Commit d9f6576ddb6 for woocommerce

commit d9f6576ddb6ac335dfebeb993a6b9ad87ec739a7
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Fri Nov 20 15:19:34 2026 +0100

    Keep a moderator's unapprove when a shopper edits the review (#71227)

    (cherry picked from commit 69d67a455340fb19c85ccbad9261176802135d2e)

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

diff --git a/plugins/woocommerce/src/Internal/OrderReviews/SubmissionHandler.php b/plugins/woocommerce/src/Internal/OrderReviews/SubmissionHandler.php
index 2d45ae99904..d365d2e236d 100644
--- a/plugins/woocommerce/src/Internal/OrderReviews/SubmissionHandler.php
+++ b/plugins/woocommerce/src/Internal/OrderReviews/SubmissionHandler.php
@@ -49,6 +49,23 @@ class SubmissionHandler {
 	 */
 	private const AUTO_REJECTED_META_KEY = '_wc_auto_rejected';

+	/**
+	 * Marks a review held by someone other than this handler, so a later edit cannot
+	 * approve it. An untagged hold came from the moderation checks and an edit may lift it.
+	 *
+	 * @since 11.4.0
+	 */
+	private const MODERATOR_HELD_META_KEY = '_wc_moderator_held';
+
+	/**
+	 * The review whose status this handler is changing, so the transition it causes is not
+	 * mistaken for a moderator's. Zero when idle. Scoped to one id so a callback that holds
+	 * a different review during the same call is still recorded.
+	 *
+	 * @var int
+	 */
+	private static int $changing_status_for = 0;
+
 	/**
 	 * Wire the AJAX endpoints.
 	 *
@@ -60,6 +77,7 @@ class SubmissionHandler {
 		add_action( 'wp_ajax_' . self::ACTION, array( $this, 'handle' ) );
 		add_action( 'wp_ajax_nopriv_' . self::ACTION, array( $this, 'handle' ) );
 		add_action( 'transition_comment_status', array( $this, 'clear_auto_rejected_flag' ), 10, 3 );
+		add_action( 'transition_comment_status', array( $this, 'record_moderator_hold' ), 10, 3 );
 	}

 	/**
@@ -84,6 +102,41 @@ class SubmissionHandler {
 		}
 	}

+	/**
+	 * Record a hold this handler did not apply, so a resubmission cannot approve it.
+	 *
+	 * Approving a pending review is a staff action. Holds the moderation checks applied
+	 * carry no tag and an edit may still lift them, which also leaves reviews held before
+	 * this tag existed working as they did.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param int|string  $new_status New comment status.
+	 * @param int|string  $old_status Old comment status.
+	 * @param \WP_Comment $comment    The comment whose status changed.
+	 */
+	public function record_moderator_hold( $new_status, $old_status, $comment ): void {
+		unset( $old_status );
+		if ( ! $comment instanceof \WP_Comment || 'review' !== $comment->comment_type ) {
+			return;
+		}
+
+		$comment_id = (int) $comment->comment_ID;
+		if ( self::$changing_status_for > 0 && $comment_id === self::$changing_status_for ) {
+			return;
+		}
+
+		if ( 'unapproved' === $new_status ) {
+			update_comment_meta( $comment_id, self::MODERATOR_HELD_META_KEY, 1 );
+			return;
+		}
+
+		// Guarded like the sibling method: delete_metadata() queries even on a cache hit.
+		if ( get_comment_meta( $comment_id, self::MODERATOR_HELD_META_KEY, true ) ) {
+			delete_comment_meta( $comment_id, self::MODERATOR_HELD_META_KEY );
+		}
+	}
+
 	/**
 	 * Entry point fired by `admin-ajax.php`.
 	 *
@@ -429,8 +482,22 @@ class SubmissionHandler {
 			'trash'      => 'trash',
 		);
 		$current_status = $status_vocab[ wp_get_comment_status( $existing_id ) ] ?? '';
+
+		// Hold back only the approval: a spam or trash verdict from the checks must still
+		// apply. The tag outlives those, so a rejected edit cannot launder the hold away
+		// and let the next clean one approve the row. Any staff action clears the tag.
+		if ( 'approve' === $target && get_comment_meta( $existing_id, self::MODERATOR_HELD_META_KEY, true ) ) {
+			$target = 'hold';
+		}
+
 		if ( $target !== $current_status ) {
-			wp_set_comment_status( $existing_id, $target );
+			$previously_changing       = self::$changing_status_for;
+			self::$changing_status_for = $existing_id;
+			try {
+				wp_set_comment_status( $existing_id, $target );
+			} finally {
+				self::$changing_status_for = $previously_changing;
+			}
 		}

 		if ( in_array( $approved, array( 'spam', 'trash' ), true ) ) {
diff --git a/plugins/woocommerce/tests/php/src/Internal/OrderReviews/SubmissionHandlerTest.php b/plugins/woocommerce/tests/php/src/Internal/OrderReviews/SubmissionHandlerTest.php
index a93717cc1d1..afb5532e736 100644
--- a/plugins/woocommerce/tests/php/src/Internal/OrderReviews/SubmissionHandlerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/OrderReviews/SubmissionHandlerTest.php
@@ -30,6 +30,13 @@ class SubmissionHandlerTest extends WC_Unit_Test_Case {
 		parent::setUp();
 		$this->comment_registration = get_option( 'comment_registration' );
 		update_option( 'woocommerce_feature_customer_review_request_enabled', 'yes' );
+
+		// The container wires this in production via init(); the test dispatches the
+		// handler directly, so register the moderation-transition hooks here to exercise
+		// the tag lifecycle. WP_UnitTestCase restores hooks on tearDown.
+		$handler = new SubmissionHandler();
+		add_action( 'transition_comment_status', array( $handler, 'clear_auto_rejected_flag' ), 10, 3 );
+		add_action( 'transition_comment_status', array( $handler, 'record_moderator_hold' ), 10, 3 );
 	}

 	/**
@@ -1636,6 +1643,206 @@ class SubmissionHandlerTest extends WC_Unit_Test_Case {
 		$this->assertSame( 1, $product->get_review_count() );
 	}

+	/**
+	 * @testdox An edit does not lift a hold a moderator set with Unapprove.
+	 */
+	public function test_edit_does_not_lift_a_moderators_unapprove(): void {
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		$first = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Never arrived. Scam shop.' );
+		$this->assertSame( 'ok', $first['status'] );
+
+		// What the Unapprove row action under Products > Reviews does.
+		wp_set_comment_status( (int) $first['comment_id'], 'hold' );
+
+		$edit = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Never arrived. Scam shop!' );
+
+		$this->assertSame( $first['comment_id'], $edit['comment_id'], 'The edit must still reuse the row.' );
+		$this->assertSame( 'pending_moderation', $edit['status'] );
+		$this->assertSame(
+			'unapproved',
+			wp_get_comment_status( (int) $first['comment_id'] ),
+			'Approving a pending review is a staff action; an edit must not do it.'
+		);
+		$this->assertStringContainsString(
+			'Scam shop!',
+			get_comment( (int) $first['comment_id'] )->comment_content,
+			'The edit itself still applies; only the status is held back.'
+		);
+	}
+
+	/**
+	 * @testdox A disallowed-keys verdict still applies to a review a moderator put on hold.
+	 */
+	public function test_disallowed_edit_of_a_moderator_held_review_is_still_rejected(): void {
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		$first = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Fine at first.' );
+		wp_set_comment_status( (int) $first['comment_id'], 'hold' );
+
+		update_option( 'disallowed_keys', 'casino-bonus' );
+		$this->submit_review( $built['order'], $product_id, $item_id, 5, 'Try my casino-bonus link.' );
+
+		$this->assertContains(
+			wp_get_comment_status( (int) $first['comment_id'] ),
+			array( 'trash', 'spam' ),
+			"A moderator's hold must not shield content the disallowed list rejects."
+		);
+	}
+
+	/**
+	 * @testdox A hold this handler applied is still lifted by a corrected edit.
+	 */
+	public function test_handlers_own_hold_stays_liftable(): void {
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		$this->submit_review( $built['order'], $product_id, $item_id, 5, 'Clean to start.' );
+
+		update_option( 'moderation_keys', 'hold-this' );
+		$held = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Now with hold-this in it.' );
+		$this->assertSame( 'pending_moderation', $held['status'] );
+
+		update_option( 'moderation_keys', '' );
+		$clean = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Clean again.' );
+
+		$this->assertSame( 'ok', $clean['status'], "The handler's own hold must not read as a moderator's." );
+	}
+
+	/**
+	 * @testdox An Unapprove is recorded on the review; a hold the checks applied is not.
+	 */
+	public function test_only_a_moderators_hold_is_recorded(): void {
+		$built = $this->make_order( 2 );
+
+		$approved = $this->submit_review( $built['order'], $built['product_ids'][0], $built['item_ids'][0], 5, 'Clean enough.' );
+		wp_set_comment_status( (int) $approved['comment_id'], 'hold' );
+
+		update_option( 'moderation_keys', 'hold-this' );
+		$auto = $this->submit_review( $built['order'], $built['product_ids'][1], $built['item_ids'][1], 5, 'Please hold-this one.' );
+
+		$this->assertNotEmpty(
+			get_comment_meta( (int) $approved['comment_id'], '_wc_moderator_held', true ),
+			"A moderator's Unapprove must be recorded."
+		);
+		$this->assertEmpty(
+			get_comment_meta( (int) $auto['comment_id'], '_wc_moderator_held', true ),
+			'An automatic hold must stay unrecorded so an edit can lift it.'
+		);
+	}
+
+	/**
+	 * @testdox An ordinary comment a moderator unapproves is left alone.
+	 */
+	public function test_moderator_hold_is_not_recorded_on_other_comment_types(): void {
+		$comment_id = (int) self::factory()->comment->create(
+			array(
+				'comment_post_ID'  => self::factory()->post->create(),
+				'comment_approved' => 1,
+			)
+		);
+
+		wp_set_comment_status( $comment_id, 'hold' );
+
+		$this->assertEmpty(
+			get_comment_meta( $comment_id, '_wc_moderator_held', true ),
+			'The listener runs for every comment on the site; it must only touch reviews.'
+		);
+	}
+
+	/**
+	 * @testdox A review held before this tag existed can still be lifted by an edit.
+	 */
+	public function test_untagged_hold_is_still_liftable(): void {
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		// Stand in for a row held on a site that upgraded into this change.
+		$comment_id = (int) wp_insert_comment(
+			array(
+				'comment_post_ID'      => $product_id,
+				'comment_author'       => 'Jane',
+				'comment_author_email' => $built['order']->get_billing_email(),
+				'comment_content'      => 'Held before the tag existed.',
+				'comment_type'         => 'review',
+				'comment_approved'     => 0,
+			)
+		);
+		add_comment_meta( $comment_id, 'rating', 4, true );
+		add_comment_meta( $comment_id, ItemEligibility::ORDER_META_KEY, (int) $built['order']->get_id(), true );
+		add_comment_meta( $comment_id, ItemEligibility::VARIATION_META_KEY, 0, true );
+
+		$edit = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Corrected text.' );
+
+		$this->assertSame( $comment_id, (int) $edit['comment_id'], 'The edit must reuse the existing row.' );
+		$this->assertSame( 'ok', $edit['status'], 'An untagged hold predates the tag and stays liftable.' );
+
+		// Staff acting on it afterwards is what records the hold for the future.
+		wp_set_comment_status( $comment_id, 'hold' );
+
+		$this->assertNotEmpty(
+			get_comment_meta( $comment_id, '_wc_moderator_held', true ),
+			'The next staff action records the hold, so a later edit cannot lift it.'
+		);
+	}
+
+	/**
+	 * @testdox A rejected edit does not launder away a moderator's hold.
+	 */
+	public function test_auto_rejected_detour_does_not_lift_a_moderators_unapprove(): void {
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		$first = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Fine at first.' );
+		wp_set_comment_status( (int) $first['comment_id'], 'hold' );
+
+		// A deliberately rejected edit moves the row out of hold and tags it auto-rejected.
+		update_option( 'disallowed_keys', 'casino-bonus' );
+		$this->submit_review( $built['order'], $product_id, $item_id, 5, 'Try my casino-bonus link.' );
+
+		// Clean text now reuses that rejected row, which must not come back approved.
+		update_option( 'disallowed_keys', '' );
+		$third = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Perfectly ordinary text.' );
+
+		$this->assertNotSame(
+			'approved',
+			wp_get_comment_status( (int) $first['comment_id'] ),
+			"A rejected edit must not clear the moderator's hold."
+		);
+		$this->assertSame( 'pending_moderation', $third['status'] );
+	}
+
+	/**
+	 * @testdox Once a moderator has acted on an automatically held review, a later edit cannot approve it.
+	 */
+	public function test_edit_does_not_lift_an_unapprove_that_followed_an_automatic_hold(): void {
+		update_option( 'moderation_keys', 'hold-this' );
+
+		$built      = $this->make_order( 1 );
+		$product_id = $built['product_ids'][0];
+		$item_id    = $built['item_ids'][0];
+
+		$held = $this->submit_review( $built['order'], $product_id, $item_id, 5, 'Please hold-this review.' );
+		$this->assertSame( 'pending_moderation', $held['status'] );
+
+		// The moderator approves it, then changes their mind. The second transition
+		// leaves a hold that is theirs, not one the checks applied.
+		wp_set_comment_status( (int) $held['comment_id'], 'approve' );
+		wp_set_comment_status( (int) $held['comment_id'], 'hold' );
+
+		$edit = $this->submit_review( $built['order'], $product_id, $item_id, 4, 'Clean text now.' );
+
+		$this->assertSame( 'pending_moderation', $edit['status'] );
+		$this->assertSame( 'unapproved', wp_get_comment_status( (int) $held['comment_id'] ) );
+	}
+
 	/**
 	 * @testdox Reviews for two variations in one submission are both included in the parent product's aggregates.
 	 */