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

591 lines 21.5 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' ), 99, 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
123 do_action( 'wpgetapi_before_do_the_call', $this );
124
125 return $this->do_the_call();
126
127 }
128
129
130 /**
131 * Sort out the parameters if they exist
132 */
133 public function do_parameters( $parameters ) {
134
135 $new_array = array();
136 $parameters = $this->decrypt( $parameters );
137
138 if( is_array( $parameters ) ) {
139 foreach ( $parameters as $key => $parameter ) {
140 if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
141 $parameter['value'] = stripslashes( $parameter['value'] );
142 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
143 }
144 }
145 }
146
147 // look for array or json string and sort these out - for using such things in shortcode
148 if( is_array( $new_array ) ) {
149
150 foreach ( $new_array as $key => $value ) {
151
152 // checking if the parameters have a json string
153 if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
154 $new_array[ $key ] = json_decode( $value, true );
155 }
156
157 // checking if the parameters have an array within the string
158 // if they do, create them as an actual array
159 if ( substr( $value, 0, 1 ) === '[' && substr( $value, -1 ) === ']' ) {
160 $value = trim( $value,"[]" );
161 $new_array[ $key ] = array( $value );
162
163 }
164
165 // set value as integer
166 if ( strpos( $value, 'integer(' ) !== false && substr( $value, -1 ) === ')' ) {
167 preg_match("/\(([^\)]*)\)/", $value, $match );
168 $result = $match[1];
169 $new_array[ $key ] = absint( $result );
170
171 }
172
173 // set value as float
174 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
175 preg_match("/\(([^\)]*)\)/", $value, $match );
176 $result = $match[1];
177 $new_array[ $key ] = (float) $result;
178
179 }
180
181 }
182
183 }
184
185 return $new_array;
186
187 }
188
189 /**
190 * Send the request and return our data
191 */
192 public function do_the_call() {
193
194 $this->init();
195
196 // build the URL
197 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
198
199 // build the request args
200 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
201
202 /**
203 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
204 * @param bool $on Whether on is set or not.
205 */
206 if ( apply_filters( 'wpgetapi_should_we_stop', false, $this ) ) {
207 return;
208 }
209
210 // POST request
211 if( $this->method == 'POST' )
212 $this->response = $this->POST_request();
213
214 // PUT request
215 if( $this->method == 'PUT' )
216 $this->response = $this->PUT_request();
217
218 // GET request
219 if( $this->method == 'GET' )
220 $this->response = $this->GET_request();
221
222 // DELETE request
223 if( $this->method == 'DELETE' )
224 $this->response = $this->DELETE_request();
225
226 // wp error
227 if( is_wp_error( $this->response ) ) {
228 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
229 }
230
231 // get response_code
232 $response_code = wp_remote_retrieve_response_code( $this->response );
233
234 // action to intercept call depending on response_code
235 $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
236
237 // get body
238 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
239 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
240
241 // returning in JSON format
242 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
243 $data = $this->return_data( $data );
244
245 return apply_filters( 'wpgetapi_raw_data', $data, $this );
246
247 }
248
249
250 /**
251 * Build the request url with the ability to add args
252 */
253 public function build_url() {
254
255 // first make sure our endpoint starts with a slash
256 if ( substr( $this->endpoint, 0, 1 ) != '/' )
257 $this->endpoint = '/' . $this->endpoint;
258
259 $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this );
260
261 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
262
263 // filter our params - added for use in Oauth 2.0 plugin
264 $params = apply_filters( 'wpgetapi_query_parameters', $this->query_parameters, $this );
265
266 // add empty array - was getting errors if it was just null or false
267 $params = empty( $params ) ? array(''=>'') : $params;
268
269 return add_query_arg( $params, $url );
270
271 }
272
273
274 /**
275 * build the header with the ability to add args
276 */
277 public function build_request_args() {
278
279 // add our header parameters to the headers
280 $headers = apply_filters( 'wpgetapi_header_parameters', array(
281 'headers' => $this->header_parameters,
282 ), $this );
283
284
285 // if doing POST request, include body paramters
286 if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
287
288 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
289
290 // do we json_encode the entire body as a string
291 if( $this->body_json_encode == true ) {
292 $this->body_parameters = json_encode( $this->body_parameters );
293 }
294
295
296 // do we urlencode the entire body as a string
297 if( $this->body_url_encode == true ) {
298 $data_raw = '';
299 foreach ($this->body_parameters as $key => $value){
300 $data_raw = $data_raw . $key . "=" . urlencode($value) . "&";
301 }
302 $this->body_parameters = $data_raw;
303 }
304
305 // add our body parameters
306 if( $this->body_parameters && ! empty( $this->body_parameters ) )
307 $headers['body'] = $this->body_parameters;
308
309 }
310
311 // add headers to our final request args
312 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
313
314 // build the args taht are sent to the request
315 $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array(
316 'timeout' => $this->timeout,
317 'sslverify' => $this->sslverify,
318 'redirection' => 5,
319 'blocking' => true,
320 'cookies' => array(),
321 ) );
322
323 return wp_parse_args( $this->final_request_args, $default_args );
324
325 }
326
327
328 /**
329 * Do a GET request
330 */
331 public function GET_request() {
332 $response = '';
333 // so we can so something before the remote_get (caching)
334 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
335 if( empty( $response ) )
336 $response = wp_remote_get( $this->final_url, $this->final_request_args );
337
338 return $response;
339 }
340
341 /**
342 * Do a POST request
343 */
344 public function POST_request() {
345 $response = wp_remote_post( $this->final_url, $this->final_request_args );
346 return $response;
347 }
348
349 /**
350 * Do a PUT request
351 */
352 public function PUT_request() {
353 $this->final_request_args['method'] = 'PUT';
354 $response = wp_remote_request( $this->final_url, $this->final_request_args );
355 return $response;
356 }
357
358 /**
359 * Do a DELETE request
360 */
361 public function DELETE_request() {
362 $this->final_request_args['method'] = 'DELETE';
363 $response = wp_remote_request( $this->final_url, $this->final_request_args );
364 return $response;
365 }
366
367
368 /**
369 * return data
370 */
371 public function return_data( $data ) {
372
373 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
374
375 // converts all data to arrays, removing objects
376 if( is_string( $data ) )
377 $data = json_decode( $data, true );
378
379 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
380
381 if( $this->results_format == 'json_string' )
382 return trim( json_encode( $data ),'"');
383
384 if( $this->results_format == 'json_decoded' )
385 return $data;
386
387 }
388
389
390 /**
391 * decrypt our parameters from the DB
392 */
393 public function decrypt( $array_or_string ) {
394
395 if( is_string($array_or_string) ){
396 $array_or_string = $this->encryption->decrypt($array_or_string);
397 }elseif( is_array($array_or_string) ){
398 foreach ( $array_or_string as $key => &$value ) {
399 if( $key === 'name' )
400 continue;
401 if ( is_array( $value ) ) {
402 $value = $this->decrypt($value);
403 }
404 else {
405 $value = $this->encryption->decrypt($value);
406
407 }
408 }
409 }
410 return $array_or_string;
411 }
412
413
414 /**
415 * debug
416 */
417 public function maybe_add_debug_info( $data, $wpgetapi ) {
418
419 if(
420 ( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) ||
421 ( isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) )
422 ) {
423
424 $this->debug = true;
425
426 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
427
428 ob_start(); ?>
429
430 <style>
431
432 .wpgetapi-debug {
433 background: #fff;
434 padding: 20px;
435 box-shadow: 0 0 1px #aaa;
436 margin: 20px;
437 font-size: 15px;
438 }
439 .wpgetapi-debug
440 .wpgetapi-debug p
441 .wpgetapi-debug h5
442 .wpgetapi-debug h6 {
443 color: #000;
444 line-height: 1 !important;
445 text-transform: none !important;
446 font-family: sans-serif !important;
447 letter-spacing: 0px !important;
448 }
449 .wpgetapi-debug p.small {
450 font-size: 85%;
451 color: #999;
452 margin: 0 0 12px !important;
453 }
454 .wpgetapi-debug h5 {
455 font-weight: bold;
456 font-size: 18px;
457 margin: 0 0 3px;
458 text-transform: none !important;
459 }
460 .wpgetapi-debug .debug-item {
461 margin: 10px 0;
462 background: #fff;
463 padding: 15px 20px;
464 display: inline-block !important;
465 width: 100% !important;
466 box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
467 border: 1px solid #efefef;
468 }
469 .wpgetapi-debug .debug-item h6 {
470 font-weight: bold;
471 font-size: 14px;
472 margin: 0;
473 text-transform: none !important;
474 }
475 .wpgetapi-debug .debug-item .debug-result,
476 .wpgetapi-debug .debug-item .debug-result pre {
477 font-family: monospace !important;
478 font-size: 13px !important;
479 margin: 0 !important;
480 padding: 8px 12px;
481 background: #f8f8f8;
482 }
483 .wpgetapi-debug .debug-item .debug-result pre {
484 border: none;
485 padding: 5px
486 }
487
488 .wpgetapi-debug.testing {
489 background: transparent;
490 padding: 0;
491 box-shadow: none;
492 }
493 .wpgetapi-debug.testing .debug-item {
494 margin: 5px 0;
495 }
496 </style>
497
498 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
499
500 <?php if( $testing ) { ?>
501
502 <div class="debug-item">
503 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
504 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
505 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
506 </div>
507
508 <div class="debug-item">
509 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
510 <p class="small"><?php esc_html_e( 'The output that has been returned from the API. This is what the shortcode or template tag will display and can be formatted however you like.', 'wpgetapi' ); ?></p>
511 <p class="small"><?php esc_html_e( 'See the ', 'wpgetapi' ); ?> <a target="_blank" href="https://wpgetapi.com/docs/?utm_campaign=Test Endpoint&utm_medium=Admin&utm_source=User"><?php esc_html_e( 'documentation', 'wpgetapi' ); ?></a> <?php esc_html_e( ' for help in formatting.', 'wpgetapi' ); ?></p>
512 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
513 </div>
514
515 <div class="debug-item">
516 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
517 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
518 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
519 </div>
520
521 <?php } else { ?>
522
523 <h5><strong><?php esc_html_e( 'WPGetAPI, Debug Mode - Plugin Version', 'wpgetapi' ); ?> <?php esc_html_e( WpGetApi()->version ); ?></strong></h5>
524 <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>
525
526 <div class="debug-item">
527 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
528 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
529 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
530 </div>
531
532 <div class="debug-item">
533 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
534 <p class="small"><?php esc_html_e( 'The resulting output that has been returned from the API.', 'wpgetapi' ); ?></p>
535 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
536 </div>
537
538 <div class="debug-item">
539 <h6><?php esc_html_e( 'Query String', 'wpgetapi' ); ?></h6>
540 <p class="small"><?php esc_html_e( 'The query string parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
541 <div class="debug-result"><?php wpgetapi_pp( $this->query_parameters ); ?></div>
542 </div>
543
544 <div class="debug-item">
545 <h6><?php esc_html_e( 'Headers', 'wpgetapi' ); ?></h6>
546 <p class="small"><?php esc_html_e( 'The header parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
547 <div class="debug-result"><?php wpgetapi_pp( $this->header_parameters ); ?></div>
548 </div>
549
550 <div class="debug-item">
551 <h6><?php esc_html_e( 'Body', 'wpgetapi' ); ?></h6>
552 <p class="small"><?php esc_html_e( 'The body parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
553 <div class="debug-result"><?php wpgetapi_pp( $this->body_parameters ); ?></div>
554 </div>
555
556 <div class="debug-item">
557 <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
558 <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
559 <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
560 </div>
561
562 <div class="debug-item">
563 <h6><?php esc_html_e( 'Arguments', 'wpgetapi' ); ?></h6>
564 <p class="small"><?php esc_html_e( 'The arguments set within your shortcode or template tag.', 'wpgetapi' ); ?></p>
565 <div class="debug-result"><?php wpgetapi_pp( $this->args ); ?></div>
566 </div>
567
568 <div class="debug-item">
569 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
570 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
571 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
572 </div>
573
574 <?php } ?>
575
576 </div>
577
578 <?php
579
580 return ob_get_clean();
581
582 } else {
583
584 return $data;
585
586 }
587
588 }
589
590
591 }