PluginProbe
WPGet API – Connect to any external REST API / 1.6.1
WPGet API – Connect to any external REST API v1.6.1
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.6.1, at includes/class-api.php

508 lines 17.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 public $body_json_encode = ''; // body_json_encode body
31 public $body_url_encode = ''; // urlencode body
32
33 public $response = ''; // the returned response
34
35 public $final_url = ''; // final URL
36 public $final_request_args = array(); // final request_args
37
38 public $results_format = 'json_string'; // reseults format
39
40 public $timeout = 10; // the timeout
41 public $sslverify = 1; // the paramaters
42 public $debug = false;
43
44
45 /**
46 * Main constructor
47 *
48 * @since 1.0.0
49 *
50 */
51 public function __construct( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) {
52
53 $this->setup = get_option( 'wpgetapi_setup' );
54 $this->api = get_option( 'wpgetapi_' . $api_id );
55
56 $this->encryption = new WpGetApi_Encryption();
57 $this->api_id = sanitize_text_field( $api_id );
58 $this->endpoint_id = sanitize_text_field( $endpoint_id );
59 $this->keys = $keys;
60 $this->args = $args;
61
62 add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 5, 2 );
63
64 }
65
66
67 /**
68 * Init our api data
69 */
70 public function init() {
71
72 // do some checks
73 if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) )
74 return 'No API found.';
75
76 // do some checks
77 if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] ))
78 return 'API not found.';
79
80 // loop through api's
81 // set base_url
82 foreach ( $this->setup['apis'] as $index => $api_item ) {
83 if( $this->api_id == $api_item['id'] ) {
84 $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : '';
85 }
86 }
87
88 // loop through endpoints
89 foreach ( $this->api['endpoints'] as $index => $endpoint ) {
90
91 if( $this->endpoint_id == $endpoint['id'] ) {
92
93 $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET';
94 $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : '';
95 $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : '';
96
97 $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array();
98
99 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
100
101 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
102
103 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
104 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
105
106 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
107 }
108 }
109
110 $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout;
111 $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify;
112
113 }
114
115
116 /**
117 * The main function to output the data
118 *
119 * @param string $field Field to retrieve
120 */
121 public function endpoint_output() {
122 return $this->do_the_call();
123 }
124
125
126 /**
127 * Sort out the parameters if they exist
128 */
129 public function do_parameters( $parameters ) {
130
131 $new_array = array();
132 $parameters = $this->decrypt($parameters);
133
134 if( is_array( $parameters ) ) {
135 foreach ( $parameters as $key => $parameter ) {
136 if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
137 $parameter['value'] = stripslashes( $parameter['value'] );
138 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
139 }
140 }
141 }
142
143 // look for array or json string and sort these out - for using such things in shortcode
144 if( is_array( $new_array ) ) {
145
146 foreach ( $new_array as $key => $value ) {
147
148 // checking if the parameters have a json string
149 if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
150 $new_array[ $key ] = json_decode( $value, true);
151 }
152
153 // checking if the parameters have an array within the string
154 // if they do, create them as an actual array
155 if ( substr( $value, 0, 1 ) === '[' && substr( $value, -1 ) === ']' ) {
156 $new_array[ $key ] = array( $value );
157 }
158
159 }
160
161 }
162
163 return $new_array;
164
165 }
166
167 /**
168 * Send the request and return our data
169 */
170 public function do_the_call() {
171
172 $this->init();
173
174 // build the URL
175 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
176
177 // build the request args
178 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
179
180 // POST request
181 if( $this->method == 'POST' )
182 $this->response = $this->POST_request();
183
184 // PUT request
185 if( $this->method == 'PUT' )
186 $this->response = $this->PUT_request();
187
188 // GET request
189 if( $this->method == 'GET' )
190 $this->response = $this->GET_request();
191
192 // wp error
193 if( is_wp_error( $this->response ) ) {
194 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
195 }
196
197 // get response_code
198 $response_code = wp_remote_retrieve_response_code( $this->response );
199
200 // action to intercept call depending on response_code
201 $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
202
203 // get body
204 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
205 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
206
207 // returning in JSON format
208 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
209 $data = $this->return_data( $data );
210
211 return apply_filters( 'wpgetapi_raw_data', $data, $this );
212
213 }
214
215
216 /**
217 * Build the request url with the ability to add args
218 */
219 public function build_url() {
220
221 // first make sure our endpoint starts with a slash
222 if ( substr( $this->endpoint, 0, 1 ) != '/' )
223 $this->endpoint = '/' . $this->endpoint;
224
225 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
226
227 // filter our params - added for use in Oauth 2.0 plugin
228 $params = apply_filters( 'wpgetapi_query_parameters', $this->query_parameters, $this );
229
230 // add empty array - was getting errors if it was just null or false
231 $params = empty( $params ) ? array(''=>'') : $params;
232
233 return add_query_arg( $params, $url );
234
235 }
236
237
238 /**
239 * build the header with the ability to add args
240 */
241 public function build_request_args() {
242
243 // add our header parameters to the headers
244 $headers = apply_filters( 'wpgetapi_header_parameters', array(
245 'headers' => $this->header_parameters,
246 ), $this );
247
248
249 // if doing POST request, include body paramters
250 if( $this->method == 'POST' || $this->method == 'PUT' ) {
251
252 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
253
254 // do we json_encode the entire body as a string
255 if( $this->body_json_encode == true )
256 $this->body_parameters = json_encode( $this->body_parameters );
257
258
259 // do we urlencode the entire body as a string
260 if( $this->body_url_encode == true ) {
261 $data_raw = '';
262 foreach ($this->body_parameters as $key => $value){
263 $data_raw = $data_raw . $key . "=" . urlencode($value) . "&";
264 }
265 $this->body_parameters = $data_raw;
266 }
267
268 // add our body parameters
269 if( $this->body_parameters && ! empty( $this->body_parameters ) )
270 $headers['body'] = $this->body_parameters;
271
272 }
273
274 // add headers to our final request args
275 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
276
277 // build the args taht are sent to the request
278 $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array(
279 'timeout' => $this->timeout,
280 'sslverify' => $this->sslverify,
281 'redirection' => 5,
282 'blocking' => true,
283 'cookies' => array(),
284 ) );
285
286 return wp_parse_args( $this->final_request_args, $default_args );
287
288 }
289
290
291 /**
292 * Do a GET request
293 */
294 public function GET_request() {
295 $response = '';
296 // so we can so something before the remote_get (caching)
297 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
298 if( empty( $response ) )
299 $response = wp_remote_get( $this->final_url, $this->final_request_args );
300
301 return $response;
302 }
303
304
305 /**
306 * Do a POST request
307 */
308 public function POST_request() {
309 $response = wp_remote_post( $this->final_url, $this->final_request_args );
310 return $response;
311 }
312
313 /**
314 * Do a PUT request
315 */
316 public function PUT_request() {
317 $this->final_request_args['method'] = 'PUT';
318 $response = wp_remote_request( $this->final_url, $this->final_request_args );
319 return $response;
320 }
321
322
323 /**
324 * return data
325 */
326 public function return_data( $data ) {
327
328 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
329
330 // converts all data to arrays, removing objects
331 if( is_string( $data ) )
332 $data = json_decode( $data, true );
333
334 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
335
336 if( $this->results_format == 'json_string' )
337 return trim( json_encode( $data ),'"');
338
339 if( $this->results_format == 'json_decoded' )
340 return $data;
341
342 }
343
344
345 /**
346 * decrypt our parameters from the DB
347 */
348 public function decrypt( $array_or_string ) {
349
350 if( is_string($array_or_string) ){
351 $array_or_string = $this->encryption->decrypt($array_or_string);
352 }elseif( is_array($array_or_string) ){
353 foreach ( $array_or_string as $key => &$value ) {
354 if( $key === 'name' )
355 continue;
356 if ( is_array( $value ) ) {
357 $value = $this->decrypt($value);
358 }
359 else {
360 $value = $this->encryption->decrypt($value);
361
362 }
363 }
364 }
365 return $array_or_string;
366 }
367
368
369 /**
370 * debug
371 */
372 public function maybe_add_debug_info( $data, $this_data ) {
373
374 if( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) {
375
376 $this->debug = true;
377
378 ob_start(); ?>
379
380 <style>
381
382 .wpgetapi-debug {
383 background: #f4f4f4;
384 padding: 20px;
385 border: 1px solid #ccc;
386 margin: 20px;
387 font-size: 15px;
388 }
389 .wpgetapi-debug
390 .wpgetapi-debug p
391 .wpgetapi-debug h5
392 .wpgetapi-debug h6 {
393 color: #000;
394 line-height: 1 !important;
395 text-transform: none !important;
396 font-family: sans-serif !important;
397 letter-spacing: 0px !important;
398 }
399 .wpgetapi-debug p.small {
400 font-size: 85%;
401 color: #999;
402 margin: 0 0 12px !important;
403 }
404 .wpgetapi-debug h5 {
405 font-weight: bold;
406 font-size: 18px;
407 margin: 0 0 3px;
408 text-transform: none !important;
409 }
410 .wpgetapi-debug .debug-item {
411 margin: 10px 0;
412 background: #fff;
413 padding: 15px 20px;
414 display: inline-block !important;
415 width: 100% !important;
416 box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
417 border: 1px solid #efefef;
418 }
419 .wpgetapi-debug .debug-item h6 {
420 font-weight: bold;
421 font-size: 16px;
422 margin: 0;
423 text-transform: none !important;
424 }
425 .wpgetapi-debug .debug-item .debug-result,
426 .wpgetapi-debug .debug-item .debug-result pre {
427 font-family: monospace !important;
428 font-size: 15px !important;
429 margin: 0 !important;
430 padding: 10px 12px;
431 background: #f8f8f8;
432 }
433 .wpgetapi-debug .debug-item .debug-result pre {
434 border: none;
435 padding: 5px
436 }
437
438 </style>
439
440 <div class="wpgetapi-debug">
441
442 <h5><strong><?php esc_html_e( 'WPGetAPI, Debug Mode - Plugin Version', 'wpgetapi' ); ?> <?php esc_html_e( WpGetApi()->version ); ?></strong></h5>
443 <p class="small"><?php esc_html_e( '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.', 'wpgetapi' ); ?><br><?php esc_html_e( 'Connecting to API\'s can be tricky - if you require help, please see the', 'wpgetapi' ); ?> <a target="_blank" href="https://wpgetapi.com/docs/"><?php esc_html_e( 'Documentation', 'wpgetapi' ); ?></a>.</p>
444
445 <div class="debug-item">
446 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
447 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
448 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
449 </div>
450
451 <div class="debug-item">
452 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
453 <p class="small"><?php esc_html_e( 'The resulting output that has been returned from the API.', 'wpgetapi' ); ?></p>
454 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
455 </div>
456
457 <div class="debug-item">
458 <h6><?php esc_html_e( 'Arguments', 'wpgetapi' ); ?></h6>
459 <p class="small"><?php esc_html_e( 'The arguments set within your shortcode or template tag.', 'wpgetapi' ); ?></p>
460 <div class="debug-result"><?php wpgetapi_pp( $this->args ); ?></div>
461 </div>
462
463 <div class="debug-item">
464 <h6><?php esc_html_e( 'Query String', 'wpgetapi' ); ?></h6>
465 <p class="small"><?php esc_html_e( 'The query string parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
466 <div class="debug-result"><?php wpgetapi_pp( $this->query_parameters ); ?></div>
467 </div>
468
469 <div class="debug-item">
470 <h6><?php esc_html_e( 'Headers', 'wpgetapi' ); ?></h6>
471 <p class="small"><?php esc_html_e( 'The header parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
472 <div class="debug-result"><?php wpgetapi_pp( $this->header_parameters ); ?></div>
473 </div>
474
475 <div class="debug-item">
476 <h6><?php esc_html_e( 'Body', 'wpgetapi' ); ?></h6>
477 <p class="small"><?php esc_html_e( 'The body parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
478 <div class="debug-result"><?php wpgetapi_pp( $this->body_parameters ); ?></div>
479 </div>
480
481 <div class="debug-item">
482 <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
483 <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
484 <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
485 </div>
486
487 <div class="debug-item">
488 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
489 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
490 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
491 </div>
492
493 </div>
494
495 <?php
496
497 return ob_get_clean();
498
499 } else {
500
501 return $data;
502
503 }
504
505 }
506
507
508 }