Automattic\WooCommerce\Blocks\Domain\Services
Hydration::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.
Метод класса: Hydration{}
Хуков нет.
Возвращает
Массив
. Response data.
Использование
$Hydration = new Hydration(); $Hydration->get_rest_api_response_data( $path );
- $path(массив)
- API paths to hydrate e.g. '/wc/store/v1/cart'.
По умолчанию: ''
Код Hydration::get_rest_api_response_data() Hydration::get rest api response data WC 9.6.0
public function get_rest_api_response_data( $path = '' ) { if ( ! str_starts_with( $path, '/wc/store' ) ) { return array(); } // Allow-list only store API routes. No other request can be hydrated for safety. $available_routes = StoreApi::container()->get( RoutesController::class )->get_all_routes( 'v1', true ); $controller_class = $this->match_route_to_handler( $path, $available_routes ); /** * We disable nonce check to support endpoints such as checkout. The caveat here is that we need to be careful to only support GET requests. No other request type should be processed without nonce check. Additionally, no GET request can modify data as part of hydration request, for example adding items to cart. * * Long term, we should consider validating nonce here, instead of disabling it temporarily. */ $this->disable_nonce_check(); $this->cache_store_notices(); $preloaded_data = array(); if ( null !== $controller_class ) { try { $response = $this->get_response_from_controller( $controller_class, $path ); if ( $response ) { $preloaded_data = array( 'body' => $response->get_data(), 'headers' => $response->get_headers(), ); } } catch ( \Exception $e ) { // This is executing in frontend of the site, a failure in hydration should not stop the site from working. wc_get_logger()->warning( 'Error in hydrating REST API request: ' . $e->getMessage(), array( 'source' => 'blocks-hydration', 'data' => array( 'path' => $path, 'controller' => $controller_class, ), 'backtrace' => true, ) ); } } else { // Preload the request and add it to the array. It will be $preloaded_requests['path'] and contain 'body' and 'headers'. $preloaded_requests = rest_preload_api_request( array(), $path ); $preloaded_data = $preloaded_requests[ $path ] ?? array(); } $this->restore_cached_store_notices(); $this->restore_nonce_check(); // Returns just the single preloaded request, or an empty array if it doesn't exist. return $preloaded_data; }