PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.6.9
JetBackup – Backup, Restore & Migrate v1.6.9
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 7 years ago SGRequest.php 7 years ago SGRequestAdapterWordpress.php 5 years ago SGResponse.php 7 years ago
SGRequestAdapterWordpress.php
196 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($otherArgs = array())
105 {
106 $args = $this->getRequestArgs();
107 $args = array_merge($args, $otherArgs);
108
109 if (count($this->params)) {
110 $this->url = rtrim($this->url, '/').'/';
111
112 if ($this->getWithQueryParams) { //standard get url, with query params
113 $this->url .= '?'.http_build_query($this->params, '', '&');
114 }
115 else { //mvs-styled get url
116 $this->url .= implode('/', array_values($this->params));
117 }
118 }
119
120 $response = wp_remote_get($this->url, $args);
121 if ($this->stream && !($response instanceof WP_Error)) {
122 $this->body = file_get_contents($response['filename']);
123 }
124 else {
125 $this->body = wp_remote_retrieve_body($response);
126 }
127 $this->httpCode = wp_remote_retrieve_response_code($response);
128
129 $headers = wp_remote_retrieve_headers($response);
130 if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) {
131 $data = $headers->getAll();
132 $this->contentType = $data['content-type'];
133 }
134 else if ($headers && is_array($headers)) {
135 $this->contentType = $headers['content-type'];
136 }
137 else {
138 $this->contentType = '';
139 }
140
141 return $this->parseResponse();
142 }
143
144 public function sendRequest($type)
145 {
146 $body = null;
147
148 if ($this->params) {
149 // $body = http_build_query($this->params, '', '&');
150 $body = $this->params;
151 }
152
153 $args = $this->getRequestArgs();
154 $args['body'] = $body;
155 $args['method'] = $type;
156
157 $response = wp_remote_request($this->url, $args);
158 if ($this->stream && !($response instanceof WP_Error)) {
159 $this->body = file_get_contents($response['filename']);
160 }
161 else {
162 $this->body = wp_remote_retrieve_body($response);
163 }
164 $this->httpCode = wp_remote_retrieve_response_code($response);
165
166 $headers = wp_remote_retrieve_headers($response);
167 if ($headers && $headers instanceof Requests_Utility_CaseInsensitiveDictionary) {
168 $data = $headers->getAll();
169 $this->contentType = $data['content-type'];
170 }
171 else if ($headers && is_array($headers)) {
172 $this->contentType = $headers['content-type'];
173 }
174 else {
175 $this->contentType = '';
176 }
177
178 return $this->parseResponse();
179 }
180
181 public function parseResponse()
182 {
183 $response = new SGResponse();
184 $response->setBody($this->body);
185 $response->setHttpStatus($this->httpCode);
186 $response->setContentType($this->contentType);
187
188 //if the response is in json format, decode it
189 if ($this->contentType == 'application/json') {
190 $response->parseJsonBody();
191 }
192
193 return $response;
194 }
195 }
196