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