[ 'ids' => int[], 'next_cursor' => int|null, 'total' => int ] * * Pagination is keyset (seek) based on the product ID: * - Only IDs strictly greater than $cursor are returned. * - Results are ordered by ID ascending. * - At most min($limit, 200) IDs are returned (hard cap 200, floor 1). * - next_cursor is the last RAW fetched ID (before excludes are removed) when * the page is full, or null when the query returned a short page (the end * of the matching set has been reached). Because next_cursor is computed * BEFORE excludes are stripped, a page whose IDs are all excluded still * advances the cursor so the client can keep looping. * * Exclusion / total semantics (documented, deliberately simple): * - $total is the builder's full match count for $filters, but ONLY on the * first page (cursor 0); it is 0 on every subsequent page. The client reads * total once (page 0) to size the selection, then walks pages by cursor, so * the count query runs once instead of once per keyset step. Excluded IDs * are NOT subtracted server-side; the client computes the effective * selection size as total - excluded.size. * - Excluded IDs are removed from the returned 'ids' page-wise, AFTER the * keyset page is fetched and AFTER next_cursor is computed. * * Query layer (injectable): the constructor accepts an optional $query_runner * callable of shape ( array $args ) => [ 'ids' => int[], 'total' => int ]. This * keeps resolve() unit-testable without WordPress and lets the REST controller * (a separate task) swap in a richer implementation. $args carries the keys * 'filters' (assoc), 'cursor' (int) and 'limit' (int). * * Default query_runner tradeoff: it reuses the existing request→builder mapping * (ProductFilterMapper::fromRequest + applyToBuilder) so this endpoint honors * the exact same positive filter semantics as /aipt/v1/products, but borrows * only the resolved wc_get_products() args and re-runs them as a lean, * ID-only, keyset-bounded WP_Query — never hydrating full product objects and * never enumerating the whole matching set in memory. KNOWN v1 LIMITATION: the * builder's negative "NOT EXISTS" predicates (no_tags / no_image / no_gallery / * no_sku / not_featured / not_on_sale) live in its private custom-SQL path and * are not exposed through getArgs(), so they are not reflected by the default * runner. Callers needing those must inject a query_runner that applies them. * * @since 2.6.5 */ class SelectionResolver { /** * Hard upper bound on IDs returned per resolve() call. */ private const MAX_LIMIT = 200; /** * Lower bound on IDs returned per resolve() call. */ private const MIN_LIMIT = 1; /** * Injected query layer: ( array $args ) => array{ids:int[],total:int}. * * @var callable */ private $query_runner; /** * @param callable|null $query_runner Optional query layer. Defaults to a * lean ID-only WP_Query over the builder args. */ public function __construct( ?callable $query_runner = null ) { $this->query_runner = null !== $query_runner ? $query_runner : array( $this, 'default_query_runner' ); } /** * Resolve one bounded keyset page of matching product IDs. * * @param array $filters Assoc filter array (same shape /aipt/v1/products accepts). * @param array $excluded_ids Manually de-selected product IDs to strip from the page. * @param int $cursor Keyset cursor; only IDs > cursor are returned. * @param int $limit Requested page size (bounded to [1, 200]). * @return array{ids:int[],next_cursor:int|null,total:int} `total` is the full * filtered match count on the first page (cursor 0) and 0 thereafter. */ public function resolve( array $filters, array $excluded_ids, int $cursor, int $limit ): array { $safe_cursor = max( 0, $cursor ); $bounded_limit = max( self::MIN_LIMIT, min( $limit, self::MAX_LIMIT ) ); $result = call_user_func( $this->query_runner, array( 'filters' => $filters, 'cursor' => $safe_cursor, 'limit' => $bounded_limit, ) ); $returned_ids = array(); if ( is_array( $result ) && isset( $result['ids'] ) && is_array( $result['ids'] ) ) { $returned_ids = array_map( 'absint', array_values( $result['ids'] ) ); } $page_count = count( $returned_ids ); $keyset_ids = array_values( array_filter( $returned_ids, static function ( $id ) use ( $safe_cursor ) { return $id > $safe_cursor; } ) ); sort( $keyset_ids, SORT_NUMERIC ); $next_cursor = ( $page_count >= $bounded_limit && ! empty( $keyset_ids ) ) ? (int) end( $keyset_ids ) : null; $excluded_set = array(); foreach ( $excluded_ids as $excluded_id ) { $excluded_set[ absint( $excluded_id ) ] = true; } $ids = array_values( array_filter( $keyset_ids, static function ( $id ) use ( $excluded_set ) { return ! isset( $excluded_set[ $id ] ); } ) ); $total = ( is_array( $result ) && isset( $result['total'] ) ) ? absint( $result['total'] ) : 0; return array( 'ids' => $ids, 'next_cursor' => $next_cursor, 'total' => $total, ); } /** * Default production query layer: a lean, ID-only, keyset-bounded WP_Query * built from the shared ProductFilter mapping. See the class docblock for * the reuse rationale and the negative-filter v1 limitation. * * @param array $args { filters:array, cursor:int, limit:int }. * @return array{ids:int[],total:int} */ private function default_query_runner( array $args ): array { $filters = ( isset( $args['filters'] ) && is_array( $args['filters'] ) ) ? $args['filters'] : array(); $cursor = isset( $args['cursor'] ) ? absint( $args['cursor'] ) : 0; $limit = isset( $args['limit'] ) ? absint( $args['limit'] ) : self::MAX_LIMIT; $mapped = ProductFilterMapper::fromRequest( $filters ); $builder = new ProductFilterBuilder(); ProductFilterMapper::applyToBuilder( $builder, $mapped ); $built_args = $builder->getArgs(); $query_args = array( 'post_type' => 'product', 'post_status' => isset( $built_args['status'] ) ? $built_args['status'] : array( 'publish', 'draft', 'pending', 'private' ), 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 'posts_per_page' => $limit, 'ignore_sticky_posts' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ); if ( isset( $built_args['include'] ) && is_array( $built_args['include'] ) ) { $include = array_map( 'absint', $built_args['include'] ); if ( empty( $include ) ) { return array( 'ids' => array(), 'total' => 0, ); } $query_args['post__in'] = $include; } if ( isset( $built_args['tax_query'] ) ) { $query_args['tax_query'] = $built_args['tax_query']; } if ( isset( $built_args['meta_query'] ) ) { $query_args['meta_query'] = $built_args['meta_query']; } $page_args = $query_args; $page_args['aipt_keyset_cursor'] = $cursor; $where_cb = static function ( $where, $query ) { global $wpdb; $keyset_cursor = (int) $query->get( 'aipt_keyset_cursor', 0 ); if ( $keyset_cursor > 0 ) { $where .= $wpdb->prepare( " AND {$wpdb->posts}.ID > %d", $keyset_cursor ); } return $where; }; add_filter( 'posts_where', $where_cb, 10, 2 ); $query = new \WP_Query( $page_args ); remove_filter( 'posts_where', $where_cb, 10 ); $ids = array_map( 'absint', (array) $query->posts ); $total = 0; if ( 0 === $cursor ) { $count_query = new \WP_Query( array_merge( $query_args, array( 'posts_per_page' => 1 ) ) ); $total = (int) $count_query->found_posts; } return array( 'ids' => $ids, 'total' => $total, ); } }