CatalogSearch
extends AbstractCatalogRoute
in package
UCP `POST /catalog/search` route.
Table of Contents
- IDENTIFIER = 'ucp-catalog-search'
- The route identifier.
- MAX_CATEGORY_FILTERS = 25
- Maximum number of `filters.categories` references honoured per request.
- MAX_QUERY_LENGTH = 256
- Maximum accepted length of the free-text `query`, in bytes.
- SCHEMA_TYPE = ''
- The routes schema.
- SCHEMA_VERSION = 1
- The routes schema version.
- $namespace : string
- Route namespace.
- $schema : AbstractSchema
- Schema class instance.
- $schema_controller : SchemaController
- Schema Controller instance.
- $mapper : UcpProductMapper|null
- Product mapper, holding per-response state.
- __construct() : mixed
- Constructor.
- get_args() : array<string|int, mixed>
- Get method arguments for this REST route.
- get_collection_params() : array<string|int, mixed>
- Retrieves the query params for the collections.
- get_item_schema() : array<string|int, mixed>
- Get item schema properties.
- get_namespace() : string
- Get the namespace for this route.
- get_path() : string
- Get the path of this REST route.
- get_path_regex() : string
- Get the path of this rest route.
- get_response() : WP_REST_Response
- Get the route response based on the type of request.
- prepare_item_for_response() : WP_REST_Response
- Prepare a single item for response.
- set_namespace() : mixed
- Set the namespace for this route.
- currency_warning() : array<string|int, mixed>|null
- Build a non-fatal warning when the requested context currency differs from the store currency.
- error_to_response() : WP_REST_Response
- Converts an error to a response object. Based on \WP_REST_Server.
- get_capability() : string
- The UCP capability this route serves.
- get_context() : array<string|int, mixed>
- Extract the recognized context fields from a request.
- get_context_param() : array<string|int, mixed>
- Retrieves the context param.
- get_request_args() : array<string|int, mixed>
- Request body arguments accepted by this route.
- get_response_by_request_method() : WP_REST_Response
- Get the route response based on the type of request.
- get_route_delete_response() : WP_REST_Response
- Get route response for DELETE requests.
- get_route_error_response() : WP_Error
- Get route response when something went wrong.
- get_route_error_response_from_object() : WP_Error
- Get route response when something went wrong and the supplied error is a WP_Error. This currently only happens when an item in the cart is out of stock, partially out of stock, can only be bought individually, or when the item is not purchasable.
- get_route_post_response() : WP_REST_Response
- Handle the request.
- get_route_response() : WP_REST_Response
- Get route response for GET requests.
- get_route_update_response() : WP_REST_Response
- Get route response for PUT requests.
- mapper() : UcpProductMapper
- Get the product mapper for the response being built.
- prepare_links() : array<string|int, mixed>
- Prepare links for the request.
- prepare_response_for_collection() : array<string|int, mixed>|mixed
- Prepares a response for insertion into a collection.
- resolve_product() : array<string|int, mixed>|null
- Resolve a catalog product by product or variation ID.
- store_currency() : string
- Get the store's configured currency code.
- truncation_messages() : array<string|int, mixed>
- Build `messages` entries for products whose variant list was cut short.
- ucp_error() : WP_REST_Response
- Build a transport-level error response.
- ucp_metadata() : array<string|int, mixed>
- Build the `ucp` envelope for a response from this route.
- decode_cursor() : int
- Decode a cursor string back into a numeric offset.
- encode_cursor() : string
- Encode a pagination offset into an opaque cursor string.
- query_products() : array<string|int, mixed>
- Run the Store API product query, recounting when the cursor is past the end.
- resolve_category_slugs() : array<string|int, mixed>
- Resolve category references to the term slugs ProductQuery filters on.
- store_api_params() : array<string|int, mixed>
- Translate the UCP search body into Store API product collection parameters.
- store_api_request() : WP_REST_Request<string|int, array<string, mixed>>
- Wrap collection parameters in the request object ProductQuery reads.
- validate_search_input() : WP_REST_Response|null
- Validate the search query string and filters before any query runs.
Constants
IDENTIFIER
The route identifier.
public
string
IDENTIFIER
= 'ucp-catalog-search'
MAX_CATEGORY_FILTERS
Maximum number of `filters.categories` references honoured per request.
public
mixed
MAX_CATEGORY_FILTERS
= 25
Each reference costs a term lookup on an unauthenticated endpoint.
MAX_QUERY_LENGTH
Maximum accepted length of the free-text `query`, in bytes.
public
mixed
MAX_QUERY_LENGTH
= 256
The term reaches a LIKE scan over product titles and SKUs, so its length
is paid per row examined.
SCHEMA_TYPE
The routes schema.
public
string
SCHEMA_TYPE
= ''
SCHEMA_VERSION
The routes schema version.
public
int
SCHEMA_VERSION
= 1
Properties
$namespace
Route namespace.
protected
string
$namespace
= 'wc/store/v1'
$schema
Schema class instance.
protected
AbstractSchema
$schema
$schema_controller
Schema Controller instance.
protected
SchemaController
$schema_controller
$mapper
Product mapper, holding per-response state.
private
UcpProductMapper|null
$mapper
Methods
__construct()
Constructor.
public
__construct(SchemaController $schema_controller, AbstractSchema $schema) : mixed
Parameters
- $schema_controller : SchemaController
-
Schema Controller instance.
- $schema : AbstractSchema
-
Schema class for this route.
Return values
mixed —get_args()
Get method arguments for this REST route.
public
get_args() : array<string|int, mixed>
Return values
array<string|int, mixed> — An array of endpoints.get_collection_params()
Retrieves the query params for the collections.
public
get_collection_params() : array<string|int, mixed>
Return values
array<string|int, mixed> — Query parameters for the collection.get_item_schema()
Get item schema properties.
public
get_item_schema() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_namespace()
Get the namespace for this route.
public
get_namespace() : string
Return values
string —get_path()
Get the path of this REST route.
public
get_path() : string
Return values
string —get_path_regex()
Get the path of this rest route.
public
static get_path_regex() : string
Return values
string —get_response()
Get the route response based on the type of request.
public
get_response(WP_REST_Request $request) : WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Request object.
Return values
WP_REST_Response —prepare_item_for_response()
Prepare a single item for response.
public
prepare_item_for_response(mixed $item, WP_REST_Request $request) : WP_REST_Response
Parameters
- $item : mixed
-
Item to format to schema.
- $request : WP_REST_Request
-
Request object.
Return values
WP_REST_Response — $response Response data.set_namespace()
Set the namespace for this route.
public
set_namespace(string $namespace) : mixed
Parameters
- $namespace : string
-
Given namespace.
Return values
mixed —currency_warning()
Build a non-fatal warning when the requested context currency differs from the store currency.
protected
currency_warning(array<string|int, mixed> $context, string $store_currency) : array<string|int, mixed>|null
Catalog amounts are always expressed in the store's own currency; the store performs no FX conversion. When an agent requests a different currency we ignore it for labeling and surface an informational message so the difference is not silently dropped.
Parameters
- $context : array<string|int, mixed>
-
Request context.
- $store_currency : string
-
Store currency code used for labeling.
Return values
array<string|int, mixed>|null —error_to_response()
Converts an error to a response object. Based on \WP_REST_Server.
protected
error_to_response(WP_Error $error) : WP_REST_Response
Parameters
- $error : WP_Error
-
WP_Error instance.
Return values
WP_REST_Response — List of associative arrays with code and message keys.get_capability()
The UCP capability this route serves.
protected
get_capability() : string
Return values
string —get_context()
Extract the recognized context fields from a request.
protected
get_context(WP_REST_Request $request) : array<string|int, mixed>
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
array<string|int, mixed> —get_context_param()
Retrieves the context param.
protected
get_context_param([array<string|int, mixed> $args = array() ]) : array<string|int, mixed>
Ensures consistent descriptions between endpoints, and populates enum from schema.
Parameters
- $args : array<string|int, mixed> = array()
-
Optional. Additional arguments for context parameter. Default empty array.
Return values
array<string|int, mixed> — Context parameter details.get_request_args()
Request body arguments accepted by this route.
protected
get_request_args() : array<string|int, mixed>
Bounds the UCP contract names its own error codes for (query length, price
range) stay in the handler, so the response carries those codes rather than
rest_invalid_param.
Return values
array<string|int, mixed> —get_response_by_request_method()
Get the route response based on the type of request.
protected
get_response_by_request_method(WP_REST_Request $request) : WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Request object.
Return values
WP_REST_Response —get_route_delete_response()
Get route response for DELETE requests.
protected
get_route_delete_response(WP_REST_Request $request) : WP_REST_Response
When implemented, should return a \WP_REST_Response.
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —get_route_error_response()
Get route response when something went wrong.
protected
get_route_error_response(string $error_code, string $error_message[, int $http_status_code = 500 ][, array<string|int, mixed> $additional_data = [] ]) : WP_Error
Parameters
- $error_code : string
-
String based error code.
- $error_message : string
-
User facing error message.
- $http_status_code : int = 500
-
HTTP status. Defaults to 500.
- $additional_data : array<string|int, mixed> = []
-
Extra data (key value pairs) to expose in the error response.
Return values
WP_Error — WP Error object.get_route_error_response_from_object()
Get route response when something went wrong and the supplied error is a WP_Error. This currently only happens when an item in the cart is out of stock, partially out of stock, can only be bought individually, or when the item is not purchasable.
protected
get_route_error_response_from_object(WP_Error $error_object[, int $http_status_code = 500 ][, array<string|int, mixed> $additional_data = [] ]) : WP_Error
Parameters
- $error_object : WP_Error
-
The WP_Error object containing the error.
- $http_status_code : int = 500
-
HTTP status. Defaults to 500.
- $additional_data : array<string|int, mixed> = []
-
Extra data (key value pairs) to expose in the error response.
Return values
WP_Error — WP Error object.get_route_post_response()
Handle the request.
protected
get_route_post_response(WP_REST_Request $request) : WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —get_route_response()
Get route response for GET requests.
protected
get_route_response(WP_REST_Request $request) : WP_REST_Response
When implemented, should return a \WP_REST_Response.
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —get_route_update_response()
Get route response for PUT requests.
protected
get_route_update_response(WP_REST_Request $request) : WP_REST_Response
When implemented, should return a \WP_REST_Response.
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —mapper()
Get the product mapper for the response being built.
protected
mapper() : UcpProductMapper
Return values
UcpProductMapper —prepare_links()
Prepare links for the request.
protected
prepare_links(mixed $item, WP_REST_Request $request) : array<string|int, mixed>
Parameters
- $item : mixed
-
Item to prepare.
- $request : WP_REST_Request
-
Request object.
Return values
array<string|int, mixed> —prepare_response_for_collection()
Prepares a response for insertion into a collection.
protected
prepare_response_for_collection(WP_REST_Response $response) : array<string|int, mixed>|mixed
Parameters
- $response : WP_REST_Response
-
Response object.
Return values
array<string|int, mixed>|mixed — Response data, ready for insertion into collection data.resolve_product()
Resolve a catalog product by product or variation ID.
protected
resolve_product(string $identifier) : array<string|int, mixed>|null
The lookup schema requires variant identifiers to be resolvable, so an identifier naming a variation resolves to its parent product — the variation is returned as one of that product's variants, not as a self-parented product of its own. Callers are told which variation was named so they can correlate it back to the request.
Parameters
- $identifier : string
-
Product ID or variation ID.
Return values
array<string|int, mixed>|null — `{ product, variation_id }`, where `variation_id` is 0 when the identifier named the product itself, or null when nothing catalog-visible matched.store_currency()
Get the store's configured currency code.
protected
store_currency() : string
Return values
string —truncation_messages()
Build `messages` entries for products whose variant list was cut short.
protected
truncation_messages(array<string|int, mixed> $truncations, array<string|int, mixed> $products, string $path_prefix) : array<string|int, mixed>
UCP has no variant pagination, so this message is the only disclosure of a
shortened list — without it an agent reports the missing variants as
nonexistent. path is the RFC 9535 JSONPath of the affected list.
Parameters
- $truncations : array<string|int, mixed>
-
Keyed by product id, from UcpProductMapper::take_truncations().
- $products : array<string|int, mixed>
-
Mapped products, in response order.
- $path_prefix : string
-
JSONPath of the products container.
Return values
array<string|int, mixed> —ucp_error()
Build a transport-level error response.
protected
ucp_error(string $code, string $message, string $severity, int $status) : WP_REST_Response
Parameters
- $code : string
-
Error code.
- $message : string
-
Error message.
- $severity : string
-
Error severity.
- $status : int
-
HTTP status.
Return values
WP_REST_Response —ucp_metadata()
Build the `ucp` envelope for a response from this route.
protected
ucp_metadata(string $status) : array<string|int, mixed>
Parameters
- $status : string
-
Response status,
successorerror.
Return values
array<string|int, mixed> —decode_cursor()
Decode a cursor string back into a numeric offset.
private
decode_cursor(string $cursor) : int
Parameters
- $cursor : string
-
Opaque cursor string.
Return values
int —encode_cursor()
Encode a pagination offset into an opaque cursor string.
private
encode_cursor(int $offset) : string
Parameters
- $offset : int
-
Numeric offset.
Return values
string —query_products()
Run the Store API product query, recounting when the cursor is past the end.
private
query_products(array<string|int, mixed> $params) : array<string|int, mixed>
found_posts is only computed when the page returned rows, and ProductQuery's
own recount keys off page rather than offset, so an empty page at a non-zero
offset is recounted here with a single-row query from the start.
Parameters
- $params : array<string|int, mixed>
-
Store API product collection parameters.
Return values
array<string|int, mixed> — `{ objects, total }`.resolve_category_slugs()
Resolve category references to the term slugs ProductQuery filters on.
private
resolve_category_slugs(array<string|int, mixed> $references) : array<string|int, mixed>
ProductQuery types a category list from its first element, so a request mixing ids and slugs is normalized to slugs here. The list is deduplicated and capped: one request must not drive an unbounded number of term lookups on an unauthenticated endpoint.
Parameters
- $references : array<string|int, mixed>
-
Category term ids or slugs.
Return values
array<string|int, mixed> —store_api_params()
Translate the UCP search body into Store API product collection parameters.
private
store_api_params(string $query, array<string|int, mixed> $filters, array<string|int, mixed>|null $category_slugs, int $limit, int $offset) : array<string|int, mixed>
Price bounds pass through in minor units for ProductQuery to convert and tax-adjust. Like core's other price filters it matches on range overlap: a variable product qualifies when its min–max span intersects the bounds, even with no variation inside them.
Parameters
- $query : string
-
Free-text search term.
- $filters : array<string|int, mixed>
-
Request filters.
- $category_slugs : array<string|int, mixed>|null
-
Resolved category slugs, or null when none were requested.
- $limit : int
-
Page size.
- $offset : int
-
Offset of the first result.
Return values
array<string|int, mixed> —store_api_request()
Wrap collection parameters in the request object ProductQuery reads.
private
store_api_request(array<string|int, mixed> $params) : WP_REST_Request<string|int, array<string, mixed>>
Parameters
- $params : array<string|int, mixed>
-
Store API product collection parameters.
Return values
WP_REST_Request<string|int, array<string, mixed>> —validate_search_input()
Validate the search query string and filters before any query runs.
private
validate_search_input(string $query, array<string|int, mixed> $filters) : WP_REST_Response|null
Values that would silently degrade to something meaningless — a price cast from a non-numeric string, a range whose minimum exceeds its maximum — are refused rather than executed, so a caller cannot spend database work on a request that was never going to match.
Parameters
- $query : string
-
Free-text search term.
- $filters : array<string|int, mixed>
-
Request filters.
