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 +16 -124 1.9.31.8.9 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,24 +122,17 @@
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
137 -
138 - if(
139 - // 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' ) ||
141 - // set the format to php array data if it is set in the args (coming from the importer)
142 - ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
143 - ) {
133 + // set the format to php array data if html format is set
134 + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) {
144 135 $this->results_format = 'json_decoded';
145 136 }
146 137
147 138 }
@@ -179,9 +170,9 @@
179 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
180 171
181 172 // build the request args
182 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
183 -
174 +
184 175 /**
185 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
186 177 * @param bool $on Whether on is set or not.
187 178 */
@@ -211,17 +202,17 @@
211 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
212 203 }
213 204
214 205 // get response_code
215 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
216 207
217 208 // action to intercept call depending on response_code
218 - $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 );
219 210
220 211 // get body
221 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
222 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
223 -
214 +
224 215 // returning in JSON format
225 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
226 217 $data = $this->return_data( $data );
227 218
@@ -275,9 +266,8 @@
275 266
276 267 // if doing POST request, include body paramters
277 268 if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
278 269
279 - // tokens are sent through here
280 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
281 271
282 272 // do we json_encode the entire body
283 273 if( $this->body_json_encode == true ) {
@@ -287,17 +277,14 @@
287 277 // do we urlencode the entire body
288 278 if( $this->body_url_encode == true ) {
289 279
290 280 // checking if the parameters have a json string (raw)
291 - if (
292 - ( ! is_array( $this->body_parameters ) ) &&
293 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
294 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
295 282 $this->body_parameters = json_decode( $this->body_parameters, true );
296 283 }
297 -
298 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
299 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
300 287 }
301 288
302 289 // do we xml the entire body
303 290 if( $this->body_xml_format == true ) {
@@ -334,10 +321,8 @@
334 321
335 322 }
336 323
337 324
338 -
339 -
340 325 /**
341 326 * Do a GET request
342 327 */
343 328 public function GET_request() {
@@ -483,9 +468,9 @@
483 468
484 469 }
485 470
486 471 }
487 -
472 +
488 473 return $raw ? $raw : $new_array;
489 474
490 475 }
491 476
@@ -525,69 +510,10 @@
525 510 ) {
526 511
527 512 $this->debug = true;
528 513
529 - // admin test endpoint button
530 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
531 515
532 - // get api status from response code
533 - $status = array(
534 - 'title' => 'Error in API response.',
535 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
536 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
537 - );
538 -
539 - if( $this->response_code == 200 ) {
540 - $status = array(
541 - 'title' => 'Success!',
542 - '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.',
543 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
544 - );
545 - }
546 - if( $this->response_code == 400 || $this->response_code == 404 ) {
547 - $status = array(
548 - 'title' => 'Bad Request',
549 - '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.',
550 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
551 - );
552 - }
553 - if( $this->response_code == 401 ) {
554 - $status = array(
555 - 'title' => 'Unauthorised',
556 - '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.',
557 - 'type' => '<span class="dashicons dashicons-warning"></span>'
558 - );
559 - }
560 - if( $this->response_code == 403 ) {
561 - $status = array(
562 - 'title' => 'Forbidden',
563 - '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.',
564 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
565 - );
566 - }
567 - if( $this->response_code == 405 ) {
568 - $status = array(
569 - 'title' => 'Method Not Allowed',
570 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
571 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
572 - );
573 - }
574 - if( $this->response_code == 415 ) {
575 - $status = array(
576 - 'title' => 'Unsupported Media Type',
577 - '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>.',
578 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
579 - );
580 - }
581 - if( $this->response_code == 500 ) {
582 - $status = array(
583 - 'title' => 'Internal Server Error',
584 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
585 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
586 - );
587 - }
588 -
589 -
590 516 ob_start(); ?>
591 517
592 518 <style>
593 519
@@ -624,10 +550,10 @@
624 550 background: #fff;
625 551 padding: 15px 20px;
626 552 display: inline-block !important;
627 553 width: 100% !important;
628 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
629 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
630 556 }
631 557 .wpgetapi-debug .debug-item h6 {
632 558 font-weight: bold;
633 559 font-size: 14px;
@@ -639,14 +565,15 @@
639 565 font-family: monospace !important;
640 566 font-size: 13px !important;
641 567 margin: 0 !important;
642 568 padding: 8px 12px;
643 - background: #f4f7f9;
569 + background: #f8f8f8;
644 570 }
645 571 .wpgetapi-debug .debug-item .debug-result pre {
646 572 border: none;
647 573 padding: 5px
648 574 }
575 +
649 576 .wpgetapi-debug.testing {
650 577 background: transparent;
651 578 padding: 0;
652 579 box-shadow: none;
@@ -653,32 +580,8 @@
653 580 }
654 581 .wpgetapi-debug.testing .debug-item {
655 582 margin: 5px 0;
656 583 }
657 -.wpgetapi-debug.testing .debug-item.status {
658 - border: none;
659 - box-shadow: none;
660 - margin-bottom: 15px;
661 -}
662 -.wpgetapi-debug .debug-item.status h5 {
663 - font-size: 15px;
664 -}
665 -.wpgetapi-debug .status .dashicons {
666 - margin: 0 5px 3px 0;
667 -}
668 -.wpgetapi-debug .status .dashicons-yes-alt {
669 - color: #5e9929;
670 -}
671 -.wpgetapi-debug .status .dashicons-dismiss {
672 - color: #cc4646;
673 -}
674 -.wpgetapi-debug .status .dashicons-warning {
675 - color: #dfa12e;
676 -}
677 -.wpgetapi-debug .status .desc {
678 - font-size: 14px;
679 - font-weight: 400 !important;
680 -}
681 584 </style>
682 585
683 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
684 587
@@ -683,15 +586,9 @@
683 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
684 587
685 588 <?php if( $testing ) { ?>
686 589
687 - <div class="debug-item status">
688 - <div class="">
689 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
690 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
691 - </div>
692 - </div>
693 - <div class="debug-item">
590 + <div class="debug-item">
694 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
695 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
696 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
697 594 </div>
@@ -698,15 +595,10 @@
698 595
699 596 <div class="debug-item">
700 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
701 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>
702 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
703 - </div>
704 -
705 - <div class="debug-item">
706 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
707 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
708 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
709 601 </div>
710 602
711 603 <div class="debug-item">
712 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>