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