PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Traits / RestRequestTrait.php
wp-staging / Framework / Traits Last commit date
ApplyFiltersTrait.php 2 months ago ArrayableTrait.php 7 months ago BatchSizeCalculateTrait.php 7 months ago BearerTokenTrait.php 4 months ago BenchmarkTrait.php 2 years ago BooleanTransientTrait.php 5 years ago DatabaseSearchReplaceTrait.php 2 weeks ago DbRowsGeneratorTrait.php 3 years ago DebugLogTrait.php 1 year ago DeveloperTimerTrait.php 9 months ago EndOfLinePlaceholderTrait.php 1 year ago EventLoggerTrait.php 1 month ago FileScanToCacheTrait.php 10 months ago FormatTrait.php 11 months ago HttpRequestTrait.php 9 months ago HydrateTrait.php 1 year ago I18nTrait.php 1 year ago IpResolverTrait.php 10 months ago MaintenanceTrait.php 5 months ago MemoryExhaustTrait.php 2 years ago MySQLRowsGeneratorTrait.php 7 months ago NoticesTrait.php 1 day ago PropertyConstructor.php 5 years ago RenameTmpDirectoryTrait.php 5 months ago ResourceTrait.php 4 months ago RestRequestTrait.php 3 months ago RestoreFileExclusionTrait.php 1 year ago SerializeTrait.php 1 year ago SetTimeLimitTrait.php 1 month ago SlashTrait.php 1 year ago SqlCommentTrait.php 3 months ago TablePrefixValidator.php 3 months ago UrlTrait.php 1 year ago ValueGetterTrait.php 1 year ago WindowsOsTrait.php 1 year ago
RestRequestTrait.php
129 lines
1 <?php
2
3 namespace WPStaging\Framework\Traits;
4
5 use WPStaging\Framework\Rest\Rest;
6
7 trait RestRequestTrait
8 {
9 /**
10 * Custom request headers sent to remote endpoint.
11 *
12 * @var array<string, string>
13 */
14 private $headers = [];
15
16 private $verifySsl = false;
17
18 /**
19 * Fire-and-forget mode.
20 * When false, requests use a tiny timeout and do not wait for a full response.
21 * Use only when caller can safely continue without immediate remote result.
22 */
23 private $isBlockingRequest = true;
24
25 /** @var string */
26 private $httpAuthUsername = '';
27
28 /** @var string */
29 private $httpAuthPassword = '';
30
31 /**
32 * @param string $url
33 * @param string $endpoint
34 * @param array $body
35 * @param string $accessToken
36 * @return array|\WP_Error
37 */
38 protected function sendRestRequest(string $url, string $endpoint, array $body = [], string $accessToken = '')
39 {
40 $headers = $this->headers;
41 $headers['Content-Type'] = 'application/json';
42
43 if (!empty($accessToken)) {
44 $headers = array_merge($headers, $this->getAuthorizationHeader($accessToken));
45 }
46
47 // Basic Auth overwrites the Authorization header when configured.
48 // The Bearer token is still available via the X-WPSTG-Request fallback header
49 // (see BearerTokenTrait::getAuthTokenFromPluginHeader), which the remote site
50 // uses when the Authorization header is consumed by .htpasswd.
51 if (!empty($this->httpAuthUsername) && !empty($this->httpAuthPassword)) {
52 $headers['Authorization'] = 'Basic ' . base64_encode($this->httpAuthUsername . ':' . $this->httpAuthPassword);
53 }
54
55 $timeout = Rest::REQUEST_TIMEOUT;
56 if (!$this->isBlockingRequest) {
57 // Non-blocking call used to trigger remote background jobs.
58 $timeout = 0.01;
59 }
60
61 $args = [
62 'method' => 'POST',
63 'headers' => $headers,
64 'blocking' => $this->isBlockingRequest,
65 'timeout' => $timeout,
66 'sslverify' => $this->verifySsl,
67 ];
68
69 if (!empty($body)) {
70 $args['body'] = json_encode($body);
71 }
72
73 return wp_remote_post(
74 $this->buildRequestUrl($url, $endpoint),
75 $args
76 );
77 }
78
79 /**
80 * Build the full request URL with REST route
81 *
82 * @param string $url Base URL
83 * @param string $endpoint REST endpoint path
84 * @return string Complete URL
85 */
86 protected function buildRequestUrl(string $url, string $endpoint): string
87 {
88 return trailingslashit($url) . '?rest_route=/' . Rest::WPSTG_ROUTE_NAMESPACE_V1 . '/' . ltrim($endpoint, '/');
89 }
90
91 /**
92 * @return void
93 */
94 protected function resetHeaders()
95 {
96 $this->headers = [];
97 }
98
99 /**
100 * @param string $username
101 * @param string $password
102 * @return void
103 */
104 protected function setHttpAuth(string $username, string $password)
105 {
106 if (empty($username) || empty($password)) {
107 $this->httpAuthUsername = '';
108 $this->httpAuthPassword = '';
109
110 return;
111 }
112
113 $this->httpAuthUsername = $username;
114 $this->httpAuthPassword = $password;
115 }
116
117 /**
118 * @param string $token
119 * @return array<string, string>
120 */
121 protected function getAuthorizationHeader(string $token): array
122 {
123 return [
124 'Authorization' => 'Bearer ' . $token,
125 'X-WPSTG-Request' => $token,
126 ];
127 }
128 }
129