This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from RebelCode/wp-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WpHandler.php
113 lines (99 loc) · 3.05 KB
/
WpHandler.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace WpOop\HttpClient;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RebelCode\Psr7\Response;
use WP_Error;
use function wp_remote_request;
/**
* A handler that uses the WordPress HTTP API to dispatch requests.
*
* @see https://developer.wordpress.org/reference/functions/wp_remote_request/
*
* @psalm-type WpHandlerOptions = array{
* timeout?: int,
* redirection?: int,
* user-agent?: string,
* reject_unsafe_urls?: bool,
* blocking?: bool,
* compress?: bool,
* decompress?: bool,
* sslverify?: bool,
* sslcertificates?: string,
* stream?: bool,
* filename?: string,
* limit_response_size?: int,
* }
*/
class WpHandler implements HandlerInterface
{
/**
* @var array<string, mixed>
*
* @psalm-var WpHandlerOptions
*/
protected $options;
/**
* Constructor.
*
* @param array<string, mixed> $options
*
* @psalm-param WpHandlerOptions $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* @inheritDoc
*/
public function handle(RequestInterface $request): ResponseInterface
{
$uri = (string) $request->getUri();
$args = $this->prepareArgs($request);
$httpVer = $request->getProtocolVersion();
/** @var WP_Error|array $responseData */
$responseData = wp_remote_request($uri, $args);
$code = wp_remote_retrieve_response_code($responseData);
$code = is_numeric($code) ? (int) $code : 400;
$reason = wp_remote_retrieve_response_message($responseData);
$headers = wp_remote_retrieve_headers($responseData);
$headers = is_array($headers) ? $headers : iterator_to_array($headers);
$body = wp_remote_retrieve_body($responseData);
return new Response($code, $headers, $body, $httpVer, $reason);
}
/**
* Prepares the args array for a specific request. The result can be used with WordPress' remote functions.
*
* @param RequestInterface $request The request.
*
* @return array<string, mixed> The prepared args array.
*
* @psalm-return WpHandlerOptions
*/
protected function prepareArgs(RequestInterface $request): array
{
return array_merge($this->options, [
'method' => $request->getMethod(),
'httpversion' => $request->getProtocolVersion(),
'headers' => $this->prepareHeaders($request),
'body' => (string) $request->getBody(),
]);
}
/**
* Transforms a request's headers into the format expected by WordPress' remote functions.
*
* @param RequestInterface $request The request.
*
* @return array<string, string> The prepared headers array.
*/
protected function prepareHeaders(RequestInterface $request): array
{
$headers = [];
foreach ($request->getHeaders() as $header => $values) {
$headers[$header] = $request->getHeaderLine($header);
}
return $headers;
}
}