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 +23 -167 2.0.51.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
@@ -381,9 +342,8 @@
381 342 $response = apply_filters( 'wpgetapi_before_get_request', $response, $this );
382 343
383 344 if( empty( $response ) )
384 345 $response = wp_remote_get( $this->final_url, $this->final_request_args );
385 -
386 346 return $response;
387 347 }
388 348
389 349 /**
@@ -404,17 +364,8 @@
404 364 return $response;
405 365 }
406 366
407 367 /**
408 - * Do a PATCH request
409 - */
410 - public function PATCH_request() {
411 - $this->final_request_args['method'] = 'PATCH';
412 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
413 - return $response;
414 - }
415 -
416 - /**
417 368 * Do a DELETE request
418 369 */
419 370 public function DELETE_request() {
420 371 $this->final_request_args['method'] = 'DELETE';
@@ -428,9 +379,9 @@
428 379 */
429 380 public function return_data( $data ) {
430 381
431 382 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
432 -
383 +
433 384 // converts all data to arrays, removing objects
434 385 if( is_string( $data ) ) {
435 386 $decode = json_decode( $data, true );
436 387 if( json_last_error() ) {
@@ -438,13 +389,13 @@
438 389 } else {
439 390 $data = $decode;
440 391 }
441 392 }
442 -
393 +
443 394 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
444 395
445 396 if( $this->results_format == 'json_string' )
446 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
397 + return trim( json_encode( $data ),'"');
447 398
448 399 if( $this->results_format == 'json_decoded' )
449 400 return $data;
450 401
@@ -453,9 +404,9 @@
453 404 /**
454 405 * Sort out the parameters if they exist
455 406 */
456 407 public function do_parameters( $parameters ) {
457 -
408 +
458 409 $new_array = array();
459 410 $raw = '';
460 411 $parameters = $this->decrypt( $parameters );
461 412
@@ -471,14 +422,8 @@
471 422 unset( $parameters[ $key ] );
472 423 continue;
473 424 }
474 425
475 - // if we have item_key from api to posts plugin
476 - // replace our keyword with the actual item_key value
477 - if ( isset( $this->args['item_key'] ) && strpos( $parameter['value'], '(importer:item_key)' ) !== false ) {
478 - $parameter['value'] = str_replace( '(importer:item_key)', $this->args['item_key'], $parameter['value'] );
479 - }
480 -
481 426 $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value'];
482 427
483 428 // if we are sending raw body
484 429 } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) {
@@ -489,15 +434,15 @@
489 434
490 435 }
491 436 }
492 437
493 - // 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
494 439 if( ! empty( $new_array ) ) {
495 440
496 441 foreach ( $new_array as $key => $value ) {
497 442
498 443 // checking if the parameters have a json string
499 - if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) {
444 + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) {
500 445 //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
501 446 $new_array[ $key ] = json_decode( $value, true );
502 447 } else
503 448
@@ -518,10 +463,8 @@
518 463 // set value as float
519 464 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
520 465 preg_match("/\(([^\)]*)\)/", $value, $match );
521 466 $result = $match[1];
522 - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
523 - $result = trim( $result,'""' );
524 467 $new_array[ $key ] = (float) $result;
525 468
526 469 }
527 470
@@ -536,9 +479,9 @@
536 479
537 480 }
538 481
539 482 }
540 -
483 +
541 484 return $raw ? $raw : $new_array;
542 485
543 486 }
544 487
@@ -578,69 +521,10 @@
578 521 ) {
579 522
580 523 $this->debug = true;
581 524
582 - // admin test endpoint button
583 525 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
584 526
585 - // get api status from response code
586 - $status = array(
587 - 'title' => 'Error in API response.',
588 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
589 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
590 - );
591 -
592 - if( $this->response_code >= 200 && $this->response_code <= 299 ) {
593 - $status = array(
594 - 'title' => 'Success!',
595 - '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.',
596 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
597 - );
598 - }
599 - if( $this->response_code >= 400 && $this->response_code <= 499 ) {
600 - $status = array(
601 - 'title' => 'Bad Request',
602 - '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.',
603 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
604 - );
605 - }
606 - if( $this->response_code == 401 ) {
607 - $status = array(
608 - 'title' => 'Unauthorised',
609 - '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.',
610 - 'type' => '<span class="dashicons dashicons-warning"></span>'
611 - );
612 - }
613 - if( $this->response_code == 403 ) {
614 - $status = array(
615 - 'title' => 'Forbidden',
616 - '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.',
617 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
618 - );
619 - }
620 - if( $this->response_code == 405 ) {
621 - $status = array(
622 - 'title' => 'Method Not Allowed',
623 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
624 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
625 - );
626 - }
627 - if( $this->response_code == 415 ) {
628 - $status = array(
629 - 'title' => 'Unsupported Media Type',
630 - '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>.',
631 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
632 - );
633 - }
634 - if( $this->response_code >= 500 && $this->response_code <= 599 ) {
635 - $status = array(
636 - 'title' => 'Internal Server Error',
637 - 'desc' => 'This indicates an issue with the API server. Contact your API provider.',
638 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
639 - );
640 - }
641 -
642 -
643 527 ob_start(); ?>
644 528
645 529 <style>
646 530
@@ -677,10 +561,10 @@
677 561 background: #fff;
678 562 padding: 15px 20px;
679 563 display: inline-block !important;
680 564 width: 100% !important;
681 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
682 - border: 1px solid #eee;
565 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
566 + border: 1px solid #efefef;
683 567 }
684 568 .wpgetapi-debug .debug-item h6 {
685 569 font-weight: bold;
686 570 font-size: 14px;
@@ -692,14 +576,15 @@
692 576 font-family: monospace !important;
693 577 font-size: 13px !important;
694 578 margin: 0 !important;
695 579 padding: 8px 12px;
696 - background: #f4f7f9;
580 + background: #f8f8f8;
697 581 }
698 582 .wpgetapi-debug .debug-item .debug-result pre {
699 583 border: none;
700 584 padding: 5px
701 585 }
586 +
702 587 .wpgetapi-debug.testing {
703 588 background: transparent;
704 589 padding: 0;
705 590 box-shadow: none;
@@ -706,32 +591,8 @@
706 591 }
707 592 .wpgetapi-debug.testing .debug-item {
708 593 margin: 5px 0;
709 594 }
710 -.wpgetapi-debug.testing .debug-item.status {
711 - border: none;
712 - box-shadow: none;
713 - margin-bottom: 15px;
714 -}
715 -.wpgetapi-debug .debug-item.status h5 {
716 - font-size: 15px;
717 -}
718 -.wpgetapi-debug .status .dashicons {
719 - margin: 0 5px 3px 0;
720 -}
721 -.wpgetapi-debug .status .dashicons-yes-alt {
722 - color: #5e9929;
723 -}
724 -.wpgetapi-debug .status .dashicons-dismiss {
725 - color: #cc4646;
726 -}
727 -.wpgetapi-debug .status .dashicons-warning {
728 - color: #dfa12e;
729 -}
730 -.wpgetapi-debug .status .desc {
731 - font-size: 14px;
732 - font-weight: 400 !important;
733 -}
734 595 </style>
735 596
736 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
737 598
@@ -736,15 +597,9 @@
736 597 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
737 598
738 599 <?php if( $testing ) { ?>
739 600
740 - <div class="debug-item status">
741 - <div class="">
742 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
743 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
744 - </div>
745 - </div>
746 - <div class="debug-item">
601 + <div class="debug-item">
747 602 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
748 603 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
749 604 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
750 605 </div>
@@ -751,8 +606,9 @@
751 606
752 607 <div class="debug-item">
753 608 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
754 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>
755 611 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
756 612 </div>
757 613
758 614 <div class="debug-item">