service_endpoint = $service_endpoint; $this->cache_ttl = $cache_ttl; parent::__construct( $service_endpoint->api_service ); } /** * Returns the cache key for the API request. * * @since 3.17.0 * * @param array $args The arguments to pass to the API request. * @return string The cache key for the API request. */ private function get_cache_key( array $args ): string { $api_service = $this->service_endpoint->api_service; $identifier = sprintf( '%s-%s-%s', $api_service->get_api_url(), $this->get_endpoint(), (string) wp_json_encode( $args ) ); return sprintf( 'parsely-api-%s', hash( 'sha256', $identifier ) ); } /** * Executes the API request, caching the response. * * If the response is already cached, it will be returned from the cache, * otherwise the API request will be made and the response will be cached. * * @since 3.17.0 * * @param array $args The arguments to pass to the API request. * @return WP_Error|array The response from the API. */ public function call( array $args = array() ) { $cache_key = $this->get_cache_key( $args ); $cache = wp_cache_get( $cache_key, PARSELY_CACHE_GROUP ); if ( false !== $cache ) { // @phpstan-ignore-next-line return $cache; } $response = $this->service_endpoint->call( $args ); if ( ! is_wp_error( $response ) ) { wp_cache_set( $cache_key, $response, PARSELY_CACHE_GROUP, $this->cache_ttl ); // phpcs:ignore } return $response; } /** * Returns the endpoint for the API request. * * @since 3.17.0 * * @return string The endpoint for the API request. */ public function get_endpoint(): string { return $this->service_endpoint->get_endpoint(); } /** * Returns the uncached endpoint for the API request. * * @since 3.17.0 * * @return Base_Service_Endpoint The uncached endpoint for the API request. */ public function get_uncached_endpoint(): Base_Service_Endpoint { return $this->service_endpoint; } /** * Returns the request options for the remote API request. * * Gets the request options from the uncached service endpoint. * * @since 3.17.0 * * @param string $method The HTTP method to use for the request. * @return WP_HTTP_Request_Args The request options for the remote API request. */ protected function get_request_options( string $method ): array { return $this->service_endpoint->get_request_options( $method ); } /** * Returns the common query arguments to send to the remote API. * * Gets the query arguments from the uncached service endpoint. * * @since 3.17.0 * * @param array $args Additional query arguments to send to the remote API. * @return array The query arguments to send to the remote API. */ protected function get_query_args( array $args = array() ): array { return $this->service_endpoint->get_query_args( $args ); } }