summaryrefslogtreecommitdiffstats
path: root/includes/class-cepis-woo-transport.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/class-cepis-woo-transport.php')
-rw-r--r--includes/class-cepis-woo-transport.php58
1 files changed, 58 insertions, 0 deletions
diff --git a/includes/class-cepis-woo-transport.php b/includes/class-cepis-woo-transport.php
new file mode 100644
index 0000000..888be54
--- /dev/null
+++ b/includes/class-cepis-woo-transport.php
@@ -0,0 +1,58 @@
+<?php
+/**
+ * The bundled PHP client, taught to speak through the WordPress HTTP API.
+ *
+ * @package CepisWooCommerce
+ */
+
+declare(strict_types=1);
+
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * A `Cepis\Transport` over {@see wp_remote_get()}.
+ *
+ * The library defaults to talking to curl directly, which is the wrong thing
+ * to do inside WordPress: a site's proxy configuration, its
+ * `http_request_args` filters, its request timeouts and everything a host adds
+ * on top of the HTTP API would all be bypassed. Routing through
+ * {@see wp_remote_get()} keeps this plugin a well-behaved guest.
+ *
+ * Exactly one request per call, no retrying — retries, backoff and
+ * `Retry-After` stay the client's job.
+ */
+final class Cepis_Woo_Transport implements Cepis\Transport {
+
+ /**
+ * Perform a single GET.
+ *
+ * @param string $url Absolute URL, query string included.
+ * @param array<string, string> $headers Request headers as name => value.
+ * @param float $timeout Seconds to allow for this attempt.
+ *
+ * @throws Cepis\NetworkException The request never completed.
+ */
+ public function get( string $url, array $headers, float $timeout ): Cepis\Response {
+ $response = wp_remote_get(
+ $url,
+ array(
+ 'headers' => $headers,
+ 'timeout' => $timeout,
+ 'redirection' => 3,
+ 'sslverify' => true,
+ )
+ );
+
+ if ( is_wp_error( $response ) ) {
+ throw new Cepis\NetworkException( esc_html( $response->get_error_message() ) );
+ }
+
+ $response_headers = wp_remote_retrieve_headers( $response );
+
+ return new Cepis\Response(
+ (int) wp_remote_retrieve_response_code( $response ),
+ is_object( $response_headers ) ? $response_headers->getAll() : (array) $response_headers,
+ (string) wp_remote_retrieve_body( $response )
+ );
+ }
+}