PluginProbe
WPGet API – Connect to any external REST API / 2.2.8
WPGet API – Connect to any external REST API v2.2.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-wpgetapi-api.php

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

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