PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Http / Client.php

Client.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, 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 FluentBooking\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