PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 trunk, at app/src/Services/RemoteAPIService.php

196 lines 4.8 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 "4" => "https://aid.wp-api.depicter.com/",
27 "5" => "https://api.my.depicter.com/mx/"
28 ];
29
30
31 /**
32 * Retrieves an endpoint by number
33 *
34 * @param int $endpointNumber
35 *
36 * @param string $branch The relative path to be appended to endpoint url
37 *
38 * @return mixed|string
39 */
40 public function endpoint( $endpointNumber = 1, $branch = '' ){
41 if( ! empty( $this->endpoints[ $endpointNumber ] ) ){
42 return $this->endpoints[ $endpointNumber ] . $branch;
43 }
44 return '';
45 }
46
47
48 private function isAbsoluteUrl( $url ){
49 return strpos($url,'://') !== false;
50 }
51
52 /**
53 * Get default options for requests
54 *
55 * @return array
56 * @throws GuzzleException
57 */
58 private function getDefaultOptions() {
59 global $wp_version;
60
61 $token = \Depicter::cache('base')->get( 'access_token', '' );
62
63 return [
64 'headers' => [
65 'user-agent' => 'WordPress/'. $wp_version .'; '. get_home_url(),
66 'timeout' => 30,
67 'X-DEPICTER-CKEY' => \Depicter::auth()->getClientKey(),
68 'X-DEPICTER-VER' => DEPICTER_VERSION,
69 'X-DEPICTER-TIER' => \Depicter::auth()->getTier(),
70 'Authorization' => 'Bearer ' . $token
71 ]
72 ];
73
74 }
75
76 /**
77 * Create and send an HTTP GET request to specified API endpoints.
78 *
79 * @param string $endpoint URI object or string.
80 * @param array $options Request options to apply.
81 * @param int $endpointNumber Endpoint number
82 *
83 * @return ResponseInterface
84 * @throws GuzzleException
85 */
86 public function get( $endpoint, $options = [], $endpointNumber = 1 )
87 {
88 // Maybe convert branch to absolute endpoint url
89 if( ! $this->isAbsoluteUrl( $endpoint ) ){
90 $endpoint = $this->endpoint( $endpointNumber, $endpoint );
91 }
92
93 $client = new Client([
94 'verify' => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
95 'proxy' => $this->getProxy()
96 ]);
97
98 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() );
99 $response = $client->get( $endpoint, $optionsWithAuth );
100
101 // try to get refresh token on failure
102 if ( $response->getStatusCode() == 401 ) {
103 \Depicter::cache('base')->delete('access_token');
104 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token
105 $response = $client->get( $endpoint, $optionsWithAuth );
106 }
107
108 return $response;
109 }
110
111 /**
112 * Create and send an HTTP POST request to specified API endpoints.
113 *
114 * @param string $endpoint URI object or string.
115 * @param array $options Request options to apply.
116 * @param int $endpointNumber Endpoint number
117 *
118 * @return ResponseInterface
119 * @throws GuzzleException
120 */
121 public function post( $endpoint, $options = [], $endpointNumber = 1 )
122 {
123 // Maybe convert branch to absolute endpoint url
124 if( ! $this->isAbsoluteUrl( $endpoint ) ){
125 $endpoint = $this->endpoint( $endpointNumber, $endpoint );
126 }
127
128 $client = new Client([
129 'verify' => ABSPATH . WPINC . '/certificates/ca-bundle.crt',
130 'proxy' => $this->getProxy()
131 ]);
132
133 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() );
134 $response = $client->post( $endpoint, $optionsWithAuth );
135
136 // try to get refresh token on failure
137 if ( $response->getStatusCode() == 401 ) {
138 \Depicter::cache('base')->delete('access_token');
139 $optionsWithAuth = Arr::merge( $options, $this->getDefaultOptions() ); // refresh token
140 $response = $client->post( $endpoint, $optionsWithAuth );
141 }
142
143 return $response;
144 }
145
146 /**
147 * Get proxy config
148 *
149 * @return string|null
150 */
151 public function getProxy() {
152
153 // Check if proxy constants are defined
154 $proxy = null;
155 if ( defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT') ) {
156 $proxy = WP_PROXY_HOST . ':' . WP_PROXY_PORT;
157
158 // Add credentials if necessary
159 if ( defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD') ) {
160 $proxy = WP_PROXY_USERNAME . ':' . WP_PROXY_PASSWORD . '@' . $proxy;
161 }
162 }
163
164 return $proxy;
165
166 }
167
168 /**
169 * Whether remote api address are accessible or not
170 *
171 * @return bool
172 */
173 public static function isAccessible()
174 {
175 return true;
176 }
177
178 /**
179 * Get auth token from server
180 *
181 * @return mixed|string
182 * @throws GuzzleException
183 */
184 public function getAuthToken() {
185 $client = new Client();
186 $response = $client->post( $this->endpoint(), $this->getDefaultOptions() );
187
188 $body = JSON::decode( $response->getBody(), true );
189
190 if ( !empty( $body['success']) ) {
191 return $body['success'];
192 }
193 return '';
194 }
195 }
196