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 +23 -159 2.0.11.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,25 +122,17 @@
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 138 }
@@ -181,9 +170,9 @@
181 170 $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this );
182 171
183 172 // build the request args
184 173 $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this );
185 -
174 +
186 175 /**
187 176 * Check if we should stop or not. Setting $on to true, will stop us calling the api.
188 177 * @param bool $on Whether on is set or not.
189 178 */
@@ -199,12 +188,8 @@
199 188 // PUT request
200 189 if( $this->method == 'PUT' )
201 190 $this->response = $this->PUT_request();
202 191
203 - // PATCH request
204 - if( $this->method == 'PATCH' )
205 - $this->response = $this->PATCH_request();
206 -
207 192 // GET request
208 193 if( $this->method == 'GET' )
209 194 $this->response = $this->GET_request();
210 195
@@ -217,17 +202,17 @@
217 202 return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this );
218 203 }
219 204
220 205 // get response_code
221 - $this->response_code = wp_remote_retrieve_response_code( $this->response );
206 + $response_code = wp_remote_retrieve_response_code( $this->response );
222 207
223 208 // action to intercept call depending on response_code
224 - $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 );
225 210
226 211 // get body
227 212 // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already
228 213 $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response;
229 -
214 +
230 215 // returning in JSON format
231 216 if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' )
232 217 $data = $this->return_data( $data );
233 218
@@ -262,12 +247,10 @@
262 247
263 248 // add empty array - was getting errors if it was just null or false
264 249 $params = empty( $params ) ? array(''=>'') : $params;
265 250
266 - $result = add_query_arg( $params, $url );
251 + return add_query_arg( $params, $url );
267 252
268 - return $result;
269 -
270 253 }
271 254
272 255
273 256 /**
@@ -281,11 +264,10 @@
281 264 ), $this );
282 265
283 266
284 267 // if doing POST request, include body paramters
285 - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) {
268 + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) {
286 269
287 - // tokens are sent through here
288 270 $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this );
289 271
290 272 // do we json_encode the entire body
291 273 if( $this->body_json_encode == true ) {
@@ -292,24 +274,21 @@
292 274 $this->body_parameters = json_encode( $this->body_parameters );
293 275 }
294 276
295 277 // do we urlencode the entire body
296 - if( $this->body_parameters && $this->body_url_encode == true ) {
278 + if( $this->body_url_encode == true ) {
297 279
298 280 // checking if the parameters have a json string (raw)
299 - if (
300 - ( ! is_array( $this->body_parameters ) ) &&
301 - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' )
302 - ) {
281 + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) {
303 282 $this->body_parameters = json_decode( $this->body_parameters, true );
304 283 }
305 -
306 - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' );
307 284
285 + $this->body_parameters = http_build_query( $this->body_parameters );
286 +
308 287 }
309 288
310 289 // do we xml the entire body
311 - if( $this->body_parameters && $this->body_xml_format == true ) {
290 + if( $this->body_xml_format == true ) {
312 291
313 292 $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' );
314 293
315 294 foreach( $this->body_parameters as $key => $value) {
@@ -319,16 +298,8 @@
319 298 $this->body_parameters = $xml->asXML();
320 299
321 300 }
322 301
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 302 // add our body parameters
332 303 if( $this->body_parameters && ! empty( $this->body_parameters ) )
333 304 $headers['body'] = $this->body_parameters;
334 305
@@ -333,14 +304,11 @@
333 304 $headers['body'] = $this->body_parameters;
334 305
335 306 }
336 307
337 - // get our body paramters as they are formatted
338 - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this );
339 -
340 308 // add headers to our final request args
341 309 $this->final_request_args = wp_parse_args( $headers, $this->final_request_args );
342 -
310 +
343 311 // build the args taht are sent to the request
344 312 $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array(
345 313 'timeout' => $this->timeout,
346 314 'sslverify' => $this->sslverify,
@@ -353,10 +321,8 @@
353 321
354 322 }
355 323
356 324
357 -
358 -
359 325 /**
360 326 * Do a GET request
361 327 */
362 328 public function GET_request() {
@@ -387,17 +353,8 @@
387 353 return $response;
388 354 }
389 355
390 356 /**
391 - * Do a PATCH request
392 - */
393 - public function PATCH_request() {
394 - $this->final_request_args['method'] = 'PATCH';
395 - $response = wp_remote_request( $this->final_url, $this->final_request_args );
396 - return $response;
397 - }
398 -
399 - /**
400 357 * Do a DELETE request
401 358 */
402 359 public function DELETE_request() {
403 360 $this->final_request_args['method'] = 'DELETE';
@@ -411,9 +368,9 @@
411 368 */
412 369 public function return_data( $data ) {
413 370
414 371 $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys );
415 -
372 +
416 373 // converts all data to arrays, removing objects
417 374 if( is_string( $data ) ) {
418 375 $decode = json_decode( $data, true );
419 376 if( json_last_error() ) {
@@ -421,9 +378,9 @@
421 378 } else {
422 379 $data = $decode;
423 380 }
424 381 }
425 -
382 +
426 383 $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys );
427 384
428 385 if( $this->results_format == 'json_string' )
429 386 return trim( json_encode( $data ),'"');
@@ -436,9 +393,9 @@
436 393 /**
437 394 * Sort out the parameters if they exist
438 395 */
439 396 public function do_parameters( $parameters ) {
440 -
397 +
441 398 $new_array = array();
442 399 $raw = '';
443 400 $parameters = $this->decrypt( $parameters );
444 401
@@ -553,69 +510,10 @@
553 510 ) {
554 511
555 512 $this->debug = true;
556 513
557 - // admin test endpoint button
558 514 $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false;
559 515
560 - // get api status from response code
561 - $status = array(
562 - 'title' => 'Error in API response.',
563 - 'desc' => 'Check all details within your endpoint. Something is misconfigured.',
564 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
565 - );
566 -
567 - if( $this->response_code == 200 ) {
568 - $status = array(
569 - 'title' => 'Success!',
570 - '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.',
571 - 'type' => '<span class="dashicons dashicons-yes-alt"></span>'
572 - );
573 - }
574 - if( $this->response_code == 400 || $this->response_code == 404 ) {
575 - $status = array(
576 - 'title' => 'Bad Request',
577 - '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.',
578 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
579 - );
580 - }
581 - if( $this->response_code == 401 ) {
582 - $status = array(
583 - 'title' => 'Unauthorised',
584 - '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.',
585 - 'type' => '<span class="dashicons dashicons-warning"></span>'
586 - );
587 - }
588 - if( $this->response_code == 403 ) {
589 - $status = array(
590 - 'title' => 'Forbidden',
591 - '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.',
592 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
593 - );
594 - }
595 - if( $this->response_code == 405 ) {
596 - $status = array(
597 - 'title' => 'Method Not Allowed',
598 - 'desc' => 'Try changing the Request Method within the endpoint settings.',
599 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
600 - );
601 - }
602 - if( $this->response_code == 415 ) {
603 - $status = array(
604 - 'title' => 'Unsupported Media Type',
605 - '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>.',
606 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
607 - );
608 - }
609 - if( $this->response_code == 500 ) {
610 - $status = array(
611 - 'title' => 'Internal Server Error',
612 - 'desc' => 'This indicates an issue with the server. Contact your API provider.',
613 - 'type' => '<span class="dashicons dashicons-dismiss"></span>'
614 - );
615 - }
616 -
617 -
618 516 ob_start(); ?>
619 517
620 518 <style>
621 519
@@ -652,10 +550,10 @@
652 550 background: #fff;
653 551 padding: 15px 20px;
654 552 display: inline-block !important;
655 553 width: 100% !important;
656 - box-shadow: 0 3px 8px rgb(0 0 0 / 6%);
657 - border: 1px solid #eee;
554 + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%);
555 + border: 1px solid #efefef;
658 556 }
659 557 .wpgetapi-debug .debug-item h6 {
660 558 font-weight: bold;
661 559 font-size: 14px;
@@ -667,14 +565,15 @@
667 565 font-family: monospace !important;
668 566 font-size: 13px !important;
669 567 margin: 0 !important;
670 568 padding: 8px 12px;
671 - background: #f4f7f9;
569 + background: #f8f8f8;
672 570 }
673 571 .wpgetapi-debug .debug-item .debug-result pre {
674 572 border: none;
675 573 padding: 5px
676 574 }
575 +
677 576 .wpgetapi-debug.testing {
678 577 background: transparent;
679 578 padding: 0;
680 579 box-shadow: none;
@@ -681,32 +580,8 @@
681 580 }
682 581 .wpgetapi-debug.testing .debug-item {
683 582 margin: 5px 0;
684 583 }
685 -.wpgetapi-debug.testing .debug-item.status {
686 - border: none;
687 - box-shadow: none;
688 - margin-bottom: 15px;
689 -}
690 -.wpgetapi-debug .debug-item.status h5 {
691 - font-size: 15px;
692 -}
693 -.wpgetapi-debug .status .dashicons {
694 - margin: 0 5px 3px 0;
695 -}
696 -.wpgetapi-debug .status .dashicons-yes-alt {
697 - color: #5e9929;
698 -}
699 -.wpgetapi-debug .status .dashicons-dismiss {
700 - color: #cc4646;
701 -}
702 -.wpgetapi-debug .status .dashicons-warning {
703 - color: #dfa12e;
704 -}
705 -.wpgetapi-debug .status .desc {
706 - font-size: 14px;
707 - font-weight: 400 !important;
708 -}
709 584 </style>
710 585
711 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
712 587
@@ -711,15 +586,9 @@
711 586 <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>">
712 587
713 588 <?php if( $testing ) { ?>
714 589
715 - <div class="debug-item status">
716 - <div class="">
717 - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5>
718 - <span class="desc"><?php _e( $status['desc'] ); ?></span>
719 - </div>
720 - </div>
721 - <div class="debug-item">
590 + <div class="debug-item">
722 591 <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6>
723 592 <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p>
724 593 <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div>
725 594 </div>
@@ -726,15 +595,10 @@
726 595
727 596 <div class="debug-item">
728 597 <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6>
729 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>
730 600 <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div>
731 - </div>
732 -
733 - <div class="debug-item">
734 - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6>
735 - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p>
736 - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div>
737 601 </div>
738 602
739 603 <div class="debug-item">
740 604 <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6>