| @@ -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,15 +38,11 @@ | ||
| 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 | - | |
| 49 | - public $pro_plugin = false; | |
| 50 | 45 | |
| 51 | 46 | |
| 52 | 47 | /** |
| 53 | 48 | * Main constructor |
| @@ -65,9 +60,8 @@ | ||
| 65 | 60 | $this->endpoint_id = sanitize_text_field( $endpoint_id ); |
| 66 | 61 | $this->keys = $keys; |
| 67 | 62 | $this->args = $args; |
| 68 | 63 | |
| 69 | - add_filter( 'wpgetapi_raw_error_data', array( $this, 'maybe_add_debug_info' ), 99, 2 ); | |
| 70 | 64 | add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 99, 2 ); |
| 71 | 65 | |
| 72 | 66 | } |
| 73 | 67 | |
| @@ -76,17 +70,11 @@ | ||
| 76 | 70 | * Init our api data |
| 77 | 71 | */ |
| 78 | 72 | public function init() { |
| 79 | 73 | |
| 80 | - if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 81 | - include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); | |
| 82 | - } | |
| 83 | - | |
| 84 | - $this->pro_plugin = is_plugin_active( 'wpgetapi-extras/wpgetapi-extras.php' ) ? true : false; | |
| 85 | - | |
| 86 | 74 | // if we are doing importer plugin, |
| 87 | 75 | // we need to intercept and modify most of our methods values |
| 88 | - if( $this->api_id === 'wpgetapi_importer' ) { | |
| 76 | + if( $this->api_id == 'wpgetapi_importer' ) { | |
| 89 | 77 | |
| 90 | 78 | $data = apply_filters( 'wpgetapi_api_importer_init', $this ); |
| 91 | 79 | |
| 92 | 80 | if( $data ) { |
| @@ -125,9 +113,8 @@ | ||
| 125 | 113 | |
| 126 | 114 | if( $this->endpoint_id == $endpoint['id'] ) { |
| 127 | 115 | |
| 128 | 116 | $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET'; |
| 129 | - $this->timeout = isset( $endpoint['timeout'] ) && ! empty( $endpoint['timeout'] ) ? absint( $endpoint['timeout'] ) : ''; | |
| 130 | 117 | $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : ''; |
| 131 | 118 | $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : ''; |
| 132 | 119 | |
| 133 | 120 | $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array(); |
| @@ -135,38 +122,20 @@ | ||
| 135 | 122 | $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array(); |
| 136 | 123 | |
| 137 | 124 | $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array(); |
| 138 | 125 | |
| 139 | - // how to encode the body | |
| 140 | 126 | $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false; |
| 141 | 127 | $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false; |
| 142 | - $this->body_base64_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'base64' ? true : false; | |
| 143 | 128 | $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false; |
| 144 | - | |
| 145 | 129 | $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node; |
| 146 | 130 | |
| 147 | 131 | $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format; |
| 148 | 132 | |
| 149 | - | |
| 150 | - if( | |
| 151 | - // set the format to php array data if html format is set | |
| 152 | - ( isset( $this->args['format'] ) && $this->args['format'] == 'html' && $this->results_format != 'xml_array' ) || | |
| 153 | - // set the format to php array data if it is set in the args (coming from the importer) | |
| 154 | - ( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' ) | |
| 155 | - ) { | |
| 133 | + // set the format to php array data if html format is set | |
| 134 | + if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) { | |
| 156 | 135 | $this->results_format = 'json_decoded'; |
| 157 | 136 | } |
| 158 | 137 | |
| 159 | - // set the format to JSON string if it is set in the args (coming from the wpdatatables integration) | |
| 160 | - if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_string' ) { | |
| 161 | - $this->results_format = 'json_string'; | |
| 162 | - } | |
| 163 | - | |
| 164 | - // set the format to JSON string if it is set in the args (coming from the wpdatatables integration) | |
| 165 | - if( isset( $this->args['results_format'] ) && $this->args['results_format'] == 'json_decoded' ) { | |
| 166 | - $this->results_format = 'json_decoded'; | |
| 167 | - } | |
| 168 | - | |
| 169 | 138 | } |
| 170 | 139 | |
| 171 | 140 | } |
| 172 | 141 | |
| @@ -201,9 +170,9 @@ | ||
| 201 | 170 | $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this ); |
| 202 | 171 | |
| 203 | 172 | // build the request args |
| 204 | 173 | $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this ); |
| 205 | - | |
| 174 | + | |
| 206 | 175 | /** |
| 207 | 176 | * Check if we should stop or not. Setting $on to true, will stop us calling the api. |
| 208 | 177 | * @param bool $on Whether on is set or not. |
| 209 | 178 | */ |
| @@ -219,12 +188,8 @@ | ||
| 219 | 188 | // PUT request |
| 220 | 189 | if( $this->method == 'PUT' ) |
| 221 | 190 | $this->response = $this->PUT_request(); |
| 222 | 191 | |
| 223 | - // PATCH request | |
| 224 | - if( $this->method == 'PATCH' ) | |
| 225 | - $this->response = $this->PATCH_request(); | |
| 226 | - | |
| 227 | 192 | // GET request |
| 228 | 193 | if( $this->method == 'GET' ) |
| 229 | 194 | $this->response = $this->GET_request(); |
| 230 | 195 | |
| @@ -237,17 +202,17 @@ | ||
| 237 | 202 | return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this ); |
| 238 | 203 | } |
| 239 | 204 | |
| 240 | 205 | // get response_code |
| 241 | - $this->response_code = wp_remote_retrieve_response_code( $this->response ); | |
| 206 | + $response_code = wp_remote_retrieve_response_code( $this->response ); | |
| 242 | 207 | |
| 243 | 208 | // action to intercept call depending on response_code |
| 244 | - $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 ); | |
| 245 | 210 | |
| 246 | 211 | // get body |
| 247 | 212 | // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already |
| 248 | 213 | $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response; |
| 249 | - | |
| 214 | + | |
| 250 | 215 | // returning in JSON format |
| 251 | 216 | if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' ) |
| 252 | 217 | $data = $this->return_data( $data ); |
| 253 | 218 | |
| @@ -267,14 +232,8 @@ | ||
| 267 | 232 | // first make sure our endpoint starts with a slash |
| 268 | 233 | if ( substr( $this->endpoint, 0, 1 ) != '/' ) |
| 269 | 234 | $this->endpoint = '/' . $this->endpoint; |
| 270 | 235 | |
| 271 | - // if we have item_key from api to posts plugin | |
| 272 | - // replace our keyword with the actual item_key value | |
| 273 | - if ( isset( $this->args['item_key'] ) && strpos( $this->endpoint, '(importer:item_key)' ) !== false ) { | |
| 274 | - $this->endpoint = str_replace( '(importer:item_key)', $this->args['item_key'], $this->endpoint ); | |
| 275 | - } | |
| 276 | - | |
| 277 | 236 | $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this ); |
| 278 | 237 | |
| 279 | 238 | $url = untrailingslashit( $this->base_url ) . $this->endpoint; |
| 280 | 239 | |
| @@ -288,12 +247,10 @@ | ||
| 288 | 247 | |
| 289 | 248 | // add empty array - was getting errors if it was just null or false |
| 290 | 249 | $params = empty( $params ) ? array(''=>'') : $params; |
| 291 | 250 | |
| 292 | - $result = add_query_arg( $params, $url ); | |
| 251 | + return add_query_arg( $params, $url ); | |
| 293 | 252 | |
| 294 | - return $result; | |
| 295 | - | |
| 296 | 253 | } |
| 297 | 254 | |
| 298 | 255 | |
| 299 | 256 | /** |
| @@ -307,13 +264,12 @@ | ||
| 307 | 264 | ), $this ); |
| 308 | 265 | |
| 309 | 266 | |
| 310 | 267 | // if doing POST request, include body paramters |
| 311 | - if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' || $this->method == 'PATCH' ) { | |
| 268 | + if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) { | |
| 312 | 269 | |
| 313 | - // tokens are sent through here | |
| 314 | 270 | $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this ); |
| 315 | - | |
| 271 | + | |
| 316 | 272 | // do we json_encode the entire body |
| 317 | 273 | if( $this->body_json_encode == true ) { |
| 318 | 274 | $this->body_parameters = json_encode( $this->body_parameters ); |
| 319 | 275 | } |
| @@ -318,30 +274,23 @@ | ||
| 318 | 274 | $this->body_parameters = json_encode( $this->body_parameters ); |
| 319 | 275 | } |
| 320 | 276 | |
| 321 | 277 | // do we urlencode the entire body |
| 322 | - if( $this->body_parameters && $this->body_url_encode == true ) { | |
| 278 | + if( $this->body_url_encode == true ) { | |
| 323 | 279 | |
| 324 | 280 | // checking if the parameters have a json string (raw) |
| 325 | - if ( | |
| 326 | - ( ! is_array( $this->body_parameters ) ) && | |
| 327 | - ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) | |
| 328 | - ) { | |
| 281 | + if ( substr( $this->body_parameters, 0, 1 ) === '{' || substr( $this->body_parameters, 0, 1 ) === '[' ) { | |
| 329 | 282 | $this->body_parameters = json_decode( $this->body_parameters, true ); |
| 330 | 283 | } |
| 331 | - | |
| 332 | - $this->body_parameters = http_build_query( $this->body_parameters, '', '&' ); | |
| 333 | 284 | |
| 285 | + $this->body_parameters = http_build_query( $this->body_parameters ); | |
| 286 | + | |
| 334 | 287 | } |
| 335 | 288 | |
| 336 | 289 | // do we xml the entire body |
| 337 | - if( $this->body_parameters && $this->body_xml_format == true ) { | |
| 338 | - | |
| 339 | - $root_node = $this->xml_root_node != '' ? '<' . $this->xml_root_node . '/>' : ''; | |
| 290 | + if( $this->body_xml_format == true ) { | |
| 340 | 291 | |
| 341 | - $xml_element = apply_filters( 'wpgetapi_xml_element', '<?xml version="1.0" encoding="UTF-8"?>' . $root_node ); | |
| 342 | - | |
| 343 | - $xml = new SimpleXmlElement( $xml_element ); | |
| 292 | + $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' ); | |
| 344 | 293 | |
| 345 | 294 | foreach( $this->body_parameters as $key => $value) { |
| 346 | 295 | $xml->addChild( $key, $value ); |
| 347 | 296 | } |
| @@ -349,18 +298,8 @@ | ||
| 349 | 298 | $this->body_parameters = $xml->asXML(); |
| 350 | 299 | |
| 351 | 300 | } |
| 352 | 301 | |
| 353 | - // do we base64 encode the entire body | |
| 354 | - if( $this->body_parameters && $this->body_base64_encode == true ) { | |
| 355 | - | |
| 356 | - $this->body_parameters = is_array( $this->body_parameters ) ? json_encode( $this->body_parameters ) : $this->body_parameters; | |
| 357 | - $this->body_parameters = base64_encode( $this->body_parameters ); | |
| 358 | - | |
| 359 | - } | |
| 360 | - | |
| 361 | - $this->body_parameters = apply_filters( 'wpgetapi_modify_formatted_body_parameters', $this->body_parameters, $this ); | |
| 362 | - | |
| 363 | 302 | // add our body parameters |
| 364 | 303 | if( $this->body_parameters && ! empty( $this->body_parameters ) ) |
| 365 | 304 | $headers['body'] = $this->body_parameters; |
| 366 | 305 | |
| @@ -365,18 +304,14 @@ | ||
| 365 | 304 | $headers['body'] = $this->body_parameters; |
| 366 | 305 | |
| 367 | 306 | } |
| 368 | 307 | |
| 369 | - // get our body paramters as they are formatted | |
| 370 | - // this won't change anything, it just allows us to get the parameters | |
| 371 | - $this->body_parameters = apply_filters( 'wpgetapi_final_body_parameters', $this->body_parameters, $this ); | |
| 372 | - | |
| 373 | 308 | // add headers to our final request args |
| 374 | 309 | $this->final_request_args = wp_parse_args( $headers, $this->final_request_args ); |
| 375 | 310 | |
| 376 | 311 | // build the args taht are sent to the request |
| 377 | 312 | $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array( |
| 378 | - 'timeout' => ( absint( $this->timeout ) > 0 ? absint( $this->timeout ) : 10 ), | |
| 313 | + 'timeout' => $this->timeout, | |
| 379 | 314 | 'sslverify' => $this->sslverify, |
| 380 | 315 | 'redirection' => 5, |
| 381 | 316 | 'blocking' => true, |
| 382 | 317 | 'cookies' => array(), |
| @@ -386,15 +321,12 @@ | ||
| 386 | 321 | |
| 387 | 322 | } |
| 388 | 323 | |
| 389 | 324 | |
| 390 | - | |
| 391 | - | |
| 392 | 325 | /** |
| 393 | 326 | * Do a GET request |
| 394 | 327 | */ |
| 395 | 328 | public function GET_request() { |
| 396 | - | |
| 397 | 329 | $response = ''; |
| 398 | 330 | // so we can so something before the remote_get (caching) |
| 399 | 331 | $response = apply_filters( 'wpgetapi_before_get_request', $response, $this ); |
| 400 | 332 | |
| @@ -399,11 +331,9 @@ | ||
| 399 | 331 | $response = apply_filters( 'wpgetapi_before_get_request', $response, $this ); |
| 400 | 332 | |
| 401 | 333 | if( empty( $response ) ) |
| 402 | 334 | $response = wp_remote_get( $this->final_url, $this->final_request_args ); |
| 403 | - | |
| 404 | 335 | return $response; |
| 405 | - | |
| 406 | 336 | } |
| 407 | 337 | |
| 408 | 338 | /** |
| 409 | 339 | * Do a POST request |
| @@ -408,18 +338,10 @@ | ||
| 408 | 338 | /** |
| 409 | 339 | * Do a POST request |
| 410 | 340 | */ |
| 411 | 341 | public function POST_request() { |
| 412 | - | |
| 413 | - $response = ''; | |
| 414 | - // so we can so something before the remote_post (caching) | |
| 415 | - $response = apply_filters( 'wpgetapi_before_post_request', $response, $this ); | |
| 416 | - | |
| 417 | - //$this->final_request_args['method'] = 'POST'; | |
| 418 | - | |
| 419 | - if( empty( $response ) ) | |
| 420 | - $response = wp_remote_post( $this->final_url, $this->final_request_args ); | |
| 421 | - | |
| 342 | + $this->final_request_args['method'] = 'POST'; | |
| 343 | + $response = wp_remote_post( $this->final_url, $this->final_request_args ); | |
| 422 | 344 | return $response; |
| 423 | 345 | } |
| 424 | 346 | |
| 425 | 347 | /** |
| @@ -431,17 +353,8 @@ | ||
| 431 | 353 | return $response; |
| 432 | 354 | } |
| 433 | 355 | |
| 434 | 356 | /** |
| 435 | - * Do a PATCH request | |
| 436 | - */ | |
| 437 | - public function PATCH_request() { | |
| 438 | - $this->final_request_args['method'] = 'PATCH'; | |
| 439 | - $response = wp_remote_request( $this->final_url, $this->final_request_args ); | |
| 440 | - return $response; | |
| 441 | - } | |
| 442 | - | |
| 443 | - /** | |
| 444 | 357 | * Do a DELETE request |
| 445 | 358 | */ |
| 446 | 359 | public function DELETE_request() { |
| 447 | 360 | $this->final_request_args['method'] = 'DELETE'; |
| @@ -455,9 +368,9 @@ | ||
| 455 | 368 | */ |
| 456 | 369 | public function return_data( $data ) { |
| 457 | 370 | |
| 458 | 371 | $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys ); |
| 459 | - | |
| 372 | + | |
| 460 | 373 | // converts all data to arrays, removing objects |
| 461 | 374 | if( is_string( $data ) ) { |
| 462 | 375 | $decode = json_decode( $data, true ); |
| 463 | 376 | if( json_last_error() ) { |
| @@ -465,13 +378,13 @@ | ||
| 465 | 378 | } else { |
| 466 | 379 | $data = $decode; |
| 467 | 380 | } |
| 468 | 381 | } |
| 469 | - | |
| 382 | + | |
| 470 | 383 | $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys ); |
| 471 | 384 | |
| 472 | 385 | if( $this->results_format == 'json_string' ) |
| 473 | - return trim( json_encode( $data, JSON_PRETTY_PRINT ),'"'); | |
| 386 | + return trim( json_encode( $data ),'"'); | |
| 474 | 387 | |
| 475 | 388 | if( $this->results_format == 'json_decoded' ) |
| 476 | 389 | return $data; |
| 477 | 390 | |
| @@ -480,9 +393,9 @@ | ||
| 480 | 393 | /** |
| 481 | 394 | * Sort out the parameters if they exist |
| 482 | 395 | */ |
| 483 | 396 | public function do_parameters( $parameters ) { |
| 484 | - | |
| 397 | + | |
| 485 | 398 | $new_array = array(); |
| 486 | 399 | $raw = ''; |
| 487 | 400 | $parameters = $this->decrypt( $parameters ); |
| 488 | 401 | |
| @@ -498,14 +411,8 @@ | ||
| 498 | 411 | unset( $parameters[ $key ] ); |
| 499 | 412 | continue; |
| 500 | 413 | } |
| 501 | 414 | |
| 502 | - // if we have item_key from api to posts plugin | |
| 503 | - // replace our keyword with the actual item_key value | |
| 504 | - if ( isset( $this->args['item_key'] ) && strpos( $parameter['value'], '(importer:item_key)' ) !== false ) { | |
| 505 | - $parameter['value'] = str_replace( '(importer:item_key)', $this->args['item_key'], $parameter['value'] ); | |
| 506 | - } | |
| 507 | - | |
| 508 | 415 | $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value']; |
| 509 | 416 | |
| 510 | 417 | // if we are sending raw body |
| 511 | 418 | } else if( empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) { |
| @@ -516,20 +423,17 @@ | ||
| 516 | 423 | |
| 517 | 424 | } |
| 518 | 425 | } |
| 519 | 426 | |
| 520 | - // now look for array or json string and sort these out - for using such things in shortcode | |
| 427 | + // look for array or json string and sort these out - for using such things in shortcode | |
| 521 | 428 | if( ! empty( $new_array ) ) { |
| 522 | 429 | |
| 523 | 430 | foreach ( $new_array as $key => $value ) { |
| 524 | 431 | |
| 525 | 432 | // checking if the parameters have a json string |
| 526 | - if ( strpos( $value, '{"' ) !== false && strpos( $value, '}' ) !== false ) { | |
| 527 | - | |
| 528 | - $tmp_val = $value; | |
| 529 | - $tmp_val = json_decode( $tmp_val, true ); | |
| 530 | - $new_array[ $key ] = $tmp_val ? $tmp_val : $value; | |
| 531 | - | |
| 433 | + if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) { | |
| 434 | + //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) { | |
| 435 | + $new_array[ $key ] = json_decode( $value, true ); | |
| 532 | 436 | } else |
| 533 | 437 | |
| 534 | 438 | // checking if the parameters have an array within the string |
| 535 | 439 | // if they do, create them as an actual array |
| @@ -539,38 +443,17 @@ | ||
| 539 | 443 | } |
| 540 | 444 | |
| 541 | 445 | // set value as integer |
| 542 | 446 | if ( strpos( $value, 'integer(' ) !== false && substr( $value, -1 ) === ')' ) { |
| 543 | - | |
| 544 | 447 | preg_match("/\(([^\)]*)\)/", $value, $match ); |
| 545 | - $result = trim( $match[1] ); | |
| 546 | - | |
| 547 | - // checking if we have additional token within | |
| 548 | - // and we need to send this directly to PRO plugin from here | |
| 549 | - if( strpos( $value, '(' ) !== false && strpos( $value, ':' ) !== false && $this->pro_plugin ) { | |
| 550 | - $result = $this->replace_tokens( $result ); | |
| 551 | - } else { | |
| 552 | - $result = $result; | |
| 553 | - } | |
| 554 | - | |
| 448 | + $result = $match[1]; | |
| 555 | 449 | $new_array[ $key ] = absint( $result ); |
| 556 | - | |
| 557 | 450 | } |
| 558 | 451 | |
| 559 | 452 | // set value as float |
| 560 | 453 | if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) { |
| 561 | - | |
| 562 | 454 | preg_match("/\(([^\)]*)\)/", $value, $match ); |
| 563 | - $result = trim( $match[1] ); | |
| 564 | - if ( substr( $result, 0, 1 ) === '"' && substr( $result, -1 ) === '"' ) | |
| 565 | - $result = trim( $result,'""' ); | |
| 566 | - | |
| 567 | - // checking if we have additional token within | |
| 568 | - // and we need to send this directly to PRO plugin from here | |
| 569 | - if( strpos( $value, '(' ) !== false && strpos( $value, ':' ) !== false && $this->pro_plugin ) { | |
| 570 | - $result = $this->replace_tokens( $result ); | |
| 571 | - } | |
| 572 | - | |
| 455 | + $result = $match[1]; | |
| 573 | 456 | $new_array[ $key ] = (float) $result; |
| 574 | 457 | |
| 575 | 458 | } |
| 576 | 459 | |
| @@ -577,19 +460,9 @@ | ||
| 577 | 460 | // set value as boolean |
| 578 | 461 | if ( strpos( $value, 'boolean(' ) !== false && substr( $value, -1 ) === ')' ) { |
| 579 | 462 | preg_match("/\(([^\)]*)\)/", $value, $match ); |
| 580 | 463 | $result = $match[1]; |
| 581 | - | |
| 582 | - // checking if we have additional token within | |
| 583 | - // and we need to send this directly to PRO plugin from here | |
| 584 | - if( strpos( $value, '(' ) !== false && strpos( $value, ':' ) !== false && $this->pro_plugin ) { | |
| 585 | - $result = $this->replace_tokens( $result ); | |
| 586 | - } else { | |
| 587 | - $result = $result; | |
| 588 | - } | |
| 589 | - | |
| 590 | 464 | $result = filter_var( $result, FILTER_VALIDATE_BOOLEAN ); |
| 591 | - | |
| 592 | 465 | $new_array[ $key ] = $result; |
| 593 | 466 | |
| 594 | 467 | } |
| 595 | 468 | |
| @@ -602,20 +475,8 @@ | ||
| 602 | 475 | } |
| 603 | 476 | |
| 604 | 477 | |
| 605 | 478 | /** |
| 606 | - * Do a direct token replacement if PRO plugin installed and is required. | |
| 607 | - */ | |
| 608 | - public function replace_tokens( $value ) { | |
| 609 | - $tokens = new WpGetApi_Extras_Tokens(); | |
| 610 | - $action_args = isset( $this->args['action_args'] ) ? $this->args['action_args'] : null; | |
| 611 | - $value = ( substr( $value, -1 ) !== ')' ? $value . ')' : $value ); | |
| 612 | - $result = $tokens->replace_tokens( $value, $action_args ); | |
| 613 | - return $result; | |
| 614 | - } | |
| 615 | - | |
| 616 | - | |
| 617 | - /** | |
| 618 | 479 | * decrypt our parameters from the DB |
| 619 | 480 | */ |
| 620 | 481 | public function decrypt( $array_or_string ) { |
| 621 | 482 | |
| @@ -649,100 +510,85 @@ | ||
| 649 | 510 | ) { |
| 650 | 511 | |
| 651 | 512 | $this->debug = true; |
| 652 | 513 | |
| 653 | - // admin test endpoint button | |
| 654 | 514 | $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false; |
| 655 | 515 | |
| 656 | - // get api status from response code | |
| 657 | - $status = array( | |
| 658 | - 'title' => 'Error in API response.', | |
| 659 | - 'desc' => 'Check all details within your endpoint. Something is misconfigured.', | |
| 660 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 661 | - ); | |
| 516 | + ob_start(); ?> | |
| 662 | 517 | |
| 663 | - $search = 'cURL error 28'; | |
| 664 | - if( isset( $data ) && ! is_array( $data ) && preg_match( "/{$search}/i", $data ) ) { | |
| 665 | - $status = array( | |
| 666 | - 'title' => 'Error!', | |
| 667 | - 'desc' => 'Timeout error. Try increasing the timeout value.', | |
| 668 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 669 | - ); | |
| 670 | - } | |
| 518 | +<style> | |
| 671 | 519 | |
| 672 | - $search = 'cURL error 60'; | |
| 673 | - if( isset( $data ) && ! is_array( $data ) && preg_match( "/{$search}/i", $data ) ) { | |
| 674 | - $status = array( | |
| 675 | - 'title' => 'Error!', | |
| 676 | - 'desc' => 'SSL verification has failed. See <a href="https://wordpress.stackexchange.com/a/167910/11827" target="_blank">here</a> for possible solution.', | |
| 677 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 678 | - ); | |
| 679 | - } | |
| 520 | +.wpgetapi-debug { | |
| 521 | + background: #fff; | |
| 522 | + padding: 20px; | |
| 523 | + box-shadow: 0 0 1px #aaa; | |
| 524 | + margin: 20px; | |
| 525 | + font-size: 15px; | |
| 526 | +} | |
| 527 | +.wpgetapi-debug | |
| 528 | +.wpgetapi-debug p | |
| 529 | +.wpgetapi-debug h5 | |
| 530 | +.wpgetapi-debug h6 { | |
| 531 | + color: #000; | |
| 532 | + line-height: 1 !important; | |
| 533 | + text-transform: none !important; | |
| 534 | + font-family: sans-serif !important; | |
| 535 | + letter-spacing: 0px !important; | |
| 536 | +} | |
| 537 | +.wpgetapi-debug p.small { | |
| 538 | + font-size: 85%; | |
| 539 | + color: #999; | |
| 540 | + margin: 0 0 12px !important; | |
| 541 | +} | |
| 542 | +.wpgetapi-debug h5 { | |
| 543 | + font-weight: bold; | |
| 544 | + font-size: 18px; | |
| 545 | + margin: 0 0 3px; | |
| 546 | + text-transform: none !important; | |
| 547 | +} | |
| 548 | +.wpgetapi-debug .debug-item { | |
| 549 | + margin: 10px 0; | |
| 550 | + background: #fff; | |
| 551 | + padding: 15px 20px; | |
| 552 | + display: inline-block !important; | |
| 553 | + width: 100% !important; | |
| 554 | + box-shadow: 3px 3px 0 rgb(0 0 0 / 8%); | |
| 555 | + border: 1px solid #efefef; | |
| 556 | +} | |
| 557 | +.wpgetapi-debug .debug-item h6 { | |
| 558 | + font-weight: bold; | |
| 559 | + font-size: 14px; | |
| 560 | + margin: 0; | |
| 561 | + text-transform: none !important; | |
| 562 | +} | |
| 563 | +.wpgetapi-debug .debug-item .debug-result, | |
| 564 | +.wpgetapi-debug .debug-item .debug-result pre { | |
| 565 | + font-family: monospace !important; | |
| 566 | + font-size: 13px !important; | |
| 567 | + margin: 0 !important; | |
| 568 | + padding: 8px 12px; | |
| 569 | + background: #f8f8f8; | |
| 570 | +} | |
| 571 | +.wpgetapi-debug .debug-item .debug-result pre { | |
| 572 | + border: none; | |
| 573 | + padding: 5px | |
| 574 | +} | |
| 680 | 575 | |
| 681 | - if( $this->response_code >= 200 && $this->response_code <= 299 ) { | |
| 682 | - $status = array( | |
| 683 | - 'title' => 'Success!', | |
| 684 | - '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.', | |
| 685 | - 'type' => '<span class="dashicons dashicons-yes-alt"></span>' | |
| 686 | - ); | |
| 687 | - } | |
| 688 | - if( $this->response_code >= 400 && $this->response_code <= 499 ) { | |
| 689 | - $status = array( | |
| 690 | - 'title' => 'Bad Request', | |
| 691 | - '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.', | |
| 692 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 693 | - ); | |
| 694 | - } | |
| 695 | - if( $this->response_code == 401 ) { | |
| 696 | - $status = array( | |
| 697 | - 'title' => 'Unauthorised', | |
| 698 | - '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.', | |
| 699 | - 'type' => '<span class="dashicons dashicons-warning"></span>' | |
| 700 | - ); | |
| 701 | - } | |
| 702 | - if( $this->response_code == 403 ) { | |
| 703 | - $status = array( | |
| 704 | - 'title' => 'Forbidden', | |
| 705 | - '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.', | |
| 706 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 707 | - ); | |
| 708 | - } | |
| 709 | - if( $this->response_code == 405 ) { | |
| 710 | - $status = array( | |
| 711 | - 'title' => 'Method Not Allowed', | |
| 712 | - 'desc' => 'Try changing the Request Method within the endpoint settings.', | |
| 713 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 714 | - ); | |
| 715 | - } | |
| 716 | - if( $this->response_code == 415 ) { | |
| 717 | - $status = array( | |
| 718 | - 'title' => 'Unsupported Media Type', | |
| 719 | - '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>.', | |
| 720 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 721 | - ); | |
| 722 | - } | |
| 723 | - if( $this->response_code >= 500 && $this->response_code <= 599 ) { | |
| 724 | - $status = array( | |
| 725 | - 'title' => 'Internal Server Error', | |
| 726 | - 'desc' => 'This indicates an issue with the API server. Contact your API provider.', | |
| 727 | - 'type' => '<span class="dashicons dashicons-dismiss"></span>' | |
| 728 | - ); | |
| 729 | - } | |
| 576 | +.wpgetapi-debug.testing { | |
| 577 | + background: transparent; | |
| 578 | + padding: 0; | |
| 579 | + box-shadow: none; | |
| 580 | +} | |
| 581 | +.wpgetapi-debug.testing .debug-item { | |
| 582 | + margin: 5px 0; | |
| 583 | +} | |
| 584 | +</style> | |
| 730 | 585 | |
| 731 | - | |
| 732 | - ob_start(); ?> | |
| 733 | - | |
| 734 | 586 | <div class="wpgetapi-debug <?php echo $testing ? 'testing' : ''; ?>"> |
| 735 | 587 | |
| 736 | 588 | <?php if( $testing ) { ?> |
| 737 | 589 | |
| 738 | - <div class="debug-item status"> | |
| 739 | - <div class=""> | |
| 740 | - <h5><?php _e( $status['type'] ); ?> <?php esc_html_e( $status['title'] ); ?></h5> | |
| 741 | - <span class="desc"><?php _e( $status['desc'] ); ?></span> | |
| 742 | - </div> | |
| 743 | - </div> | |
| 744 | - <div class="debug-item"> | |
| 590 | + <div class="debug-item"> | |
| 745 | 591 | <h6><?php esc_html_e( 'Full URL', 'wpgetapi' ); ?></h6> |
| 746 | 592 | <p class="small"><?php esc_html_e( 'The full URL that is being called.', 'wpgetapi' ); ?></p> |
| 747 | 593 | <div class="debug-result"><?php esc_html_e( $this->final_url ); ?></div> |
| 748 | 594 | </div> |
| @@ -749,15 +595,10 @@ | ||
| 749 | 595 | |
| 750 | 596 | <div class="debug-item"> |
| 751 | 597 | <h6><?php esc_html_e( 'Data Output', 'wpgetapi' ); ?></h6> |
| 752 | 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> | |
| 753 | 600 | <div class="debug-result"><?php wpgetapi_pp( $data ); ?></div> |
| 754 | - </div> | |
| 755 | - | |
| 756 | - <div class="debug-item"> | |
| 757 | - <h6><?php esc_html_e( 'Final Request Arguments', 'wpgetapi' ); ?></h6> | |
| 758 | - <p class="small"><?php esc_html_e( 'The final request arguments sent in the API request. Includes headers and body arguments.', 'wpgetapi' ); ?></p> | |
| 759 | - <div class="debug-result"><?php wpgetapi_pp( $this->final_request_args ); ?></div> | |
| 760 | 601 | </div> |
| 761 | 602 | |
| 762 | 603 | <div class="debug-item"> |
| 763 | 604 | <h6><?php esc_html_e( 'Response', 'wpgetapi' ); ?></h6> |