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 +26 -184 2.1.01.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,12 +38,10 @@
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 - public $sslverify = 1; // ssl verify
43 + public $sslverify = 1; // the paramaters
47 44 public $debug = false;
48 45
49 46
50 47 /**
@@ -75,9 +72,9 @@
75 72 public function init() {
76 73
77 74 // if we are doing importer plugin,
78 75 // we need to intercept and modify most of our methods values
79 - if( $this->api_id === 'wpgetapi_importer' ) {
76 + if( $this->api_id == 'wpgetapi_importer' ) {
80 77
81 78 $data = apply_filters( 'wpgetapi_api_importer_init', $this );
82 79
83 80 if( $data ) {
@@ -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;
@@ -257,14 +237,8 @@
257 237 // first make sure our endpoint starts with a slash
258 238 if ( substr( $this->endpoint, 0, 1 ) != '/' )
259 239 $this->endpoint = '/' . $this->endpoint;
260 240
261 - // if we have item_key from api to posts plugin
262 - // replace our keyword with the actual item_key value
263 - if ( isset( $this->args['item_key'] ) && strpos( $this->endpoint, '(importer:item_key)' ) !== false ) {
264 - $this->endpoint = str_replace( '(importer:item_key)', $this->args['item_key'], $this->endpoint );
265 - }
266 -
267 241 $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this );
268 242
269 243 $url = untrailingslashit( $this->base_url ) . $this->endpoint;
270 244
@@ -278,12 +252,10 @@
278 252
279 253 // add empty array - was getting errors if it was just null or false
280 254 $params = empty( $params ) ? array(''=>'') : $params;
281 255
282 - $result = add_query_arg( $params, $url );
256 + return add_query_arg( $params, $url );
283 257
284 - return $result;
285 -
286 258 }
287 259
288 260
289 261 /**
@@ -297,9 +269,9 @@
297 269 ), $this );
298 270
299 271
300 272 // if doing POST request, include body paramters
301 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
273 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
302 274
303 275 // tokens are sent through here
304 276 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
305 277
@@ -308,9 +280,9 @@
308 280 $this->body_parameters = json_encode( $this->body_parameters );
309 281 }
310 282
311 283 // do we urlencode the entire body
312 - if( $this->body_parameters && $this->body_url_encode == true ) {
284 + if( $this->body_url_encode == true ) {
313 285
314 286 // checking if the parameters have a json string (raw)
315 287 if (
316 288 ( ! is_array( $this->body_parameters ) ) &&
@@ -323,10 +295,10 @@
323 295
324 296 }
325 297
326 298 // do we xml the entire body
327 - if( $this->body_parameters && $this->body_xml_format == true ) {
328 -
299 + if( $this->body_xml_format == true ) {
300 +
329 301 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
330 302
331 303 foreach( $this->body_parameters as $key => $value) {
332 304 $xml->addChild( $key, $value );
@@ -335,16 +307,8 @@
335 307 $this->body_parameters = $xml->asXML();
336 308
337 309 }
338 310
339 - // do we base64 encode the entire body
340 - if( $this->body_parameters && $this->body_base64_encode == true ) {
341 -
342 - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters;
343 - $this->body_parameters = base64_encode( $this->body_parameters );
344 -
345 - }
346 -
347 311 // add our body parameters
348 312 if( $this->body_parameters && ! empty( $this->body_parameters ) )
349 313 $headers['body'] = $this->body_parameters;
350 314
@@ -349,11 +313,8 @@
349 313 $headers['body'] = $this->body_parameters;
350 314
351 315 }
352 316
353 - // get our body paramters as they are formatted
354 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
355 -
356 317 // add headers to our final request args
357 318 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
358 319
359 320 // build the args taht are sent to the request
@@ -375,9 +336,8 @@
375 336 /**
376 337 * Do a GET request
377 338 */
378 339 public function GET_request() {
379 -
380 340 $response = '';
381 341 // so we can so something before the remote_get (caching)
382 342 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
383 343
@@ -382,11 +342,9 @@
382 342 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
383 343
384 344 if( empty( $response ) )
385 345 $response = wp_remote_get( $this->final_url, $this->final_request_args );
386 -
387 346 return $response;
388 -
389 347 }
390 348
391 349 /**
392 350 * Do a POST request
@@ -391,18 +349,10 @@
391 349 /**
392 350 * Do a POST request
393 351 */
394 352 public function POST_request() {
395 -
396 - $response = '';
397 - // so we can so something before the remote_post (caching)
398 - $response = apply_filters( 'wpgetapi_before_post_request', $response, $this );
399 -
400 353 $this->final_request_args['method'] = 'POST';
401 -
402 - if( empty( $response ) )
403 - $response = wp_remote_post( $this->final_url, $this->final_request_args );
404 -
354 + $response = wp_remote_post( $this->final_url, $this->final_request_args );
405 355 return $response;
406 356 }
407 357
408 358 /**
@@ -414,17 +364,8 @@
414 364 return $response;
415 365 }
416 366
417 367 /**
418 - * Do a PATCH request
419 - */
420 - public function PATCH_request() {
421 - $this->final_request_args['method'] = 'PATCH';
422 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
423 - return $response;
424 - }
425 -
426 - /**
427 368 * Do a DELETE request
428 369 */
429 370 public function DELETE_request() {
430 371 $this->final_request_args['method'] = 'DELETE';
@@ -438,9 +379,9 @@
438 379 */
439 380 public function return_data( $data ) {
440 381
441 382 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
442 -
383 +
443 384 // converts all data to arrays, removing objects
444 385 if( is_string( $data ) ) {
445 386 $decode = json_decode( $data, true );
446 387 if( json_last_error() ) {
@@ -448,13 +389,13 @@
448 389 } else {
449 390 $data = $decode;
450 391 }
451 392 }
452 -
393 +
453 394 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
454 395
455 396 if( $this->results_format == 'json_string' )
456 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
397 + return trim( json_encode( $data ),'"');
457 398
458 399 if( $this->results_format == 'json_decoded' )
459 400 return $data;
460 401
@@ -463,9 +404,9 @@
463 404 /**
464 405 * Sort out the parameters if they exist
465 406 */
466 407 public function do_parameters( $parameters ) {
467 -
408 +
468 409 $new_array = array();
469 410 $raw = '';
470 411 $parameters = $this->decrypt( $parameters );
471 412
@@ -481,14 +422,8 @@
481 422 unset( $parameters[ $key ] );
482 423 continue;
483 424 }
484 425
485 - // if we have item_key from api to posts plugin
486 - // replace our keyword with the actual item_key value
487 - if ( isset( $this->args['item_key'] ) && strpos( $parameter['value'], '(importer:item_key)' ) !== false ) {
488 - $parameter['value'] = str_replace( '(importer:item_key)', $this->args['item_key'], $parameter['value'] );
489 - }
490 -
491 426 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
492 427
493 428 // if we are sending raw body
494 429 } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
@@ -499,20 +434,17 @@
499 434
500 435 }
501 436 }
502 437
503 - // now look for array or json string and sort these out - for using such things in shortcode
438 + // look for array or json string and sort these out - for using such things in shortcode
504 439 if( ! empty( $new_array ) ) {
505 440
506 441 foreach ( $new_array as $key => $value ) {
507 442
508 443 // checking if the parameters have a json string
509 - if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) {
510 -
511 - $tmp_val = $value;
512 - $tmp_val = json_decode( $tmp_val, true );
513 - $new_array[ $key ] = $tmp_val ? $tmp_val : $value;
514 -
444 + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) {
445 + //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
446 + $new_array[ $key ] = json_decode( $value, true );
515 447 } else
516 448
517 449 // checking if the parameters have an array within the string
518 450 // if they do, create them as an actual array
@@ -531,10 +463,8 @@
531 463 // set value as float
532 464 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
533 465 preg_match("/\(([^\)]*)\)/", $value, $match );
534 466 $result = $match[1];
535 - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
536 - $result = trim( $result,'""' );
537 467 $new_array[ $key ] = (float) $result;
538 468
539 469 }
540 470
@@ -549,9 +479,9 @@
549 479
550 480 }
551 481
552 482 }
553 -
483 +
554 484 return $raw ? $raw : $new_array;
555 485
556 486 }
557 487
@@ -591,69 +521,10 @@
591 521 ) {
592 522
593 523 $this->debug = true;
594 524
595 - // admin test endpoint button
596 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
597 526
598 - // get api status from response code
599 - $status = array(
600 - 'title' => 'Error in API response.',
601 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
602 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
603 - );
604 -
605 - if( $this->response_code >= 200 && $this->response_code <= 299 ) {
606 - $status = array(
607 - 'title' => 'Success!',
608 - '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.',
609 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
610 - );
611 - }
612 - if( $this->response_code >= 400 && $this->response_code <= 499 ) {
613 - $status = array(
614 - 'title' => 'Bad Request',
615 - '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.',
616 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
617 - );
618 - }
619 - if( $this->response_code == 401 ) {
620 - $status = array(
621 - 'title' => 'Unauthorised',
622 - '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.',
623 - 'type' => '<span class="dashicons dashicons-warning"></span>'
624 - );
625 - }
626 - if( $this->response_code == 403 ) {
627 - $status = array(
628 - 'title' => 'Forbidden',
629 - '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.',
630 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
631 - );
632 - }
633 - if( $this->response_code == 405 ) {
634 - $status = array(
635 - 'title' => 'Method Not Allowed',
636 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
637 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
638 - );
639 - }
640 - if( $this->response_code == 415 ) {
641 - $status = array(
642 - 'title' => 'Unsupported Media Type',
643 - '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>.',
644 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
645 - );
646 - }
647 - if( $this->response_code >= 500 && $this->response_code <= 599 ) {
648 - $status = array(
649 - 'title' => 'Internal Server Error',
650 - 'desc' => 'This indicates an issue with the API server. Contact your API provider.',
651 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
652 - );
653 - }
654 -
655 -
656 527 ob_start(); ?>
657 528
658 529 <style>
659 530
@@ -690,11 +561,10 @@
690 561 background: #fff;
691 562 padding: 15px 20px;
692 563 display: inline-block !important;
693 564 width: 100% !important;
694 - box-shadow: 0 4px 4px -2px rgb(0 0 0 / 9%);
695 - border: 1px solid #eee;
696 - margin-bottom: 20px !important;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
697 567 }
698 568 .wpgetapi-debug .debug-item h6 {
699 569 font-weight: bold;
700 570 font-size: 14px;
@@ -706,14 +576,15 @@
706 576 font-family: monospace !important;
707 577 font-size: 13px !important;
708 578 margin: 0 !important;
709 579 padding: 8px 12px;
710 - background: #f4f7f9;
580 + background: #f8f8f8;
711 581 }
712 582 .wpgetapi-debug .debug-item .debug-result pre {
713 583 border: none;
714 584 padding: 5px
715 585 }
586 +
716 587 .wpgetapi-debug.testing {
717 588 background: transparent;
718 589 padding: 0;
719 590 box-shadow: none;
@@ -720,32 +591,8 @@
720 591 }
721 592 .wpgetapi-debug.testing .debug-item {
722 593 margin: 5px 0;
723 594 }
724 -.wpgetapi-debug.testing .debug-item.status {
725 - border: none;
726 - box-shadow: none;
727 - margin-bottom: 15px;
728 -}
729 -.wpgetapi-debug .debug-item.status h5 {
730 - font-size: 15px;
731 -}
732 -.wpgetapi-debug .status .dashicons {
733 - margin: 0 5px 3px 0;
734 -}
735 -.wpgetapi-debug .status .dashicons-yes-alt {
736 - color: #5e9929;
737 -}
738 -.wpgetapi-debug .status .dashicons-dismiss {
739 - color: #cc4646;
740 -}
741 -.wpgetapi-debug .status .dashicons-warning {
742 - color: #dfa12e;
743 -}
744 -.wpgetapi-debug .status .desc {
745 - font-size: 14px;
746 - font-weight: 400 !important;
747 -}
748 595 </style>
749 596
750 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
751 598
@@ -750,15 +597,9 @@
750 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
751 598
752 599 <?php if( $testing ) { ?>
753 600
754 - <div class="debug-item status">
755 - <div class="">
756 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
757 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
758 - </div>
759 - </div>
760 - <div class="debug-item">
601 + <div class="debug-item">
761 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
762 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
763 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
764 605 </div>
@@ -765,8 +606,9 @@
765 606
766 607 <div class="debug-item">
767 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
768 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>
769 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
770 612 </div>
771 613
772 614 <div class="debug-item">