PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.5.2
JetBackup – Backup, Restore & Migrate v1.5.2
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / com / lib / Request / SGRequestAdapterWordpress.php
backup / com / lib / Request Last commit date
SGIRequestAdapter.php 5 years ago SGRequest.php 5 years ago SGRequestAdapterWordpress.php 5 years ago SGResponse.php 5 years ago
SGRequestAdapterWordpress.php
195 lines
1 <?php
2
3 require_once(SG_REQUEST_PATH.'SGIRequestAdapter.php');
4 require_once(SG_REQUEST_PATH.'SGResponse.php');
5
6 class SGRequestAdapterWordpress implements SGIRequestAdapter
7 {
8 private $headers = array();
9 private $params = array();
10 private $url;
11 private $getWithQueryParams = true;
12
13 private $body;
14 private $httpCode;
15 private $contentType;
16
17 private $stream = false;
18
19 public function __construct()
20 {
21 $reloadMethod = SGConfig::get('SG_BACKGROUND_RELOAD_METHOD');
22 if ($reloadMethod == SG_RELOAD_METHOD_STREAM) {
23 $this->stream = true;
24 }
25 }
26
27 public function setGetWithQueryParams($getWithQueryParams)
28 {
29 $this->getWithQueryParams = $getWithQueryParams;
30 }
31
32 public function addHeader($header)
33 {
34 $this->headers[] = $header;
35 }
36
37 public function setHeaders($headers)
38 {
39 $this->headers = $headers;
40 }
41
42 public function setUrl($url)
43 {
44 $this->url = $url;
45 }
46
47 public function setParams($params)
48 {
49 $this->params = $params;
50 }
51
52 public function getRequestArgs()
53 {
54 $args = array(
55 'headers' => $this->headers,
56 'sslverify' => false,
57 'stream' => $this->stream,
58 'timeout' => 30
59 );
60
61 if (!function_exists("curl_init")) {
62 $args['sslverify'] = true;
63 }
64
65 return $args;
66 }
67
68 public function sendPostRequest()
69 {
70 $body = null;
71
72 if (count($this->params)) {
73 // $body = http_build_query($this->params, '', '&');
74 $body = $this->params;
75 }
76
77 $args = $this->getRequestArgs();
78 $args['body'] = $body;
79
80 $response = wp_remote_post($this->url, $args);
81 if ($this->stream && !($response instanceof WP_Error)) {
82 $this->body = file_get_contents($response['filename']);
83 }
84 else {
85 $this->body = wp_remote_retrieve_body($response);
86 }
87 $this->httpCode = wp_remote_retrieve_response_code($response);
88
89 $headers = wp_remote_retrieve_headers($response);
90 if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) {
91 $data = $headers->getAll();
92 $this->contentType = $data['content-type'];
93 }
94 else if ($headers && is_array($headers)) {
95 $this->contentType = $headers['content-type'];
96 }
97 else {
98 $this->contentType = '';
99 }
100
101 return $this->parseResponse();
102 }
103
104 public function sendGetRequest()
105 {
106 $args = $this->getRequestArgs();
107
108 if (count($this->params)) {
109 $this->url = rtrim($this->url, '/').'/';
110
111 if ($this->getWithQueryParams) { //standard get url, with query params
112 $this->url .= '?'.http_build_query($this->params, '', '&');
113 }
114 else { //mvs-styled get url
115 $this->url .= implode('/', array_values($this->params));
116 }
117 }
118
119 $response = wp_remote_get($this->url, $args);
120 if ($this->stream && !($response instanceof WP_Error)) {
121 $this->body = file_get_contents($response['filename']);
122 }
123 else {
124 $this->body = wp_remote_retrieve_body($response);
125 }
126 $this->httpCode = wp_remote_retrieve_response_code($response);
127
128 $headers = wp_remote_retrieve_headers($response);
129 if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) {
130 $data = $headers->getAll();
131 $this->contentType = $data['content-type'];
132 }
133 else if ($headers && is_array($headers)) {
134 $this->contentType = $headers['content-type'];
135 }
136 else {
137 $this->contentType = '';
138 }
139
140 return $this->parseResponse();
141 }
142
143 public function sendRequest($type)
144 {
145 $body = null;
146
147 if ($this->params) {
148 // $body = http_build_query($this->params, '', '&');
149 $body = $this->params;
150 }
151
152 $args = $this->getRequestArgs();
153 $args['body'] = $body;
154 $args['method'] = $type;
155
156 $response = wp_remote_request($this->url, $args);
157 if ($this->stream && !($response instanceof WP_Error)) {
158 $this->body = file_get_contents($response['filename']);
159 }
160 else {
161 $this->body = wp_remote_retrieve_body($response);
162 }
163 $this->httpCode = wp_remote_retrieve_response_code($response);
164
165 $headers = wp_remote_retrieve_headers($response);
166 if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) {
167 $data = $headers->getAll();
168 $this->contentType = $data['content-type'];
169 }
170 else if ($headers && is_array($headers)) {
171 $this->contentType = $headers['content-type'];
172 }
173 else {
174 $this->contentType = '';
175 }
176
177 return $this->parseResponse();
178 }
179
180 public function parseResponse()
181 {
182 $response = new SGResponse();
183 $response->setBody($this->body);
184 $response->setHttpStatus($this->httpCode);
185 $response->setContentType($this->contentType);
186
187 //if the response is in json format, decode it
188 if ($this->contentType == 'application/json') {
189 $response->parseJsonBody();
190 }
191
192 return $response;
193 }
194 }
195