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 +15 -131 1.9.81.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' ) {
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,21 +307,13 @@
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
335 - //}
315 + }
336 316
337 317 // add headers to our final request args
338 318 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
339 319
@@ -384,17 +364,8 @@
384 364 return $response;
385 365 }
386 366
387 367 /**
388 - * Do a PATCH request
389 - */
390 - public function PATCH_request() {
391 - $this->final_request_args['method'] = 'PATCH';
392 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
393 - return $response;
394 - }
395 -
396 - /**
397 368 * Do a DELETE request
398 369 */
399 370 public function DELETE_request() {
400 371 $this->final_request_args['method'] = 'DELETE';
@@ -508,9 +479,9 @@
508 479
509 480 }
510 481
511 482 }
512 -
483 +
513 484 return $raw ? $raw : $new_array;
514 485
515 486 }
516 487
@@ -550,69 +521,10 @@
550 521 ) {
551 522
552 523 $this->debug = true;
553 524
554 - // admin test endpoint button
555 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
556 526
557 - // get api status from response code
558 - $status = array(
559 - 'title' => 'Error in API response.',
560 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
561 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
562 - );
563 -
564 - if( $this->response_code == 200 ) {
565 - $status = array(
566 - 'title' => 'Success!',
567 - '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.',
568 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
569 - );
570 - }
571 - if( $this->response_code == 400 || $this->response_code == 404 ) {
572 - $status = array(
573 - 'title' => 'Bad Request',
574 - '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.',
575 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
576 - );
577 - }
578 - if( $this->response_code == 401 ) {
579 - $status = array(
580 - 'title' => 'Unauthorised',
581 - '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.',
582 - 'type' => '<span class="dashicons dashicons-warning"></span>'
583 - );
584 - }
585 - if( $this->response_code == 403 ) {
586 - $status = array(
587 - 'title' => 'Forbidden',
588 - '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.',
589 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
590 - );
591 - }
592 - if( $this->response_code == 405 ) {
593 - $status = array(
594 - 'title' => 'Method Not Allowed',
595 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
596 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
597 - );
598 - }
599 - if( $this->response_code == 415 ) {
600 - $status = array(
601 - 'title' => 'Unsupported Media Type',
602 - '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>.',
603 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
604 - );
605 - }
606 - if( $this->response_code == 500 ) {
607 - $status = array(
608 - 'title' => 'Internal Server Error',
609 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
610 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
611 - );
612 - }
613 -
614 -
615 527 ob_start(); ?>
616 528
617 529 <style>
618 530
@@ -649,10 +561,10 @@
649 561 background: #fff;
650 562 padding: 15px 20px;
651 563 display: inline-block !important;
652 564 width: 100% !important;
653 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
654 - border: 1px solid #eee;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
655 567 }
656 568 .wpgetapi-debug .debug-item h6 {
657 569 font-weight: bold;
658 570 font-size: 14px;
@@ -664,14 +576,15 @@
664 576 font-family: monospace !important;
665 577 font-size: 13px !important;
666 578 margin: 0 !important;
667 579 padding: 8px 12px;
668 - background: #f4f7f9;
580 + background: #f8f8f8;
669 581 }
670 582 .wpgetapi-debug .debug-item .debug-result pre {
671 583 border: none;
672 584 padding: 5px
673 585 }
586 +
674 587 .wpgetapi-debug.testing {
675 588 background: transparent;
676 589 padding: 0;
677 590 box-shadow: none;
@@ -678,32 +591,8 @@
678 591 }
679 592 .wpgetapi-debug.testing .debug-item {
680 593 margin: 5px 0;
681 594 }
682 -.wpgetapi-debug.testing .debug-item.status {
683 - border: none;
684 - box-shadow: none;
685 - margin-bottom: 15px;
686 -}
687 -.wpgetapi-debug .debug-item.status h5 {
688 - font-size: 15px;
689 -}
690 -.wpgetapi-debug .status .dashicons {
691 - margin: 0 5px 3px 0;
692 -}
693 -.wpgetapi-debug .status .dashicons-yes-alt {
694 - color: #5e9929;
695 -}
696 -.wpgetapi-debug .status .dashicons-dismiss {
697 - color: #cc4646;
698 -}
699 -.wpgetapi-debug .status .dashicons-warning {
700 - color: #dfa12e;
701 -}
702 -.wpgetapi-debug .status .desc {
703 - font-size: 14px;
704 - font-weight: 400 !important;
705 -}
706 595 </style>
707 596
708 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
709 598
@@ -708,15 +597,9 @@
708 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
709 598
710 599 <?php if( $testing ) { ?>
711 600
712 - <div class="debug-item status">
713 - <div class="">
714 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
715 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
716 - </div>
717 - </div>
718 - <div class="debug-item">
601 + <div class="debug-item">
719 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
720 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
721 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
722 605 </div>
@@ -723,8 +606,9 @@
723 606
724 607 <div class="debug-item">
725 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
726 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>
727 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
728 612 </div>
729 613
730 614 <div class="debug-item">