PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.32
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.32
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Http / Client.php

Client.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.32, at vendor/wpfluent/framework/src/WPFluent/Http/Client.php

108 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Http;
4
5 class Client
6 {
7 protected function request($url, $args = [])
8 {
9 $response = wp_remote_request($url, $args);
10
11 if (is_wp_error($response)) {
12 throw new class(
13 $response->get_error_message(), 500
14 ) extends \Exception {};
15 }
16
17 return $this->makeResponse($response);
18 }
19
20 protected function makeResponse($response)
21 {
22 return new class($response) {
23
24 protected $response = null;
25
26 public function __construct($response) {
27 $this->response = $response;
28 }
29
30 public function toArray() {
31 return $this->response;
32 }
33
34 public function isOkay() {
35 return $this->getCode() == 200;
36 }
37
38 public function throw() {
39 $class = sprintf(
40 'WpOrg\Requests\Exception\Http\Status%d', $this->getCode()
41 );
42
43 if (!class_exists($class)) {
44 $class = 'WpOrg\Requests\Exception\Http\Status\Http';
45 }
46
47 throw new $class;
48 }
49
50 public function throwIf(callable $callback) {
51 if ($callback($this)) {
52 return $this->throw();
53 }
54 }
55
56 public function getCode() {
57 return wp_remote_retrieve_response_code($this->response);
58 }
59
60 public function getMessage() {
61 return wp_remote_retrieve_response_message($this->response);
62 }
63
64 public function getBody() {
65 return wp_remote_retrieve_body($this->response);
66 }
67
68 public function isJson() {
69 $header = $this->getHeader('content-type');
70 return str_contains($header, 'application/json');
71 }
72
73 public function getJson() {
74 if ($this->isJson()) {
75 return json_decode($this->getBody(), true);
76 }
77 }
78
79 public function getHeaders() {
80 return wp_remote_retrieve_headers($this->response);
81 }
82
83 public function getHeader($key) {
84 return wp_remote_retrieve_header($this->response, $key);
85 }
86
87 public function getCookies() {
88 return wp_remote_retrieve_cookies($this->response);
89 }
90
91 public function getCookie($name, $isObject = false) {
92 if ($isObject) {
93 // Return the WP_Http_Cookie object
94 return wp_remote_retrieve_cookie($this->response, $name);
95 }
96 return wp_remote_retrieve_cookie_value($this->response, $name);
97 }
98 };
99 }
100
101 public static function __callStatic($method, $args)
102 {
103 return (new static)->request(array_shift($args), array_merge(
104 count($args) ? $args : [], ['method' => strtoupper($method)]
105 ));
106 }
107 }
108