1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 )
);
}
}
|