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