PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Services / RemoteAPIService.php

RemoteAPIService.php in Depicter — Popup & Slider Builder 1.2.0, at app/src/Services/RemoteAPIService.php

162 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Services;
3
4
5 use Averta\Core\Utility\Arr;
6 use Averta\WordPress\Utility\JSON;
7 use Depicter\GuzzleHttp\Client;
8 use Depicter\GuzzleHttp\Exception\GuzzleException;
9 use Psr\Http\Message\ResponseInterface;
10
11 /**
12 * Performs an HTTP request and returns its response.
13 *
14 * @package Depicter\Services
15 */
16 class RemoteAPIService
17 {
18 /**
19 * List of endpoints
20 *
21 * @var array
22 */
23 protected $endpoints = [
24 "1" => "https://wp-api.depicter.com/",
25 "2" => "https://pre.wp-api.depicter.com/"
26 ];
27
28
29 /**
30 * Retrieves an endpoint by number
31 *
32 * @param int $endpointNumber
33 *
34 * @param string $branch The relative path to be appended to endpoint url
35 *
36 * @return mixed|string
37 */
38 public function endpoint( $endpointNumber = 1, $branch = '' ){
39 if( ! empty( $this->endpoints[ $endpointNumber ] ) ){
40 return $this->endpoints[ $endpointNumber ] . $branch;
41 }
42 return '';
43 }
44
45
46 private function isAbsoluteUrl( $url ){
47 return strpos($url,'://') !== false;
48 }
49
50 /**
51 * Get default options for requests
52 *
53 * @return array
54 * @throws GuzzleException
55 */
56 private function getDefaultOptions() {
57 global $wp_version;
58
59 $token = \Depicter::options()->get( 'access_token', '' );
60
61 return [
62 'headers' => [
63 'user-agent' => 'WordPress/'. $wp_version .'; '. get_home_url(),
64 'timeout' => 30,
65 'X-DEPICTER-CKEY' => \Depicter::options()->get( 'client_key' ),
66 'X-DEPICTER-VER' => DEPICTER_VERSION,
67 'Authorization' => 'Bearer ' . $token
68 ]
69 ];
70 }
71
72 /**
73 * Create and send an HTTP GET request to specified API endpoints.
74 *
75 * @param string $endpoint URI object or string.
76 * @param array $options Request options to apply.
77 * @param int $endpointNumber Endpoint number
78 *
79 * @return ResponseInterface
80 * @throws GuzzleException
81 */
82 public function get( $endpoint, $options = [], $endpointNumber = 1 )
83 {
84 // Maybe convert branch to absolute endpoint url
85 if( ! $this->isAbsoluteUrl( $endpoint ) ){
86 $endpoint = $this->endpoint( $endpointNumber, $endpoint );
87 }
88
89 $client = new Client();
90 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() );
91 $response = $client->get( $endpoint, $optionsWithAuth );
92
93 // try to get refresh token on failure
94 if ( $response->getStatusCode() == 401 ) {
95 \Depicter::options()->delete('access_token');
96 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token
97 $response = $client->get( $endpoint, $optionsWithAuth );
98 }
99
100 return $response;
101 }
102
103 /**
104 * Create and send an HTTP POST request to specified API endpoints.
105 *
106 * @param string $endpoint URI object or string.
107 * @param array $options Request options to apply.
108 * @param int $endpointNumber Endpoint number
109 *
110 * @return ResponseInterface
111 * @throws GuzzleException
112 */
113 public function post( $endpoint, $options = [], $endpointNumber = 1 )
114 {
115 // Maybe convert branch to absolute endpoint url
116 if( ! $this->isAbsoluteUrl( $endpoint ) ){
117 $endpoint = $this->endpoint( $endpointNumber, $endpoint );
118 }
119
120 $client = new Client();
121 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() );
122 $response = $client->post( $endpoint, $optionsWithAuth );
123
124 // try to get refresh token on failure
125 if ( $response->getStatusCode() == 401 ) {
126 \Depicter::options()->delete('access_token');
127 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token
128 $response = $client->post( $endpoint, $optionsWithAuth );
129 }
130
131 return $response;
132 }
133
134 /**
135 * Whether remote api address are accessible or not
136 *
137 * @return bool
138 */
139 public static function isAccessible()
140 {
141 return true;
142 }
143
144 /**
145 * Get auth token from server
146 *
147 * @return mixed|string
148 * @throws GuzzleException
149 */
150 public function getAuthToken() {
151 $client = new Client();
152 $response = $client->post( $this->endpoint(), $this->getDefaultOptions() );
153
154 $body = JSON::decode( $response->getBody(), true );
155
156 if ( !empty( $body['success']) ) {
157 return $body['success'];
158 }
159 return '';
160 }
161 }
162