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