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 +24 -172 2.0.41.8.9 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,38 +122,20 @@
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
139 -
140 - if(
141 - // 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' ) ||
143 - // set the format to php array data if it is set in the args (coming from the importer)
144 - ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' )
145 - ) {
133 + // set the format to php array data if html format is set
134 + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) {
146 135 $this->results_format = 'json_decoded';
147 136 }
148 137
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 138 }
160 139
161 140 }
162 141
@@ -191,9 +170,9 @@
191 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
192 171
193 172 // build the request args
194 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
195 -
174 +
196 175 /**
197 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
198 177 * @param bool $on Whether on is set or not.
199 178 */
@@ -209,12 +188,8 @@
209 188 // PUT request
210 189 if( $this->method == 'PUT' )
211 190 $this->response = $this->PUT_request();
212 191
213 - // PATCH request
214 - if( $this->method == 'PATCH' )
215 - $this->response = $this->PATCH_request();
216 -
217 192 // GET request
218 193 if( $this->method == 'GET' )
219 194 $this->response = $this->GET_request();
220 195
@@ -227,17 +202,17 @@
227 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
228 203 }
229 204
230 205 // get response_code
231 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
232 207
233 208 // action to intercept call depending on response_code
234 - $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 );
235 210
236 211 // get body
237 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
238 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
239 -
214 +
240 215 // returning in JSON format
241 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
242 217 $data = $this->return_data( $data );
243 218
@@ -272,12 +247,10 @@
272 247
273 248 // add empty array - was getting errors if it was just null or false
274 249 $params = empty( $params ) ? array(''=>'') : $params;
275 250
276 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
277 252
278 - return $result;
279 -
280 253 }
281 254
282 255
283 256 /**
@@ -291,11 +264,10 @@
291 264 ), $this );
292 265
293 266
294 267 // if doing POST request, include body paramters
295 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
296 269
297 - // tokens are sent through here
298 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
299 271
300 272 // do we json_encode the entire body
301 273 if( $this->body_json_encode == true ) {
@@ -302,24 +274,21 @@
302 274 $this->body_parameters = json_encode( $this->body_parameters );
303 275 }
304 276
305 277 // do we urlencode the entire body
306 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
307 279
308 280 // checking if the parameters have a json string (raw)
309 - if (
310 - ( ! is_array( $this->body_parameters ) ) &&
311 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
312 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
313 282 $this->body_parameters = json_decode( $this->body_parameters, true );
314 283 }
315 -
316 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
317 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
318 287 }
319 288
320 289 // do we xml the entire body
321 - if( $this->body_parameters && $this->body_xml_format == true ) {
290 + if( $this->body_xml_format == true ) {
322 291
323 292 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
324 293
325 294 foreach( $this->body_parameters as $key => $value) {
@@ -329,16 +298,8 @@
329 298 $this->body_parameters = $xml->asXML();
330 299
331 300 }
332 301
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 302 // add our body parameters
342 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
343 304 $headers['body'] = $this->body_parameters;
344 305
@@ -343,11 +304,8 @@
343 304 $headers['body'] = $this->body_parameters;
344 305
345 306 }
346 307
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 308 // add headers to our final request args
351 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
352 310
353 311 // build the args taht are sent to the request
@@ -363,10 +321,8 @@
363 321
364 322 }
365 323
366 324
367 -
368 -
369 325 /**
370 326 * Do a GET request
371 327 */
372 328 public function GET_request() {
@@ -397,17 +353,8 @@
397 353 return $response;
398 354 }
399 355
400 356 /**
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 357 * Do a DELETE request
411 358 */
412 359 public function DELETE_request() {
413 360 $this->final_request_args['method'] = 'DELETE';
@@ -421,9 +368,9 @@
421 368 */
422 369 public function return_data( $data ) {
423 370
424 371 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
425 -
372 +
426 373 // converts all data to arrays, removing objects
427 374 if( is_string( $data ) ) {
428 375 $decode = json_decode( $data, true );
429 376 if( json_last_error() ) {
@@ -431,13 +378,13 @@
431 378 } else {
432 379 $data = $decode;
433 380 }
434 381 }
435 -
382 +
436 383 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
437 384
438 385 if( $this->results_format == 'json_string' )
439 - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"');
386 + return trim( json_encode( $data ),'"');
440 387
441 388 if( $this->results_format == 'json_decoded' )
442 389 return $data;
443 390
@@ -446,9 +393,9 @@
446 393 /**
447 394 * Sort out the parameters if they exist
448 395 */
449 396 public function do_parameters( $parameters ) {
450 -
397 +
451 398 $new_array = array();
452 399 $raw = '';
453 400 $parameters = $this->decrypt( $parameters );
454 401
@@ -482,9 +429,9 @@
482 429
483 430 foreach ( $new_array as $key => $value ) {
484 431
485 432 // checking if the parameters have a json string
486 - if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) {
433 + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) {
487 434 //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) {
488 435 $new_array[ $key ] = json_decode( $value, true );
489 436 } else
490 437
@@ -505,10 +452,8 @@
505 452 // set value as float
506 453 if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) {
507 454 preg_match("/\(([^\)]*)\)/", $value, $match );
508 455 $result = $match[1];
509 - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' )
510 - $result = trim( $result,'""' );
511 456 $new_array[ $key ] = (float) $result;
512 457
513 458 }
514 459
@@ -565,69 +510,10 @@
565 510 ) {
566 511
567 512 $this->debug = true;
568 513
569 - // admin test endpoint button
570 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
571 515
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 516 ob_start(); ?>
631 517
632 518 <style>
633 519
@@ -664,10 +550,10 @@
664 550 background: #fff;
665 551 padding: 15px 20px;
666 552 display: inline-block !important;
667 553 width: 100% !important;
668 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
669 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
670 556 }
671 557 .wpgetapi-debug .debug-item h6 {
672 558 font-weight: bold;
673 559 font-size: 14px;
@@ -679,14 +565,15 @@
679 565 font-family: monospace !important;
680 566 font-size: 13px !important;
681 567 margin: 0 !important;
682 568 padding: 8px 12px;
683 - background: #f4f7f9;
569 + background: #f8f8f8;
684 570 }
685 571 .wpgetapi-debug .debug-item .debug-result pre {
686 572 border: none;
687 573 padding: 5px
688 574 }
575 +
689 576 .wpgetapi-debug.testing {
690 577 background: transparent;
691 578 padding: 0;
692 579 box-shadow: none;
@@ -693,32 +580,8 @@
693 580 }
694 581 .wpgetapi-debug.testing .debug-item {
695 582 margin: 5px 0;
696 583 }
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 584 </style>
722 585
723 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
724 587
@@ -723,15 +586,9 @@
723 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
724 587
725 588 <?php if( $testing ) { ?>
726 589
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">
590 + <div class="debug-item">
734 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
735 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
736 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
737 594 </div>
@@ -738,15 +595,10 @@
738 595
739 596 <div class="debug-item">
740 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
741 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>
742 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
743 - </div>
744 -
745 - <div class="debug-item">
746 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
747 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
748 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
749 601 </div>
750 602
751 603 <div class="debug-item">
752 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>