Commit a4683a731b8 for woocommerce

commit a4683a731b8f813be3fbaaa58d22beb6e14b6e06
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Thu Nov 26 14:12:19 2026 -0300

    Remove stage_delivery_ping() so no webhook stage method sends a request (#71479)

    * Remove stage_delivery_ping() so no webhook stage method sends a request

    A stage method must only change the object in memory, but
    stage_delivery_ping() sent the ping during the stage call. deliver_ping()
    now sends the ping and saves, as it did in 11.3.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Add changelog entry for the webhook ping stage method removal

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/wooai-1854-webhook-stage-ping b/plugins/woocommerce/changelog/wooai-1854-webhook-stage-ping
new file mode 100644
index 00000000000..689fbeb76df
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1854-webhook-stage-ping
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Stop an experimental webhook stage method from sending the ping; the saving path is unchanged.
+
diff --git a/plugins/woocommerce/includes/class-wc-webhook.php b/plugins/woocommerce/includes/class-wc-webhook.php
index f436f74ed10..ce5cd47b6e8 100644
--- a/plugins/woocommerce/includes/class-wc-webhook.php
+++ b/plugins/woocommerce/includes/class-wc-webhook.php
@@ -784,33 +784,9 @@ class WC_Webhook extends WC_Legacy_Webhook {
 	 * Send a test ping to the delivery URL, sent when the webhook is first created.
 	 *
 	 * @since  2.2.0
-	 * @uses   self::stage_delivery_ping()
 	 * @return bool|WP_Error
 	 */
 	public function deliver_ping() {
-		$result = $this->write_through(
-			function () {
-				return $this->stage_delivery_ping();
-			}
-		);
-
-		if ( true === $result ) {
-			$this->save();
-		}
-
-		return $result;
-	}
-
-	/**
-	 * Send a test ping to the delivery URL and, when it succeeds, clear the pending delivery flag in memory.
-	 * The next save() writes it. deliver_ping() calls this method, then save().
-	 *
-	 * @since  11.4.0
-	 * @return bool|WP_Error True when the ping succeeded, an error otherwise.
-	 */
-	public function stage_delivery_ping() {
-		$this->refuse_unstaged_override( 'deliver_ping', 'stage_delivery_ping' );
-
 		$args = array(
 			'user-agent' => sprintf( 'WooCommerce/%s Hookshot (WordPress/%s)', Constants::get_constant( 'WC_VERSION' ), $GLOBALS['wp_version'] ),
 			'body'       => 'webhook_id=' . $this->get_id(),
@@ -841,6 +817,7 @@ class WC_Webhook extends WC_Legacy_Webhook {
 		}

 		$this->set_pending_delivery( false );
+		$this->save();

 		return true;
 	}
diff --git a/plugins/woocommerce/phpstan.neon b/plugins/woocommerce/phpstan.neon
index d27817cdf57..5c6e8f8316b 100644
--- a/plugins/woocommerce/phpstan.neon
+++ b/plugins/woocommerce/phpstan.neon
@@ -90,6 +90,11 @@ parameters:
 			identifier: woocommerce.stagedWrite
 			message: '#^Method WC_Shipping_Zone::add_shipping_method\(\) writes outside save\(\)\.#'
 			path: includes/class-wc-shipping-zone.php
+		# The ping is an HTTP request to the delivery URL. It cannot wait for a later save(), so it has no stage_*() twin.
+		-
+			identifier: woocommerce.stagedWrite
+			message: '#^Method WC_Webhook::deliver_ping\(\) writes outside save\(\)\.#'
+			path: includes/class-wc-webhook.php
 		# WP_Ability::prepare_input() and sanitize_input() exist from WordPress 7.2, and Ability calls them only when they exist. Remove when WooCommerce requires WordPress 7.2.
 		-
 			identifier: staticMethod.notFound
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-webhook-tests.php b/plugins/woocommerce/tests/php/includes/class-wc-webhook-tests.php
index beb0aafacad..f32f5c9ee49 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-webhook-tests.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-webhook-tests.php
@@ -744,15 +744,19 @@ class WC_Webhook_Test extends WC_Unit_Test_Case {
 		$webhook = $this->create_active_webhook( 'product.updated' );
 		$webhook->save();

+		$requests = 0;
 		add_filter(
 			'pre_http_request',
-			function () use ( $response_code ) {
+			function () use ( $response_code, &$requests ) {
+				++$requests;
 				return $this->mock_http_response( $response_code );
 			}
 		);

 		$result = $webhook->deliver_ping();

+		$this->assertSame( 1, $requests, 'The ping should send one request, and its save should not ping again.' );
+
 		if ( $expect_success ) {
 			$this->assertTrue( $result, "A ping answered with {$response_code} should succeed." );
 		} else {
@@ -1433,17 +1437,14 @@ class WC_Webhook_Test extends WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox A class that overrides the saving delivery methods keeps working through them, and the stage methods refuse it.
+	 * @testdox A class that overrides log_delivery() keeps working through it, and stage_delivery_result() refuses it.
 	 */
-	public function test_stage_delivery_methods_refuse_a_class_that_has_not_adopted_them() {
+	public function test_stage_delivery_result_refuses_a_class_that_has_not_adopted_it() {
 		// phpcs:disable Squiz.Commenting.FunctionComment.Missing, Generic.CodeAnalysis.UselessOverridingMethod.Found
 		$webhook = new class() extends WC_Webhook {
 			public function log_delivery( $delivery_id, $request, $response, $duration ) {
 				parent::log_delivery( $delivery_id, $request, $response, $duration );
 			}
-			public function deliver_ping() {
-				return parent::deliver_ping();
-			}
 		};
 		// phpcs:enable Squiz.Commenting.FunctionComment.Missing, Generic.CodeAnalysis.UselessOverridingMethod.Found
 		$webhook->set_status( 'active' );
@@ -1473,29 +1474,38 @@ class WC_Webhook_Test extends WC_Unit_Test_Case {
 		$webhook->log_delivery( 'delivery', $request, $response, 0.1 );
 		$this->assertSame( 1, wc_get_webhook( $webhook->get_id() )->get_failure_count() );

-		$http = static fn() => array(
-			'response' => array(
-				'code'    => 200,
-				'message' => 'OK',
-			),
-			'headers'  => array(),
-			'body'     => '',
-		);
-		add_filter( 'pre_http_request', $http );
-		$this->assertTrue( $webhook->deliver_ping() );
-		remove_filter( 'pre_http_request', $http );
+		try {
+			$webhook->stage_delivery_result( 500 );
+			$this->fail( 'stage_delivery_result() did not refuse the class.' );
+		} catch ( WC_Data_Exception $e ) {
+			$this->assertSame( 'woocommerce_data_staging_unsupported', $e->getErrorCode() );
+			$this->assertStringContainsString( 'stage_delivery_result()', $e->getMessage() );
+		}
+	}

-		$calls = array(
-			'stage_delivery_result' => array( 500 ),
-			'stage_delivery_ping'   => array(),
+	/**
+	 * @testdox No stage method of WC_Webhook sends an HTTP request.
+	 */
+	public function test_stage_methods_send_no_request(): void {
+		$webhook = $this->create_active_webhook( 'order.created' );
+		$webhook->save();
+
+		$requests = array();
+		add_filter(
+			'pre_http_request',
+			function ( $response, $args, $url ) use ( &$requests ) {
+				$requests[] = $url;
+				return $this->mock_http_response( 200 );
+			},
+			10,
+			3
 		);
-		foreach ( $calls as $stage_method => $args ) {
-			try {
-				$webhook->$stage_method( ...$args );
-				$this->fail( $stage_method . ' did not refuse the class.' );
-			} catch ( WC_Data_Exception $e ) {
-				$this->assertSame( 'woocommerce_data_staging_unsupported', $e->getErrorCode() );
-				$this->assertStringContainsString( $stage_method . '()', $e->getMessage() );
+
+		$args_by_method = array( 'stage_delivery_result' => array( 500 ) );
+		foreach ( get_class_methods( WC_Webhook::class ) as $method ) {
+			if ( 0 === strpos( $method, 'stage_' ) ) {
+				$webhook->$method( ...( $args_by_method[ $method ] ?? array() ) );
+				$this->assertSame( array(), $requests, "{$method}() must only change the webhook in memory." );
 			}
 		}
 	}