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