Commit 30628e1435e for woocommerce

commit 30628e1435ed936ea893440f80117c464651348c
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date:   Thu Dec 3 14:38:03 2026 +0000

    Let each resource declare which extension filter clauses it accepts (#71394)

    * Abilities: let each list ability declare its extension filter clauses

    Co-authored-by: matticbot <matticbot@users.noreply.github.com>

    * Put each key of the ID query var map on its own line

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

    * Log why a query filter failed and tell the agent how to recover

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

    * Declare the query filter clauses on the resource instead of the list ability

    register_resource() takes an optional query_clauses argument. A filter
    on that resource can return only those clauses. Without it, a filter
    can return any clause.

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

    * Let a customer query filter return exclude_ids

    A customer filter can now return the user IDs of customers to leave out.
    The customers-query ability maps them to customer rows, and guests stay in the result.

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

    * Apply extension query filters in the order refunds list ability

    order-refunds-query accepted filter registrations on order_refund but
    never added the filters to its schema or applied them. It now applies
    include_ids and exclude_ids.

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

    * Read include_ids and exclude_ids in the product terms list ability

    Trunk added the product term filters with the old post__in keys. The
    test resources now pass the callbacks that register_resource() needs.

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

    * Fail the customers query when the excluded users cannot be read

    A failed lookup of the customer IDs of excluded users now fails the call with the same filter error and log line as any other filter failure, instead of listing the customers the filter excluded.

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

    ---------

    Co-authored-by: zaguiini <luis.felipe.zaguini@automattic.com>
    Co-authored-by: matticbot <matticbot@users.noreply.github.com>
    Co-authored-by: Luis Felipe Zaguini <luisfelipezaguini@gmail.com>
    Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/extensible-abilities-list-filters b/plugins/woocommerce/changelog/extensible-abilities-list-filters
new file mode 100644
index 00000000000..188d4980b58
--- /dev/null
+++ b/plugins/woocommerce/changelog/extensible-abilities-list-filters
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Apply extension query filters in the experimental order refunds list ability; behind a feature flag.
+
diff --git a/plugins/woocommerce/changelog/extensible-abilities-query-clauses b/plugins/woocommerce/changelog/extensible-abilities-query-clauses
new file mode 100644
index 00000000000..e3e26becfa7
--- /dev/null
+++ b/plugins/woocommerce/changelog/extensible-abilities-query-clauses
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+Comment: Let experimental extensible abilities resources declare the query filter clauses they accept; behind a feature flag.
+
diff --git a/plugins/woocommerce/changelog/wooai-1756-customer-query-filter-exclude-ids b/plugins/woocommerce/changelog/wooai-1756-customer-query-filter-exclude-ids
new file mode 100644
index 00000000000..47b1e9d686f
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1756-customer-query-filter-exclude-ids
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Let a customer query filter return exclude_ids in the experimental extensible abilities; no change for merchants.
diff --git a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
index a6fdfcb43e7..6298be0bac8 100644
--- a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
+++ b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
@@ -95,11 +95,6 @@ class ExtensibleAbilities {
 	 */
 	private static array $filters = array();

-	/**
-	 * Query arguments that a filter callback can return.
-	 */
-	private const QUERY_ARG_KEYS = array( 'meta_query', 'tax_query', 'include_ids', 'exclude_ids' );
-
 	/**
 	 * Type names that the REST schema check knows. It accepts any value for another name.
 	 */
@@ -128,6 +123,9 @@ class ExtensibleAbilities {
 	 *                                          returns `get_type()`, its `product_term` the taxonomy, its `coupon` the
 	 *                                          discount type, its `customer` the user role and its `product_review`
 	 *                                          the comment type.
+	 *     @type string[] $query_clauses        Optional. Clauses that the list ability of the resource runs, such as
+	 *                                          `meta_query`, `tax_query`, `include_ids` and `exclude_ids`. A query
+	 *                                          filter can return only these. Without it, a filter can return any clause.
 	 * }
 	 */
 	public static function register_resource( string $resource_name, array $args = array() ): void {
@@ -146,6 +144,14 @@ class ExtensibleAbilities {
 			}
 		}

+		if ( isset( $args['query_clauses'] ) ) {
+			if ( ! is_array( $args['query_clauses'] ) || empty( $args['query_clauses'] ) || array_filter( $args['query_clauses'], static fn( $clause ) => ! is_string( $clause ) ) ) {
+				wc_doing_it_wrong( __METHOD__, 'The "query_clauses" argument must be a list of clause names.', '11.5.0' );
+				return;
+			}
+			$args['query_clauses'] = array_values( $args['query_clauses'] );
+		}
+
 		self::$resources[ $resource_name ] = self::$resources[ $resource_name ] ?? $args;
 	}

@@ -387,21 +393,17 @@ class ExtensibleAbilities {
 	 * @param array $args {
 	 *     Filter arguments.
 	 *
-	 *     @type string   $resource  Resource whose list ability applies the filters. Core's `products-query`,
-	 *                               `orders-query`, `customers-query` and `product-terms-query` apply the filters
-	 *                               of `product`, `order`, `customer` and `product_term`. A `product_term`
-	 *                               `meta_query` reads term meta. A `customer` filter returns only `include_ids`, which are
-	 *                               user IDs, so it matches registered customers only. The list ability
-	 *                               of another resource must call `get_query_args()` and `add_query_filters_schema()` itself.
+	 *     @type string   $resource  Resource whose list ability applies the filters. The list ability calls
+	 *                               `get_query_args()` and `add_query_filters_schema()`. The resource can name
+	 *                               the clauses its list ability runs.
 	 *     @type string   $namespace Slug of the extension that owns the filter, such as `subscriptions`.
 	 *     @type string   $filter    Filter name inside the namespace, such as `subscribable`.
 	 *     @type array    $schema    JSON schema of the value. Its `description` tells agents how to use the filter.
-	 *     @type callable $callback  Receives the value, sanitized with the schema, and returns query arguments:
-	 *                               a `meta_query`, for products also a `tax_query`, `include_ids` or
-	 *                               `exclude_ids`. Core adds each one to the query with AND. Return a
-	 *                               WP_Error to refuse the value. A `tax_query` is accepted only for
-	 *                               `product`. Any other key, a `tax_query` for another resource, or a key other
-	 *                               than `include_ids` for `customer`, fails the call.
+	 *     @type callable $callback  Receives the value, sanitized with the schema, and returns query clauses,
+	 *                               such as a `meta_query`, `include_ids` or `exclude_ids`. The list ability adds
+	 *                               each one to the query with AND. Return a WP_Error to refuse the value. A clause
+	 *                               that the resource does not accept, or a value that is not an array of
+	 *                               clause arrays, fails the call and is logged.
 	 *                               `include_ids` and `exclude_ids` are lists of object IDs. Use them for
 	 *                               data that is not meta, such as order items or a table of the extension:
 	 *                               query that data and return the IDs that match. Core keeps the IDs that
@@ -507,13 +509,11 @@ class ExtensibleAbilities {
 	}

 	/**
-	 * Get the query arguments of the filters in the input of a list ability.
-	 * Each key holds one clause for each filter, so `array_merge_recursive()`
-	 * adds them to WP_Query, wc_get_orders() or WP_User_Query arguments with AND. `post__in`
-	 * holds the IDs that every `include_ids` filter returned, without the IDs
-	 * that any `exclude_ids` filter returned, or `0` when none remain, because
-	 * an empty `post__in` does not filter. Without `include_ids`, the excluded
-	 * IDs go to `post__not_in`, which WP_Query ignores next to `post__in`.
+	 * Get the query clauses of the filters in the input of a list ability, which maps them to its backend.
+	 * Each clause key holds one clause for each filter, to add to the query with AND. `include_ids` holds the
+	 * IDs that every `include_ids` filter returned, without the IDs that any `exclude_ids` filter returned, or
+	 * `0` when none remain, so a backend that ignores an empty list still matches nothing. Without
+	 * `include_ids`, `exclude_ids` holds the excluded IDs.
 	 *
 	 * @since 11.3.0
 	 *
@@ -522,6 +522,7 @@ class ExtensibleAbilities {
 	 * @return array<string, array>|\WP_Error The error of a value that fails its schema, or of a filter that refused its value or failed.
 	 */
 	public static function get_query_args( string $resource_name, array $input ) {
+		$clauses    = self::$resources[ $resource_name ]['query_clauses'] ?? null;
 		$query_args = array();
 		$include    = null;
 		$exclude    = array();
@@ -541,45 +542,40 @@ class ExtensibleAbilities {
 					return $value;
 				}
 				try {
-					$clauses = call_user_func( $args['callback'], $value );
+					$filter_clauses = call_user_func( $args['callback'], $value );
 				} catch ( \Throwable $e ) {
 					self::log_error( sprintf( 'Ability filter "%s.%s" of "%s" failed: %s', $namespace, $filter, $resource_name, $e->getMessage() ) );
-					$clauses = null;
+					return self::get_filter_failed_error( $namespace . '.' . $filter );
+				}
+				if ( is_wp_error( $filter_clauses ) ) {
+					return $filter_clauses;
 				}
-				if ( is_wp_error( $clauses ) ) {
-					return $clauses;
+				if ( ! is_array( $filter_clauses ) || in_array( false, array_map( 'is_array', $filter_clauses ), true ) ) {
+					self::log_error( sprintf( 'Ability filter "%s.%s" of "%s" must return an array of clause arrays.', $namespace, $filter, $resource_name ) );
+					return self::get_filter_failed_error( $namespace . '.' . $filter );
 				}
-				if (
-					! is_array( $clauses )
-					|| ! empty( array_diff_key( $clauses, array_flip( self::QUERY_ARG_KEYS ) ) )
-					|| ( 'product' !== $resource_name && isset( $clauses['tax_query'] ) )
-					|| ( 'customer' === $resource_name && ! empty( array_diff_key( $clauses, array( 'include_ids' => true ) ) ) )
-					|| in_array( false, array_map( 'is_array', $clauses ), true )
-				) {
-					return new \WP_Error(
-						'woocommerce_ability_extension_filter_failed',
-						/* translators: %s: filter name, such as subscriptions.subscribable. */
-						sprintf( __( 'The "%s" filter failed.', 'woocommerce' ), $namespace . '.' . $filter ),
-						array( 'status' => 500 )
-					);
+				$refused = null === $clauses ? array() : array_diff( array_keys( $filter_clauses ), $clauses );
+				if ( ! empty( $refused ) ) {
+					self::log_error( sprintf( 'Ability filter "%s.%s" of "%s" returned the clause "%s". The resource accepts: %s.', $namespace, $filter, $resource_name, implode( '", "', $refused ), implode( ', ', $clauses ) ) );
+					return self::get_filter_failed_error( $namespace . '.' . $filter );
 				}
-				if ( isset( $clauses['include_ids'] ) ) {
-					$filter_ids = self::get_filter_ids( $clauses['include_ids'], $namespace . '.' . $filter );
+				if ( isset( $filter_clauses['include_ids'] ) ) {
+					$filter_ids = self::get_filter_ids( $filter_clauses['include_ids'], $namespace . '.' . $filter, $resource_name );
 					if ( is_wp_error( $filter_ids ) ) {
 						return $filter_ids;
 					}
 					$include = null === $include ? $filter_ids : array_values( array_intersect( $include, $filter_ids ) );
-					unset( $clauses['include_ids'] );
+					unset( $filter_clauses['include_ids'] );
 				}
-				if ( isset( $clauses['exclude_ids'] ) ) {
-					$filter_ids = self::get_filter_ids( $clauses['exclude_ids'], $namespace . '.' . $filter );
+				if ( isset( $filter_clauses['exclude_ids'] ) ) {
+					$filter_ids = self::get_filter_ids( $filter_clauses['exclude_ids'], $namespace . '.' . $filter, $resource_name );
 					if ( is_wp_error( $filter_ids ) ) {
 						return $filter_ids;
 					}
 					$exclude = array_merge( $exclude, $filter_ids );
-					unset( $clauses['exclude_ids'] );
+					unset( $filter_clauses['exclude_ids'] );
 				}
-				foreach ( $clauses as $key => $clause ) {
+				foreach ( $filter_clauses as $key => $clause ) {
 					if ( empty( $clause ) ) {
 						continue;
 					}
@@ -588,10 +584,10 @@ class ExtensibleAbilities {
 			}
 		}
 		if ( null !== $include ) {
-			$include                = array_values( array_diff( $include, $exclude ) );
-			$query_args['post__in'] = empty( $include ) ? array( 0 ) : $include;
+			$include                   = array_values( array_diff( $include, $exclude ) );
+			$query_args['include_ids'] = empty( $include ) ? array( 0 ) : $include;
 		} elseif ( ! empty( $exclude ) ) {
-			$query_args['post__not_in'] = array_values( array_unique( $exclude ) );
+			$query_args['exclude_ids'] = array_values( array_unique( $exclude ) );
 		}
 		return $query_args;
 	}
@@ -599,11 +595,12 @@ class ExtensibleAbilities {
 	/**
 	 * The IDs that a filter returned, as integers.
 	 *
-	 * @param array  $ids    IDs from the filter callback.
-	 * @param string $filter Filter name, such as `subscriptions.related_to`.
+	 * @param array  $ids           IDs from the filter callback.
+	 * @param string $filter        Filter name, such as `subscriptions.related_to`.
+	 * @param string $resource_name Resource.
 	 * @return int[]|\WP_Error An error when an ID is not a positive integer.
 	 */
-	private static function get_filter_ids( array $ids, string $filter ) {
+	private static function get_filter_ids( array $ids, string $filter, string $resource_name ) {
 		$valid = array_filter(
 			array_map(
 				static fn( $id ) => filter_var( $id, FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1 ) ) ),
@@ -611,16 +608,57 @@ class ExtensibleAbilities {
 			)
 		);
 		if ( count( $valid ) !== count( $ids ) ) {
-			return new \WP_Error(
-				'woocommerce_ability_extension_filter_failed',
-				/* translators: %s: filter name, such as subscriptions.subscribable. */
-				sprintf( __( 'The "%s" filter failed.', 'woocommerce' ), $filter ),
-				array( 'status' => 500 )
-			);
+			self::log_error( sprintf( 'Ability filter "%s" of "%s" must return IDs that are positive integers.', $filter, $resource_name ) );
+			return self::get_filter_failed_error( $filter );
 		}
 		return array_values( array_unique( $valid ) );
 	}

+	/**
+	 * The error of a query filter that failed. The log names the cause for the extension developer.
+	 *
+	 * @param string $filter Filter name, such as `subscriptions.related_to`.
+	 * @return \WP_Error
+	 */
+	private static function get_filter_failed_error( string $filter ): \WP_Error {
+		return new \WP_Error(
+			'woocommerce_ability_extension_filter_failed',
+			sprintf(
+				/* translators: %s: filter name, such as subscriptions.subscribable. */
+				__( 'The "%s" filter failed. Call again without it, and ask the store owner to check the extensible-abilities log in WooCommerce > Status > Logs.', 'woocommerce' ),
+				$filter
+			),
+			array( 'status' => 500 )
+		);
+	}
+
+	/**
+	 * The error of a list ability that could not apply the clauses of the filters in its input, with the
+	 * same code, message and log as a filter that failed.
+	 *
+	 * @internal
+	 *
+	 * @since 11.5.0
+	 *
+	 * @param string $resource_name Resource.
+	 * @param array  $input         Input of the list ability.
+	 * @param string $reason        Cause, for the log.
+	 * @return \WP_Error
+	 */
+	public static function query_clauses_failed_error( string $resource_name, array $input, string $reason ): \WP_Error {
+		$names = array();
+		foreach ( self::$filters[ $resource_name ] ?? array() as $namespace => $filters ) {
+			foreach ( array_keys( $filters ) as $filter ) {
+				if ( array_key_exists( $filter, (array) ( $input['filters']['extensions'][ $namespace ] ?? array() ) ) ) {
+					$names[] = $namespace . '.' . $filter;
+				}
+			}
+		}
+		$filter = implode( '", "', $names );
+		self::log_error( sprintf( 'Ability filter "%s" of "%s" failed: %s', $filter, $resource_name, $reason ) );
+		return self::get_filter_failed_error( $filter );
+	}
+
 	/**
 	 * Register a validator that can refuse a change to an object of a resource.
 	 *
diff --git a/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php b/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
index 82903ffdd52..9230e19af11 100644
--- a/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
+++ b/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
@@ -173,6 +173,10 @@ class AbilitiesLoader {
 	/**
 	 * Register the resources that Core's abilities format, so extensions can register fields on them.
 	 *
+	 * The query clauses name what each list ability runs. `include_ids` and `exclude_ids` hold the IDs of the
+	 * objects the ability lists. Customers hold user IDs, not the customer IDs of the `include` and `exclude`
+	 * inputs, and a customer `exclude_ids` keeps every guest. A product term `meta_query` reads term meta.
+	 *
 	 * @internal
 	 *
 	 * @since 11.3.0
@@ -185,6 +189,7 @@ class AbilitiesLoader {
 			array(
 				'object_id_callback'   => $data_id,
 				'object_type_callback' => $data_type,
+				'query_clauses'        => array( 'meta_query', 'tax_query', 'include_ids', 'exclude_ids' ),
 			)
 		);
 		ExtensibleAbilities::register_resource(
@@ -192,6 +197,7 @@ class AbilitiesLoader {
 			array(
 				'object_id_callback'   => $data_id,
 				'object_type_callback' => $data_type,
+				'query_clauses'        => array( 'meta_query', 'include_ids', 'exclude_ids' ),
 			)
 		);
 		ExtensibleAbilities::register_resource(
@@ -206,6 +212,7 @@ class AbilitiesLoader {
 			array(
 				'object_id_callback'   => $data_id,
 				'object_type_callback' => $data_type,
+				'query_clauses'        => array( 'include_ids', 'exclude_ids' ),
 			)
 		);
 		ExtensibleAbilities::register_resource(
@@ -220,6 +227,7 @@ class AbilitiesLoader {
 			array(
 				'object_id_callback'   => $data_id,
 				'object_type_callback' => static fn( \WC_Customer $customer ): string => $customer->get_role(),
+				'query_clauses'        => array( 'include_ids', 'exclude_ids' ),
 			)
 		);
 		ExtensibleAbilities::register_resource(
@@ -234,6 +242,7 @@ class AbilitiesLoader {
 			array(
 				'object_id_callback'   => static fn( \WP_Term $term ): int => $term->term_id,
 				'object_type_callback' => static fn( \WP_Term $term ): string => $term->taxonomy,
+				'query_clauses'        => array( 'meta_query', 'include_ids', 'exclude_ids' ),
 			)
 		);
 	}
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
index 48383f7cb4c..8ba87c30311 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CustomersQuery.php
@@ -111,7 +111,18 @@ class CustomersQuery extends AbstractDomainAbility implements AbilityDefinition
 			return $extension_args;
 		}

-		$exclude  = $input['exclude'] ?? array();
+		$exclude = $input['exclude'] ?? array();
+		if ( ! empty( $extension_args['exclude_ids'] ) ) {
+			global $wpdb;
+			$table    = CustomerLookup::get_db_table_name();
+			$user_ids = implode( ',', array_map( 'intval', $extension_args['exclude_ids'] ) );
+			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- The IDs are integers and the table name is Core's.
+			$customer_ids = $wpdb->get_col( "SELECT customer_id FROM {$table} WHERE user_id IN ({$user_ids})" );
+			if ( '' !== $wpdb->last_error ) {
+				return ExtensibleAbilities::query_clauses_failed_error( 'customer', $input, 'The customer IDs of the excluded users could not be read: ' . $wpdb->last_error );
+			}
+			$exclude = array_merge( $exclude, array_map( 'intval', $customer_ids ) );
+		}
 		$include  = array_values( array_diff( array_unique( $input['include'] ?? array() ), $exclude ) );
 		$page     = $input['page'] ?? 1;
 		$per_page = $input['per_page'] ?? ( empty( $input['include'] ) ? self::DEFAULT_PER_PAGE : max( 1, count( $include ) ) );
@@ -147,7 +158,7 @@ class CustomersQuery extends AbstractDomainAbility implements AbilityDefinition
 			);
 		}

-		$users = $extension_args['post__in'] ?? array();
+		$users = $extension_args['include_ids'] ?? array();
 		if ( isset( $input['user_id'] ) ) {
 			$users = ( empty( $users ) || in_array( $input['user_id'], array_map( 'intval', $users ), true ) ) ? array( $input['user_id'] ) : array( 0 );
 		}
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderRefundsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderRefundsQuery.php
index 4394daadb66..009fd1a5a78 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderRefundsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderRefundsQuery.php
@@ -82,6 +82,11 @@ class OrderRefundsQuery extends AbstractDomainAbility implements AbilityDefiniti
 			return $order;
 		}

+		$extension_args = ExtensibleAbilities::get_query_args( 'order_refund', $input );
+		if ( is_wp_error( $extension_args ) ) {
+			return $extension_args;
+		}
+
 		$refunds = $order->get_refunds();
 		usort(
 			$refunds,
@@ -98,6 +103,18 @@ class OrderRefundsQuery extends AbstractDomainAbility implements AbilityDefiniti
 				static fn( \WC_Order_Refund $refund ): bool => in_array( $refund->get_id(), $input['include'], true )
 			);
 		}
+		if ( isset( $extension_args['include_ids'] ) ) {
+			$refunds = array_filter(
+				$refunds,
+				static fn( \WC_Order_Refund $refund ): bool => in_array( $refund->get_id(), $extension_args['include_ids'], true )
+			);
+		}
+		if ( isset( $extension_args['exclude_ids'] ) ) {
+			$refunds = array_filter(
+				$refunds,
+				static fn( \WC_Order_Refund $refund ): bool => ! in_array( $refund->get_id(), $extension_args['exclude_ids'], true )
+			);
+		}

 		$refunds = array_values( $refunds );
 		ExtensibleAbilities::prime( 'order_refund', $refunds );
@@ -123,7 +140,7 @@ class OrderRefundsQuery extends AbstractDomainAbility implements AbilityDefiniti
 	 * @return array
 	 */
 	private static function get_input_schema(): array {
-		return array(
+		$schema = array(
 			'type'                 => 'object',
 			'properties'           => array(
 				'id'      => array(
@@ -145,6 +162,8 @@ class OrderRefundsQuery extends AbstractDomainAbility implements AbilityDefiniti
 			'required'             => array( 'id' ),
 			'additionalProperties' => false,
 		);
+
+		return ExtensibleAbilities::add_query_filters_schema( $schema, 'order_refund' );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
index a8d9fa343e5..db03946623e 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/OrdersQuery.php
@@ -194,18 +194,18 @@ class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {
 			return $extension_args;
 		}

-		if ( isset( $extension_args['post__not_in'] ) ) {
-			$args['exclude'] = array_merge( (array) ( $args['exclude'] ?? array() ), $extension_args['post__not_in'] );
-			unset( $extension_args['post__not_in'] );
+		if ( isset( $extension_args['exclude_ids'] ) ) {
+			$args['exclude'] = array_merge( (array) ( $args['exclude'] ?? array() ), $extension_args['exclude_ids'] );
+			unset( $extension_args['exclude_ids'] );
 		}

 		$hpos = OrderUtil::custom_orders_table_usage_is_enabled();

 		// Both stores merge extension args recursively, which would add the extension IDs to include instead of intersecting them.
 		$include = empty( $input['include'] ) ? null : $input['include'];
-		if ( isset( $extension_args['post__in'] ) ) {
-			$include = null === $include ? $extension_args['post__in'] : array_intersect( $include, $extension_args['post__in'] );
-			unset( $extension_args['post__in'] );
+		if ( isset( $extension_args['include_ids'] ) ) {
+			$include = null === $include ? $extension_args['include_ids'] : array_intersect( $include, $extension_args['include_ids'] );
+			unset( $extension_args['include_ids'] );
 		}
 		if ( null !== $search ) {
 			if ( $hpos ) {
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
index a3465f3a59d..6f8a9d07354 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductTermsQuery.php
@@ -123,10 +123,10 @@ class ProductTermsQuery extends AbstractDomainAbility implements AbilityDefiniti
 		if ( isset( $extension_args['meta_query'] ) ) {
 			$args['meta_query'] = $extension_args['meta_query']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- The filters of extensions read term meta.
 		}
-		$excluded = $extension_args['post__not_in'] ?? array();
-		$include  = $args['include'] ?? $extension_args['post__in'] ?? null;
-		if ( isset( $args['include'], $extension_args['post__in'] ) ) {
-			$include = array_intersect( $include, $extension_args['post__in'] );
+		$excluded = $extension_args['exclude_ids'] ?? array();
+		$include  = $args['include'] ?? $extension_args['include_ids'] ?? null;
+		if ( isset( $args['include'], $extension_args['include_ids'] ) ) {
+			$include = array_intersect( $include, $extension_args['include_ids'] );
 		}
 		if ( null !== $include ) {
 			// WordPress ignores exclude next to include, and an empty include does not filter.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
index a855f6cc371..f39df5e704a 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
@@ -179,6 +179,16 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 		if ( is_wp_error( $extension_args ) ) {
 			return $extension_args;
 		}
+		$id_query_vars = array(
+			'include_ids' => 'post__in',
+			'exclude_ids' => 'post__not_in',
+		);
+		foreach ( $id_query_vars as $key => $query_var ) {
+			if ( isset( $extension_args[ $key ] ) ) {
+				$extension_args[ $query_var ] = $extension_args[ $key ];
+				unset( $extension_args[ $key ] );
+			}
+		}

 		foreach ( self::TERM_FILTERS as $field => $taxonomy ) {
 			if ( ! empty( $input[ $field ] ) ) {
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CustomersQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CustomersQueryTest.php
index 53a8316b09c..0efafa8a464 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CustomersQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CustomersQueryTest.php
@@ -462,7 +462,20 @@ class CustomersQueryTest extends \WC_Unit_Test_Case {

 		$members = static fn( bool $value ) => $input + array( 'filters' => array( 'extensions' => array( 'test-ext' => array( 'members' => $value ) ) ) );
 		$this->assertSame( array( $registered[1][0] ), array_column( $ability->execute( $members( true ) )['customers'], 'id' ), 'An extension filter narrows include instead of adding to it.' );
-		$this->assertSame( 'woocommerce_ability_extension_filter_failed', $ability->execute( $members( false ) )->get_error_code(), 'A customer filter can only include user IDs.' );
+		$this->assertEqualsCanonicalizing( array( $registered[0][0], $guest ), array_column( $ability->execute( $members( false ) )['customers'], 'id' ), 'An extension filter excludes by user ID and keeps guests.' );
+		$listed = $ability->execute( array( 'filters' => array( 'extensions' => array( 'test-ext' => array( 'members' => false ) ) ) ) );
+		$this->assertNotContains( $registered[1][0], array_column( $listed['customers'], 'id' ), 'An extension filter also excludes when include is not set.' );
+
+		global $wpdb;
+		$break      = static fn( string $query ): string => str_contains( $query, 'WHERE user_id IN' ) ? 'SELECT missing_column FROM ' . $wpdb->users : $query;
+		$suppressed = $wpdb->suppress_errors( true );
+		add_filter( 'query', $break );
+		$failed = $ability->execute( $members( false ) );
+		remove_filter( 'query', $break );
+		$wpdb->suppress_errors( $suppressed );
+		$this->assertWPError( $failed, 'A failed lookup of the excluded users fails the call instead of listing them.' );
+		$this->assertSame( 'woocommerce_ability_extension_filter_failed', $failed->get_error_code() );
+		$this->assertStringStartsWith( 'The "test-ext.members" filter failed.', $failed->get_error_message() );

 		update_option( 'woocommerce_feature_extensible_abilities_enabled', 'no' );
 		$this->assertSame( $baseline, CustomersQuery::execute( $input ) );
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index 0b706233e41..64c8197f84c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -777,9 +777,17 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Should return the error of a filter that refuses its value, or that returns arguments Core does not support.
+	 * @testdox Should return the error of a filter that refuses its value, or log and refuse arguments the list ability does not accept.
 	 */
 	public function test_a_filter_that_refuses_or_fails_returns_an_error(): void {
+		$logged = array();
+		$logger = $this->getMockBuilder( \WC_Logger_Interface::class )->getMock();
+		$logger->method( 'error' )->willReturnCallback(
+			static function ( string $message ) use ( &$logged ) {
+				$logged[] = $message;
+			}
+		);
+		add_filter( 'woocommerce_logging_class', static fn() => $logger );
 		add_action(
 			'woocommerce_extensible_abilities_init',
 			static function () {
@@ -811,6 +819,15 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		$this->assertSame( 'woocommerce_ability_extension_filter_failed', $failed->get_data()['code'] );
 		$this->assertSame( 'woocommerce_ability_extension_filter_failed', $taxed->get_data()['code'] );
 		$this->assertSame( 'woocommerce_ability_extension_filter_failed', $scalar->get_data()['code'] );
+		$this->assertSame( 'The "test-ext.taxed" filter failed. Call again without it, and ask the store owner to check the extensible-abilities log in WooCommerce > Status > Logs.', $taxed->get_data()['message'] );
+		$this->assertSame(
+			array(
+				'Ability filter "test-ext.broken" of "order" returned the clause "post__in". The resource accepts: meta_query, include_ids, exclude_ids.',
+				'Ability filter "test-ext.taxed" of "order" returned the clause "tax_query". The resource accepts: meta_query, include_ids, exclude_ids.',
+				'Ability filter "test-ext.scalar" of "product" must return an array of clause arrays.',
+			),
+			$logged
+		);
 	}

 	/**
@@ -868,6 +885,51 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		$this->assertSame( array( 'meta_query' => array( array( array( 'key' => 'gift' ) ) ) ), ExtensibleAbilities::get_query_args( 'ticket', $input ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
 	}

+	/**
+	 * @testdox Should accept only the clauses that the resource names, any clause when it names none, and return the IDs as include_ids and exclude_ids.
+	 */
+	public function test_the_resource_names_the_clauses_it_accepts(): void {
+		$register_resources = static function () {
+			ExtensibleAbilities::register_resource( 'ticket', self::get_ticket_resource_args() );
+			ExtensibleAbilities::register_resource( 'id_ticket', array( 'query_clauses' => array( 'include_ids', 'exclude_ids' ) ) + self::get_ticket_resource_args() );
+		};
+		$register           = static function () {
+			foreach ( array( 'ticket', 'id_ticket' ) as $resource ) {
+				ExtensibleAbilities::register_query_filter(
+					array(
+						'resource'  => $resource,
+						'namespace' => 'test-ext',
+						'filter'    => 'tagged',
+						'schema'    => array( 'type' => 'boolean' ),
+						'callback'  => static fn( bool $tagged ) => $tagged
+							? array(
+								'tax_query'   => array( array( 'taxonomy' => 'ticket_tag' ) ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
+								'include_ids' => array( 3, 5 ),
+							)
+							: array( 'exclude_ids' => array( 7 ) ),
+					)
+				);
+			}
+		};
+		add_action( 'woocommerce_extensible_abilities_resources_init', $register_resources );
+		add_action( 'woocommerce_extensible_abilities_init', $register );
+		$this->get_ability( 'woocommerce/products-query' )['input_schema'];
+		remove_action( 'woocommerce_extensible_abilities_resources_init', $register_resources );
+		remove_action( 'woocommerce_extensible_abilities_init', $register );
+
+		$input = static fn( bool $tagged ) => array( 'filters' => array( 'extensions' => array( 'test-ext' => array( 'tagged' => $tagged ) ) ) );
+
+		$this->assertSame(
+			array(
+				'tax_query'   => array( array( array( 'taxonomy' => 'ticket_tag' ) ) ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
+				'include_ids' => array( 3, 5 ),
+			),
+			ExtensibleAbilities::get_query_args( 'ticket', $input( true ) )
+		);
+		$this->assertSame( 'woocommerce_ability_extension_filter_failed', ExtensibleAbilities::get_query_args( 'id_ticket', $input( true ) )->get_error_code(), 'The resource does not name tax_query.' );
+		$this->assertSame( array( 'exclude_ids' => array( 7 ) ), ExtensibleAbilities::get_query_args( 'id_ticket', $input( false ) ) );
+	}
+
 	/**
 	 * @testdox Should refuse a filter whose namespace or name is only numbers.
 	 */
@@ -1141,6 +1203,54 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
 		}
 	}

+	/**
+	 * @testdox Should apply the include_ids and exclude_ids of an extension filter in the $resource_name list ability.
+	 * @dataProvider provide_filtered_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_list_abilities_apply_extension_filters( string $resource_name, string $ability, string $list_key, callable $create_input ): void {
+		$target = 0;
+		$this->add_test_action(
+			'woocommerce_extensible_abilities_init',
+			static function () use ( $resource_name, &$target ) {
+				ExtensibleAbilities::register_query_filter(
+					array(
+						'resource'  => $resource_name,
+						'namespace' => 'test-ext',
+						'filter'    => 'only',
+						'schema'    => array( 'type' => 'boolean' ),
+						'callback'  => static function ( bool $only ) use ( &$target ) {
+							return array( $only ? 'include_ids' : 'exclude_ids' => array( $target ) );
+						},
+					)
+				);
+			}
+		);
+
+		$input  = $create_input();
+		$id_key = 'customer' === $resource_name ? 'user_id' : 'id';
+		$ids    = array_column( $this->run_ability( $ability, $input )[ $list_key ], $id_key );
+		$target = $ids[0];
+		$run    = fn( bool $only ) => array_column( $this->run_ability( $ability, $input + array( 'filters' => array( 'extensions' => array( 'test-ext' => array( 'only' => $only ) ) ) ) )[ $list_key ], $id_key );
+
+		$this->assertCount( 2, $ids );
+		$this->assertSame( array( $ids[0] ), $run( true ) );
+		$this->assertSame( array( $ids[1] ), $run( false ) );
+	}
+
+	/**
+	 * List abilities that apply extension filters, with a fixture factory for each.
+	 *
+	 * @return array<string, array>
+	 */
+	public function provide_filtered_list_abilities(): array {
+		return array_intersect_key( $this->provide_list_abilities(), array_flip( array( 'product', 'customer', 'order_refund' ) ) );
+	}
+
 	/**
 	 * List abilities that prime their objects, with a fixture factory for each.
 	 *