Hydration
in package
Service class that handles hydration of API data for blocks.
Table of Contents
- $asset_data_registry : AssetDataRegistry
- Instance of the asset data registry.
- $cached_cart_context : string|null
- Snapshot of WC()->cart->cart_context taken by cache_cart_context(), to restore after hydrating the API.
- $cached_store_notices : array<string|int, mixed>|null
- Snapshot of store notices taken by cache_store_notices(), to restore after hydrating the API.
- __construct() : mixed
- Constructor.
- disable_nonce_check_callback() : mixed
- Callback to disable the nonce check. While we could use `__return_true`, we use a custom named callback so that we can remove it later without affecting other filters.
- get_rest_api_response_data() : array<string|int, mixed>
- Hydrates the asset data registry with data from the API. Disables notices and nonces so requests contain valid data that is not polluted by the current session.
- cache_cart_context() : void
- Cache the cart context before hydrating the API.
- cache_store_notices() : mixed
- Cache notices before hydrating the API if the customer has a session and the notice functions are available.
- disable_nonce_check() : mixed
- Disable the nonce check temporarily.
- restore_cached_cart_context() : void
- Restore the cart context, only if a snapshot was taken this cycle.
- restore_cached_store_notices() : mixed
- Restore notices into current session from cache, only if a snapshot was taken this cycle.
- restore_nonce_check() : mixed
- Restore the nonce check.
- store_notice_functions_available() : bool
- Whether the store notice functions (`wc_get_notices()`, `wc_clear_notices()`, `wc_set_notices()`) are available to call.
- get_response_from_controller() : false|mixed|null
- Helper method to generate GET response from a controller. Also fires the `rest_request_after_callbacks` for backward compatibility.
- match_route_to_handler() : array<string|int, mixed>|null
- Inspired from WP core's `match_request_to_handler`, this matches a given path from available route regexes.
Properties
$asset_data_registry
Instance of the asset data registry.
protected
AssetDataRegistry
$asset_data_registry
$cached_cart_context
Snapshot of WC()->cart->cart_context taken by cache_cart_context(), to restore after hydrating the API.
protected
string|null
$cached_cart_context
= null
null means no snapshot has been taken this cycle, either because the method has not run yet or because
its guards skipped it (for example, there was no cart). restore_cached_cart_context() only restores from a
non-null string, so a null value always leaves the cart untouched.
$cached_store_notices
Snapshot of store notices taken by cache_store_notices(), to restore after hydrating the API.
protected
array<string|int, mixed>|null
$cached_store_notices
= null
null means no snapshot has been taken this cycle, either because the method has not run yet or because
its guards skipped the snapshot (for example, the notice functions were unavailable). restore_cached_store_notices()
only restores from a non-null array, so a null value always leaves the session untouched.
Methods
__construct()
Constructor.
public
__construct(AssetDataRegistry $asset_data_registry) : mixed
Parameters
- $asset_data_registry : AssetDataRegistry
-
Instance of the asset data registry.
Return values
mixed —disable_nonce_check_callback()
Callback to disable the nonce check. While we could use `__return_true`, we use a custom named callback so that we can remove it later without affecting other filters.
public
disable_nonce_check_callback() : mixed
Return values
mixed —get_rest_api_response_data()
Hydrates the asset data registry with data from the API. Disables notices and nonces so requests contain valid data that is not polluted by the current session.
public
get_rest_api_response_data([string $path = '' ]) : array<string|int, mixed>
Parameters
- $path : string = ''
-
API path to hydrate e.g. '/wc/store/v1/cart'.
Return values
array<string|int, mixed> — Response data.cache_cart_context()
Cache the cart context before hydrating the API.
protected
cache_cart_context() : void
Dispatching a Store API cart route runs CartController::load_cart(), which sets cart_context to
store-api on the shared cart and never puts it back. That is correct for a real Store API request, where
the whole request is Store API — but hydration runs inside a front-end render, so without a snapshot the
rest of that render inherits the flag. Shipping code branches on it, so the page would silently take the
block path.
Tags
Return values
void —cache_store_notices()
Cache notices before hydrating the API if the customer has a session and the notice functions are available.
protected
cache_store_notices() : mixed
Return values
mixed —disable_nonce_check()
Disable the nonce check temporarily.
protected
disable_nonce_check() : mixed
Return values
mixed —restore_cached_cart_context()
Restore the cart context, only if a snapshot was taken this cycle.
protected
restore_cached_cart_context() : void
Tags
Return values
void —restore_cached_store_notices()
Restore notices into current session from cache, only if a snapshot was taken this cycle.
protected
restore_cached_store_notices() : mixed
Return values
mixed —restore_nonce_check()
Restore the nonce check.
protected
restore_nonce_check() : mixed
Return values
mixed —store_notice_functions_available()
Whether the store notice functions (`wc_get_notices()`, `wc_clear_notices()`, `wc_set_notices()`) are available to call.
protected
store_notice_functions_available() : bool
These three functions share a single definition site in includes/wc-notice-functions.php, which
WooCommerce loads only for frontend/REST requests (or via wc_load_cart()). On a plain wp-admin load the
functions may therefore be absent even when a WooCommerce session exists, so this seam must be checked
independently of the session guard. It is protected so tests can override it to force the unavailable
branch deterministically.
Tags
Return values
bool — True if the notice functions are defined and safe to call.get_response_from_controller()
Helper method to generate GET response from a controller. Also fires the `rest_request_after_callbacks` for backward compatibility.
private
get_response_from_controller(string $controller_class, string $path[, array<string|int, mixed> $url_params = array() ][, array<string|int, mixed> $query_params = array() ]) : false|mixed|null
Parameters
- $controller_class : string
-
Controller class FQN that will respond to the request.
- $path : string
-
Request path regex.
- $url_params : array<string|int, mixed> = array()
-
URL parameters extracted from route (e.g., ['id' => '123']).
- $query_params : array<string|int, mixed> = array()
-
Query string parameters (e.g., ['key' => 'value']).
Return values
false|mixed|null — Responsematch_route_to_handler()
Inspired from WP core's `match_request_to_handler`, this matches a given path from available route regexes.
private
match_route_to_handler(string $path, array<string|int, mixed> $available_routes) : array<string|int, mixed>|null
Extracts URL parameters from regex named groups and query string parameters.
Parameters
- $path : string
-
The path to match (may include query string).
- $available_routes : array<string|int, mixed>
-
Available routes in { $regex1 => $controller_class1, ... } format.
