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