Commit 54f8b78eed2 for woocommerce

commit 54f8b78eed2604a416b3dccd2f68c5f323c34b6f
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Mon Nov 23 11:57:40 2026 -0300

    Match the ability input backport to WordPress 7.2 prepare_input() (#70836)

    * Backport WP_Ability::prepare_input() in the ability base class

    WordPress 7.2 replaced coerce_input() with the public prepare_input()
    and the protected sanitize_input() (changeset 65370). Match those names,
    so the backport turns off on 7.2 and check_permissions() uses
    prepare_input().

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

    * Fix repeated ability input preparation (#70840)

    * Fix repeated ability input preparation

    Check permissions against the prepared input and avoid sanitizing twice on older WordPress. Cover non-idempotent normalization, sanitizer calls and preparation errors, and update direct permission checks to prepare first.

    * Clarify direct ability permission checks

    * Document ability input and MCP adapter migration

    * Name the WordPress 7.2 input check in the ability base class

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

    * Ask stores to update MCP Adapter older than 0.7.2

    MCP Adapter 0.7.2 prepares the ability input before it checks permissions.

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

    * Merge the ability input changelog entries into one

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

    * Say when to remove the WordPress 7.2 and MCP Adapter workarounds

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

    * Read the bundled MCP Adapter version when WP_MCP_VERSION is not defined

    A bundled adapter defines WP_MCP_VERSION only on rest_api_init, so admin
    pages did not show the update notice for it.

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

    * Tell bundled MCP Adapter users to update the plugin that includes it

    The notice now reads the class version only when another plugin already
    loaded the class, so an adapter that is only autoloadable does not count
    as active. When WP_MCP_VERSION is not defined, the adapter comes bundled
    in another plugin, and the notice asks to update that plugin instead.

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

    * Name the plugin that bundles an outdated MCP Adapter

    When the adapter class loads from a regular plugin, the notice names that
    plugin, so the store owner knows which plugin to update.

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

    * Note that a bundled adapter loaded only on rest_api_init gets no notice

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

    * Name the bundling plugin only from a single root plugin file

    get_plugins() for a plugin folder also returns headers from its
    subfolders and sorts them by name, so the first entry can be an
    example plugin instead of the plugin that bundles MCP Adapter.

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

    * Let store managers dismiss the outdated MCP Adapter notice

    A bundled adapter can only be updated by the plugin that includes it,
    so the notice would otherwise stay on every admin page until then.

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

    * Let WordPress 7.2 prepare the ability input and validate it after conversion

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

    * Say that a direct permission check must stop on a prepare_input() error

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

    * Link the MCP Adapter notice to the network Plugins screen

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

    * Skip the second input validation when conversion changes nothing

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

    * Call WP_Ability::prepare_input() directly instead of through reflection

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

    * Let WordPress 7.2 sanitize the ability input and keep the PHP 8.5 test quiet

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

    * Skip the conversion minimum test when WordPress prepares the input

    WordPress 7.2 owns input preparation, so the WooCommerce test has
    nothing to assert there.

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

    * Name the real reason for skipping the single validation test

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

    * Say when to remove the conversion minimum test

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
    Co-authored-by: Oussama Elgoumri <65380769+euvoor@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/update-ability-prepare-input b/plugins/woocommerce/changelog/update-ability-prepare-input
new file mode 100644
index 00000000000..0b3ae3fae02
--- /dev/null
+++ b/plugins/woocommerce/changelog/update-ability-prepare-input
@@ -0,0 +1,4 @@
+Significance: patch
+Type: update
+
+WooCommerce abilities over MCP now need MCP Adapter 0.7.2 or later. Stores on an older version see a notice to update it.
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index ef9aa6eade7..f0d12409c48 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -21,6 +21,7 @@ use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableControlle
 use Automattic\WooCommerce\Internal\DownloadPermissionsAdjuster;
 use Automattic\WooCommerce\Internal\Features\FeaturesController;
 use Automattic\WooCommerce\Internal\LegacyAssets\LegacySelect2UsageTracker;
+use Automattic\WooCommerce\Internal\MCP\McpAdapterVersionNotice;
 use Automattic\WooCommerce\Internal\MCP\McpEndpointRemoval;
 use Automattic\WooCommerce\Internal\Abilities\AbilitiesLoader;
 use Automattic\WooCommerce\Internal\Abilities\AbilitiesTracking;
@@ -415,6 +416,7 @@ final class WooCommerce {
 		$container->get( AddressProviderController::class );
 		$container->get( AbilitiesLoader::class );
 		$container->get( McpEndpointRemoval::class );
+		$container->get( McpAdapterVersionNotice::class );
 		$container->get( CouponCodeLookupInvalidator::class );
 		$container->get( ProductVersionStringInvalidator::class );
 		$container->get( OrdersVersionStringInvalidator::class );
diff --git a/plugins/woocommerce/phpstan.neon b/plugins/woocommerce/phpstan.neon
index 510ee841afc..d27817cdf57 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
+		# 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
+			message: '#^Call to an undefined static method WP_Ability::(prepare|sanitize)_input\(\)\.$#'
+			path: src/Internal/Abilities/Ability.php
 		# PHPStan has no WP-CLI stubs, so it cannot see WP_CLI or its helper functions. Remove once stubs are added.
 		-
 			message: '#unknown class WP_CLI|Function WP_CLI\\Utils\\\w+ not found#'
diff --git a/plugins/woocommerce/src/Internal/Abilities/Ability.php b/plugins/woocommerce/src/Internal/Abilities/Ability.php
index 4fe8f2ee107..2d3b5444056 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Ability.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Ability.php
@@ -12,10 +12,12 @@ defined( 'ABSPATH' ) || exit;
 /**
  * The base class of the WooCommerce abilities.
  *
- * WordPress 7.2 converts valid ability input to the schema types with
- * WP_Ability::coerce_input() before the callbacks run. This class backports
- * that method, so the callbacks get converted input on WordPress 7.0 and 7.1 too.
- * It also prepares the input for callers that call check_permissions() directly.
+ * WordPress 7.2 prepares the ability input with WP_Ability::prepare_input() before
+ * the callbacks run. This class backports that method, so the callbacks get converted
+ * input on WordPress 7.0 and 7.1 too. Direct permission checks must pass the result
+ * of prepare_input() to check_permissions(), and must return early when that result
+ * is a WP_Error, because check_permissions() hands it to the permission callback.
+ * Pass raw input to execute(), which prepares it before checking permissions.
  *
  * @since 11.4.0
  */
@@ -27,8 +29,8 @@ class Ability extends \WP_Ability {
 	 *
 	 * Invalid input stays unchanged, so validate_input() refuses it as before.
 	 *
-	 * Remove this override, coerce_input() and get_input_error() when WooCommerce
-	 * requires WordPress 7.2, because WP_Ability::execute() converts the input there.
+	 * Remove when WooCommerce requires WordPress 7.2, together with prepare_input(),
+	 * sanitize_input(), core_prepares_input() and get_input_error().
 	 *
 	 * @since 11.4.0
 	 *
@@ -37,56 +39,56 @@ class Ability extends \WP_Ability {
 	 */
 	final public function normalize_input( $input = null ) {
 		$input = parent::normalize_input( $input );
-		if ( method_exists( \WP_Ability::class, 'coerce_input' ) || null === $input || is_wp_error( $input ) || is_wp_error( $this->validate_input( $input ) ) ) {
+		if ( self::core_prepares_input() || null === $input || is_wp_error( $input ) || is_wp_error( $this->validate_input( $input ) ) ) {
 			return $input;
 		}

-		return $this->coerce_input( $input );
+		return $this->sanitize_input( $input );
 	}

 	/**
-	 * Apply the schema default, validate and convert the input, then check the permissions.
+	 * Normalize, validate and convert the input, the same way WordPress 7.2 does.
 	 *
-	 * Some callers, such as older MCP adapter versions, call this method directly with
-	 * raw input. The permission callback then gets the same input as through execute().
-	 * Input that is already prepared passes through unchanged.
+	 * WordPress 7.2 and later handle this in WP_Ability::prepare_input().
 	 *
 	 * @since 11.4.0
 	 *
 	 * @param mixed $input Optional. The raw input provided for the ability. Default `null`.
-	 * @return bool|\WP_Error Whether the ability has the necessary permission, or the input error.
+	 * @return mixed|\WP_Error The prepared input, or a WP_Error when any step fails.
 	 */
-	final public function check_permissions( $input = null ) {
-		$input = $this->normalize_input( $input );
-		if ( is_wp_error( $input ) ) {
-			return $input;
+	public function prepare_input( $input = null ) {
+		if ( self::core_prepares_input() ) {
+			return parent::prepare_input( $input );
 		}

-		$is_valid = $this->validate_input( $input );
-		if ( is_wp_error( $is_valid ) ) {
-			return $is_valid;
-		}
-
-		$input = $this->coerce_input( $input );
+		$input = $this->normalize_input( $input );
 		if ( is_wp_error( $input ) ) {
 			return $input;
 		}

-		return parent::check_permissions( $input );
+		// This class's normalize_input() override already sanitizes the input.
+		$is_valid = $this->validate_input( $input );
+		return is_wp_error( $is_valid ) ? $is_valid : $input;
 	}

 	/**
 	 * Convert validated input to the schema types, the same way WordPress 7.2 does.
 	 *
-	 * Converting can fail on input that validated, such as items that are unique
-	 * as strings but not as integers. The input is then refused.
+	 * Converting can make validated input invalid, such as items that are unique
+	 * as strings but not as integers, or a number too large for an integer that
+	 * becomes 0 and breaks a minimum. The converted input is validated again and
+	 * refused when it fails.
 	 *
 	 * @since 11.4.0
 	 *
 	 * @param mixed $input Validated ability input.
 	 * @return mixed|\WP_Error The converted input, or a WP_Error when it cannot be converted.
 	 */
-	public function coerce_input( $input ) {
+	protected function sanitize_input( $input ) {
+		if ( self::core_prepares_input() ) {
+			return parent::sanitize_input( $input );
+		}
+
 		$schema = $this->get_input_schema();
 		if ( null === $input || empty( $schema ) ) {
 			return $input;
@@ -107,7 +109,21 @@ class Ability extends \WP_Ability {
 			);
 		}

-		return $sanitized;
+		if ( $sanitized === $input ) {
+			return $sanitized;
+		}
+
+		$is_valid = $this->validate_input( $sanitized );
+		return is_wp_error( $is_valid ) ? $is_valid : $sanitized;
+	}
+
+	/**
+	 * Check whether WordPress prepares the ability input itself, as WordPress 7.2 and later do.
+	 *
+	 * @return bool
+	 */
+	private static function core_prepares_input(): bool {
+		return method_exists( \WP_Ability::class, 'prepare_input' );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/MCP/McpAdapterVersionNotice.php b/plugins/woocommerce/src/Internal/MCP/McpAdapterVersionNotice.php
new file mode 100644
index 00000000000..771bea4cff3
--- /dev/null
+++ b/plugins/woocommerce/src/Internal/MCP/McpAdapterVersionNotice.php
@@ -0,0 +1,156 @@
+<?php
+/**
+ * McpAdapterVersionNotice class file.
+ */
+
+declare( strict_types=1 );
+
+namespace Automattic\WooCommerce\Internal\MCP;
+
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * Tells store managers to update the MCP Adapter plugin when its version cannot run WooCommerce abilities correctly.
+ *
+ * MCP Adapter 0.7.2 prepares the ability input before it checks permissions. Older versions pass raw input
+ * to the permission callbacks of WooCommerce abilities.
+ *
+ * Remove when MCP Adapter requires WordPress 7.2.
+ *
+ * @since 11.4.0
+ */
+class McpAdapterVersionNotice {
+
+	/**
+	 * The lowest MCP Adapter version that WooCommerce abilities support.
+	 *
+	 * @var string
+	 */
+	public const MIN_VERSION = '0.7.2';
+
+	/**
+	 * Name of the admin notice, used for dismissal.
+	 *
+	 * @var string
+	 */
+	public const NOTICE = 'mcp_adapter_outdated';
+
+	/**
+	 * Constructor.
+	 */
+	public function __construct() {
+		add_action( 'admin_notices', array( $this, 'handle_admin_notices' ) );
+	}
+
+	/**
+	 * Handle the admin_notices hook.
+	 *
+	 * @internal
+	 */
+	public function handle_admin_notices(): void {
+		$version = defined( 'WP_MCP_VERSION' ) ? (string) WP_MCP_VERSION : null;
+		$bundled = null === $version;
+		// A plugin that loads a bundled adapter only on rest_api_init gets no notice, because admin pages never load the class.
+		if ( $bundled && class_exists( 'WP\MCP\Core\McpAdapter', false ) ) {
+			$version = (string) constant( 'WP\MCP\Core\McpAdapter::VERSION' );
+		}
+		if ( ! self::is_outdated( $version ) || ! current_user_can( 'manage_woocommerce' ) || \WC_Admin_Notices::user_has_dismissed_notice( self::NOTICE ) ) {
+			return;
+		}
+
+		$bundled_by = $bundled ? self::get_bundling_plugin_name() : null;
+		if ( null !== $bundled_by ) {
+			$message = sprintf(
+				/* translators: 1: Required MCP Adapter version, 2: Plugin name, 3: Active MCP Adapter version. */
+				__( 'WooCommerce abilities need MCP Adapter %1$s or later, but %2$s loads version %3$s. Update %2$s so MCP clients can keep using WooCommerce.', 'woocommerce' ),
+				self::MIN_VERSION,
+				$bundled_by,
+				$version
+			);
+		} elseif ( $bundled ) {
+			$message = sprintf(
+				/* translators: 1: Required MCP Adapter version, 2: Active MCP Adapter version. */
+				__( 'WooCommerce abilities need MCP Adapter %1$s or later, but another plugin loads version %2$s. Update the plugin that includes MCP Adapter so MCP clients can keep using WooCommerce.', 'woocommerce' ),
+				self::MIN_VERSION,
+				$version
+			);
+		} else {
+			$message = sprintf(
+				/* translators: 1: Required MCP Adapter version, 2: Active MCP Adapter version. */
+				__( 'WooCommerce abilities need MCP Adapter %1$s or later, but version %2$s is active. Update the MCP Adapter plugin so MCP clients can keep using WooCommerce.', 'woocommerce' ),
+				self::MIN_VERSION,
+				$version
+			);
+		}
+		$message = esc_html( $message );
+
+		if ( current_user_can( 'update_plugins' ) ) {
+			$message .= sprintf( ' <a href="%1$s">%2$s</a>', esc_url( network_admin_url( 'plugins.php' ) ), esc_html__( 'Go to Plugins', 'woocommerce' ) );
+		}
+
+		$dismiss_url = wp_nonce_url(
+			add_query_arg( 'wc-hide-notice', self::NOTICE ),
+			'woocommerce_hide_notices_nonce',
+			'_wc_notice_nonce'
+		);
+
+		echo wp_kses_post(
+			'<div class="notice notice-warning is-dismissible">'
+			. '<a class="woocommerce-message-close notice-dismiss" style="text-decoration: none;" href="' . esc_url( $dismiss_url ) . '" aria-label="' . esc_attr__( 'Dismiss this notice', 'woocommerce' ) . '"></a>'
+			. '<p>' . $message . '</p>'
+			. '</div>'
+		);
+	}
+
+	/**
+	 * Get the name of the plugin that bundles the loaded MCP Adapter class.
+	 *
+	 * @return string|null The plugin name, or null when the class does not load from a regular plugin.
+	 */
+	private static function get_bundling_plugin_name(): ?string {
+		// @phpstan-ignore-next-line class.notFound -- MCP Adapter is an optional plugin, and only a loaded class reaches this method.
+		$file        = wp_normalize_path( (string) ( new \ReflectionClass( \WP\MCP\Core\McpAdapter::class ) )->getFileName() );
+		$plugins_dir = trailingslashit( wp_normalize_path( WP_PLUGIN_DIR ) );
+		if ( ! str_starts_with( $file, $plugins_dir ) || ! function_exists( 'get_plugins' ) ) {
+			return null;
+		}
+
+		$slug = strtok( substr( $file, strlen( $plugins_dir ) ), '/' );
+
+		return self::get_root_plugin_name( get_plugins( '/' . $slug ) );
+	}
+
+	/**
+	 * Get the name of the only plugin whose main file sits at the root of a plugin folder.
+	 *
+	 * The get_plugins() result also holds plugin headers from subfolders, so those entries are skipped.
+	 *
+	 * @internal
+	 *
+	 * @param array $plugins The result of get_plugins() for one plugin folder, keyed by file path relative to that folder.
+	 * @return string|null The plugin name, or null when the folder does not have exactly one root plugin file.
+	 */
+	public static function get_root_plugin_name( array $plugins ): ?string {
+		$root = array_filter(
+			$plugins,
+			static fn( $path ) => false === strpos( (string) $path, '/' ),
+			ARRAY_FILTER_USE_KEY
+		);
+		if ( 1 !== count( $root ) ) {
+			return null;
+		}
+		$plugin = reset( $root );
+
+		return empty( $plugin['Name'] ) ? null : (string) $plugin['Name'];
+	}
+
+	/**
+	 * Check whether an MCP Adapter version is older than the version WooCommerce abilities support.
+	 *
+	 * @param string|null $version The active MCP Adapter version, or null when no adapter is loaded.
+	 * @return bool
+	 */
+	public static function is_outdated( ?string $version ): bool {
+		return null !== $version && version_compare( $version, self::MIN_VERSION, '<' );
+	}
+}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 7ff64f857f3..7cad60c10b3 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -3301,14 +3301,20 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Should check the permissions of a direct call with a numeric string id or no input, as the MCP adapter sends them.
+	 * @testdox Should prepare a numeric string id or schema default before checking permissions directly.
 	 */
-	public function test_check_permissions_converts_raw_input(): void {
+	public function test_prepare_input_for_direct_permission_checks(): void {
 		$order   = wc_create_order();
 		$ability = wp_get_ability( 'woocommerce/orders-query' );

-		$this->assertTrue( $ability->check_permissions( array( 'id' => (string) $order->get_id() ) ), 'A numeric string id is converted.' );
-		$this->assertTrue( $ability->check_permissions( null ), 'No input gets the schema default.' );
+		$prepared = $ability->prepare_input( array( 'id' => (string) $order->get_id() ) );
+		$this->assertNotWPError( $prepared );
+		$this->assertSame( $order->get_id(), $prepared['id'] );
+		$this->assertTrue( $ability->check_permissions( $prepared ), 'A numeric string id is converted.' );
+
+		$prepared = $ability->prepare_input();
+		$this->assertNotWPError( $prepared );
+		$this->assertTrue( $ability->check_permissions( $prepared ), 'No input gets the schema default.' );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
index e18c6eb42a8..6c41cacc341 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilityTest.php
@@ -57,8 +57,13 @@ class AbilityTest extends \WC_Unit_Test_Case {
 					'type'                 => 'object',
 					'additionalProperties' => false,
 					'properties'           => array(
+						'text'  => array( 'type' => 'string' ),
 						'flag'  => array( 'type' => 'boolean' ),
 						'count' => array( 'type' => 'integer' ),
+						'page'  => array(
+							'type'    => 'integer',
+							'minimum' => 1,
+						),
 						'ids'   => array(
 							'type'        => 'array',
 							'uniqueItems' => true,
@@ -336,26 +341,47 @@ class AbilityTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Should pass converted input to the permission callback when check_permissions() is called directly.
+	 * @testdox Should pass raw input unchanged when the caller skips preparation.
+	 */
+	public function test_direct_permission_check_does_not_prepare_raw_input(): void {
+		$sut = $this->get_ability();
+
+		$this->assertTrue(
+			$sut->check_permissions(
+				array(
+					'flag'  => 'false',
+					'count' => '5',
+				)
+			)
+		);
+		$this->assertSame( 'false', $this->received['permission']['flag'], 'A direct permission check does not convert the boolean string.' );
+		$this->assertSame( '5', $this->received['permission']['count'], 'A direct permission check does not convert the numeric string.' );
+		$this->assertArrayNotHasKey( 'execute', $this->received );
+	}
+
+	/**
+	 * @testdox Should prepare raw input before calling check_permissions() directly.
 	 */
-	public function test_check_permissions_converts_the_input(): void {
-		$result = $this->get_ability()->check_permissions(
+	public function test_prepares_input_for_a_direct_permission_check(): void {
+		$sut      = $this->get_ability();
+		$prepared = $sut->prepare_input(
 			array(
 				'flag'  => 'false',
 				'count' => '5',
 			)
 		);

-		$this->assertTrue( $result );
+		$this->assertNotWPError( $prepared );
+		$this->assertTrue( $sut->check_permissions( $prepared ) );
 		$this->assertSame( false, $this->received['permission']['flag'] );
 		$this->assertSame( 5, $this->received['permission']['count'] );
 	}

 	/**
-	 * @testdox Should refuse invalid input in check_permissions() and not run the permission callback.
+	 * @testdox Should refuse invalid input during preparation and not run the permission callback.
 	 */
-	public function test_check_permissions_refuses_invalid_input(): void {
-		$result = $this->get_ability()->check_permissions( array( 'flag' => 'maybe' ) );
+	public function test_prepare_input_refuses_invalid_input(): void {
+		$result = $this->get_ability()->prepare_input( array( 'flag' => 'maybe' ) );

 		$this->assertWPError( $result );
 		$this->assertSame( 'ability_invalid_input', $result->get_error_code() );
@@ -376,4 +402,198 @@ class AbilityTest extends \WC_Unit_Test_Case {

 		$this->assertSame( $once, $ability->normalize_input( $once ) );
 	}
+
+	/**
+	 * Entry points that prepare input before checking permissions.
+	 *
+	 * @return array<string, array{0: bool}>
+	 */
+	public function provider_preparation_entry_points(): array {
+		return array(
+			'execute'           => array( false ),
+			'direct permission' => array( true ),
+		);
+	}
+
+	/**
+	 * @testdox Should normalize once and give the permission and execution callbacks the same input.
+	 * @dataProvider provider_preparation_entry_points
+	 *
+	 * @param bool $direct_permission Whether to prepare input for a direct permission check.
+	 */
+	public function test_prepares_input_once_with_a_non_idempotent_filter( bool $direct_permission ): void {
+		if ( version_compare( get_bloginfo( 'version' ), '7.1-alpha', '<' ) ) {
+			$this->markTestSkipped( 'The normalization filter requires WordPress 7.1.' );
+		}
+
+		$sut   = $this->get_ability();
+		$calls = 0;
+		add_filter(
+			'wp_ability_normalize_input',
+			static function ( $input, $name ) use ( &$calls ) {
+				if ( 'test/convert-input' === $name ) {
+					++$calls;
+					$input['text'] = htmlspecialchars( $input['text'], ENT_QUOTES, 'UTF-8', true );
+				}
+				return $input;
+			},
+			10,
+			2
+		);
+
+		$input = array( 'text' => '&' );
+		if ( $direct_permission ) {
+			$prepared = $sut->prepare_input( $input );
+			$this->assertNotWPError( $prepared );
+			$this->assertTrue( $sut->check_permissions( $prepared ) );
+		} else {
+			$this->assertNotWPError( $sut->execute( $input ) );
+			$this->assertSame( '&amp;', $this->received['execute']['text'] );
+		}
+
+		$this->assertSame( 1, $calls );
+		$this->assertSame( '&amp;', $this->received['permission']['text'] );
+	}
+
+	/**
+	 * @testdox Should preserve normalization errors without calling either callback.
+	 * @dataProvider provider_preparation_entry_points
+	 *
+	 * @param bool $direct_permission Whether to prepare input for a direct permission check.
+	 */
+	public function test_preserves_normalization_errors( bool $direct_permission ): void {
+		if ( version_compare( get_bloginfo( 'version' ), '7.1-alpha', '<' ) ) {
+			$this->markTestSkipped( 'The normalization filter requires WordPress 7.1.' );
+		}
+
+		$sut   = $this->get_ability();
+		$error = new \WP_Error( 'test_normalization_error', 'Cannot normalize this input.' );
+		add_filter(
+			'wp_ability_normalize_input',
+			static function ( $input, $name ) use ( $error ) {
+				return 'test/convert-input' === $name ? $error : $input;
+			},
+			10,
+			2
+		);
+
+		$result = $direct_permission ? $sut->prepare_input( array() ) : $sut->execute( array() );
+
+		$this->assertSame( $error, $result );
+		$this->assertArrayNotHasKey( 'permission', $this->received );
+		$this->assertArrayNotHasKey( 'execute', $this->received );
+	}
+
+	/**
+	 * @testdox Should sanitize once when preparing input, including on older WordPress versions.
+	 * @dataProvider provider_preparation_entry_points
+	 *
+	 * @param bool $direct_permission Whether to prepare input for a direct permission check.
+	 */
+	public function test_sanitizes_input_once( bool $direct_permission ): void {
+		$sut = new class(
+			'test/count-sanitization',
+			array(
+				'label'               => 'Count sanitization',
+				'description'         => 'Count calls to the real sanitizer.',
+				'category'            => 'woocommerce',
+				'input_schema'        => array( 'type' => 'boolean' ),
+				'permission_callback' => '__return_true',
+				'execute_callback'    => static function ( $input ) {
+					return $input;
+				},
+			)
+		) extends Ability {
+			/**
+			 * Number of sanitizer calls.
+			 *
+			 * @var int
+			 */
+			public $sanitizations = 0;
+
+			/**
+			 * Count calls while retaining the actual schema sanitization.
+			 *
+			 * @param mixed $input Validated input.
+			 * @return mixed|\WP_Error
+			 */
+			protected function sanitize_input( $input ) {
+				++$this->sanitizations;
+				return parent::sanitize_input( $input );
+			}
+		};
+
+		if ( $direct_permission ) {
+			$prepared = $sut->prepare_input( 'false' );
+			$this->assertFalse( $prepared );
+			$this->assertTrue( $sut->check_permissions( $prepared ) );
+		} else {
+			$this->assertFalse( $sut->execute( 'false' ) );
+		}
+
+		$this->assertSame( 1, $sut->sanitizations );
+	}
+
+	/**
+	 * @testdox Should reject conversion failures in prepare_input before any callback runs.
+	 */
+	public function test_prepare_input_refuses_conversion_errors(): void {
+		$sut    = $this->get_ability();
+		$result = $sut->prepare_input( array( 'ids' => array( '1', '01' ) ) );
+
+		$this->assertWPError( $result );
+		$this->assertSame( 'ability_invalid_input', $result->get_error_code() );
+		$this->assertArrayNotHasKey( 'permission', $this->received );
+		$this->assertArrayNotHasKey( 'execute', $this->received );
+	}
+
+	/**
+	 * Remove when WooCommerce requires WordPress 7.2, together with the backport in Ability.
+	 *
+	 * @testdox Should refuse a number that converts to a value the schema does not allow, and not run the callbacks.
+	 */
+	public function test_refuses_a_number_that_converts_below_the_minimum(): void {
+		if ( method_exists( \WP_Ability::class, 'prepare_input' ) ) {
+			$this->markTestSkipped( 'WordPress prepares the input itself since 7.2.' );
+		}
+
+		$sut = $this->get_ability();
+
+		// PHP 8.5 reports casting an out-of-range float to an integer as a deprecation.
+		set_error_handler( '__return_true', E_DEPRECATED ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
+		try {
+			$results = array( $sut->execute( array( 'page' => 2.0 ** 64 ) ), $sut->prepare_input( array( 'page' => 2.0 ** 64 ) ) );
+		} finally {
+			restore_error_handler();
+		}
+
+		foreach ( $results as $result ) {
+			$this->assertWPError( $result );
+			$this->assertSame( 'ability_invalid_input', $result->get_error_code() );
+		}
+		$this->assertArrayNotHasKey( 'permission', $this->received );
+		$this->assertArrayNotHasKey( 'execute', $this->received );
+	}
+
+	/**
+	 * @testdox Should validate input that needs no conversion once.
+	 */
+	public function test_validates_input_that_needs_no_conversion_once(): void {
+		if ( ! method_exists( \WP_Ability::class, 'prepare_input' ) ) {
+			$this->markTestSkipped( 'WP_Ability::prepare_input() exists since WordPress 7.2.' );
+		}
+
+		$calls    = 0;
+		$callback = function ( $is_valid ) use ( &$calls ) {
+			++$calls;
+			return $is_valid;
+		};
+		add_filter( 'wp_ability_validate_input', $callback );
+
+		$result = $this->get_ability()->prepare_input( array( 'count' => 5 ) );
+
+		remove_filter( 'wp_ability_validate_input', $callback );
+		$this->assertSame( array( 'count' => 5 ), $result );
+		$this->assertSame( 1, $calls );
+	}
 }
diff --git a/plugins/woocommerce/tests/php/src/Internal/MCP/McpAdapterVersionNoticeTest.php b/plugins/woocommerce/tests/php/src/Internal/MCP/McpAdapterVersionNoticeTest.php
new file mode 100644
index 00000000000..0d80f047c01
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/MCP/McpAdapterVersionNoticeTest.php
@@ -0,0 +1,55 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\MCP;
+
+use Automattic\WooCommerce\Internal\MCP\McpAdapterVersionNotice;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for the McpAdapterVersionNotice class.
+ */
+class McpAdapterVersionNoticeTest extends WC_Unit_Test_Case {
+
+	/**
+	 * @testdox Should treat only a loaded MCP Adapter older than the minimum version as outdated.
+	 *
+	 * @testWith [null, false]
+	 *           ["0.7.1", true]
+	 *           ["0.7.2", false]
+	 *           ["0.8.0", false]
+	 *
+	 * @param string|null $version  The active MCP Adapter version.
+	 * @param bool        $expected Whether the version is outdated.
+	 */
+	public function test_is_outdated( ?string $version, bool $expected ): void {
+		$this->assertSame( $expected, McpAdapterVersionNotice::is_outdated( $version ) );
+	}
+
+	/**
+	 * @testdox Should name only the plugin whose main file sits at the root of the plugin folder.
+	 */
+	public function test_get_root_plugin_name_skips_subfolder_plugins(): void {
+		$plugins = array(
+			'examples/example.php' => array( 'Name' => 'A Example' ),
+			'main.php'             => array( 'Name' => 'Z Host' ),
+		);
+
+		$this->assertSame( 'Z Host', McpAdapterVersionNotice::get_root_plugin_name( $plugins ) );
+	}
+
+	/**
+	 * @testdox Should return no plugin name when the plugin folder has no single root plugin file.
+	 */
+	public function test_get_root_plugin_name_returns_null_when_ambiguous(): void {
+		$plugins = array(
+			'one.php'              => array( 'Name' => 'One' ),
+			'two.php'              => array( 'Name' => 'Two' ),
+			'examples/example.php' => array( 'Name' => 'Example' ),
+		);
+
+		$this->assertNull( McpAdapterVersionNotice::get_root_plugin_name( $plugins ) );
+		$this->assertNull( McpAdapterVersionNotice::get_root_plugin_name( array( 'examples/example.php' => array( 'Name' => 'Example' ) ) ) );
+	}
+}