| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Http\Response; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use WP_Error; |
| 7 |
use DateTimeInterface; |
| 8 |
use WP_REST_Response; |
| 9 |
use InvalidArgumentException; |
| 10 |
use FluentCommunity\Framework\Http\Request\File; |
| 11 |
|
| 12 |
class Response |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Response Data |
| 16 |
* @var mixed |
| 17 |
*/ |
| 18 |
protected $data = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Response Status Code |
| 22 |
* @var integer |
| 23 |
*/ |
| 24 |
protected $code = 200; |
| 25 |
|
| 26 |
/** |
| 27 |
* Response Headers |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $headers = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Response Cookies |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
protected $cookies = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* Construct the response instance. |
| 40 |
* |
| 41 |
* @param array $data |
| 42 |
* @param integer $code |
| 43 |
* @param array $headers |
| 44 |
*/ |
| 45 |
public function __construct($data = null, $code = 200, $headers = []) |
| 46 |
{ |
| 47 |
$this->data = $data; |
| 48 |
$this->code = $code; |
| 49 |
$this->headers = $headers; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates an instance of self. |
| 54 |
* |
| 55 |
* @param mixed $data |
| 56 |
* @param integer $code |
| 57 |
* @param array $headers |
| 58 |
* @return self |
| 59 |
*/ |
| 60 |
public static function make($data = null, $code = 200, $headers = []) |
| 61 |
{ |
| 62 |
return new static($data, $code, $headers); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Send json response. |
| 67 |
* |
| 68 |
* @param array $data |
| 69 |
* @param integer $code |
| 70 |
* @return string|false JSON encoded string, or false if fails to encode. |
| 71 |
*/ |
| 72 |
public function json($data = null, $code = 200) |
| 73 |
{ |
| 74 |
return wp_send_json($data, $code); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Send rest response. |
| 79 |
* |
| 80 |
* @param mixed $data |
| 81 |
* @param integer $code |
| 82 |
* @param array $headers |
| 83 |
* @return \WP_REST_Response |
| 84 |
*/ |
| 85 |
public function send($data = null, $code = 200, $headers = []) |
| 86 |
{ |
| 87 |
$response = new WP_REST_Response($data, $code, $headers); |
| 88 |
|
| 89 |
return $this->maybeMergeHeaders( |
| 90 |
$this->MaybeMergeCookies($response) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Send a success rest response. |
| 96 |
* |
| 97 |
* @param mixed $data |
| 98 |
* @param integer $code |
| 99 |
* @return \WP_REST_Response |
| 100 |
*/ |
| 101 |
public function sendSuccess($data = null, $code = 200, $headers = []) |
| 102 |
{ |
| 103 |
return $this->send($data, $code, $headers); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Send an error json response |
| 108 |
* @param array $data |
| 109 |
* @param integer $code |
| 110 |
* @return \WP_REST_Response |
| 111 |
*/ |
| 112 |
public function sendError($data = null, $code = 422, $headers = []) |
| 113 |
{ |
| 114 |
if (!$code || $code < 400 ) { |
| 115 |
$code = 422; |
| 116 |
} |
| 117 |
|
| 118 |
return $this->send($data, $code, $headers); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Convert the WP_Error to WP_REST_Response |
| 123 |
* |
| 124 |
* @param \WP_Error $wpError |
| 125 |
* @return \WP_REST_Response |
| 126 |
*/ |
| 127 |
public function wpErrorToResponse(WP_Error $wpError) |
| 128 |
{ |
| 129 |
return rest_convert_error_to_response($wpError); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Set response headers |
| 134 |
* @param string|array $key |
| 135 |
* @param string|null $value |
| 136 |
* @return self |
| 137 |
*/ |
| 138 |
public function withHeader($key, $value = null) |
| 139 |
{ |
| 140 |
if (is_array($key) && !$value) { |
| 141 |
$this->headers = $key; |
| 142 |
} else { |
| 143 |
$this->headers = [$key => $value]; |
| 144 |
} |
| 145 |
|
| 146 |
return $this; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Set response cookie |
| 151 |
* |
| 152 |
* @param string $name |
| 153 |
* @param mixed $value |
| 154 |
* @param int $minutes |
| 155 |
* @param string $path |
| 156 |
* @param string|null $domain |
| 157 |
* @param bool $secure |
| 158 |
* @param bool $httpOnly |
| 159 |
* @return self |
| 160 |
*/ |
| 161 |
public function withCookie( |
| 162 |
$name, |
| 163 |
$value, |
| 164 |
$minutes, |
| 165 |
$path = '/', |
| 166 |
$domain = null, |
| 167 |
$secure = false, |
| 168 |
$httpOnly = true |
| 169 |
) |
| 170 |
{ |
| 171 |
$cookie = $this->buildCookie( |
| 172 |
$name, $value, $minutes, $path, $domain, $secure, $httpOnly |
| 173 |
); |
| 174 |
|
| 175 |
$this->cookies[] = $cookie; |
| 176 |
|
| 177 |
return $this; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Build cookie header for response |
| 182 |
* |
| 183 |
* @param string $name |
| 184 |
* @param mixed $value |
| 185 |
* @param int $minutes |
| 186 |
* @param string $path |
| 187 |
* @param string|null $domain |
| 188 |
* @param bool $secure |
| 189 |
* @param bool $httpOnly |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
protected function buildCookie( |
| 193 |
$name, |
| 194 |
$value, |
| 195 |
$minutes, |
| 196 |
$path, |
| 197 |
$domain, |
| 198 |
$secure, |
| 199 |
$httpOnly |
| 200 |
) { |
| 201 |
$expiration = static::expiresAt($minutes); |
| 202 |
|
| 203 |
$cookieHeader = "{$name}=" . rawurlencode($value); |
| 204 |
|
| 205 |
$cookieHeader .= "; Expires=" . gmdate('D, d-M-Y H:i:s T', $expiration); |
| 206 |
|
| 207 |
$cookieHeader .= "; Path=" . $path; |
| 208 |
|
| 209 |
if ($domain) { |
| 210 |
$cookieHeader .= "; Domain=" . $domain; |
| 211 |
} |
| 212 |
|
| 213 |
if ($secure) { |
| 214 |
$cookieHeader .= "; Secure"; |
| 215 |
} |
| 216 |
|
| 217 |
if ($httpOnly) { |
| 218 |
$cookieHeader .= "; HttpOnly"; |
| 219 |
} |
| 220 |
|
| 221 |
return $cookieHeader; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Merge additional headers if exist. |
| 226 |
* |
| 227 |
* @param \WP_REST_Response $response |
| 228 |
*/ |
| 229 |
protected function maybeMergeHeaders(WP_REST_Response $response) |
| 230 |
{ |
| 231 |
if ($this->headers) { |
| 232 |
foreach ($this->headers as $key => $value) { |
| 233 |
$response->header($key, $value); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$this->headers = []; |
| 238 |
|
| 239 |
return $response; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Merge cookies if exist. |
| 244 |
* |
| 245 |
* @param \WP_REST_Response $response |
| 246 |
*/ |
| 247 |
protected function MaybeMergeCookies(WP_REST_Response $response) |
| 248 |
{ |
| 249 |
if ($this->cookies) { |
| 250 |
foreach ($this->cookies as $cookie) { |
| 251 |
$response->header('Set-Cookie', $cookie); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
$this->cookies = []; |
| 256 |
|
| 257 |
return $response; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Prepares expiration time in minutes. |
| 262 |
* |
| 263 |
* @param int|DateTimeInterface $minutes |
| 264 |
* @return int |
| 265 |
*/ |
| 266 |
protected static function expiresAt($minutes = 0) |
| 267 |
{ |
| 268 |
if (is_int($minutes)) { |
| 269 |
if ($minutes === 0) { |
| 270 |
return $minutes; |
| 271 |
} |
| 272 |
|
| 273 |
$minutes = new DateTime('+' . $minutes . ' minutes'); |
| 274 |
} |
| 275 |
|
| 276 |
if (!($minutes instanceof DateTimeInterface)) { |
| 277 |
throw new InvalidArgumentException( |
| 278 |
'Invalid expiration time provided.' |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
return $minutes->getTimestamp(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Send the response |
| 287 |
* |
| 288 |
* @return \WP_REST_Response |
| 289 |
*/ |
| 290 |
public function toArray() |
| 291 |
{ |
| 292 |
$response = new WP_REST_Response( |
| 293 |
$this->data, $this->code |
| 294 |
); |
| 295 |
|
| 296 |
return $this->MaybeMergeCookies( |
| 297 |
$this->maybeMergeHeaders($response) |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Send a file download response. |
| 303 |
* |
| 304 |
* @param string $filePath |
| 305 |
* @param string|null $fileName |
| 306 |
* @return void |
| 307 |
*/ |
| 308 |
public static function download($filePath, $fileName = null) |
| 309 |
{ |
| 310 |
if ($filePath instanceof File) { |
| 311 |
$array = $filePath->toArray(); |
| 312 |
$filePath = $array['tmp_name']; |
| 313 |
$fileName ??= $fileName ?? $array['name']; |
| 314 |
} |
| 315 |
|
| 316 |
if (!file_exists($filePath) || !is_readable($filePath)) { |
| 317 |
wp_die('File does not exist or unreadable.', '404 Not Found'); |
| 318 |
} |
| 319 |
|
| 320 |
$mimeType = mime_content_type($filePath) ?: 'application/octet-stream'; |
| 321 |
|
| 322 |
$fileName = sanitize_file_name($fileName ?? basename($filePath)); |
| 323 |
|
| 324 |
header('Content-Description: File Transfer'); |
| 325 |
header('Content-Type: ' . $mimeType); |
| 326 |
header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
| 327 |
header('Content-Transfer-Encoding: binary'); |
| 328 |
header('Expires: 0'); |
| 329 |
header('Cache-Control: must-revalidate'); |
| 330 |
header('Pragma: public'); |
| 331 |
header('Content-Length: ' . filesize($filePath)); |
| 332 |
|
| 333 |
if (php_sapi_name() === 'cli') { |
| 334 |
ob_start(); |
| 335 |
readfile($filePath); |
| 336 |
$content = ob_get_clean(); |
| 337 |
add_action('shutdown', function() { exit; }); |
| 338 |
return $content; |
| 339 |
} else { |
| 340 |
ob_get_level() && ob_end_clean() && flush(); |
| 341 |
readfile($filePath); |
| 342 |
exit; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|