PluginProbe
WPGet API – Connect to any external REST API / 1.1.0
WPGet API – Connect to any external REST API v1.1.0
1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.10 2.2.2 2.2.3 All 97 releases
wpgetapi / includes / class-api.php

class-api.php in WPGet API – Connect to any external REST API 1.1.0, at includes/class-api.php

336 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 /**
7 * The main class
8 *
9 * @since 1.0.0
10 */
11 class WpGetApi_Api {
12
13 public $encryption = ''; // the encryption class
14
15 public $setup = ''; // store the setup data from the admin page
16 public $api = ''; // store the api's from the admin page
17
18 public $api_id = ''; // the api_id that we are wanting to get
19 public $endpoint_id = ''; // the endpoint_id that we are wanting to get
20 public $args = ''; // the args
21 public $keys = ''; // the keys we want to retrieve
22
23 public $base_url = ''; // base url of the API
24 public $method = 'GET'; // the endpoint method GET POST etc
25 public $cache_time = ''; // the time to cache
26 public $endpoint = ''; // the endpoint that we will get
27 public $query_parameters = ''; // the query paramaters appended to url
28 public $header_parameters = ''; // the header paramaters
29 public $body_parameters = ''; // the body paramaters
30
31 public $response = ''; // the returned response
32
33 public $final_url = ''; // final URL
34 public $final_headers = ''; // final headers
35
36 public $results_format = 'json_string'; // reseults format
37
38 public $timeout = 10; // the timeout
39 public $sslverify = 1; // the paramaters
40
41
42 /**
43 * Main constructor
44 *
45 * @since 1.0.0
46 *
47 */
48 public function __construct( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) {
49
50 $this->setup = get_option( 'wpgetapi_setup' );
51 $this->api = get_option( 'wpgetapi_' . $api_id );
52
53 $this->encryption = new WpGetApi_Encryption();
54 $this->api_id = sanitize_text_field( $api_id );
55 $this->endpoint_id = sanitize_text_field( $endpoint_id );
56 $this->keys = $keys;
57 $this->args = $args;
58
59 add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 5, 2 );
60
61 }
62
63
64 /**
65 * Init our api data
66 */
67 public function init() {
68
69 // do some checks
70 if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) )
71 return 'No API found.';
72
73 // do some checks
74 if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] ))
75 return 'API not found.';
76
77 // loop through api's
78 // set base_url
79 foreach ( $this->setup['apis'] as $index => $api_item ) {
80 if( $this->api_id == $api_item['id'] ) {
81 $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : '';
82 }
83 }
84
85 // loop through endpoints
86 foreach ( $this->api['endpoints'] as $index => $endpoint ) {
87
88 if( $this->endpoint_id == $endpoint['id'] ) {
89
90 $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET';
91 $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : '';
92 $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : '';
93 $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array();
94 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
95 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
96 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
97 }
98 }
99
100 $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout;
101 $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify;
102
103
104 }
105
106
107 /**
108 * The main function to output the data
109 *
110 * @param string $field Field to retrieve
111 */
112 public function endpoint_output() {
113 return $this->do_the_call();
114 }
115
116
117 /**
118 * Sort out the parameters if they exist
119 */
120 public function do_parameters( $parameters ) {
121
122 $array = array();
123 $parameters = $this->decrypt($parameters);
124
125 if( is_array( $parameters ) ) {
126 foreach ( $parameters as $key => $parameter ) {
127 if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) )
128 $array[ sanitize_text_field( $parameter['name'] ) ] = sanitize_text_field( $parameter['value'] );
129 }
130 }
131
132 return $array;
133
134 }
135
136 /**
137 * Send the request and return our data
138 */
139 public function do_the_call() {
140
141 $this->init();
142
143 // build the URL
144 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url() );
145
146 // build the headers
147 $this->final_headers = apply_filters( 'wpgetapi_final_headers', $this->build_headers() );
148
149 // POST request
150 if( $this->method == 'POST' )
151 $this->response = $this->POST_request();
152
153 // GET request
154 if( $this->method == 'GET' )
155 $this->response = $this->GET_request();
156
157 // wp error
158 if( is_wp_error( $this->response ) ) {
159 return json_encode( $this->response );
160 }
161
162 $data = wp_remote_retrieve_body( $this->response );
163
164 // returning in JSON format
165 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
166 $data = $this->output_json( $data );
167
168 return apply_filters( 'wpgetapi_raw_data', $data, $this );
169
170 }
171
172
173 /**
174 * return JSON data
175 */
176 public function output_json( $data ) {
177
178 // converts all data to arrays, removing objects
179 $data = json_decode( $data, true );
180 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
181
182 if( $this->results_format == 'json_string' )
183 return json_encode( $data );
184
185 if( $this->results_format == 'json_decoded' )
186 return $data;
187
188 }
189
190
191 /**
192 * build the request url with the ability to add args
193 */
194 public function build_url() {
195 // make sure our endpoint starts with a slash
196 if ( substr( $this->endpoint, 0, 1 ) != '/' )
197 $this->endpoint = '/' . $this->endpoint;
198 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
199 $params = empty( $this->query_parameters ) ? array(''=>'') : $this->query_parameters;
200 return add_query_arg( $params, $url );
201 }
202
203 /**
204 * build the header with the ability to add args
205 */
206 public function build_headers() {
207
208 if( $this->method == 'POST' ) {
209
210 if( isset( $this->body_parameters ) && $this->body_parameters != '' ) {
211
212 // checking if the body paramters have a json string
213 foreach ($this->body_parameters as $key => $value ) {
214
215 if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
216 $this->body_parameters[ $key ] = json_decode( $value, true);
217 }
218
219 }
220
221 $body = apply_filters( 'wpgetapi_body_parameters', array(
222 'body' => json_encode( $this->body_parameters ),
223 ) );
224
225 $this->header_parameters = wp_parse_args( $body, $this->header_parameters );
226 }
227
228 }
229
230 // build the header
231 $defaults = apply_filters( 'wpgetapi_default_header_parameters', array(
232 'timeout' => $this->timeout,
233 'sslverify' => $this->sslverify,
234 'redirection' => 5,
235 'blocking' => true,
236 'cookies' => array(),
237 ) );
238
239 return wp_parse_args( $this->header_parameters, $defaults );
240
241 }
242
243
244 /**
245 * Do a GET request
246 */
247 public function GET_request() {
248 $response = '';
249 // so we can so something before the remote_get (caching)
250 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
251 if( empty( $response ) )
252 $response = wp_remote_get( $this->final_url, $this->final_headers );
253
254 return $response;
255 }
256
257
258 /**
259 * Do a POST request
260 */
261 public function POST_request() {
262 $response = wp_remote_post( $this->final_url, $this->final_headers );
263 return $response;
264 }
265
266
267 /**
268 * decrypt our parameters from the DB
269 */
270 public function decrypt( $array_or_string ) {
271
272 if( is_string($array_or_string) ){
273 $array_or_string = $this->encryption->decrypt($array_or_string);
274 }elseif( is_array($array_or_string) ){
275 foreach ( $array_or_string as $key => &$value ) {
276 if( $key == 'name' )
277 continue;
278 if ( is_array( $value ) ) {
279 $value = $this->decrypt($value);
280 }
281 else {
282 $value = $this->encryption->decrypt($value);
283 }
284 }
285 }
286 return $array_or_string;
287 }
288
289
290 /**
291 * debug
292 */
293 public function maybe_add_debug_info( $data, $this_data ) {
294
295 if( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) {
296
297 ob_start(); ?>
298
299 <div class="wpgetapi-debug" style="background:#efefef; padding:20px; border:1px solid #ccc; ">
300
301 <p style="margin-bottom:5px"><strong>WPGetAPI, Debug Mode - Plugin Version <?php esc_html_e( WpGetApi()->version ); ?></strong></p>
302 <p style="font-size:80%;margin-bottom:20px"><i>Below you will find debug info for the current API call. To turn this off, set debug to false in your shortcode or template tag.</i></p>
303
304 <strong>Final URL</strong><br>
305 <?php esc_html_e( $this->final_url ); ?><br><br>
306
307 <strong>Data Output</strong><br>
308 <?php wpgetapi_pp( $data ); ?>
309
310 <strong>Query Parameters</strong><br>
311 <?php wpgetapi_pp( $this->query_parameters ); ?>
312
313 <strong>Headers</strong><br>
314 <?php wpgetapi_pp( $this->final_headers ); ?>
315
316 <strong>Response</strong><br>
317 <?php wpgetapi_pp($this->response); ?>
318
319 </div>
320
321 <?php
322
323 return ob_get_clean();
324
325 } else {
326
327 return $data;
328
329 }
330
331
332
333 }
334
335
336 }