setup = get_option( 'wpgetapi_setup' ); $this->api = get_option( 'wpgetapi_' . $api_id ); $this->encryption = new WpGetApi_Encryption(); $this->api_id = sanitize_text_field( $api_id ); $this->endpoint_id = sanitize_text_field( $endpoint_id ); $this->keys = $keys; $this->args = $args; add_filter( 'wpgetapi_raw_data', array( $this, 'maybe_add_debug_info' ), 99, 2 ); } /** * Init our api data */ public function init() { // if we are doing importer plugin, // we need to intercept and modify most of our methods values if( $this->api_id == 'wpgetapi_importer' ) { $data = apply_filters( 'wpgetapi_api_importer_init', $this ); if( $data ) { foreach ($data as $key => $value) { $this->{$key} = $value; } $this->query_parameters = ! empty( $this->query_parameters ) ? $this->do_parameters( $this->query_parameters ) : array(); $this->header_parameters = ! empty( $this->header_parameters ) ? $this->do_parameters( $this->header_parameters ) : array(); $this->body_parameters = ! empty( $this->body_parameters ) ? $this->do_parameters( $this->body_parameters ) : array(); return; } } // do some checks if( empty( $this->setup ) || ! isset( $this->setup['apis'] ) || empty( $this->setup['apis'] ) ) return 'No API found.'; // do some checks if( ! $this->api || ! isset( $this->api['endpoints'] ) || empty( $this->api['endpoints'] )) return 'API not found.'; // loop through api's // set base_url foreach ( $this->setup['apis'] as $index => $api_item ) { if( $this->api_id == $api_item['id'] ) { $this->base_url = isset( $api_item['base_url'] ) && ! empty( $api_item['base_url'] ) ? esc_url_raw( $api_item['base_url'] ) : ''; } } // loop through endpoints foreach ( $this->api['endpoints'] as $index => $endpoint ) { if( $this->endpoint_id == $endpoint['id'] ) { $this->method = isset( $endpoint['method'] ) && $endpoint['method'] != '' ? sanitize_text_field( $endpoint['method'] ) : 'GET'; $this->cache_time = isset( $endpoint['cache_time'] ) && ! empty( $endpoint['cache_time'] ) ? absint( $endpoint['cache_time'] ) : ''; $this->endpoint = isset( $endpoint['endpoint'] ) && ! empty( $endpoint['endpoint'] ) ? sanitize_text_field( $endpoint['endpoint'] ) : ''; $this->query_parameters = isset( $endpoint['query_parameters'] ) && ! empty( $endpoint['query_parameters'] ) ? $this->do_parameters( $endpoint['query_parameters'] ) : array(); $this->header_parameters = isset( $endpoint['header_parameters'] ) && ! empty( $endpoint['header_parameters'] ) ? $this->do_parameters( $endpoint['header_parameters'] ) : array(); $this->body_parameters = isset( $endpoint['body_parameters'] ) && ! empty( $endpoint['body_parameters'] ) ? $this->do_parameters( $endpoint['body_parameters'] ) : array(); $this->body_json_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'true' ? true : false; $this->body_url_encode = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'url' ? true : false; $this->body_xml_format = isset( $endpoint['body_json_encode'] ) && $endpoint['body_json_encode'] == 'xml' ? true : false; $this->xml_root_node = isset( $endpoint['xml_root'] ) && ! empty( $endpoint['xml_root'] ) ? $endpoint['xml_root'] : $this->xml_root_node; $this->results_format = isset( $endpoint['results_format'] ) ? sanitize_text_field( $endpoint['results_format'] ) : $this->results_format; // set the format to php array data if html format is set if( isset( $this->args['format'] ) && $this->args['format'] == 'html' ) { $this->results_format = 'json_decoded'; } } } $this->timeout = isset( $this->api['timeout'] ) && ! empty( $this->api['timeout'] ) ? $this->api['timeout'] : $this->timeout; $this->sslverify = isset( $this->api['sslverify'] ) ? absint( $this->api['sslverify'] ) : $this->sslverify; } /** * The main function to output the data * * @param string $field Field to retrieve */ public function endpoint_output() { do_action( 'wpgetapi_before_do_the_call', $this ); return $this->do_the_call(); } /** * Send the request and return our data */ public function do_the_call() { $this->init(); // build the URL $this->final_url = apply_filters( 'wpgetapi_final_url', $this->build_url(), $this ); // build the request args $this->final_request_args = apply_filters( 'wpgetapi_final_request_args', $this->build_request_args(), $this ); /** * Check if we should stop or not. Setting $on to true, will stop us calling the api. * @param bool $on Whether on is set or not. */ if ( apply_filters( 'wpgetapi_should_we_stop', false, $this ) ) { return; } // POST request if( $this->method == 'POST' ) $this->response = $this->POST_request(); // PUT request if( $this->method == 'PUT' ) $this->response = $this->PUT_request(); // GET request if( $this->method == 'GET' ) $this->response = $this->GET_request(); // DELETE request if( $this->method == 'DELETE' ) $this->response = $this->DELETE_request(); // wp error if( is_wp_error( $this->response ) ) { return apply_filters( 'wpgetapi_raw_error_data', json_encode( $this->response ), $this ); } // get response_code $response_code = wp_remote_retrieve_response_code( $this->response ); // action to intercept call depending on response_code $this->response = apply_filters( 'wpgetapi_before_retrieve_body', $this->response, $response_code, $this ); // get body // modified in version 1.6.1 to allow for OAuth 2.0 plugin returning body already $data = isset( $this->response['body'] ) ? $this->response['body'] : $this->response; // returning in JSON format if( $this->results_format == 'json_string' || $this->results_format == 'json_decoded' ) $data = $this->return_data( $data ); if( $data === 'null' ) $data = 'Result is null. Check that all endpoint settings are correct.'; return apply_filters( 'wpgetapi_raw_data', $data, $this ); } /** * Build the request url with the ability to add args */ public function build_url() { // first make sure our endpoint starts with a slash if ( substr( $this->endpoint, 0, 1 ) != '/' ) $this->endpoint = '/' . $this->endpoint; $this->endpoint = apply_filters( 'wpgetapi_endpoint', $this->endpoint, $this ); $url = untrailingslashit( $this->base_url ) . $this->endpoint; // if we have a URL pagination event from the import plugin // we need to modify the URL here if( isset( $this->args['paginate_url'] ) ) $url = $this->args['paginate_url']; // filter our params - added for use in Oauth 2.0 plugin $params = apply_filters( 'wpgetapi_query_parameters', $this->query_parameters, $this ); // add empty array - was getting errors if it was just null or false $params = empty( $params ) ? array(''=>'') : $params; return add_query_arg( $params, $url ); } /** * build the header with the ability to add args */ public function build_request_args() { // add our header parameters to the headers $headers = apply_filters( 'wpgetapi_header_parameters', array( 'headers' => $this->header_parameters, ), $this ); // if doing POST request, include body paramters if( $this->method == 'POST' || $this->method == 'PUT' || $this->method == 'DELETE' ) { $this->body_parameters = apply_filters( 'wpgetapi_body_parameters', $this->body_parameters, $this ); // do we json_encode the entire body if( $this->body_json_encode == true ) { $this->body_parameters = json_encode( $this->body_parameters ); } // do we urlencode the entire body if( $this->body_url_encode == true ) { $data_raw = ''; foreach ($this->body_parameters as $key => $value){ $data_raw = $data_raw . $key . "=" . urlencode($value) . "&"; } $this->body_parameters = $data_raw; } // do we xml the entire body if( $this->body_xml_format == true ) { $xml = new SimpleXmlElement( '<' . $this->xml_root_node . '/>' ); foreach( $this->body_parameters as $key => $value) { $xml->addChild( $key, $value ); } $this->body_parameters = $xml->asXML(); } // add our body parameters if( $this->body_parameters && ! empty( $this->body_parameters ) ) $headers['body'] = $this->body_parameters; } // add headers to our final request args $this->final_request_args = wp_parse_args( $headers, $this->final_request_args ); // build the args taht are sent to the request $default_args = apply_filters( 'wpgetapi_default_request_args_parameters', array( 'timeout' => $this->timeout, 'sslverify' => $this->sslverify, 'redirection' => 5, 'blocking' => true, 'cookies' => array(), ) ); return wp_parse_args( $this->final_request_args, $default_args ); } /** * Do a GET request */ public function GET_request() { $response = ''; // so we can so something before the remote_get (caching) $response = apply_filters( 'wpgetapi_before_get_request', $response, $this ); if( empty( $response ) ) $response = wp_remote_get( $this->final_url, $this->final_request_args ); return $response; } /** * Do a POST request */ public function POST_request() { $this->final_request_args['method'] = 'POST'; $response = wp_remote_post( $this->final_url, $this->final_request_args ); return $response; } /** * Do a PUT request */ public function PUT_request() { $this->final_request_args['method'] = 'PUT'; $response = wp_remote_request( $this->final_url, $this->final_request_args ); return $response; } /** * Do a DELETE request */ public function DELETE_request() { $this->final_request_args['method'] = 'DELETE'; $response = wp_remote_request( $this->final_url, $this->final_request_args ); return $response; } /** * return data */ public function return_data( $data ) { $data = apply_filters( 'wpgetapi_json_response_body_before_decode', $data, $this->keys ); // converts all data to arrays, removing objects if( is_string( $data ) ) $data = json_decode( $data, true ); $data = apply_filters( 'wpgetapi_json_response_body', $data, $this->keys ); if( $this->results_format == 'json_string' ) return trim( json_encode( $data ),'"'); if( $this->results_format == 'json_decoded' ) return $data; } /** * Sort out the parameters if they exist */ public function do_parameters( $parameters ) { $new_array = array(); $parameters = $this->decrypt( $parameters ); if( is_array( $parameters ) ) { foreach ( $parameters as $key => $parameter ) { if( ! empty( $parameter['name'] ) && ! empty( $parameter['value'] ) ) { $parameter['value'] = stripslashes( $parameter['value'] ); // if we have wpgetapi_on, delete it so we don't send to api if( $parameter['name'] == 'wpgetapi_on' ) { unset( $parameters[ $key ] ); continue; } $new_array[ sanitize_text_field( $parameter['name'] ) ] = $parameter['value']; } } } // look for array or json string and sort these out - for using such things in shortcode if( is_array( $new_array ) ) { foreach ( $new_array as $key => $value ) { // checking if the parameters have a json string if ( strpos( $value, '{' ) !== false && strpos( $value, '}' ) !== false ) { //if ( substr( $value, 0, 1 ) === '{' && substr( $value, -1 ) === '}' ) { $new_array[ $key ] = json_decode( $value, true ); } else // checking if the parameters have an array within the string // if they do, create them as an actual array if ( substr( $value, 0, 1 ) === '[' && substr( $value, -1 ) === ']' ) { $value = trim( $value,"[]" ); $new_array[ $key ] = array( $value ); } // set value as integer if ( strpos( $value, 'integer(' ) !== false && substr( $value, -1 ) === ')' ) { preg_match("/\(([^\)]*)\)/", $value, $match ); $result = $match[1]; $new_array[ $key ] = absint( $result ); } // set value as float if ( strpos( $value, 'float(' ) !== false && substr( $value, -1 ) === ')' ) { preg_match("/\(([^\)]*)\)/", $value, $match ); $result = $match[1]; $new_array[ $key ] = (float) $result; } // set value as boolean if ( strpos( $value, 'boolean(' ) !== false && substr( $value, -1 ) === ')' ) { preg_match("/\(([^\)]*)\)/", $value, $match ); $result = $match[1]; $result = filter_var( $result, FILTER_VALIDATE_BOOLEAN ); $new_array[ $key ] = $result; } } } return $new_array; } /** * decrypt our parameters from the DB */ public function decrypt( $array_or_string ) { if( is_string($array_or_string) ){ $array_or_string = $this->encryption->decrypt($array_or_string); }elseif( is_array($array_or_string) ){ foreach ( $array_or_string as $key => &$value ) { if( $key === 'name' ) continue; if ( is_array( $value ) ) { $value = $this->decrypt($value); } else { $value = $this->encryption->decrypt($value); } } } return $array_or_string; } /** * debug */ public function maybe_add_debug_info( $data, $wpgetapi ) { if( ( isset( $this->args['debug'] ) && ( $this->args['debug'] === true || $this->args['debug'] === 'true' ) ) || ( isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ) ) { $this->debug = true; $testing = isset( $this->args['test_endpoint'] ) && ( $this->args['test_endpoint'] === true ) ? true : false; ob_start(); ?>

final_url ); ?>

response); ?>
version ); ?>


.

final_url ); ?>

query_parameters ); ?>

header_parameters ); ?>

body_parameters ); ?>

final_request_args ); ?>

args ); ?>

response); ?>