PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.2.0
Kubio AI Page Builder v2.2.0
2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / Core / License / RequestResponse.php
kubio / lib / src / Core / License Last commit date
ActivationForm.php 2 years ago CheckForm.php 2 years ago Endpoint.php 4 years ago License.php 2 years ago RequestResponse.php 4 years ago Updater.php 4 years ago
RequestResponse.php
105 lines
1 <?php
2 namespace Kubio\Core\License;
3
4 class RequestResponse {
5
6 private $response;
7 private $response_body;
8 private $response_code;
9
10 public function __construct( $response ) {
11 $this->response = $response;
12 $this->response_body = json_decode( wp_remote_retrieve_body( $this->response ) );
13
14 $this->response_code = wp_remote_retrieve_response_code( $this->response );
15
16 if ( ! $this->response_body ) {
17 $this->response_body = new \stdClass();
18 $this->response_body->errors = array(
19 'body' => wp_remote_retrieve_body( $this->response ),
20 );
21 $this->response_body->status = 'error';
22 }
23 }
24
25 public function getMessage( $implode = false ) {
26 $message = array();
27 if ( $this->isWPError() ) {
28 $message = $this->getWPError();
29 } else {
30 if ( $this->isSuccess() ) {
31 $message = $this->getResponseBody()->body;
32
33 } else {
34 $message = $this->getResponseBody()->errors;
35 }
36 }
37
38 if ( $implode ) {
39 $message = $this->flattenResponse( $message );
40
41 return implode( ',', (array) $message );
42 }
43
44 return $message;
45 }
46
47 private function flattenResponse( $data = array() ) {
48 $result = array();
49
50 if ( ! is_array( $data ) ) {
51 $data = array( $data );
52 }
53
54 foreach ( $data as $values ) {
55 if ( is_object( $values ) ) {
56 $values = (array) $values;
57 }
58
59 if ( is_array( $values ) ) {
60 $result = array_merge( $result, $this->flattenResponse( $values ) );
61 } else {
62 $result[] = $values;
63 }
64 }
65
66 return $result;
67 }
68
69 public function isSuccess() {
70 return ( ! $this->isWPError() && $this->getResponseBody() && $this->getResponseBody()->status !== 'error' );
71 }
72
73 /**
74 * @return array|mixed|object
75 */
76 public function getResponseBody() {
77 return $this->response_body;
78 }
79
80 /**
81 * @return int|string
82 */
83 public function getResponseCode() {
84
85 if ( $this->isWPError() ) {
86 return 403;
87 }
88
89 return $this->response_code;
90 }
91
92 public function isWPError() {
93 return ( $this->response instanceof \WP_Error );
94 }
95
96 public function getWPError() {
97 return $this->response->get_error_message();
98 }
99
100 public function isError() {
101 return ( $this->isWPError() || ! $this->isSuccess() );
102 }
103
104 }
105