Commit 49ead42cec9 for woocommerce

commit 49ead42cec9e63735f5b36a3233afbc997b37326
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Thu Nov 19 12:13:15 2026 -0300

    Let extensions load field data for a whole list in one query (#71102)

    * Let extensions load field data for a whole list in one query

    Add ExtensibleAbilities::register_prime(). Before a list ability formats
    its objects, Core calls every prime callback of the resource once with
    the full list, so an extension with a custom table can load its field
    data in one query. A callback that throws is logged, and the ability
    still formats the objects.

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

    * Document prime callbacks for reads and own resources, and tighten the prime tests

    The register_prime() docblock now says that prime callbacks run only on
    reads, so an extension keys its cache by object ID. The prime() docblock
    says that an extension with its own resource calls it from its list
    abilities. The tests check that the prime callback runs before the fields
    are formatted, and that two orders give one call for orders and one for
    items.

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

    * Tell extensions to empty the prime cache first, and use smaller order fixtures

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

    * Keep the read going when the logger throws, and tell extensions to drop cache entries on save

    A prime cache filled on a read stays stale after a write in the same
    request, because writes do not call prime callbacks. The docblock now
    tells extensions to remove the entry when the object is saved.

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

    * Test the prime calls of every list ability, and fail when a new resource has no prime test

    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-1730-extensible-abilities-prime b/plugins/woocommerce/changelog/wooai-1730-extensible-abilities-prime
new file mode 100644
index 00000000000..665b48042a2
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1730-extensible-abilities-prime
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add ExtensibleAbilities::register_prime(), so an extension can load the field data of a whole list of objects in one query.
diff --git a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
index 925ba1ad4f2..ec7f0c27b66 100644
--- a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
+++ b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
@@ -62,6 +62,13 @@ class ExtensibleAbilities {
 	 */
 	private static array $syncs = array();

+	/**
+	 * Prime callbacks keyed by resource.
+	 *
+	 * @var array<string, array<int, array<string, mixed>>>
+	 */
+	private static array $primes = array();
+
 	/**
 	 * Whether the store enabled the experimental feature.
 	 *
@@ -497,10 +504,7 @@ class ExtensibleAbilities {
 				try {
 					$clauses = call_user_func( $args['callback'], $value );
 				} catch ( \Throwable $e ) {
-					wc_get_logger()->error(
-						sprintf( 'Ability filter "%s.%s" of "%s" failed: %s', $namespace, $filter, $resource_name, $e->getMessage() ),
-						array( 'source' => 'extensible-abilities' )
-					);
+					self::log_error( sprintf( 'Ability filter "%s.%s" of "%s" failed: %s', $namespace, $filter, $resource_name, $e->getMessage() ) );
 					$clauses = null;
 				}
 				if ( is_wp_error( $clauses ) ) {
@@ -706,12 +710,90 @@ class ExtensibleAbilities {
 	}

 	/**
-	 * Check the arguments of a validator or a sync, and report the first
-	 * problem with wc_doing_it_wrong().
+	 * Register a prime callback on a resource. Before an ability formats a list
+	 * of objects of the resource, Core calls every prime callback of the
+	 * resource once, with the full list. An extension loads the data of its
+	 * fields for the whole list in one query, keeps it in its own cache, and
+	 * its get_callback reads from there.
+	 *
+	 * Call it inside the `woocommerce_extensible_abilities_init` action. Prime
+	 * callbacks run only when the `extensible_abilities` feature is on, in the
+	 * order they were registered.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param array $args {
+	 *     Prime arguments.
+	 *
+	 *     @type string   $resource  Resource that an ability formats, such as `product`.
+	 *     @type string   $namespace Slug of the extension that owns the callback, such as `google`.
+	 *     @type callable $callback  Receives `object[] $objects`, every object the ability formats. The
+	 *                               callback skips the objects it does not need. Core ignores the
+	 *                               return value. When it throws, Core logs the error and formats
+	 *                               the objects anyway, so the get_callback must still load its
+	 *                               data for an object that is not in the cache. Core calls prime
+	 *                               callbacks only on reads, not on writes. Key the cache by object ID,
+	 *                               and remove the entry of an object when the object is saved, for
+	 *                               example on `woocommerce_update_product`. Otherwise a write after a
+	 *                               read in the same request returns the old value. Empty the cache at the start of
+	 *                               every call, so a failed load leaves no old entries.
+	 * }
+	 */
+	public static function register_prime( array $args ): void {
+		if ( ! self::check_callback_args( $args, __METHOD__, 'prime callback', '11.4.0' ) ) {
+			return;
+		}
+
+		self::$primes[ $args['resource'] ][] = $args;
+	}
+
+	/**
+	 * Call every prime callback of a resource with the objects that an ability
+	 * is about to format. A callback that throws is logged, and the next one runs.
+	 * An extension that registers its own resource calls it from its list
+	 * abilities before add_fields_to_object(), as Core's abilities do.
+	 *
+	 * @since 11.4.0
+	 *
+	 * @param string   $resource_name Resource.
+	 * @param object[] $objects       Objects to format.
+	 */
+	public static function prime( string $resource_name, array $objects ): void {
+		if ( empty( self::$primes[ $resource_name ] ) || empty( $objects ) || ! self::is_enabled() ) {
+			return;
+		}
+
+		$objects = array_values( $objects );
+		foreach ( self::$primes[ $resource_name ] as $args ) {
+			try {
+				call_user_func( $args['callback'], $objects );
+			} catch ( \Throwable $e ) {
+				self::log_error( sprintf( 'Ability prime callback of "%s" for "%s" failed: %s', $args['namespace'], $resource_name, $e->getMessage() ) );
+			}
+		}
+	}
+
+	/**
+	 * Log an error from an extension callback. A logger that throws does not
+	 * stop the ability.
+	 *
+	 * @param string $message Message to log.
+	 */
+	private static function log_error( string $message ): void {
+		try {
+			wc_get_logger()->error( $message, array( 'source' => 'extensible-abilities' ) );
+		} catch ( \Throwable $e ) {
+			unset( $e );
+		}
+	}
+
+	/**
+	 * Check the arguments of a validator, a sync or a prime callback, and report
+	 * the first problem with wc_doing_it_wrong().
 	 *
-	 * @param array  $args    Validator or sync arguments.
+	 * @param array  $args    Validator, sync or prime arguments.
 	 * @param string $method  Method that registers the callback.
-	 * @param string $kind    `validator` or `sync`, for the message.
+	 * @param string $kind    `validator`, `sync` or `prime callback`, for the message.
 	 * @param string $version Version that added the method.
 	 * @return bool Whether the arguments are valid.
 	 */
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
index a6b49b646de..c11595ba206 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
@@ -141,11 +141,15 @@ class CustomersQuery extends AbstractDomainAbility implements AbilityDefinition
 				);
 				continue;
 			}
+			$customers[] = $customer;
+		}
+		ExtensibleAbilities::prime( 'customer', $customers );
+		foreach ( $customers as $index => $customer ) {
 			$formatted = self::format_customer( $customer );
 			if ( is_wp_error( $formatted ) ) {
 				return $formatted;
 			}
-			$customers[] = $formatted;
+			$customers[ $index ] = $formatted;
 		}

 		$total = (int) $query->get_total();
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductReviewsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductReviewsQuery.php
index 6ced523d42f..9ee50483160 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductReviewsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductReviewsQuery.php
@@ -247,15 +247,14 @@ class ProductReviewsQuery extends AbstractDomainAbility implements AbilityDefini
 		$with_product       = count( $product_ids ) !== 1;
 		$with_status        = 'all' === $status;
 		$store_reply_counts = self::get_store_reply_counts( wp_list_pluck( $matches->comments, 'comment_ID' ) );
-		foreach ( $matches->comments as $comment ) {
-			$comment = get_comment( $comment );
-			if ( $comment ) {
-				$review = self::format_review( $comment, $with_product, $with_status, $store_reply_counts[ (int) $comment->comment_ID ] ?? 0 );
-				if ( is_wp_error( $review ) ) {
-					return $review;
-				}
-				$reviews[] = $review;
+		$comments           = array_filter( array_map( 'get_comment', $matches->comments ) );
+		ExtensibleAbilities::prime( 'product_review', $comments );
+		foreach ( $comments as $comment ) {
+			$review = self::format_review( $comment, $with_product, $with_status, $store_reply_counts[ (int) $comment->comment_ID ] ?? 0 );
+			if ( is_wp_error( $review ) ) {
+				return $review;
 			}
+			$reviews[] = $review;
 		}
 		$ratings    = self::get_rating_summary( $scope, $total );
 		$by_product = $ratings['by_product'];
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
index 57b12d34bb7..10c297c19ce 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
@@ -8,6 +8,7 @@ declare( strict_types=1 );
 namespace Automattic\WooCommerce\Internal\Abilities\Domain;

 use Automattic\WooCommerce\Abilities\AbilityDefinition;
+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
 use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\ProductTermAbilityTrait;

 defined( 'ABSPATH' ) || exit;
@@ -137,6 +138,7 @@ class ProductTermsQuery extends AbstractDomainAbility implements AbilityDefiniti
 			_prime_post_caches( $image_ids );
 		}

+		ExtensibleAbilities::prime( 'product_term', $terms );
 		$formatted = array();
 		foreach ( $terms as $term ) {
 			$data = self::format_term( $term );
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderAbilityTrait.php
index 6c282973bc7..f9683314bea 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderAbilityTrait.php
@@ -428,11 +428,14 @@ trait OrderAbilityTrait {
 	 * @return array|\WP_Error
 	 */
 	protected static function format_orders_for_response( array $orders, array $includes ) {
+		ExtensibleAbilities::prime( 'order', $orders );
 		if ( in_array( 'line_items', $includes, true ) ) {
 			$product_ids = array();
+			$items       = array();
 			foreach ( $orders as $order ) {
 				foreach ( $order->get_items() as $item ) {
 					if ( $item instanceof \WC_Order_Item_Product ) {
+						$items[]       = $item;
 						$product_ids[] = $item->get_product_id();
 						$product_ids[] = $item->get_variation_id();
 					}
@@ -443,6 +446,7 @@ trait OrderAbilityTrait {
 				// Prime caches to reduce future queries.
 				_prime_post_caches( $product_ids );
 			}
+			ExtensibleAbilities::prime( 'order_item', $items );
 		}

 		$formatted = array();
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
index 5c94053135c..90fbd757bc1 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
@@ -1812,6 +1812,7 @@ trait ProductAbilityTrait {
 	 * @return array|\WP_Error
 	 */
 	protected static function format_products_for_response( array $products ) {
+		ExtensibleAbilities::prime( 'product', $products );
 		$formatted = array();
 		foreach ( $products as $product ) {
 			$data = self::format_product_for_response( $product );
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index 6e8a8a62e0e..b57c52d435b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -1016,6 +1016,269 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		$this->assertSame( array( 'test-ext' => array( 'note' => 'order-' . $order->get_id() ) ), $noted['order']['extensions'] );
 	}

+	/**
+	 * @testdox Should call the $resource_name prime callbacks of each extension once with the full list, in registration order.
+	 * @dataProvider provide_list_abilities
+	 *
+	 * @param string   $resource_name Resource the ability lists.
+	 * @param string   $ability       List ability.
+	 * @param string   $list_key      Output key of the list.
+	 * @param callable $create_input  Creates two objects and returns the ability input that lists them.
+	 */
+	public function test_prime_callbacks_get_the_full_list_once( string $resource_name, string $ability, string $list_key, callable $create_input ): void {
+		$calls = array();
+		$cache = array();
+		$this->add_test_action(
+			'woocommerce_extensible_abilities_init',
+			static function () use ( $resource_name, &$cache ) {
+				ExtensibleAbilities::register_field(
+					array(
+						'resource'      => $resource_name,
+						'namespace'     => 'test-ext',
+						'field'         => 'primed',
+						'output_schema' => array( 'type' => 'string' ),
+						'get_callback'  => static function ( $data_object ) use ( &$cache ) {
+							return $cache[ self::prime_object_id( $data_object ) ] ?? null;
+						},
+					)
+				);
+			}
+		);
+		foreach ( array( 'test-ext', 'other_ext' ) as $extension_namespace ) {
+			$this->register_prime(
+				$resource_name,
+				$extension_namespace,
+				static function ( array $objects ) use ( &$calls, &$cache, $extension_namespace ) {
+					$ids     = array_map( array( self::class, 'prime_object_id' ), array_values( $objects ) );
+					$calls[] = array( $extension_namespace, $ids );
+					if ( 'test-ext' === $extension_namespace ) {
+						$cache = array_combine( $ids, array_map( static fn( int $id ) => 'primed-' . $id, $ids ) );
+					}
+				}
+			);
+		}
+
+		$list = $this->run_ability( $ability, $create_input() )[ $list_key ];
+
+		$ids = array_column( $list, 'id' );
+		$this->assertCount( 2, $ids );
+		$this->assertSame( array( array( 'test-ext', $ids ), array( 'other_ext', $ids ) ), $calls );
+		foreach ( $list as $listed ) {
+			$this->assertSame( 'primed-' . $listed['id'], $listed['extensions']['test-ext']['primed'] );
+		}
+	}
+
+	/**
+	 * List abilities that prime their objects, with a fixture factory for each.
+	 *
+	 * @return array<string, array>
+	 */
+	public function provide_list_abilities(): array {
+		return array(
+			'product'        => array(
+				'product',
+				'woocommerce/products-query',
+				'products',
+				static function (): array {
+					\WC_Helper_Product::create_simple_product( true, array( 'name' => 'Pen one' ) );
+					\WC_Helper_Product::create_simple_product( true, array( 'name' => 'Pen two' ) );
+					return array( 'search' => 'Pen' );
+				},
+			),
+			'customer'       => array(
+				'customer',
+				'woocommerce/customers-query',
+				'customers',
+				static function (): array {
+					return array( 'include' => self::factory()->user->create_many( 2, array( 'role' => 'customer' ) ) );
+				},
+			),
+			'product_term'   => array(
+				'product_term',
+				'woocommerce/product-terms-query',
+				'terms',
+				static function (): array {
+					return array(
+						'taxonomy'   => 'product_cat',
+						'include'    => self::factory()->term->create_many( 2, array( 'taxonomy' => 'product_cat' ) ),
+						'hide_empty' => false,
+					);
+				},
+			),
+			'product_review' => array(
+				'product_review',
+				'woocommerce/product-reviews-query',
+				'reviews',
+				static function (): array {
+					$product = \WC_Helper_Product::create_simple_product()->get_id();
+					self::factory()->comment->create_many(
+						2,
+						array(
+							'comment_post_ID'  => $product,
+							'comment_type'     => 'review',
+							'comment_approved' => '1',
+						)
+					);
+					return array( 'product_ids' => array( $product ) );
+				},
+			),
+		);
+	}
+
+	/**
+	 * @testdox Should have a prime test for every Core resource.
+	 */
+	public function test_every_core_resource_has_a_prime_test(): void {
+		AbilitiesLoader::register_core_resources();
+		$resources = new \ReflectionProperty( ExtensibleAbilities::class, 'resources' );
+		$resources->setAccessible( true );
+
+		$covered = array_merge( array_keys( $this->provide_list_abilities() ), array( 'order', 'order_item' ) );
+
+		$this->assertEqualsCanonicalizing( array_keys( $resources->getValue() ), $covered, 'Add the new resource to provide_list_abilities().' );
+	}
+
+	/**
+	 * ID of an object a prime callback receives.
+	 *
+	 * @param \WC_Data|\WP_Term|\WP_Comment $data_object Object.
+	 * @return int
+	 */
+	private static function prime_object_id( $data_object ): int {
+		if ( $data_object instanceof \WP_Term ) {
+			return $data_object->term_id;
+		}
+		if ( $data_object instanceof \WP_Comment ) {
+			return (int) $data_object->comment_ID;
+		}
+		return $data_object->get_id();
+	}
+
+	/**
+	 * @testdox Should call the order and order item prime callbacks once with every order and every product line.
+	 */
+	public function test_prime_callbacks_get_orders_and_line_items(): void {
+		$product = \WC_Helper_Product::create_simple_product();
+		$orders  = array( wc_create_order(), wc_create_order() );
+		foreach ( $orders as $order ) {
+			$order->add_product( $product );
+			$order->save();
+		}
+		$calls = array();
+		foreach ( array( 'order', 'order_item' ) as $resource_name ) {
+			$this->register_prime(
+				$resource_name,
+				'test-ext',
+				static function ( array $objects ) use ( &$calls, $resource_name ) {
+					$calls[ $resource_name ][] = array_map( static fn( $data_object ) => $data_object->get_id(), $objects );
+				}
+			);
+		}
+
+		$this->run_ability(
+			'woocommerce/orders-query',
+			array(
+				'per_page'           => 2,
+				'include_line_items' => true,
+			)
+		);
+
+		$item_ids = array_merge( ...array_map( static fn( \WC_Order $order ) => array_keys( $order->get_items() ), $orders ) );
+		$this->assertCount( 2, $item_ids );
+		$this->assertCount( 1, $calls['order'] );
+		$this->assertEqualsCanonicalizing( array_map( static fn( \WC_Order $order ) => $order->get_id(), $orders ), $calls['order'][0] );
+		$this->assertCount( 1, $calls['order_item'] );
+		$this->assertEqualsCanonicalizing( $item_ids, $calls['order_item'][0] );
+	}
+
+	/**
+	 * @testdox Should log a prime callback that throws, run the next one, and still return the fields.
+	 */
+	public function test_a_prime_callback_that_throws_is_logged(): void {
+		$product = \WC_Helper_Product::create_simple_product( true, array( 'name' => 'Pen' ) );
+		$product->update_meta_data( '_test_code', 'A1' );
+		$product->save();
+		$logger = $this->getMockBuilder( \WC_Logger_Interface::class )->getMock();
+		$logger->expects( $this->once() )->method( 'error' )->with(
+			'Ability prime callback of "test-ext" for "product" failed: Table is missing',
+			array( 'source' => 'extensible-abilities' )
+		);
+		add_filter( 'woocommerce_logging_class', static fn() => $logger );
+		$calls = 0;
+		$this->register_prime(
+			'product',
+			'test-ext',
+			static function () {
+				throw new \RuntimeException( 'Table is missing' );
+			}
+		);
+		$this->register_prime(
+			'product',
+			'other_ext',
+			static function () use ( &$calls ) {
+				++$calls;
+			}
+		);
+
+		$products = $this->run_ability( 'woocommerce/products-query', array( 'search' => 'Pen' ) )['products'];
+
+		$this->assertSame( 1, $calls );
+		$this->assertSame( array( 'code' => 'A1' ), $products[0]['extensions']['test-ext'] );
+	}
+
+	/**
+	 * @testdox Should run the next prime callback and return the fields when the logger also throws.
+	 */
+	public function test_a_logger_that_throws_does_not_stop_the_read(): void {
+		$product = \WC_Helper_Product::create_simple_product( true, array( 'name' => 'Pen' ) );
+		$product->update_meta_data( '_test_code', 'A1' );
+		$product->save();
+		$logger = $this->getMockBuilder( \WC_Logger_Interface::class )->getMock();
+		$logger->method( 'error' )->willThrowException( new \RuntimeException( 'Logger broke' ) );
+		add_filter( 'woocommerce_logging_class', static fn() => $logger );
+		$calls = 0;
+		$this->register_prime(
+			'product',
+			'test-ext',
+			static function () {
+				throw new \RuntimeException( 'Table is missing' );
+			}
+		);
+		$this->register_prime(
+			'product',
+			'other_ext',
+			static function () use ( &$calls ) {
+				++$calls;
+			}
+		);
+
+		$products = $this->run_ability( 'woocommerce/products-query', array( 'search' => 'Pen' ) )['products'];
+
+		$this->assertSame( 1, $calls );
+		$this->assertSame( array( 'code' => 'A1' ), $products[0]['extensions']['test-ext'] );
+	}
+
+	/**
+	 * @testdox Should not call prime callbacks with the feature off.
+	 */
+	public function test_prime_callbacks_do_not_run_with_the_feature_off(): void {
+		\WC_Helper_Product::create_simple_product( true, array( 'name' => 'Pen' ) );
+		$calls = 0;
+		$this->register_prime(
+			'product',
+			'test-ext',
+			static function () use ( &$calls ) {
+				++$calls;
+			}
+		);
+		$this->get_output_schema( 'woocommerce/products-query' );
+		$this->set_feature( false );
+
+		$this->run_ability( 'woocommerce/products-query', array( 'search' => 'Pen' ) );
+
+		$this->assertSame( 0, $calls );
+	}
+
 	/**
 	 * @testdox Should list the fields of each namespace in the output schemas, and add nothing for a resource with no fields.
 	 */
@@ -6566,6 +6829,28 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		);
 	}

+	/**
+	 * Register a prime callback inside woocommerce_extensible_abilities_init, the way an extension does.
+	 *
+	 * @param string   $resource_name       Resource.
+	 * @param string   $extension_namespace Extension namespace.
+	 * @param callable $callback            Prime callback.
+	 */
+	private function register_prime( string $resource_name, string $extension_namespace, callable $callback ): void {
+		$this->add_test_action(
+			'woocommerce_extensible_abilities_init',
+			static function () use ( $resource_name, $extension_namespace, $callback ) {
+				ExtensibleAbilities::register_prime(
+					array(
+						'resource'  => $resource_name,
+						'namespace' => $extension_namespace,
+						'callback'  => $callback,
+					)
+				);
+			}
+		);
+	}
+
 	/**
 	 * Register test-ext/order-status, an ActionableAbility of an extension that changes orders.
 	 *
@@ -6706,7 +6991,7 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 	private function reset_registries(): void {
 		global $wp_actions;

-		foreach ( array( 'fields', 'validators', 'syncs', 'filters' ) as $property ) {
+		foreach ( array( 'fields', 'validators', 'syncs', 'primes', 'filters' ) as $property ) {
 			$reflection = new \ReflectionProperty( ExtensibleAbilities::class, $property );
 			$reflection->setAccessible( true );
 			$reflection->setValue( null, array() );