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

827 lines 31.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 public $body_base64_encode = ''; // base64 encode body
33 public $body_xml_format = ''; // xml format body
34 public $xml_root_node = 'root'; // root node for xml
35
36 public $response = ''; // the returned response
37
38 public $final_url = ''; // final URL
39 public $final_request_args = array(); // final request_args
40
41 public $results_format = 'json_string'; // results format
42
43 public $response_code = ''; // the response code from the call
44
45 public $timeout = 10; // the timeout
46 public $sslverify = 1; // the paramaters
47 public $debug = false;
48
49
50 /**
51 * Main constructor
52 *
53 * @since 1.0.0
54 *
55 */
56 public function __construct( $api_id = '', $endpoint_id = '', $args = array(), $keys = array() ) {
57
58 $this->setup = get_option( 'wpgetapi_setup' );
59 $this->api = get_option( 'wpgetapi_' . $api_id );
60
61 $this->encryption = new WpGetApi_Encryption();
62 $this->api_id = sanitize_text_field( $api_id );
63 $this->endpoint_id = sanitize_text_field( $endpoint_id );
64 $this->keys = $keys;
65 $this->args = $args;
66
67 add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 99, 2 );
68
69 }
70
71
72 /**
73 * Init our api data
74 */
75 public function init() {
76
77 // if we are doing importer plugin,
78 // we need to intercept and modify most of our methods values
79 if( $this->api_id == 'wpgetapi_importer' ) {
80
81 $data = apply_filters( 'wpgetapi_api_importer_init', $this );
82
83 if( $data ) {
84
85 foreach ($data as $key => $value) {
86 $this->{$key} = $value;
87 }
88
89 $this->query_parameters = ! empty( $this->query_parameters ) ? $this->do_parameters( $this->query_parameters ) : array();
90 $this->header_parameters = ! empty( $this->header_parameters ) ? $this->do_parameters( $this->header_parameters ) : array();
91 $this->body_parameters = ! empty( $this->body_parameters ) ? $this->do_parameters( $this->body_parameters ) : array();
92 return;
93
94 }
95
96 }
97
98 // do some checks
99 if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) )
100 return 'No API found.';
101
102 // do some checks
103 if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] ))
104 return 'API not found.';
105
106 // loop through api's
107 // set base_url
108 foreach ( $this->setup['apis'] as $index => $api_item ) {
109 if( $this->api_id == $api_item['id'] ) {
110 $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : '';
111 }
112 }
113
114 // loop through endpoints
115 foreach ( $this->api['endpoints'] as $index => $endpoint ) {
116
117 if( $this->endpoint_id == $endpoint['id'] ) {
118
119 $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET';
120 $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : '';
121 $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : '';
122
123 $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array();
124
125 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
126
127 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
128
129 // how to encode the body
130 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
131 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
132 $this->body_base64_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'base64' ? true : false;
133 $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false;
134
135 $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node;
136
137 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
138
139
140 if(
141 // set the format to php array data if html format is set
142 ( isset( $this->args['format'] ) && $this->args['format'] == 'html' && $this->results_format != 'xml_array' ) ||
143 // set the format to php array data if it is set in the args (coming from the importer)
144 ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
145 ) {
146 $this->results_format = 'json_decoded';
147 }
148
149 // set the format to JSON string if it is set in the args (coming from the wpdatatables integration)
150 if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_string' ) {
151 $this->results_format = 'json_string';
152 }
153
154 // set the format to JSON string if it is set in the args (coming from the wpdatatables integration)
155 if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' ) {
156 $this->results_format = 'json_decoded';
157 }
158
159 }
160
161 }
162
163 $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout;
164 $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify;
165
166 }
167
168
169 /**
170 * The main function to output the data
171 *
172 * @param string $field Field to retrieve
173 */
174 public function endpoint_output() {
175
176 do_action( 'wpgetapi_before_do_the_call', $this );
177
178 return $this->do_the_call();
179
180 }
181
182
183 /**
184 * Send the request and return our data
185 */
186 public function do_the_call() {
187
188 $this->init();
189
190 // build the URL
191 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
192
193 // build the request args
194 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
195
196 /**
197 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
198 * @param bool $on Whether on is set or not.
199 */
200
201 if ( apply_filters( 'wpgetapi_should_we_stop', false, $this ) ) {
202 return;
203 }
204
205 // POST request
206 if( $this->method == 'POST' )
207 $this->response = $this->POST_request();
208
209 // PUT request
210 if( $this->method == 'PUT' )
211 $this->response = $this->PUT_request();
212
213 // PATCH request
214 if( $this->method == 'PATCH' )
215 $this->response = $this->PATCH_request();
216
217 // GET request
218 if( $this->method == 'GET' )
219 $this->response = $this->GET_request();
220
221 // DELETE request
222 if( $this->method == 'DELETE' )
223 $this->response = $this->DELETE_request();
224
225 // wp error
226 if( is_wp_error( $this->response ) ) {
227 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
228 }
229
230 // get response_code
231 $this->response_code = wp_remote_retrieve_response_code( $this->response );
232
233 // action to intercept call depending on response_code
234 $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $this->response_code, $this );
235
236 // get body
237 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
238 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
239
240 // returning in JSON format
241 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
242 $data = $this->return_data( $data );
243
244 if( $data === 'null' )
245 $data = 'Result is null. Check that all endpoint settings are correct.';
246
247 return apply_filters( 'wpgetapi_raw_data', $data, $this );
248
249 }
250
251
252 /**
253 * Build the request url with the ability to add args
254 */
255 public function build_url() {
256
257 // first make sure our endpoint starts with a slash
258 if ( substr( $this->endpoint, 0, 1 ) != '/' )
259 $this->endpoint = '/' . $this->endpoint;
260
261 $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this );
262
263 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
264
265 // if we have a URL pagination event from the import plugin
266 // we need to modify the URL here
267 if( isset( $this->args['paginate_url'] ) )
268 $url = $this->args['paginate_url'];
269
270 // filter our params - added for use in Oauth 2.0 plugin
271 $params = apply_filters( 'wpgetapi_query_parameters', $this->query_parameters, $this );
272
273 // add empty array - was getting errors if it was just null or false
274 $params = empty( $params ) ? array(''=>'') : $params;
275
276 $result = add_query_arg( $params, $url );
277
278 return $result;
279
280 }
281
282
283 /**
284 * build the header with the ability to add args
285 */
286 public function build_request_args() {
287
288 // add our header parameters to the headers
289 $headers = apply_filters( 'wpgetapi_header_parameters', array(
290 'headers' => $this->header_parameters,
291 ), $this );
292
293
294 // if doing POST request, include body paramters
295 if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
296
297 // tokens are sent through here
298 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
299
300 // do we json_encode the entire body
301 if( $this->body_json_encode == true ) {
302 $this->body_parameters = json_encode( $this->body_parameters );
303 }
304
305 // do we urlencode the entire body
306 if( $this->body_parameters && $this->body_url_encode == true ) {
307
308 // checking if the parameters have a json string (raw)
309 if (
310 ( ! is_array( $this->body_parameters ) ) &&
311 ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
312 ) {
313 $this->body_parameters = json_decode( $this->body_parameters, true );
314 }
315
316 $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
317
318 }
319
320 // do we xml the entire body
321 if( $this->body_parameters && $this->body_xml_format == true ) {
322
323 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
324
325 foreach( $this->body_parameters as $key => $value) {
326 $xml->addChild( $key, $value );
327 }
328
329 $this->body_parameters = $xml->asXML();
330
331 }
332
333 // do we base64 encode the entire body
334 if( $this->body_parameters && $this->body_base64_encode == true ) {
335
336 $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
337 $this->body_parameters = base64_encode( $this->body_parameters );
338
339 }
340
341 // add our body parameters
342 if( $this->body_parameters && ! empty( $this->body_parameters ) )
343 $headers['body'] = $this->body_parameters;
344
345 }
346
347 // get our body paramters as they are formatted
348 $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
349
350 // add headers to our final request args
351 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
352
353 // build the args taht are sent to the request
354 $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array(
355 'timeout' => $this->timeout,
356 'sslverify' => $this->sslverify,
357 'redirection' => 5,
358 'blocking' => true,
359 'cookies' => array(),
360 ) );
361
362 return wp_parse_args( $this->final_request_args, $default_args );
363
364 }
365
366
367
368
369 /**
370 * Do a GET request
371 */
372 public function GET_request() {
373 $response = '';
374 // so we can so something before the remote_get (caching)
375 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
376
377 if( empty( $response ) )
378 $response = wp_remote_get( $this->final_url, $this->final_request_args );
379 return $response;
380 }
381
382 /**
383 * Do a POST request
384 */
385 public function POST_request() {
386 $this->final_request_args['method'] = 'POST';
387 $response = wp_remote_post( $this->final_url, $this->final_request_args );
388 return $response;
389 }
390
391 /**
392 * Do a PUT request
393 */
394 public function PUT_request() {
395 $this->final_request_args['method'] = 'PUT';
396 $response = wp_remote_request( $this->final_url, $this->final_request_args );
397 return $response;
398 }
399
400 /**
401 * Do a PATCH request
402 */
403 public function PATCH_request() {
404 $this->final_request_args['method'] = 'PATCH';
405 $response = wp_remote_request( $this->final_url, $this->final_request_args );
406 return $response;
407 }
408
409 /**
410 * Do a DELETE request
411 */
412 public function DELETE_request() {
413 $this->final_request_args['method'] = 'DELETE';
414 $response = wp_remote_request( $this->final_url, $this->final_request_args );
415 return $response;
416 }
417
418
419 /**
420 * return data
421 */
422 public function return_data( $data ) {
423
424 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
425
426 // converts all data to arrays, removing objects
427 if( is_string( $data ) ) {
428 $decode = json_decode( $data, true );
429 if( json_last_error() ) {
430 return esc_html( $data );
431 } else {
432 $data = $decode;
433 }
434 }
435
436 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
437
438 if( $this->results_format == 'json_string' )
439 return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
440
441 if( $this->results_format == 'json_decoded' )
442 return $data;
443
444 }
445
446 /**
447 * Sort out the parameters if they exist
448 */
449 public function do_parameters( $parameters ) {
450
451 $new_array = array();
452 $raw = '';
453 $parameters = $this->decrypt( $parameters );
454
455 if( is_array( $parameters ) ) {
456 foreach ( $parameters as $key => $parameter ) {
457
458 if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
459
460 $parameter['value'] = stripslashes( $parameter['value'] );
461
462 // if we have wpgetapi_on, delete it so we don't send to api
463 if( $parameter['name'] == 'wpgetapi_on' ) {
464 unset( $parameters[ $key ] );
465 continue;
466 }
467
468 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
469
470 // if we are sending raw body
471 } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
472
473 $raw = stripslashes( $parameter['value'] );
474
475 }
476
477 }
478 }
479
480 // look for array or json string and sort these out - for using such things in shortcode
481 if( ! empty( $new_array ) ) {
482
483 foreach ( $new_array as $key => $value ) {
484
485 // checking if the parameters have a json string
486 if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) {
487 //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
488 $new_array[ $key ] = json_decode( $value, true );
489 } else
490
491 // checking if the parameters have an array within the string
492 // if they do, create them as an actual array
493 if ( substr( $value, 0, 1 ) === '[' && substr( $value, -1 ) === ']' ) {
494 $value = trim( $value,"[]" );
495 $new_array[ $key ] = array( $value );
496 }
497
498 // set value as integer
499 if ( strpos( $value, 'integer(' ) !== false && substr( $value, -1 ) === ')' ) {
500 preg_match("/\(([^\)]*)\)/", $value, $match );
501 $result = $match[1];
502 $new_array[ $key ] = absint( $result );
503 }
504
505 // set value as float
506 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
507 preg_match("/\(([^\)]*)\)/", $value, $match );
508 $result = $match[1];
509 if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
510 $result = trim( $result,'""' );
511 $new_array[ $key ] = (float) $result;
512
513 }
514
515 // set value as boolean
516 if ( strpos( $value, 'boolean(' ) !== false && substr( $value, -1 ) === ')' ) {
517 preg_match("/\(([^\)]*)\)/", $value, $match );
518 $result = $match[1];
519 $result = filter_var( $result, FILTER_VALIDATE_BOOLEAN );
520 $new_array[ $key ] = $result;
521
522 }
523
524 }
525
526 }
527
528 return $raw ? $raw : $new_array;
529
530 }
531
532
533 /**
534 * decrypt our parameters from the DB
535 */
536 public function decrypt( $array_or_string ) {
537
538 if( is_string($array_or_string) ){
539 $array_or_string = $this->encryption->decrypt($array_or_string);
540 }elseif( is_array($array_or_string) ){
541 foreach ( $array_or_string as $key => &$value ) {
542 if( $key === 'name' )
543 continue;
544 if ( is_array( $value ) ) {
545 $value = $this->decrypt($value);
546 }
547 else {
548 $value = $this->encryption->decrypt($value);
549
550 }
551 }
552 }
553 return $array_or_string;
554 }
555
556
557 /**
558 * debug
559 */
560 public function maybe_add_debug_info( $data, $wpgetapi ) {
561
562 if(
563 ( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) ||
564 ( isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) )
565 ) {
566
567 $this->debug = true;
568
569 // admin test endpoint button
570 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
571
572 // get api status from response code
573 $status = array(
574 'title' => 'Error in API response.',
575 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
576 'type' => '<span class="dashicons dashicons-dismiss"></span>'
577 );
578
579 if( $this->response_code == 200 ) {
580 $status = array(
581 'title' => 'Success!',
582 'desc' => 'Your API is returning a success message and is returning the data as shown in the Data Output section below. You can modify this by changing the Results Format option or see <a href="https://wpgetapi.com/docs/format-api-data-as-html/">here</a> for info on formatting this data.',
583 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
584 );
585 }
586 if( $this->response_code == 400 || $this->response_code == 404 ) {
587 $status = array(
588 'title' => 'Bad Request',
589 'desc' => 'This could indicate that the URL you are calling is incorrect or that some other data is missing or incorrect. Please check your Base URL and Endpoint are correct as well as the Request Method.',
590 'type' => '<span class="dashicons dashicons-dismiss"></span>'
591 );
592 }
593 if( $this->response_code == 401 ) {
594 $status = array(
595 'title' => 'Unauthorised',
596 'desc' => 'You are successfully connecting to your API. But your API key, login or API credentials are incorrect. See <a href="https://wpgetapi.com/docs/authentication-authorization/">here</a> for info on correctly authorizing your API requests.',
597 'type' => '<span class="dashicons dashicons-warning"></span>'
598 );
599 }
600 if( $this->response_code == 403 ) {
601 $status = array(
602 'title' => 'Forbidden',
603 'desc' => 'This could indicate that you may need to get your IP address whitelisted with your API provider or that you do not have access to this API.',
604 'type' => '<span class="dashicons dashicons-dismiss"></span>'
605 );
606 }
607 if( $this->response_code == 405 ) {
608 $status = array(
609 'title' => 'Method Not Allowed',
610 'desc' => 'Try changing the Request Method within the endpoint settings.',
611 'type' => '<span class="dashicons dashicons-dismiss"></span>'
612 );
613 }
614 if( $this->response_code == 415 ) {
615 $status = array(
616 'title' => 'Unsupported Media Type',
617 'desc' => 'Try adding "Content-type" to the Headers with the Value of "json/application" as shown <a href="https://wpgetapi.com/docs/troubleshooting/">here</a>.',
618 'type' => '<span class="dashicons dashicons-dismiss"></span>'
619 );
620 }
621 if( $this->response_code == 500 ) {
622 $status = array(
623 'title' => 'Internal Server Error',
624 'desc' => 'This indicates an issue with the server. Contact your API provider.',
625 'type' => '<span class="dashicons dashicons-dismiss"></span>'
626 );
627 }
628
629
630 ob_start(); ?>
631
632 <style>
633
634 .wpgetapi-debug {
635 background: #fff;
636 padding: 20px;
637 box-shadow: 0 0 1px #aaa;
638 margin: 20px;
639 font-size: 15px;
640 }
641 .wpgetapi-debug
642 .wpgetapi-debug p
643 .wpgetapi-debug h5
644 .wpgetapi-debug h6 {
645 color: #000;
646 line-height: 1 !important;
647 text-transform: none !important;
648 font-family: sans-serif !important;
649 letter-spacing: 0px !important;
650 }
651 .wpgetapi-debug p.small {
652 font-size: 85%;
653 color: #999;
654 margin: 0 0 12px !important;
655 }
656 .wpgetapi-debug h5 {
657 font-weight: bold;
658 font-size: 18px;
659 margin: 0 0 3px;
660 text-transform: none !important;
661 }
662 .wpgetapi-debug .debug-item {
663 margin: 10px 0;
664 background: #fff;
665 padding: 15px 20px;
666 display: inline-block !important;
667 width: 100% !important;
668 box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
669 border: 1px solid #eee;
670 }
671 .wpgetapi-debug .debug-item h6 {
672 font-weight: bold;
673 font-size: 14px;
674 margin: 0;
675 text-transform: none !important;
676 }
677 .wpgetapi-debug .debug-item .debug-result,
678 .wpgetapi-debug .debug-item .debug-result pre {
679 font-family: monospace !important;
680 font-size: 13px !important;
681 margin: 0 !important;
682 padding: 8px 12px;
683 background: #f4f7f9;
684 }
685 .wpgetapi-debug .debug-item .debug-result pre {
686 border: none;
687 padding: 5px
688 }
689 .wpgetapi-debug.testing {
690 background: transparent;
691 padding: 0;
692 box-shadow: none;
693 }
694 .wpgetapi-debug.testing .debug-item {
695 margin: 5px 0;
696 }
697 .wpgetapi-debug.testing .debug-item.status {
698 border: none;
699 box-shadow: none;
700 margin-bottom: 15px;
701 }
702 .wpgetapi-debug .debug-item.status h5 {
703 font-size: 15px;
704 }
705 .wpgetapi-debug .status .dashicons {
706 margin: 0 5px 3px 0;
707 }
708 .wpgetapi-debug .status .dashicons-yes-alt {
709 color: #5e9929;
710 }
711 .wpgetapi-debug .status .dashicons-dismiss {
712 color: #cc4646;
713 }
714 .wpgetapi-debug .status .dashicons-warning {
715 color: #dfa12e;
716 }
717 .wpgetapi-debug .status .desc {
718 font-size: 14px;
719 font-weight: 400 !important;
720 }
721 </style>
722
723 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
724
725 <?php if( $testing ) { ?>
726
727 <div class="debug-item status">
728 <div class="">
729 <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
730 <span class="desc"><?php _e( $status['desc'] ); ?></span>
731 </div>
732 </div>
733 <div class="debug-item">
734 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
735 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
736 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
737 </div>
738
739 <div class="debug-item">
740 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
741 <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>
742 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
743 </div>
744
745 <div class="debug-item">
746 <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
747 <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
748 <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
749 </div>
750
751 <div class="debug-item">
752 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
753 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
754 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
755 </div>
756
757 <?php } else { ?>
758
759 <h5><strong><?php esc_html_e( 'WPGetAPI, Debug Mode - Plugin Version', 'wpgetapi' ); ?> <?php esc_html_e( WpGetApi()->version ); ?></strong></h5>
760 <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>
761
762 <div class="debug-item">
763 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
764 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
765 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
766 </div>
767
768 <div class="debug-item">
769 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
770 <p class="small"><?php esc_html_e( 'The resulting output that has been returned from the API.', 'wpgetapi' ); ?></p>
771 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
772 </div>
773
774 <div class="debug-item">
775 <h6><?php esc_html_e( 'Query String', 'wpgetapi' ); ?></h6>
776 <p class="small"><?php esc_html_e( 'The query string parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
777 <div class="debug-result"><?php wpgetapi_pp( $this->query_parameters ); ?></div>
778 </div>
779
780 <div class="debug-item">
781 <h6><?php esc_html_e( 'Headers', 'wpgetapi' ); ?></h6>
782 <p class="small"><?php esc_html_e( 'The header parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
783 <div class="debug-result"><?php wpgetapi_pp( $this->header_parameters ); ?></div>
784 </div>
785
786 <div class="debug-item">
787 <h6><?php esc_html_e( 'Body', 'wpgetapi' ); ?></h6>
788 <p class="small"><?php esc_html_e( 'The body parameters (if any) that you have set within the endpoint settings.', 'wpgetapi' ); ?></p>
789 <div class="debug-result"><?php wpgetapi_pp( $this->body_parameters ); ?></div>
790 </div>
791
792 <div class="debug-item">
793 <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
794 <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
795 <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
796 </div>
797
798 <div class="debug-item">
799 <h6><?php esc_html_e( 'Arguments', 'wpgetapi' ); ?></h6>
800 <p class="small"><?php esc_html_e( 'The arguments set within your shortcode or template tag.', 'wpgetapi' ); ?></p>
801 <div class="debug-result"><?php wpgetapi_pp( $this->args ); ?></div>
802 </div>
803
804 <div class="debug-item">
805 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>
806 <p class="small"><?php esc_html_e( 'The full response from the API request.', 'wpgetapi' ); ?></p>
807 <div class="debug-result"><?php wpgetapi_pp( $this->response); ?></div>
808 </div>
809
810 <?php } ?>
811
812 </div>
813
814 <?php
815
816 return ob_get_clean();
817
818 } else {
819
820 return $data;
821
822 }
823
824 }
825
826
827 }