PluginProbe
WPGet API – Connect to any external REST API / 1.8.9
WPGet API – Connect to any external REST API v1.8.9
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 +32 -214 2.1.31.8.9 View file →
@@ -28,9 +28,8 @@
28 28 public $header_parameters = ''; // the header paramaters
29 29 public $body_parameters = ''; // the body paramaters
30 30 public $body_json_encode = ''; // body_json_encode body
31 31 public $body_url_encode = ''; // urlencode body
32 - public $body_base64_encode = ''; // base64 encode body
33 32 public $body_xml_format = ''; // xml format body
34 33 public $xml_root_node = 'root'; // root node for xml
35 34
36 35 public $response = ''; // the returned response
@@ -39,12 +38,10 @@
39 38 public $final_request_args = array(); // final request_args
40 39
41 40 public $results_format = 'json_string'; // results format
42 41
43 - public $response_code = ''; // the response code from the call
44 -
45 42 public $timeout = 10; // the timeout
46 - public $sslverify = 1; // ssl verify
43 + public $sslverify = 1; // the paramaters
47 44 public $debug = false;
48 45
49 46
50 47 /**
@@ -75,9 +72,9 @@
75 72 public function init() {
76 73
77 74 // if we are doing importer plugin,
78 75 // we need to intercept and modify most of our methods values
79 - if( $this->api_id === 'wpgetapi_importer' ) {
76 + if( $this->api_id == 'wpgetapi_importer' ) {
80 77
81 78 $data = apply_filters( 'wpgetapi_api_importer_init', $this );
82 79
83 80 if( $data ) {
@@ -125,38 +122,20 @@
125 122 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
126 123
127 124 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
128 125
129 - // how to encode the body
130 126 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
131 127 $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 128 $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false;
134 -
135 129 $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node;
136 130
137 131 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
138 132
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 - ) {
133 + // set the format to php array data if html format is set
134 + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) {
146 135 $this->results_format = 'json_decoded';
147 136 }
148 137
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 138 }
160 139
161 140 }
162 141
@@ -191,9 +170,9 @@
191 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
192 171
193 172 // build the request args
194 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
195 -
174 +
196 175 /**
197 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
198 177 * @param bool $on Whether on is set or not.
199 178 */
@@ -209,12 +188,8 @@
209 188 // PUT request
210 189 if( $this->method == 'PUT' )
211 190 $this->response = $this->PUT_request();
212 191
213 - // PATCH request
214 - if( $this->method == 'PATCH' )
215 - $this->response = $this->PATCH_request();
216 -
217 192 // GET request
218 193 if( $this->method == 'GET' )
219 194 $this->response = $this->GET_request();
220 195
@@ -227,17 +202,17 @@
227 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
228 203 }
229 204
230 205 // get response_code
231 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
232 207
233 208 // action to intercept call depending on response_code
234 - $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $this->response_code, $this );
209 + $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
235 210
236 211 // get body
237 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
238 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
239 -
214 +
240 215 // returning in JSON format
241 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
242 217 $data = $this->return_data( $data );
243 218
@@ -257,14 +232,8 @@
257 232 // first make sure our endpoint starts with a slash
258 233 if ( substr( $this->endpoint, 0, 1 ) != '/' )
259 234 $this->endpoint = '/' . $this->endpoint;
260 235
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 - }
266 -
267 236 $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this );
268 237
269 238 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
270 239
@@ -278,12 +247,10 @@
278 247
279 248 // add empty array - was getting errors if it was just null or false
280 249 $params = empty( $params ) ? array(''=>'') : $params;
281 250
282 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
283 252
284 - return $result;
285 -
286 253 }
287 254
288 255
289 256 /**
@@ -297,11 +264,10 @@
297 264 ), $this );
298 265
299 266
300 267 // if doing POST request, include body paramters
301 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
302 269
303 - // tokens are sent through here
304 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
305 271
306 272 // do we json_encode the entire body
307 273 if( $this->body_json_encode == true ) {
@@ -308,30 +274,23 @@
308 274 $this->body_parameters = json_encode( $this->body_parameters );
309 275 }
310 276
311 277 // do we urlencode the entire body
312 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
313 279
314 280 // 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 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
319 282 $this->body_parameters = json_decode( $this->body_parameters, true );
320 283 }
321 -
322 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
323 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
324 287 }
325 288
326 289 // 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 . '/>' : '';
290 + if( $this->body_xml_format == true ) {
330 291
331 - $xml_element = apply_filters( 'wpgetapi_xml_element', '<?xml version="1.0" encoding="UTF-8"?>' . $root_node );
332 -
333 - $xml = new SimpleXmlElement( $xml_element );
292 + $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
334 293
335 294 foreach( $this->body_parameters as $key => $value) {
336 295 $xml->addChild( $key, $value );
337 296 }
@@ -339,18 +298,8 @@
339 298 $this->body_parameters = $xml->asXML();
340 299
341 300 }
342 301
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 302 // add our body parameters
354 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
355 304 $headers['body'] = $this->body_parameters;
356 305
@@ -355,12 +304,8 @@
355 304 $headers['body'] = $this->body_parameters;
356 305
357 306 }
358 307
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 308 // add headers to our final request args
364 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
365 310
366 311 // build the args taht are sent to the request
@@ -376,15 +321,12 @@
376 321
377 322 }
378 323
379 324
380 -
381 -
382 325 /**
383 326 * Do a GET request
384 327 */
385 328 public function GET_request() {
386 -
387 329 $response = '';
388 330 // so we can so something before the remote_get (caching)
389 331 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
390 332
@@ -389,11 +331,9 @@
389 331 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
390 332
391 333 if( empty( $response ) )
392 334 $response = wp_remote_get( $this->final_url, $this->final_request_args );
393 -
394 335 return $response;
395 -
396 336 }
397 337
398 338 /**
399 339 * Do a POST request
@@ -398,18 +338,10 @@
398 338 /**
399 339 * Do a POST request
400 340 */
401 341 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 -
342 + $this->final_request_args['method'] = 'POST';
343 + $response = wp_remote_post( $this->final_url, $this->final_request_args );
412 344 return $response;
413 345 }
414 346
415 347 /**
@@ -421,17 +353,8 @@
421 353 return $response;
422 354 }
423 355
424 356 /**
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 357 * Do a DELETE request
435 358 */
436 359 public function DELETE_request() {
437 360 $this->final_request_args['method'] = 'DELETE';
@@ -445,9 +368,9 @@
445 368 */
446 369 public function return_data( $data ) {
447 370
448 371 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
449 -
372 +
450 373 // converts all data to arrays, removing objects
451 374 if( is_string( $data ) ) {
452 375 $decode = json_decode( $data, true );
453 376 if( json_last_error() ) {
@@ -455,13 +378,13 @@
455 378 } else {
456 379 $data = $decode;
457 380 }
458 381 }
459 -
382 +
460 383 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
461 384
462 385 if( $this->results_format == 'json_string' )
463 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
386 + return trim( json_encode( $data ),'"');
464 387
465 388 if( $this->results_format == 'json_decoded' )
466 389 return $data;
467 390
@@ -470,9 +393,9 @@
470 393 /**
471 394 * Sort out the parameters if they exist
472 395 */
473 396 public function do_parameters( $parameters ) {
474 -
397 +
475 398 $new_array = array();
476 399 $raw = '';
477 400 $parameters = $this->decrypt( $parameters );
478 401
@@ -488,14 +411,8 @@
488 411 unset( $parameters[ $key ] );
489 412 continue;
490 413 }
491 414
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 415 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
499 416
500 417 // if we are sending raw body
501 418 } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
@@ -506,20 +423,17 @@
506 423
507 424 }
508 425 }
509 426
510 - // now look for array or json string and sort these out - for using such things in shortcode
427 + // look for array or json string and sort these out - for using such things in shortcode
511 428 if( ! empty( $new_array ) ) {
512 429
513 430 foreach ( $new_array as $key => $value ) {
514 431
515 432 // 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 -
433 + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) {
434 + //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
435 + $new_array[ $key ] = json_decode( $value, true );
522 436 } else
523 437
524 438 // checking if the parameters have an array within the string
525 439 // if they do, create them as an actual array
@@ -538,10 +452,8 @@
538 452 // set value as float
539 453 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
540 454 preg_match("/\(([^\)]*)\)/", $value, $match );
541 455 $result = $match[1];
542 - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
543 - $result = trim( $result,'""' );
544 456 $new_array[ $key ] = (float) $result;
545 457
546 458 }
547 459
@@ -598,69 +510,10 @@
598 510 ) {
599 511
600 512 $this->debug = true;
601 513
602 - // admin test endpoint button
603 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
604 515
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 516 ob_start(); ?>
664 517
665 518 <style>
666 519
@@ -697,11 +550,10 @@
697 550 background: #fff;
698 551 padding: 15px 20px;
699 552 display: inline-block !important;
700 553 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;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
704 556 }
705 557 .wpgetapi-debug .debug-item h6 {
706 558 font-weight: bold;
707 559 font-size: 14px;
@@ -713,14 +565,15 @@
713 565 font-family: monospace !important;
714 566 font-size: 13px !important;
715 567 margin: 0 !important;
716 568 padding: 8px 12px;
717 - background: #f4f7f9;
569 + background: #f8f8f8;
718 570 }
719 571 .wpgetapi-debug .debug-item .debug-result pre {
720 572 border: none;
721 573 padding: 5px
722 574 }
575 +
723 576 .wpgetapi-debug.testing {
724 577 background: transparent;
725 578 padding: 0;
726 579 box-shadow: none;
@@ -727,32 +580,8 @@
727 580 }
728 581 .wpgetapi-debug.testing .debug-item {
729 582 margin: 5px 0;
730 583 }
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 584 </style>
756 585
757 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
758 587
@@ -757,15 +586,9 @@
757 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
758 587
759 588 <?php if( $testing ) { ?>
760 589
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">
590 + <div class="debug-item">
768 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
769 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
770 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
771 594 </div>
@@ -772,15 +595,10 @@
772 595
773 596 <div class="debug-item">
774 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
775 598 <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>
599 + <p class="small"><?php esc_html_e( 'See the ', 'wpgetapi' ); ?> <a target="_blank" href="https://wpgetapi.com/docs/?utm_campaign=Test Endpoint&utm_medium=Admin&utm_source=User"><?php esc_html_e( 'documentation', 'wpgetapi' ); ?></a> <?php esc_html_e( ' for help in formatting.', 'wpgetapi' ); ?></p>
776 600 <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 601 </div>
784 602
785 603 <div class="debug-item">
786 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>