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 -121 1.9.71.9.0 View file →
@@ -38,10 +38,8 @@
38 38 public $final_request_args = array(); // final request_args
39 39
40 40 public $results_format = 'json_string'; // results format
41 41
42 - public $response_code = ''; // the response code from the call
43 -
44 42 public $timeout = 10; // the timeout
45 43 public $sslverify = 1; // the paramaters
46 44 public $debug = false;
47 45
@@ -124,13 +122,11 @@
124 122 $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array();
125 123
126 124 $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array();
127 125
128 - // how to encode the body
129 126 $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false;
130 127 $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false;
131 128 $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false;
132 -
133 129 $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node;
134 130
135 131 $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format;
136 132
@@ -136,9 +132,9 @@
136 132
137 133
138 134 if(
139 135 // set the format to php array data if html format is set
140 - ( isset( $this->args['format'] ) && $this->args['format'] == 'html' && $this->results_format != 'xml_array' ) ||
136 + ( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) ||
141 137 // set the format to php array data if it is set in the args (coming from the importer)
142 138 ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
143 139 ) {
144 140 $this->results_format = 'json_decoded';
@@ -197,12 +193,8 @@
197 193 // PUT request
198 194 if( $this->method == 'PUT' )
199 195 $this->response = $this->PUT_request();
200 196
201 - // PATCH request
202 - if( $this->method == 'PATCH' )
203 - $this->response = $this->PATCH_request();
204 -
205 197 // GET request
206 198 if( $this->method == 'GET' )
207 199 $this->response = $this->GET_request();
208 200
@@ -215,12 +207,12 @@
215 207 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
216 208 }
217 209
218 210 // get response_code
219 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
211 + $response_code = wp_remote_retrieve_response_code( $this->response );
220 212
221 213 // action to intercept call depending on response_code
222 - $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 );
223 215
224 216 // get body
225 217 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
226 218 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
@@ -260,12 +252,10 @@
260 252
261 253 // add empty array - was getting errors if it was just null or false
262 254 $params = empty( $params ) ? array(''=>'') : $params;
263 255
264 - $result = add_query_arg( $params, $url );
256 + return add_query_arg( $params, $url );
265 257
266 - return $result;
267 -
268 258 }
269 259
270 260
271 261 /**
@@ -279,9 +269,9 @@
279 269 ), $this );
280 270
281 271
282 272 // if doing POST request, include body paramters
283 - //if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
273 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
284 274
285 275 // tokens are sent through here
286 276 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
287 277
@@ -290,9 +280,9 @@
290 280 $this->body_parameters = json_encode( $this->body_parameters );
291 281 }
292 282
293 283 // do we urlencode the entire body
294 - if( $this->body_parameters && $this->body_url_encode == true ) {
284 + if( $this->body_url_encode == true ) {
295 285
296 286 // checking if the parameters have a json string (raw)
297 287 if (
298 288 ( ! is_array( $this->body_parameters ) ) &&
@@ -305,9 +295,9 @@
305 295
306 296 }
307 297
308 298 // do we xml the entire body
309 - if( $this->body_parameters && $this->body_xml_format == true ) {
299 + if( $this->body_xml_format == true ) {
310 300
311 301 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
312 302
313 303 foreach( $this->body_parameters as $key => $value) {
@@ -321,9 +311,9 @@
321 311 // add our body parameters
322 312 if( $this->body_parameters && ! empty( $this->body_parameters ) )
323 313 $headers['body'] = $this->body_parameters;
324 314
325 - //}
315 + }
326 316
327 317 // add headers to our final request args
328 318 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
329 319
@@ -374,17 +364,8 @@
374 364 return $response;
375 365 }
376 366
377 367 /**
378 - * Do a PATCH request
379 - */
380 - public function PATCH_request() {
381 - $this->final_request_args['method'] = 'PATCH';
382 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
383 - return $response;
384 - }
385 -
386 - /**
387 368 * Do a DELETE request
388 369 */
389 370 public function DELETE_request() {
390 371 $this->final_request_args['method'] = 'DELETE';
@@ -498,9 +479,9 @@
498 479
499 480 }
500 481
501 482 }
502 -
483 +
503 484 return $raw ? $raw : $new_array;
504 485
505 486 }
506 487
@@ -540,69 +521,10 @@
540 521 ) {
541 522
542 523 $this->debug = true;
543 524
544 - // admin test endpoint button
545 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
546 526
547 - // get api status from response code
548 - $status = array(
549 - 'title' => 'Error in API response.',
550 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
551 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
552 - );
553 -
554 - if( $this->response_code == 200 ) {
555 - $status = array(
556 - 'title' => 'Success!',
557 - '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.',
558 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
559 - );
560 - }
561 - if( $this->response_code == 400 || $this->response_code == 404 ) {
562 - $status = array(
563 - 'title' => 'Bad Request',
564 - '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.',
565 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
566 - );
567 - }
568 - if( $this->response_code == 401 ) {
569 - $status = array(
570 - 'title' => 'Unauthorised',
571 - '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.',
572 - 'type' => '<span class="dashicons dashicons-warning"></span>'
573 - );
574 - }
575 - if( $this->response_code == 403 ) {
576 - $status = array(
577 - 'title' => 'Forbidden',
578 - '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.',
579 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
580 - );
581 - }
582 - if( $this->response_code == 405 ) {
583 - $status = array(
584 - 'title' => 'Method Not Allowed',
585 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
586 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
587 - );
588 - }
589 - if( $this->response_code == 415 ) {
590 - $status = array(
591 - 'title' => 'Unsupported Media Type',
592 - '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>.',
593 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
594 - );
595 - }
596 - if( $this->response_code == 500 ) {
597 - $status = array(
598 - 'title' => 'Internal Server Error',
599 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
600 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
601 - );
602 - }
603 -
604 -
605 527 ob_start(); ?>
606 528
607 529 <style>
608 530
@@ -639,10 +561,10 @@
639 561 background: #fff;
640 562 padding: 15px 20px;
641 563 display: inline-block !important;
642 564 width: 100% !important;
643 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
644 - border: 1px solid #eee;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
645 567 }
646 568 .wpgetapi-debug .debug-item h6 {
647 569 font-weight: bold;
648 570 font-size: 14px;
@@ -654,14 +576,15 @@
654 576 font-family: monospace !important;
655 577 font-size: 13px !important;
656 578 margin: 0 !important;
657 579 padding: 8px 12px;
658 - background: #f4f7f9;
580 + background: #f8f8f8;
659 581 }
660 582 .wpgetapi-debug .debug-item .debug-result pre {
661 583 border: none;
662 584 padding: 5px
663 585 }
586 +
664 587 .wpgetapi-debug.testing {
665 588 background: transparent;
666 589 padding: 0;
667 590 box-shadow: none;
@@ -668,32 +591,8 @@
668 591 }
669 592 .wpgetapi-debug.testing .debug-item {
670 593 margin: 5px 0;
671 594 }
672 -.wpgetapi-debug.testing .debug-item.status {
673 - border: none;
674 - box-shadow: none;
675 - margin-bottom: 15px;
676 -}
677 -.wpgetapi-debug .debug-item.status h5 {
678 - font-size: 15px;
679 -}
680 -.wpgetapi-debug .status .dashicons {
681 - margin: 0 5px 3px 0;
682 -}
683 -.wpgetapi-debug .status .dashicons-yes-alt {
684 - color: #5e9929;
685 -}
686 -.wpgetapi-debug .status .dashicons-dismiss {
687 - color: #cc4646;
688 -}
689 -.wpgetapi-debug .status .dashicons-warning {
690 - color: #dfa12e;
691 -}
692 -.wpgetapi-debug .status .desc {
693 - font-size: 14px;
694 - font-weight: 400 !important;
695 -}
696 595 </style>
697 596
698 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
699 598
@@ -698,15 +597,9 @@
698 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
699 598
700 599 <?php if( $testing ) { ?>
701 600
702 - <div class="debug-item status">
703 - <div class="">
704 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
705 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
706 - </div>
707 - </div>
708 - <div class="debug-item">
601 + <div class="debug-item">
709 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
710 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
711 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
712 605 </div>
@@ -713,8 +606,9 @@
713 606
714 607 <div class="debug-item">
715 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
716 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>
717 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
718 612 </div>
719 613
720 614 <div class="debug-item">