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 +18 -142 2.0.21.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';
@@ -145,13 +139,8 @@
145 139 ) {
146 140 $this->results_format = 'json_decoded';
147 141 }
148 142
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 143 }
155 144
156 145 }
157 146
@@ -204,12 +193,8 @@
204 193 // PUT request
205 194 if( $this->method == 'PUT' )
206 195 $this->response = $this->PUT_request();
207 196
208 - // PATCH request
209 - if( $this->method == 'PATCH' )
210 - $this->response = $this->PATCH_request();
211 -
212 197 // GET request
213 198 if( $this->method == 'GET' )
214 199 $this->response = $this->GET_request();
215 200
@@ -222,12 +207,12 @@
222 207 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
223 208 }
224 209
225 210 // get response_code
226 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
211 + $response_code = wp_remote_retrieve_response_code( $this->response );
227 212
228 213 // action to intercept call depending on response_code
229 - $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 );
230 215
231 216 // get body
232 217 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
233 218 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
@@ -267,12 +252,10 @@
267 252
268 253 // add empty array - was getting errors if it was just null or false
269 254 $params = empty( $params ) ? array(''=>'') : $params;
270 255
271 - $result = add_query_arg( $params, $url );
256 + return add_query_arg( $params, $url );
272 257
273 - return $result;
274 -
275 258 }
276 259
277 260
278 261 /**
@@ -286,9 +269,9 @@
286 269 ), $this );
287 270
288 271
289 272 // if doing POST request, include body paramters
290 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
273 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
291 274
292 275 // tokens are sent through here
293 276 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
294 277
@@ -297,9 +280,9 @@
297 280 $this->body_parameters = json_encode( $this->body_parameters );
298 281 }
299 282
300 283 // do we urlencode the entire body
301 - if( $this->body_parameters && $this->body_url_encode == true ) {
284 + if( $this->body_url_encode == true ) {
302 285
303 286 // checking if the parameters have a json string (raw)
304 287 if (
305 288 ( ! is_array( $this->body_parameters ) ) &&
@@ -312,9 +295,9 @@
312 295
313 296 }
314 297
315 298 // do we xml the entire body
316 - if( $this->body_parameters && $this->body_xml_format == true ) {
299 + if( $this->body_xml_format == true ) {
317 300
318 301 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
319 302
320 303 foreach( $this->body_parameters as $key => $value) {
@@ -324,16 +307,8 @@
324 307 $this->body_parameters = $xml->asXML();
325 308
326 309 }
327 310
328 - // do we base64 encode the entire body
329 - if( $this->body_parameters && $this->body_base64_encode == true ) {
330 -
331 - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
332 - $this->body_parameters = base64_encode( $this->body_parameters );
333 -
334 - }
335 -
336 311 // add our body parameters
337 312 if( $this->body_parameters && ! empty( $this->body_parameters ) )
338 313 $headers['body'] = $this->body_parameters;
339 314
@@ -338,11 +313,8 @@
338 313 $headers['body'] = $this->body_parameters;
339 314
340 315 }
341 316
342 - // get our body paramters as they are formatted
343 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
344 -
345 317 // add headers to our final request args
346 318 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
347 319
348 320 // build the args taht are sent to the request
@@ -392,17 +364,8 @@
392 364 return $response;
393 365 }
394 366
395 367 /**
396 - * Do a PATCH request
397 - */
398 - public function PATCH_request() {
399 - $this->final_request_args['method'] = 'PATCH';
400 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
401 - return $response;
402 - }
403 -
404 - /**
405 368 * Do a DELETE request
406 369 */
407 370 public function DELETE_request() {
408 371 $this->final_request_args['method'] = 'DELETE';
@@ -416,9 +379,9 @@
416 379 */
417 380 public function return_data( $data ) {
418 381
419 382 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
420 -
383 +
421 384 // converts all data to arrays, removing objects
422 385 if( is_string( $data ) ) {
423 386 $decode = json_decode( $data, true );
424 387 if( json_last_error() ) {
@@ -426,13 +389,13 @@
426 389 } else {
427 390 $data = $decode;
428 391 }
429 392 }
430 -
393 +
431 394 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
432 395
433 396 if( $this->results_format == 'json_string' )
434 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
397 + return trim( json_encode( $data ),'"');
435 398
436 399 if( $this->results_format == 'json_decoded' )
437 400 return $data;
438 401
@@ -441,9 +404,9 @@
441 404 /**
442 405 * Sort out the parameters if they exist
443 406 */
444 407 public function do_parameters( $parameters ) {
445 -
408 +
446 409 $new_array = array();
447 410 $raw = '';
448 411 $parameters = $this->decrypt( $parameters );
449 412
@@ -516,9 +479,9 @@
516 479
517 480 }
518 481
519 482 }
520 -
483 +
521 484 return $raw ? $raw : $new_array;
522 485
523 486 }
524 487
@@ -558,69 +521,10 @@
558 521 ) {
559 522
560 523 $this->debug = true;
561 524
562 - // admin test endpoint button
563 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
564 526
565 - // get api status from response code
566 - $status = array(
567 - 'title' => 'Error in API response.',
568 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
569 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
570 - );
571 -
572 - if( $this->response_code == 200 ) {
573 - $status = array(
574 - 'title' => 'Success!',
575 - '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.',
576 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
577 - );
578 - }
579 - if( $this->response_code == 400 || $this->response_code == 404 ) {
580 - $status = array(
581 - 'title' => 'Bad Request',
582 - '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.',
583 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
584 - );
585 - }
586 - if( $this->response_code == 401 ) {
587 - $status = array(
588 - 'title' => 'Unauthorised',
589 - '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.',
590 - 'type' => '<span class="dashicons dashicons-warning"></span>'
591 - );
592 - }
593 - if( $this->response_code == 403 ) {
594 - $status = array(
595 - 'title' => 'Forbidden',
596 - '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.',
597 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
598 - );
599 - }
600 - if( $this->response_code == 405 ) {
601 - $status = array(
602 - 'title' => 'Method Not Allowed',
603 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
604 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
605 - );
606 - }
607 - if( $this->response_code == 415 ) {
608 - $status = array(
609 - 'title' => 'Unsupported Media Type',
610 - '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>.',
611 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
612 - );
613 - }
614 - if( $this->response_code == 500 ) {
615 - $status = array(
616 - 'title' => 'Internal Server Error',
617 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
618 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
619 - );
620 - }
621 -
622 -
623 527 ob_start(); ?>
624 528
625 529 <style>
626 530
@@ -657,10 +561,10 @@
657 561 background: #fff;
658 562 padding: 15px 20px;
659 563 display: inline-block !important;
660 564 width: 100% !important;
661 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
662 - border: 1px solid #eee;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
663 567 }
664 568 .wpgetapi-debug .debug-item h6 {
665 569 font-weight: bold;
666 570 font-size: 14px;
@@ -672,14 +576,15 @@
672 576 font-family: monospace !important;
673 577 font-size: 13px !important;
674 578 margin: 0 !important;
675 579 padding: 8px 12px;
676 - background: #f4f7f9;
580 + background: #f8f8f8;
677 581 }
678 582 .wpgetapi-debug .debug-item .debug-result pre {
679 583 border: none;
680 584 padding: 5px
681 585 }
586 +
682 587 .wpgetapi-debug.testing {
683 588 background: transparent;
684 589 padding: 0;
685 590 box-shadow: none;
@@ -686,32 +591,8 @@
686 591 }
687 592 .wpgetapi-debug.testing .debug-item {
688 593 margin: 5px 0;
689 594 }
690 -.wpgetapi-debug.testing .debug-item.status {
691 - border: none;
692 - box-shadow: none;
693 - margin-bottom: 15px;
694 -}
695 -.wpgetapi-debug .debug-item.status h5 {
696 - font-size: 15px;
697 -}
698 -.wpgetapi-debug .status .dashicons {
699 - margin: 0 5px 3px 0;
700 -}
701 -.wpgetapi-debug .status .dashicons-yes-alt {
702 - color: #5e9929;
703 -}
704 -.wpgetapi-debug .status .dashicons-dismiss {
705 - color: #cc4646;
706 -}
707 -.wpgetapi-debug .status .dashicons-warning {
708 - color: #dfa12e;
709 -}
710 -.wpgetapi-debug .status .desc {
711 - font-size: 14px;
712 - font-weight: 400 !important;
713 -}
714 595 </style>
715 596
716 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
717 598
@@ -716,15 +597,9 @@
716 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
717 598
718 599 <?php if( $testing ) { ?>
719 600
720 - <div class="debug-item status">
721 - <div class="">
722 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
723 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
724 - </div>
725 - </div>
726 - <div class="debug-item">
601 + <div class="debug-item">
727 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
728 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
729 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
730 605 </div>
@@ -731,8 +606,9 @@
731 606
732 607 <div class="debug-item">
733 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
734 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>
735 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
736 612 </div>
737 613
738 614 <div class="debug-item">