PluginProbe
WPGet API – Connect to any external REST API / 1.9.0
WPGet API – Connect to any external REST API v1.9.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 +17 -136 2.0.01.9.0 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,10 +38,8 @@
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 43 public $sslverify = 1; // the paramaters
47 44 public $debug = false;
48 45
@@ -125,14 +122,11 @@
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
@@ -138,9 +132,9 @@
138 132
139 133
140 134 if(
141 135 // 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' ) ||
136 + ( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) ||
143 137 // set the format to php array data if it is set in the args (coming from the importer)
144 138 ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
145 139 ) {
146 140 $this->results_format = 'json_decoded';
@@ -199,12 +193,8 @@
199 193 // PUT request
200 194 if( $this->method == 'PUT' )
201 195 $this->response = $this->PUT_request();
202 196
203 - // PATCH request
204 - if( $this->method == 'PATCH' )
205 - $this->response = $this->PATCH_request();
206 -
207 197 // GET request
208 198 if( $this->method == 'GET' )
209 199 $this->response = $this->GET_request();
210 200
@@ -217,12 +207,12 @@
217 207 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
218 208 }
219 209
220 210 // get response_code
221 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
211 + $response_code = wp_remote_retrieve_response_code( $this->response );
222 212
223 213 // action to intercept call depending on response_code
224 - $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $this->response_code, $this );
214 + $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this );
225 215
226 216 // get body
227 217 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
228 218 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
@@ -262,12 +252,10 @@
262 252
263 253 // add empty array - was getting errors if it was just null or false
264 254 $params = empty( $params ) ? array(''=>'') : $params;
265 255
266 - $result = add_query_arg( $params, $url );
256 + return add_query_arg( $params, $url );
267 257
268 - return $result;
269 -
270 258 }
271 259
272 260
273 261 /**
@@ -281,9 +269,9 @@
281 269 ), $this );
282 270
283 271
284 272 // if doing POST request, include body paramters
285 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
273 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
286 274
287 275 // tokens are sent through here
288 276 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
289 277
@@ -292,9 +280,9 @@
292 280 $this->body_parameters = json_encode( $this->body_parameters );
293 281 }
294 282
295 283 // do we urlencode the entire body
296 - if( $this->body_parameters && $this->body_url_encode == true ) {
284 + if( $this->body_url_encode == true ) {
297 285
298 286 // checking if the parameters have a json string (raw)
299 287 if (
300 288 ( ! is_array( $this->body_parameters ) ) &&
@@ -307,9 +295,9 @@
307 295
308 296 }
309 297
310 298 // do we xml the entire body
311 - if( $this->body_parameters && $this->body_xml_format == true ) {
299 + if( $this->body_xml_format == true ) {
312 300
313 301 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
314 302
315 303 foreach( $this->body_parameters as $key => $value) {
@@ -319,16 +307,8 @@
319 307 $this->body_parameters = $xml->asXML();
320 308
321 309 }
322 310
323 - // do we base64 encode the entire body
324 - if( $this->body_parameters && $this->body_base64_encode == true ) {
325 -
326 - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
327 - $this->body_parameters = base64_encode( $this->body_parameters );
328 -
329 - }
330 -
331 311 // add our body parameters
332 312 if( $this->body_parameters && ! empty( $this->body_parameters ) )
333 313 $headers['body'] = $this->body_parameters;
334 314
@@ -333,11 +313,8 @@
333 313 $headers['body'] = $this->body_parameters;
334 314
335 315 }
336 316
337 - // get our body paramters as they are formatted
338 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
339 -
340 317 // add headers to our final request args
341 318 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
342 319
343 320 // build the args taht are sent to the request
@@ -387,17 +364,8 @@
387 364 return $response;
388 365 }
389 366
390 367 /**
391 - * Do a PATCH request
392 - */
393 - public function PATCH_request() {
394 - $this->final_request_args['method'] = 'PATCH';
395 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
396 - return $response;
397 - }
398 -
399 - /**
400 368 * Do a DELETE request
401 369 */
402 370 public function DELETE_request() {
403 371 $this->final_request_args['method'] = 'DELETE';
@@ -411,9 +379,9 @@
411 379 */
412 380 public function return_data( $data ) {
413 381
414 382 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
415 -
383 +
416 384 // converts all data to arrays, removing objects
417 385 if( is_string( $data ) ) {
418 386 $decode = json_decode( $data, true );
419 387 if( json_last_error() ) {
@@ -421,9 +389,9 @@
421 389 } else {
422 390 $data = $decode;
423 391 }
424 392 }
425 -
393 +
426 394 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
427 395
428 396 if( $this->results_format == 'json_string' )
429 397 return trim( json_encode( $data ),'"');
@@ -436,9 +404,9 @@
436 404 /**
437 405 * Sort out the parameters if they exist
438 406 */
439 407 public function do_parameters( $parameters ) {
440 -
408 +
441 409 $new_array = array();
442 410 $raw = '';
443 411 $parameters = $this->decrypt( $parameters );
444 412
@@ -511,9 +479,9 @@
511 479
512 480 }
513 481
514 482 }
515 -
483 +
516 484 return $raw ? $raw : $new_array;
517 485
518 486 }
519 487
@@ -553,69 +521,10 @@
553 521 ) {
554 522
555 523 $this->debug = true;
556 524
557 - // admin test endpoint button
558 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
559 526
560 - // get api status from response code
561 - $status = array(
562 - 'title' => 'Error in API response.',
563 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
564 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
565 - );
566 -
567 - if( $this->response_code == 200 ) {
568 - $status = array(
569 - 'title' => 'Success!',
570 - 'desc' => 'Your API is returning a success message and is returning the data as shown in the Data Output section below. You can modify this by changing the Results Format option or see <a href="https://wpgetapi.com/docs/format-api-data-as-html/">here</a> for info on formatting this data.',
571 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
572 - );
573 - }
574 - if( $this->response_code == 400 || $this->response_code == 404 ) {
575 - $status = array(
576 - 'title' => 'Bad Request',
577 - 'desc' => 'This could indicate that the URL you are calling is incorrect or that some other data is missing or incorrect. Please check your Base URL and Endpoint are correct as well as the Request Method.',
578 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
579 - );
580 - }
581 - if( $this->response_code == 401 ) {
582 - $status = array(
583 - 'title' => 'Unauthorised',
584 - 'desc' => 'You are successfully connecting to your API. But your API key, login or API credentials are incorrect. See <a href="https://wpgetapi.com/docs/authentication-authorization/">here</a> for info on correctly authorizing your API requests.',
585 - 'type' => '<span class="dashicons dashicons-warning"></span>'
586 - );
587 - }
588 - if( $this->response_code == 403 ) {
589 - $status = array(
590 - 'title' => 'Forbidden',
591 - 'desc' => 'This could indicate that you may need to get your IP address whitelisted with your API provider or that you do not have access to this API.',
592 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
593 - );
594 - }
595 - if( $this->response_code == 405 ) {
596 - $status = array(
597 - 'title' => 'Method Not Allowed',
598 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
599 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
600 - );
601 - }
602 - if( $this->response_code == 415 ) {
603 - $status = array(
604 - 'title' => 'Unsupported Media Type',
605 - 'desc' => 'Try adding "Content-type" to the Headers with the Value of "json/application" as shown <a href="https://wpgetapi.com/docs/troubleshooting/">here</a>.',
606 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
607 - );
608 - }
609 - if( $this->response_code == 500 ) {
610 - $status = array(
611 - 'title' => 'Internal Server Error',
612 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
613 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
614 - );
615 - }
616 -
617 -
618 527 ob_start(); ?>
619 528
620 529 <style>
621 530
@@ -652,10 +561,10 @@
652 561 background: #fff;
653 562 padding: 15px 20px;
654 563 display: inline-block !important;
655 564 width: 100% !important;
656 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
657 - border: 1px solid #eee;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
658 567 }
659 568 .wpgetapi-debug .debug-item h6 {
660 569 font-weight: bold;
661 570 font-size: 14px;
@@ -667,14 +576,15 @@
667 576 font-family: monospace !important;
668 577 font-size: 13px !important;
669 578 margin: 0 !important;
670 579 padding: 8px 12px;
671 - background: #f4f7f9;
580 + background: #f8f8f8;
672 581 }
673 582 .wpgetapi-debug .debug-item .debug-result pre {
674 583 border: none;
675 584 padding: 5px
676 585 }
586 +
677 587 .wpgetapi-debug.testing {
678 588 background: transparent;
679 589 padding: 0;
680 590 box-shadow: none;
@@ -681,32 +591,8 @@
681 591 }
682 592 .wpgetapi-debug.testing .debug-item {
683 593 margin: 5px 0;
684 594 }
685 -.wpgetapi-debug.testing .debug-item.status {
686 - border: none;
687 - box-shadow: none;
688 - margin-bottom: 15px;
689 -}
690 -.wpgetapi-debug .debug-item.status h5 {
691 - font-size: 15px;
692 -}
693 -.wpgetapi-debug .status .dashicons {
694 - margin: 0 5px 3px 0;
695 -}
696 -.wpgetapi-debug .status .dashicons-yes-alt {
697 - color: #5e9929;
698 -}
699 -.wpgetapi-debug .status .dashicons-dismiss {
700 - color: #cc4646;
701 -}
702 -.wpgetapi-debug .status .dashicons-warning {
703 - color: #dfa12e;
704 -}
705 -.wpgetapi-debug .status .desc {
706 - font-size: 14px;
707 - font-weight: 400 !important;
708 -}
709 595 </style>
710 596
711 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
712 598
@@ -711,15 +597,9 @@
711 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
712 598
713 599 <?php if( $testing ) { ?>
714 600
715 - <div class="debug-item status">
716 - <div class="">
717 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
718 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
719 - </div>
720 - </div>
721 - <div class="debug-item">
601 + <div class="debug-item">
722 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
723 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
724 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
725 605 </div>
@@ -726,8 +606,9 @@
726 606
727 607 <div class="debug-item">
728 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
729 609 <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>
610 + <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>
730 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
731 612 </div>
732 613
733 614 <div class="debug-item">