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

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